@footgun/cobalt 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -0
- package/bundle.js +284 -0
- package/cobalt2.jpeg +0 -0
- package/esbuild.js +20 -0
- package/examples/01-primitives/Game.js +8 -0
- package/examples/01-primitives/component-animation.js +8 -0
- package/examples/01-primitives/component-transform.js +13 -0
- package/examples/01-primitives/constants.js +6 -0
- package/examples/01-primitives/deps.js +2 -0
- package/examples/01-primitives/entity-sprite.js +47 -0
- package/examples/01-primitives/index.html +191 -0
- package/examples/01-primitives/system-renderer.js +37 -0
- package/examples/02-sprites/Game.js +8 -0
- package/examples/02-sprites/assets/spritesheet.json +6276 -0
- package/examples/02-sprites/assets/spritesheet.png +0 -0
- package/examples/02-sprites/assets/spritesheet_emissive.png +0 -0
- package/examples/02-sprites/component-animation.js +8 -0
- package/examples/02-sprites/component-transform.js +13 -0
- package/examples/02-sprites/constants.js +6 -0
- package/examples/02-sprites/deps.js +2 -0
- package/examples/02-sprites/entity-sprite.js +47 -0
- package/examples/02-sprites/index.html +310 -0
- package/examples/02-sprites/system-renderer.js +38 -0
- package/examples/03-tiles/Game.js +8 -0
- package/examples/03-tiles/assets/spelunky-tiles.png +0 -0
- package/examples/03-tiles/assets/spelunky0.png +0 -0
- package/examples/03-tiles/assets/spelunky1.png +0 -0
- package/examples/03-tiles/component-animation.js +8 -0
- package/examples/03-tiles/component-transform.js +13 -0
- package/examples/03-tiles/constants.js +6 -0
- package/examples/03-tiles/deps.js +2 -0
- package/examples/03-tiles/entity-sprite.js +47 -0
- package/examples/03-tiles/index.html +309 -0
- package/examples/03-tiles/system-renderer.js +38 -0
- package/examples/04-overlay/assets/spritesheet.json +22 -0
- package/examples/04-overlay/assets/spritesheet.png +0 -0
- package/examples/04-overlay/assets/spritesheet_emissive.png +0 -0
- package/examples/04-overlay/constants.js +6 -0
- package/examples/04-overlay/deps.js +1 -0
- package/examples/04-overlay/index.html +133 -0
- package/examples/05-bloom/Game.js +8 -0
- package/examples/05-bloom/assets/spritesheet.json +6276 -0
- package/examples/05-bloom/assets/spritesheet.png +0 -0
- package/examples/05-bloom/assets/spritesheet_emissive.png +0 -0
- package/examples/05-bloom/component-animation.js +8 -0
- package/examples/05-bloom/component-transform.js +13 -0
- package/examples/05-bloom/constants.js +6 -0
- package/examples/05-bloom/deps.js +2 -0
- package/examples/05-bloom/entity-sprite.js +47 -0
- package/examples/05-bloom/index.html +357 -0
- package/examples/05-bloom/system-renderer.js +38 -0
- package/examples/06-displacement/Game.js +8 -0
- package/examples/06-displacement/assets/displacement_map_repeat.jpg +0 -0
- package/examples/06-displacement/assets/spelunky-tiles.png +0 -0
- package/examples/06-displacement/assets/spelunky0.png +0 -0
- package/examples/06-displacement/assets/spelunky1.png +0 -0
- package/examples/06-displacement/component-animation.js +8 -0
- package/examples/06-displacement/component-transform.js +13 -0
- package/examples/06-displacement/constants.js +6 -0
- package/examples/06-displacement/deps.js +2 -0
- package/examples/06-displacement/entity-sprite.js +47 -0
- package/examples/06-displacement/index.html +350 -0
- package/examples/06-displacement/system-renderer.js +38 -0
- package/examples/07-sdl/assets/spritesheet.json +22 -0
- package/examples/07-sdl/assets/spritesheet.png +0 -0
- package/examples/07-sdl/assets/spritesheet_emissive.png +0 -0
- package/examples/07-sdl/main.js +109 -0
- package/examples/07-sdl/package.json +19 -0
- package/examples/08-light/Game.js +8 -0
- package/examples/08-light/assets/spelunky-tiles.png +0 -0
- package/examples/08-light/assets/spelunky0.png +0 -0
- package/examples/08-light/assets/spelunky1.png +0 -0
- package/examples/08-light/constants.js +6 -0
- package/examples/08-light/deps.js +2 -0
- package/examples/08-light/index.html +477 -0
- package/package.json +34 -0
- package/src/bloom/bloom.js +467 -0
- package/src/bloom/bloom.wgsl +176 -0
- package/src/cobalt.js +231 -0
- package/src/create-texture-from-buffer.js +39 -0
- package/src/create-texture-from-url.js +35 -0
- package/src/create-texture.js +46 -0
- package/src/deps.js +3 -0
- package/src/displacement/composition.wgsl +58 -0
- package/src/displacement/displacement-composition.ts +161 -0
- package/src/displacement/displacement-parameters-buffer.ts +44 -0
- package/src/displacement/displacement-texture.ts +221 -0
- package/src/displacement/displacement.js +160 -0
- package/src/displacement/displacement.wgsl +31 -0
- package/src/displacement/triangles-buffer.ts +95 -0
- package/src/fb-blit/fb-blit.js +161 -0
- package/src/fb-blit/fb-blit.wgsl +40 -0
- package/src/fb-texture/fb-texture.js +56 -0
- package/src/light/README.md +61 -0
- package/src/light/light.js +148 -0
- package/src/light/lights-buffer.ts +98 -0
- package/src/light/lights-renderer.ts +278 -0
- package/src/light/public-api.js +20 -0
- package/src/light/readme/01_illumination.webp +0 -0
- package/src/light/readme/02_lights_texture.webp +0 -0
- package/src/light/readme/03_lights_texture_decomposed.webp +0 -0
- package/src/light/readme/04_lights_texture_mask.webp +0 -0
- package/src/light/readme/05_lights_obstacle_decomposition.webp +0 -0
- package/src/light/readme/06_lights_hard_cast_shadows.webp +0 -0
- package/src/light/texture/lights-texture-initializer.ts +191 -0
- package/src/light/texture/lights-texture-mask.ts +286 -0
- package/src/light/texture/lights-texture.ts +121 -0
- package/src/light/types.ts +23 -0
- package/src/light/viewport.ts +63 -0
- package/src/overlay/constants.js +1 -0
- package/src/overlay/overlay.js +341 -0
- package/src/overlay/overlay.wgsl +88 -0
- package/src/primitives/constants.js +1 -0
- package/src/primitives/primitives.js +252 -0
- package/src/primitives/primitives.wgsl +54 -0
- package/src/primitives/public-api.js +325 -0
- package/src/scene-composite/scene-composite.js +168 -0
- package/src/scene-composite/scene-composite.wgsl +94 -0
- package/src/sprite/constants.js +1 -0
- package/src/sprite/create-sprite-quads.js +60 -0
- package/src/sprite/public-api.js +215 -0
- package/src/sprite/read-spritesheet.js +103 -0
- package/src/sprite/sorted-binary-insert.js +45 -0
- package/src/sprite/sprite.js +268 -0
- package/src/sprite/sprite.wgsl +103 -0
- package/src/sprite/spritesheet.js +212 -0
- package/src/tile/atlas.js +193 -0
- package/src/tile/tile.js +171 -0
- package/src/tile/tile.wgsl +105 -0
- package/src/uuid.js +3 -0
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { vec3 } from './deps.js'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default function componentTransform (obj) {
|
|
5
|
+
return {
|
|
6
|
+
position: obj.position || vec3.create(0, 0, 0),
|
|
7
|
+
rotation: obj.rotation || 0,
|
|
8
|
+
|
|
9
|
+
// if set, this means the position is relative to another transform
|
|
10
|
+
// rather than being in world space
|
|
11
|
+
relativeTo: obj.relativeTo,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as Cobalt from '../../bundle.js'
|
|
2
|
+
import animationComponent from './component-animation.js'
|
|
3
|
+
import transformComponent from './component-transform.js'
|
|
4
|
+
import { ECS, vec3, vec4 } from './deps.js'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export default function spriteEntity (world, opts) {
|
|
8
|
+
const ENTITY = ECS.createEntity(world)
|
|
9
|
+
|
|
10
|
+
ECS.addComponentToEntity(world, ENTITY, 'transform', transformComponent({
|
|
11
|
+
position: vec3.create(opts.position[0], opts.position[1], 0)
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
ECS.addComponentToEntity(world, ENTITY, 'animation', animationComponent({
|
|
15
|
+
name: opts.name
|
|
16
|
+
}))
|
|
17
|
+
|
|
18
|
+
const tint = vec4.create(0, 0, 0, 0)
|
|
19
|
+
|
|
20
|
+
const opacity = opts.opacity ?? 255
|
|
21
|
+
|
|
22
|
+
const spriteNode = opts.spriteNode
|
|
23
|
+
|
|
24
|
+
const cobaltSpriteId = spriteNode.addSprite(opts.name,
|
|
25
|
+
opts.position,
|
|
26
|
+
vec3.create(1, 1, 1),
|
|
27
|
+
tint,
|
|
28
|
+
opacity / 255,
|
|
29
|
+
opts.rotation || 0,
|
|
30
|
+
opts.zIndex)
|
|
31
|
+
|
|
32
|
+
ECS.addComponentToEntity(world, ENTITY, 'sprite', {
|
|
33
|
+
name: opts.name,
|
|
34
|
+
|
|
35
|
+
layer: opts.layer,
|
|
36
|
+
rotation: 0, // radians
|
|
37
|
+
scale: vec3.create(1, 1, 1),
|
|
38
|
+
opacity: 1, // 0 is transparent, 1 is opaque
|
|
39
|
+
tint: vec4.create(0, 0, 0, 0),
|
|
40
|
+
|
|
41
|
+
// cobalt references
|
|
42
|
+
cobaltSpriteId,
|
|
43
|
+
spriteNode,
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
return ENTITY
|
|
47
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<title class="titleText">Web GPU prototyping</title>
|
|
6
|
+
<meta name="description" content="Web GPU 2d cobalt" />
|
|
7
|
+
<meta name="author" content="Michael Reinstein" />
|
|
8
|
+
<meta name="viewport" content="width=device-width" />
|
|
9
|
+
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
|
|
10
|
+
|
|
11
|
+
<meta name="apple-mobile-web-app-capable" content="yes" />
|
|
12
|
+
<meta name="mobile-web-app-capable" content="yes" />
|
|
13
|
+
|
|
14
|
+
<style>
|
|
15
|
+
|
|
16
|
+
body {
|
|
17
|
+
padding-inline: 40px;
|
|
18
|
+
margin: 0;
|
|
19
|
+
overscroll-behavior: none;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
img {
|
|
23
|
+
image-rendering: -moz-crisp-edges;
|
|
24
|
+
image-rendering: -webkit-crisp-edges;
|
|
25
|
+
image-rendering: pixelated;
|
|
26
|
+
image-rendering: crisp-edges;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.viewport-container {
|
|
30
|
+
position: fixed;
|
|
31
|
+
top: 0;
|
|
32
|
+
left: 0;
|
|
33
|
+
bottom: 0;
|
|
34
|
+
right: 0;
|
|
35
|
+
|
|
36
|
+
display: flex;
|
|
37
|
+
align-items: center;
|
|
38
|
+
justify-content: center;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
canvas {
|
|
42
|
+
background-color: green;
|
|
43
|
+
border: none;
|
|
44
|
+
image-rendering: -moz-crisp-edges;
|
|
45
|
+
image-rendering: -webkit-crisp-edges;
|
|
46
|
+
image-rendering: pixelated;
|
|
47
|
+
image-rendering: crisp-edges;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
canvas::-webkit-scrollbar {
|
|
51
|
+
display: none;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
</style>
|
|
55
|
+
</head>
|
|
56
|
+
<body>
|
|
57
|
+
<h1>Bloom Node</h1>
|
|
58
|
+
<p>
|
|
59
|
+
That classic beautiful glow.
|
|
60
|
+
</p>
|
|
61
|
+
<div class="viewport-container">
|
|
62
|
+
<canvas id="viewport" width="480" height="270"></canvas>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<script type="module">
|
|
66
|
+
import Game from './Game.js'
|
|
67
|
+
import * as Cobalt from '../../bundle.js'
|
|
68
|
+
import constants from './constants.js'
|
|
69
|
+
import dat from 'https://cdn.skypack.dev/pin/dat.gui@v0.7.9-2wtQAdFH5SRwnJLDWGNz/mode=imports,min/optimized/dat.gui.js'
|
|
70
|
+
import debounce from 'https://cdn.skypack.dev/pin/lodash.debounce@v4.0.8-4GXU9B066R3Th6HmjZmO/lodash.debounce.js'
|
|
71
|
+
//import animationSystem from './system-animation.js'
|
|
72
|
+
import rendererSystem from './system-renderer.js'
|
|
73
|
+
import spriteEntity from './entity-sprite.js'
|
|
74
|
+
import { ECS, vec2 } from './deps.js'
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
async function main () {
|
|
78
|
+
|
|
79
|
+
const canvas = document.querySelector('canvas')
|
|
80
|
+
|
|
81
|
+
const viewportWidth = constants.GAME_WIDTH
|
|
82
|
+
const viewportHeight = constants.GAME_HEIGHT
|
|
83
|
+
Game.renderer = await Cobalt.init(canvas, viewportWidth, viewportHeight)
|
|
84
|
+
|
|
85
|
+
// instantiate all resource nodes
|
|
86
|
+
const hdrTex = await Cobalt.initNode(Game.renderer, {
|
|
87
|
+
type: 'cobalt:fbTexture',
|
|
88
|
+
refs: { },
|
|
89
|
+
options: {
|
|
90
|
+
label: 'hdr color texture',
|
|
91
|
+
format: 'rgba16float',
|
|
92
|
+
mip_count: 1,
|
|
93
|
+
viewportScale: 1.0,
|
|
94
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.TEXTURE_BINDING,
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const spritesheet = await Cobalt.initNode(Game.renderer, {
|
|
99
|
+
type: 'cobalt:spritesheet',
|
|
100
|
+
refs: { },
|
|
101
|
+
options: {
|
|
102
|
+
spriteSheetJsonUrl: './assets/spritesheet.json',
|
|
103
|
+
colorTextureUrl: 'assets/spritesheet.png',
|
|
104
|
+
emissiveTextureUrl: 'assets/spritesheet_emissive.png'
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
const emissiveTex = await Cobalt.initNode(Game.renderer, {
|
|
109
|
+
type: 'cobalt:fbTexture',
|
|
110
|
+
refs: { },
|
|
111
|
+
options: {
|
|
112
|
+
label: 'hdr emissive texture',
|
|
113
|
+
format: 'rgba16float',
|
|
114
|
+
mip_count: 1,
|
|
115
|
+
viewportScale: 1.0,
|
|
116
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
const bloomTex = await Cobalt.initNode(Game.renderer, {
|
|
121
|
+
type: 'cobalt:fbTexture',
|
|
122
|
+
refs: { },
|
|
123
|
+
options: {
|
|
124
|
+
label: 'hdr bloom texture',
|
|
125
|
+
format: 'rgba16float',
|
|
126
|
+
mip_count: 7,
|
|
127
|
+
viewportScale: 0.5,
|
|
128
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.TEXTURE_BINDING,
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
const spriteNode = await Cobalt.initNode(Game.renderer, {
|
|
134
|
+
type: 'cobalt:sprite',
|
|
135
|
+
refs: {
|
|
136
|
+
// key is the var name defined in this node
|
|
137
|
+
// value is the var name in the cobalt resources dictionary
|
|
138
|
+
spritesheet: spritesheet,
|
|
139
|
+
emissive: emissiveTex,
|
|
140
|
+
hdr: hdrTex,
|
|
141
|
+
},
|
|
142
|
+
options: {
|
|
143
|
+
loadOp: 'clear',
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
const bloomNode = await Cobalt.initNode(Game.renderer, {
|
|
149
|
+
type: 'cobalt:bloom',
|
|
150
|
+
refs: {
|
|
151
|
+
// key is the var name defined in this node
|
|
152
|
+
// value is the var name in the cobalt resources dictionary
|
|
153
|
+
emissive: emissiveTex,
|
|
154
|
+
hdr: hdrTex,
|
|
155
|
+
bloom: bloomTex
|
|
156
|
+
},
|
|
157
|
+
options: {
|
|
158
|
+
// any extra options you want to pass to this node
|
|
159
|
+
bloom_intensity: 45.0,
|
|
160
|
+
bloom_combine_constant: 0.3,
|
|
161
|
+
bloom_knee: 0.2,
|
|
162
|
+
bloom_threshold: 0.1, // 1.0
|
|
163
|
+
|
|
164
|
+
// sprite instance 1
|
|
165
|
+
sprite_instances: [
|
|
166
|
+
{
|
|
167
|
+
emissive_intensity: 1.0,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
emissive_intensity: 0.5,
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
}
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
const tmpTex = await Cobalt.initNode(Game.renderer, {
|
|
177
|
+
type: 'cobalt:fbTexture',
|
|
178
|
+
refs: { },
|
|
179
|
+
options: {
|
|
180
|
+
label: 'bloom + hdr compositing',
|
|
181
|
+
format: 'bgra8unorm',
|
|
182
|
+
mip_count: 1,
|
|
183
|
+
viewportScale: 1.0,
|
|
184
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
const compositeNode = await Cobalt.initNode(Game.renderer, {
|
|
189
|
+
type: 'cobalt:composite',
|
|
190
|
+
refs: {
|
|
191
|
+
hdr: hdrTex,
|
|
192
|
+
bloom: bloomTex,
|
|
193
|
+
combined: tmpTex,
|
|
194
|
+
},
|
|
195
|
+
options: { }
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
const b = await Cobalt.initNode(Game.renderer, {
|
|
200
|
+
type: 'cobalt:fbBlit',
|
|
201
|
+
refs: {
|
|
202
|
+
in: tmpTex,
|
|
203
|
+
out: 'FRAME_TEXTURE_VIEW',
|
|
204
|
+
},
|
|
205
|
+
options: { }
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
// use resizeViewport to init values on load:
|
|
211
|
+
resizeViewport(Game.renderer, window.innerWidth, window.innerHeight)
|
|
212
|
+
|
|
213
|
+
// window resize is *expensive* - best to debounce:
|
|
214
|
+
const debouncedResize = debounce(function () {
|
|
215
|
+
resizeViewport(Game.renderer, window.innerWidth, window.innerHeight)
|
|
216
|
+
}, 50)
|
|
217
|
+
|
|
218
|
+
window.addEventListener('resize', debouncedResize, { passive: true })
|
|
219
|
+
|
|
220
|
+
const gui = new dat.GUI()
|
|
221
|
+
|
|
222
|
+
const config = bloomNode.options
|
|
223
|
+
|
|
224
|
+
gui.add(config, 'bloom_intensity', 0, 200).onChange((v) => {
|
|
225
|
+
updateFinalParams(config.bloom_intensity, config.bloom_combine_constant, config.bloom_knee, config.bloom_threshold)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
gui.add(config, 'bloom_combine_constant', 0.0, 1.0).onChange((v) => {
|
|
229
|
+
updateFinalParams(config.bloom_intensity, config.bloom_combine_constant, config.bloom_knee, config.bloom_threshold)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
gui.add(config, 'bloom_knee', 0.0, 50.0).onChange((v) => {
|
|
233
|
+
updateFinalParams(config.bloom_intensity, config.bloom_combine_constant, config.bloom_knee, config.bloom_threshold)
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
gui.add(config, 'bloom_threshold', 0.0, 1.0).onChange((v) => {
|
|
237
|
+
updateFinalParams(config.bloom_intensity, config.bloom_combine_constant, config.bloom_knee, config.bloom_threshold)
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
const updateFinalParams = function (bloom_intensity=40.0, bloom_combine_constant=0.68, bloom_knee=0.2, bloom_threshold=0.1) {
|
|
241
|
+
const { device } = Game.renderer
|
|
242
|
+
|
|
243
|
+
device.queue.writeBuffer(
|
|
244
|
+
compositeNode.data.params_buf,
|
|
245
|
+
0,
|
|
246
|
+
new Float32Array([ bloom_intensity, bloom_combine_constant ])
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
device.queue.writeBuffer(
|
|
250
|
+
bloomNode.data.params_buf,
|
|
251
|
+
0,
|
|
252
|
+
new Float32Array([ bloom_threshold,
|
|
253
|
+
bloom_threshold - bloom_knee,
|
|
254
|
+
bloom_knee * 2.0,
|
|
255
|
+
0.25 / bloom_knee,
|
|
256
|
+
bloom_combine_constant,
|
|
257
|
+
// require byte alignment bs
|
|
258
|
+
0,
|
|
259
|
+
0,
|
|
260
|
+
0, ])
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
const world = ECS.createWorld()
|
|
266
|
+
ECS.addSystem(world, rendererSystem(Game.renderer))
|
|
267
|
+
|
|
268
|
+
Cobalt.setViewportPosition(Game.renderer, [ 240, 135 ])
|
|
269
|
+
|
|
270
|
+
ECS.addSystem(world, function cameraLoopSystem(world) {
|
|
271
|
+
const onUpdate = function (dt) {
|
|
272
|
+
const elapsed = performance.now()
|
|
273
|
+
const x = (Math.sin(elapsed / 2000) * 0.5 + 0.5) * 128
|
|
274
|
+
const y = (Math.sin(elapsed / 5000) * 0.5 + 0.5) * 170
|
|
275
|
+
|
|
276
|
+
//const { tileScale } = Game.renderer.tile
|
|
277
|
+
|
|
278
|
+
//console.log(x, y)
|
|
279
|
+
Cobalt.setViewportPosition(Game.renderer, [ x, y ])
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return { onUpdate }
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
Game.world = world
|
|
286
|
+
|
|
287
|
+
// test sprites
|
|
288
|
+
const s2 = spriteEntity(world, {
|
|
289
|
+
name: 'health_empty-0.png',
|
|
290
|
+
position: [ 50, 50 ],
|
|
291
|
+
zIndex: 4,
|
|
292
|
+
emissiveIntensity: 0.5,
|
|
293
|
+
spriteNode,
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
const s = spriteEntity(world, {
|
|
297
|
+
name: 'health-0.png',
|
|
298
|
+
position: [ 240, 135 ],
|
|
299
|
+
zIndex: 4,
|
|
300
|
+
emissiveIntensity: 0.5,
|
|
301
|
+
spriteNode,
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
const gameLoop = function () {
|
|
306
|
+
tick(Game)
|
|
307
|
+
requestAnimationFrame(gameLoop)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
requestAnimationFrame(gameLoop)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
function tick (context) {
|
|
316
|
+
const newTime = performance.now()
|
|
317
|
+
const frameTime = newTime - context.lastFrameTime
|
|
318
|
+
context.lastFrameTime = newTime
|
|
319
|
+
ECS.update(context.world, frameTime)
|
|
320
|
+
ECS.cleanup(context.world)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
function resizeViewport (renderer, width, height) {
|
|
325
|
+
|
|
326
|
+
const { canvas, device } = renderer
|
|
327
|
+
|
|
328
|
+
// determine which screen dimension is most constrained
|
|
329
|
+
// we floor the render scale to an integer because we get weird texture artifacts when trying to render at
|
|
330
|
+
// certain float values (e.g., 3.0145833333333334)
|
|
331
|
+
const renderScale = Math.floor(Math.min(width/constants.GAME_WIDTH, height/constants.GAME_HEIGHT))
|
|
332
|
+
|
|
333
|
+
canvas.width = Math.ceil(constants.GAME_WIDTH)
|
|
334
|
+
canvas.height = Math.ceil(constants.GAME_HEIGHT)
|
|
335
|
+
|
|
336
|
+
Cobalt.setViewportDimensions(renderer, constants.GAME_WIDTH, constants.GAME_HEIGHT)
|
|
337
|
+
|
|
338
|
+
// https://www.khronos.org/webgl/wiki/HandlingHighDPI
|
|
339
|
+
// webgl display resolution size within canvas
|
|
340
|
+
const resolution = window.devicePixelRatio || 1
|
|
341
|
+
|
|
342
|
+
// center the canvas if native window doesn't match aspect ratio
|
|
343
|
+
canvas.style.width = canvas.width * renderScale + 'px'
|
|
344
|
+
canvas.style.height = canvas.height * renderScale + 'px'
|
|
345
|
+
|
|
346
|
+
canvas.style.left = Math.round((width - canvas.width * renderScale) / 2) + 'px'
|
|
347
|
+
canvas.style.top = Math.round((height - canvas.height * renderScale) / 2) + 'px'
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
main()
|
|
352
|
+
|
|
353
|
+
</script>
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
</body>
|
|
357
|
+
</html>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as Cobalt from '../../bundle.js'
|
|
2
|
+
import Game from './Game.js'
|
|
3
|
+
import { ECS } from './deps.js'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const SPRITE_QUERY = [ 'sprite' ]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// @param Object renderer Cobalt render state
|
|
10
|
+
export default function createRendererSystem (renderer) {
|
|
11
|
+
|
|
12
|
+
// temporary variables, allocated once to avoid garbage collection
|
|
13
|
+
const buf = new Float32Array(136) // tile instance data stored in a UBO
|
|
14
|
+
const removedEntities = {
|
|
15
|
+
count: 0,
|
|
16
|
+
entries: new Array(100)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return function rendererSystem (world) {
|
|
20
|
+
|
|
21
|
+
const onUpdate = function (/*dt*/) {
|
|
22
|
+
|
|
23
|
+
const device = renderer.device
|
|
24
|
+
const context = renderer.context
|
|
25
|
+
|
|
26
|
+
ECS.getEntities(world, SPRITE_QUERY, 'removed', removedEntities)
|
|
27
|
+
|
|
28
|
+
for (let i=0; i < removedEntities.count; i++) {
|
|
29
|
+
const oldSprite = removedEntities.entries[i]
|
|
30
|
+
oldSprite.spriteNode.removeSprite(oldSprite.sprite.cobaltSpriteId)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Cobalt.draw(Game.renderer)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return { onUpdate }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { vec3 } from './deps.js'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export default function componentTransform (obj) {
|
|
5
|
+
return {
|
|
6
|
+
position: obj.position || vec3.create(0, 0, 0),
|
|
7
|
+
rotation: obj.rotation || 0,
|
|
8
|
+
|
|
9
|
+
// if set, this means the position is relative to another transform
|
|
10
|
+
// rather than being in world space
|
|
11
|
+
relativeTo: obj.relativeTo,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as Cobalt from '../../bundle.js'
|
|
2
|
+
import animationComponent from './component-animation.js'
|
|
3
|
+
import transformComponent from './component-transform.js'
|
|
4
|
+
import { ECS, vec3, vec4 } from './deps.js'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export default function spriteEntity (world, opts) {
|
|
8
|
+
const ENTITY = ECS.createEntity(world)
|
|
9
|
+
|
|
10
|
+
ECS.addComponentToEntity(world, ENTITY, 'transform', transformComponent({
|
|
11
|
+
position: vec3.create(opts.position[0], opts.position[1], 0)
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
ECS.addComponentToEntity(world, ENTITY, 'animation', animationComponent({
|
|
15
|
+
name: opts.name
|
|
16
|
+
}))
|
|
17
|
+
|
|
18
|
+
const tint = vec4.create(0, 0, 0, 0)
|
|
19
|
+
|
|
20
|
+
const opacity = opts.opacity ?? 255
|
|
21
|
+
|
|
22
|
+
const spriteNode = opts.spriteNode
|
|
23
|
+
|
|
24
|
+
const cobaltSpriteId = spriteNode.addSprite(opts.name,
|
|
25
|
+
opts.position,
|
|
26
|
+
vec3.create(1, 1, 1),
|
|
27
|
+
tint,
|
|
28
|
+
opacity / 255,
|
|
29
|
+
opts.rotation || 0,
|
|
30
|
+
opts.zIndex)
|
|
31
|
+
|
|
32
|
+
ECS.addComponentToEntity(world, ENTITY, 'sprite', {
|
|
33
|
+
name: opts.name,
|
|
34
|
+
|
|
35
|
+
layer: opts.layer,
|
|
36
|
+
rotation: 0, // radians
|
|
37
|
+
scale: vec3.create(1, 1, 1),
|
|
38
|
+
opacity: 1, // 0 is transparent, 1 is opaque
|
|
39
|
+
tint: vec4.create(0, 0, 0, 0),
|
|
40
|
+
|
|
41
|
+
// cobalt references
|
|
42
|
+
cobaltSpriteId,
|
|
43
|
+
spriteNode,
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
return ENTITY
|
|
47
|
+
}
|