@footgun/cobalt 0.6.14 → 0.7.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/CHANGELOG.md +19 -0
- package/bundle.js +1318 -1304
- package/examples/01-primitives/index.html +1 -1
- package/examples/02-sprites/entity-sprite.js +10 -15
- package/examples/02-sprites/hdr.html +321 -0
- package/examples/02-sprites/index.html +21 -120
- package/examples/02-sprites/system-renderer.js +2 -2
- package/examples/03-tiles/index.html +87 -32
- package/examples/03-tiles/system-renderer.js +2 -2
- package/examples/04-overlay/index.html +178 -21
- package/examples/05-bloom/index.html +23 -23
- package/examples/05-bloom/system-renderer.js +2 -2
- package/examples/06-displacement/index.html +20 -112
- package/examples/06-displacement/system-renderer.js +2 -2
- package/examples/08-light/index.html +51 -123
- package/package.json +1 -1
- package/src/cobalt.js +8 -8
- package/src/sprite/public-api.js +57 -177
- package/src/sprite/sprite.js +301 -177
- package/src/sprite/sprite.wgsl +68 -87
- package/src/sprite-hdr/public-api.js +95 -0
- package/src/sprite-hdr/sprite.js +414 -0
- package/src/sprite-hdr/sprite.wgsl +101 -0
- package/src/{sprite → spritesheet}/create-sprite-quads.js +11 -11
- package/src/{sprite → spritesheet}/read-spritesheet.js +62 -28
- package/src/spritesheet/spritesheet.js +75 -0
- package/src/{tile → tile-hdr}/atlas.js +5 -3
- package/src/{tile → tile-hdr}/tile.js +15 -6
- package/examples/04-overlay/deps.js +0 -1
- package/src/overlay/constants.js +0 -1
- package/src/overlay/overlay.js +0 -343
- package/src/overlay/overlay.wgsl +0 -88
- package/src/sprite/constants.js +0 -1
- package/src/sprite/sorted-binary-insert.js +0 -45
- package/src/sprite/spritesheet.js +0 -215
- /package/examples/02-sprites/{Game.js → Global.js} +0 -0
- /package/examples/03-tiles/{Game.js → Global.js} +0 -0
- /package/examples/05-bloom/{Game.js → Global.js} +0 -0
- /package/examples/06-displacement/{Game.js → Global.js} +0 -0
- /package/examples/08-light/{Game.js → Global.js} +0 -0
- /package/src/{tile → tile-hdr}/tile.wgsl +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Cobalt from '../../bundle.js'
|
|
2
|
-
import
|
|
2
|
+
import Global from './Global.js'
|
|
3
3
|
import { ECS } from './deps.js'
|
|
4
4
|
|
|
5
5
|
|
|
@@ -30,7 +30,7 @@ export default function createRendererSystem (renderer) {
|
|
|
30
30
|
oldSprite.spriteNode.removeSprite(oldSprite.sprite.cobaltSpriteId)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
Cobalt.draw(
|
|
33
|
+
Cobalt.draw(Global.renderer)
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
return { onUpdate }
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<html lang="en">
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
|
-
<title class="titleText">
|
|
5
|
+
<title class="titleText">WebGPU - Cobalt displacement postprocessing</title>
|
|
6
6
|
<meta name="description" content="Web GPU 2d cobalt" />
|
|
7
7
|
<meta name="author" content="Michael Reinstein" />
|
|
8
8
|
<meta name="viewport" content="width=device-width" />
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
</div>
|
|
64
64
|
|
|
65
65
|
<script type="module">
|
|
66
|
-
import
|
|
66
|
+
import Global from './Global.js'
|
|
67
67
|
import * as Cobalt from '../../bundle.js'
|
|
68
68
|
import constants from './constants.js'
|
|
69
69
|
import dat from 'https://cdn.skypack.dev/pin/dat.gui@v0.7.9-2wtQAdFH5SRwnJLDWGNz/mode=imports,min/optimized/dat.gui.js'
|
|
@@ -79,21 +79,22 @@ async function main () {
|
|
|
79
79
|
|
|
80
80
|
const viewportWidth = constants.GAME_WIDTH
|
|
81
81
|
const viewportHeight = constants.GAME_HEIGHT
|
|
82
|
-
|
|
82
|
+
Global.renderer = await Cobalt.init(canvas, viewportWidth, viewportHeight)
|
|
83
83
|
|
|
84
84
|
// instantiate all resource nodes
|
|
85
|
-
const tileAtlasNode = await Cobalt.initNode(
|
|
85
|
+
const tileAtlasNode = await Cobalt.initNode(Global.renderer, {
|
|
86
86
|
type: 'cobalt:tileAtlas',
|
|
87
87
|
refs: { },
|
|
88
88
|
options: {
|
|
89
89
|
label: 'tile atlas',
|
|
90
90
|
tileSize: 16,
|
|
91
91
|
tileScale: 1.0,
|
|
92
|
-
textureUrl: 'assets/spelunky-tiles.png'
|
|
92
|
+
textureUrl: 'assets/spelunky-tiles.png',
|
|
93
|
+
outputFormat: 'rgba16float',
|
|
93
94
|
}
|
|
94
95
|
})
|
|
95
96
|
|
|
96
|
-
const hdrTex = await Cobalt.initNode(
|
|
97
|
+
const hdrTex = await Cobalt.initNode(Global.renderer, {
|
|
97
98
|
type: 'cobalt:fbTexture',
|
|
98
99
|
refs: { },
|
|
99
100
|
options: {
|
|
@@ -105,75 +106,11 @@ async function main () {
|
|
|
105
106
|
}
|
|
106
107
|
})
|
|
107
108
|
|
|
108
|
-
const
|
|
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
|
-
const bloomNode = await Cobalt.initNode(Game.renderer, {
|
|
133
|
-
type: 'cobalt:bloom',
|
|
134
|
-
refs: {
|
|
135
|
-
// key is the var name defined in this node
|
|
136
|
-
// value is the var name in the cobalt resources dictionary
|
|
137
|
-
emissive: emissiveTex,
|
|
138
|
-
hdr: hdrTex,
|
|
139
|
-
bloom: bloomTex
|
|
140
|
-
},
|
|
141
|
-
options: {
|
|
142
|
-
// any extra options you want to pass to this node
|
|
143
|
-
bloom_intensity: 0.0,
|
|
144
|
-
bloom_combine_constant: 0.0,
|
|
145
|
-
bloom_knee: 0.2,
|
|
146
|
-
bloom_threshold: 0.1, // 1.0
|
|
147
|
-
|
|
148
|
-
// sprite instance 1
|
|
149
|
-
sprite_instances: [
|
|
150
|
-
{
|
|
151
|
-
emissive_intensity: 1.0,
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
emissive_intensity: 0.5,
|
|
155
|
-
},
|
|
156
|
-
],
|
|
157
|
-
}
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
const tmpTex = await Cobalt.initNode(Game.renderer, {
|
|
161
|
-
type: 'cobalt:fbTexture',
|
|
162
|
-
refs: { },
|
|
163
|
-
options: {
|
|
164
|
-
label: 'bloom + hdr compositing',
|
|
165
|
-
format: 'PREFERRED_TEXTURE_FORMAT',
|
|
166
|
-
mip_count: 1,
|
|
167
|
-
viewportScale: 1.0,
|
|
168
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
|
|
169
|
-
}
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
const perlinTex = await Cobalt.createTextureFromUrl(Game.renderer, 'displacement map', './assets/displacement_map_repeat.jpg')
|
|
109
|
+
const perlinTex = await Cobalt.createTextureFromUrl(Global.renderer, 'displacement map', './assets/displacement_map_repeat.jpg')
|
|
173
110
|
|
|
174
111
|
// instantiate all runnable nodes
|
|
175
|
-
const tileNode = await Cobalt.initNode(
|
|
176
|
-
type: 'cobalt:
|
|
112
|
+
const tileNode = await Cobalt.initNode(Global.renderer, {
|
|
113
|
+
type: 'cobalt:tileHDR',
|
|
177
114
|
refs: {
|
|
178
115
|
// key is the var name defined in this node
|
|
179
116
|
// value is the var name in the cobalt resources dictionary
|
|
@@ -183,12 +120,11 @@ async function main () {
|
|
|
183
120
|
options: {
|
|
184
121
|
textureUrl: 'assets/spelunky1.png',
|
|
185
122
|
scrollScale: 0.6,
|
|
186
|
-
//zIndex: 0,
|
|
187
123
|
}
|
|
188
124
|
})
|
|
189
125
|
|
|
190
|
-
const tileNode2 = await Cobalt.initNode(
|
|
191
|
-
type: 'cobalt:
|
|
126
|
+
const tileNode2 = await Cobalt.initNode(Global.renderer, {
|
|
127
|
+
type: 'cobalt:tileHDR',
|
|
192
128
|
refs: {
|
|
193
129
|
// key is the var name defined in this node
|
|
194
130
|
// value is the var name in the cobalt resources dictionary
|
|
@@ -198,36 +134,13 @@ async function main () {
|
|
|
198
134
|
options: {
|
|
199
135
|
textureUrl: 'assets/spelunky0.png',
|
|
200
136
|
scrollScale: 1.0,
|
|
201
|
-
//zIndex: 5,
|
|
202
|
-
}
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const compositeNode = await Cobalt.initNode(Game.renderer, {
|
|
207
|
-
type: 'cobalt:composite',
|
|
208
|
-
refs: {
|
|
209
|
-
hdr: hdrTex,
|
|
210
|
-
bloom: bloomTex,
|
|
211
|
-
combined: tmpTex,
|
|
212
|
-
},
|
|
213
|
-
options: {
|
|
214
|
-
bloom_combine_constant: 0.0,
|
|
215
137
|
}
|
|
216
138
|
})
|
|
217
139
|
|
|
218
|
-
const
|
|
219
|
-
type: 'cobalt:fbBlit',
|
|
220
|
-
refs: {
|
|
221
|
-
in: tmpTex,
|
|
222
|
-
out: 'FRAME_TEXTURE_VIEW',
|
|
223
|
-
},
|
|
224
|
-
options: { }
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
const displacement = await Cobalt.initNode(Game.renderer, {
|
|
140
|
+
const displacement = await Cobalt.initNode(Global.renderer, {
|
|
228
141
|
type: 'cobalt:displacement',
|
|
229
142
|
refs: {
|
|
230
|
-
color:
|
|
143
|
+
color: hdrTex,
|
|
231
144
|
map: perlinTex,
|
|
232
145
|
out: 'FRAME_TEXTURE_VIEW',
|
|
233
146
|
},
|
|
@@ -244,14 +157,12 @@ async function main () {
|
|
|
244
157
|
[ 310, 140 ],
|
|
245
158
|
])
|
|
246
159
|
|
|
247
|
-
|
|
248
|
-
|
|
249
160
|
// use resizeViewport to init values on load:
|
|
250
|
-
resizeViewport(
|
|
161
|
+
resizeViewport(Global.renderer, window.innerWidth, window.innerHeight)
|
|
251
162
|
|
|
252
163
|
// window resize is *expensive* - best to debounce:
|
|
253
164
|
const debouncedResize = debounce(function () {
|
|
254
|
-
resizeViewport(
|
|
165
|
+
resizeViewport(Global.renderer, window.innerWidth, window.innerHeight)
|
|
255
166
|
}, 50)
|
|
256
167
|
|
|
257
168
|
window.addEventListener('resize', debouncedResize, { passive: true })
|
|
@@ -273,12 +184,11 @@ async function main () {
|
|
|
273
184
|
displacement.data.displacementParameters.setParameters(displacement.options);
|
|
274
185
|
})
|
|
275
186
|
|
|
276
|
-
|
|
277
187
|
|
|
278
188
|
const world = ECS.createWorld()
|
|
279
|
-
ECS.addSystem(world, rendererSystem(
|
|
189
|
+
ECS.addSystem(world, rendererSystem(Global.renderer))
|
|
280
190
|
|
|
281
|
-
Cobalt.setViewportPosition(
|
|
191
|
+
Cobalt.setViewportPosition(Global.renderer, [ 240, 135 ])
|
|
282
192
|
|
|
283
193
|
ECS.addSystem(world, function displacementSystem (world) {
|
|
284
194
|
const onUpdate = function (dt) {
|
|
@@ -292,15 +202,13 @@ async function main () {
|
|
|
292
202
|
return { onUpdate }
|
|
293
203
|
})
|
|
294
204
|
|
|
295
|
-
|
|
296
|
-
|
|
205
|
+
Global.world = world
|
|
297
206
|
|
|
298
207
|
const gameLoop = function () {
|
|
299
|
-
tick(
|
|
208
|
+
tick(Global)
|
|
300
209
|
requestAnimationFrame(gameLoop)
|
|
301
210
|
}
|
|
302
211
|
|
|
303
|
-
|
|
304
212
|
requestAnimationFrame(gameLoop)
|
|
305
213
|
}
|
|
306
214
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Cobalt from '../../bundle.js'
|
|
2
|
-
import
|
|
2
|
+
import Global from './Global.js'
|
|
3
3
|
import { ECS } from './deps.js'
|
|
4
4
|
|
|
5
5
|
|
|
@@ -30,7 +30,7 @@ export default function createRendererSystem (renderer) {
|
|
|
30
30
|
oldSprite.spriteNode.removeSprite(oldSprite.sprite.cobaltSpriteId)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
Cobalt.draw(
|
|
33
|
+
Cobalt.draw(Global.renderer)
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
return { onUpdate }
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<html lang="en">
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
|
-
<title class="titleText">
|
|
5
|
+
<title class="titleText">WebGPU - Cobalt real time point lighting and shadow casting</title>
|
|
6
6
|
<meta name="description" content="Web GPU 2d cobalt" />
|
|
7
7
|
<meta name="author" content="Michael Reinstein" />
|
|
8
8
|
<meta name="viewport" content="width=device-width" />
|
|
@@ -55,12 +55,14 @@
|
|
|
55
55
|
</head>
|
|
56
56
|
<body>
|
|
57
57
|
|
|
58
|
+
Drag the mouse to create a shadow segment.
|
|
59
|
+
|
|
58
60
|
<div class="viewport-container">
|
|
59
61
|
<canvas id="viewport" width="480" height="270"></canvas>
|
|
60
62
|
</div>
|
|
61
63
|
|
|
62
64
|
<script type="module">
|
|
63
|
-
import
|
|
65
|
+
import Global from './Global.js'
|
|
64
66
|
import * as Cobalt from '../../bundle.js'
|
|
65
67
|
import constants from './constants.js'
|
|
66
68
|
import dat from 'https://cdn.skypack.dev/pin/dat.gui@v0.7.9-2wtQAdFH5SRwnJLDWGNz/mode=imports,min/optimized/dat.gui.js'
|
|
@@ -76,24 +78,25 @@ async function main () {
|
|
|
76
78
|
|
|
77
79
|
const viewportWidth = constants.GAME_WIDTH
|
|
78
80
|
const viewportHeight = constants.GAME_HEIGHT
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
Global.renderer = await Cobalt.init(canvas, viewportWidth, viewportHeight)
|
|
82
|
+
Global.renderer.viewport.zoom = 1;
|
|
81
83
|
|
|
82
84
|
const viewportCenter = [0, 0];
|
|
83
85
|
|
|
84
86
|
// instantiate all resource nodes
|
|
85
|
-
const tileAtlasNode = await Cobalt.initNode(
|
|
87
|
+
const tileAtlasNode = await Cobalt.initNode(Global.renderer, {
|
|
86
88
|
type: 'cobalt:tileAtlas',
|
|
87
89
|
refs: { },
|
|
88
90
|
options: {
|
|
89
91
|
label: 'tile atlas',
|
|
90
92
|
tileSize: 16,
|
|
91
93
|
tileScale: 1.0,
|
|
92
|
-
textureUrl: 'assets/spelunky-tiles.png'
|
|
94
|
+
textureUrl: 'assets/spelunky-tiles.png',
|
|
95
|
+
outputFormat: 'rgba16float',
|
|
93
96
|
}
|
|
94
97
|
})
|
|
95
98
|
|
|
96
|
-
const hdrTex = await Cobalt.initNode(
|
|
99
|
+
const hdrTex = await Cobalt.initNode(Global.renderer, {
|
|
97
100
|
type: 'cobalt:fbTexture',
|
|
98
101
|
refs: { },
|
|
99
102
|
options: {
|
|
@@ -105,7 +108,7 @@ async function main () {
|
|
|
105
108
|
}
|
|
106
109
|
})
|
|
107
110
|
|
|
108
|
-
const lightCompositeHdrTex = await Cobalt.initNode(
|
|
111
|
+
const lightCompositeHdrTex = await Cobalt.initNode(Global.renderer, {
|
|
109
112
|
type: 'cobalt:fbTexture',
|
|
110
113
|
refs: { },
|
|
111
114
|
options: {
|
|
@@ -117,73 +120,9 @@ async function main () {
|
|
|
117
120
|
}
|
|
118
121
|
})
|
|
119
122
|
|
|
120
|
-
const emissiveTex = await Cobalt.initNode(Game.renderer, {
|
|
121
|
-
type: 'cobalt:fbTexture',
|
|
122
|
-
refs: { },
|
|
123
|
-
options: {
|
|
124
|
-
label: 'hdr emissive texture',
|
|
125
|
-
format: 'rgba16float',
|
|
126
|
-
mip_count: 1,
|
|
127
|
-
viewportScale: 1.0,
|
|
128
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
|
|
129
|
-
}
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
const bloomTex = await Cobalt.initNode(Game.renderer, {
|
|
133
|
-
type: 'cobalt:fbTexture',
|
|
134
|
-
refs: { },
|
|
135
|
-
options: {
|
|
136
|
-
label: 'hdr bloom texture',
|
|
137
|
-
format: 'rgba16float',
|
|
138
|
-
mip_count: 7,
|
|
139
|
-
viewportScale: 0.5,
|
|
140
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.TEXTURE_BINDING,
|
|
141
|
-
}
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
const bloomNode = await Cobalt.initNode(Game.renderer, {
|
|
145
|
-
type: 'cobalt:bloom',
|
|
146
|
-
refs: {
|
|
147
|
-
// key is the var name defined in this node
|
|
148
|
-
// value is the var name in the cobalt resources dictionary
|
|
149
|
-
emissive: emissiveTex,
|
|
150
|
-
hdr: hdrTex,
|
|
151
|
-
bloom: bloomTex
|
|
152
|
-
},
|
|
153
|
-
options: {
|
|
154
|
-
// any extra options you want to pass to this node
|
|
155
|
-
bloom_intensity: 45.0,
|
|
156
|
-
bloom_combine_constant: 0.3,
|
|
157
|
-
bloom_knee: 0.2,
|
|
158
|
-
bloom_threshold: 0.1, // 1.0
|
|
159
|
-
|
|
160
|
-
// sprite instance 1
|
|
161
|
-
sprite_instances: [
|
|
162
|
-
{
|
|
163
|
-
emissive_intensity: 1.0,
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
emissive_intensity: 0.5,
|
|
167
|
-
},
|
|
168
|
-
],
|
|
169
|
-
}
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
const tmpTex = await Cobalt.initNode(Game.renderer, {
|
|
173
|
-
type: 'cobalt:fbTexture',
|
|
174
|
-
refs: { },
|
|
175
|
-
options: {
|
|
176
|
-
label: 'bloom + hdr compositing',
|
|
177
|
-
format: 'PREFERRED_TEXTURE_FORMAT',
|
|
178
|
-
mip_count: 1,
|
|
179
|
-
viewportScale: 1.0,
|
|
180
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
|
|
181
|
-
}
|
|
182
|
-
})
|
|
183
|
-
|
|
184
123
|
// instantiate all runnable nodes
|
|
185
|
-
const tileNode = await Cobalt.initNode(
|
|
186
|
-
type: 'cobalt:
|
|
124
|
+
const tileNode = await Cobalt.initNode(Global.renderer, {
|
|
125
|
+
type: 'cobalt:tileHDR',
|
|
187
126
|
refs: {
|
|
188
127
|
// key is the var name defined in this node
|
|
189
128
|
// value is the var name in the cobalt resources dictionary
|
|
@@ -193,12 +132,11 @@ async function main () {
|
|
|
193
132
|
options: {
|
|
194
133
|
textureUrl: 'assets/spelunky1.png',
|
|
195
134
|
scrollScale: 0.6,
|
|
196
|
-
//zIndex: 0,
|
|
197
135
|
}
|
|
198
136
|
})
|
|
199
137
|
|
|
200
|
-
const tileNode2 = await Cobalt.initNode(
|
|
201
|
-
type: 'cobalt:
|
|
138
|
+
const tileNode2 = await Cobalt.initNode(Global.renderer, {
|
|
139
|
+
type: 'cobalt:tileHDR',
|
|
202
140
|
refs: {
|
|
203
141
|
// key is the var name defined in this node
|
|
204
142
|
// value is the var name in the cobalt resources dictionary
|
|
@@ -208,28 +146,11 @@ async function main () {
|
|
|
208
146
|
options: {
|
|
209
147
|
textureUrl: 'assets/spelunky0.png',
|
|
210
148
|
scrollScale: 1.0,
|
|
211
|
-
//zIndex: 5,
|
|
212
149
|
}
|
|
213
150
|
})
|
|
214
151
|
|
|
215
|
-
|
|
216
|
-
const convertToViewportPosition = (clientPosition /* [number, number] */) /* [number, number] */ => {
|
|
217
|
-
const canvasBox = canvas.getBoundingClientRect();
|
|
218
|
-
return [
|
|
219
|
-
((clientPosition[0] - canvasBox.left) / canvasBox.width) * constants.GAME_WIDTH /Game.renderer.viewport.zoom,
|
|
220
|
-
((clientPosition[1] - canvasBox.top) / canvasBox.height) * constants.GAME_HEIGHT /Game.renderer.viewport.zoom,
|
|
221
|
-
];
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const convertToWorldPosition = (viewportPosition /* [number, number] */) /* [number, number] */ => {
|
|
225
|
-
return [
|
|
226
|
-
viewportPosition[0] + viewportCenter[0] - 0.5 * constants.GAME_WIDTH / Game.renderer.viewport.zoom,
|
|
227
|
-
viewportPosition[1] + viewportCenter[1] - 0.5 * constants.GAME_HEIGHT/ Game.renderer.viewport.zoom,
|
|
228
|
-
];
|
|
229
|
-
};
|
|
230
|
-
|
|
231
152
|
// lights and shadows node
|
|
232
|
-
const light = await Cobalt.initNode(
|
|
153
|
+
const light = await Cobalt.initNode(Global.renderer, {
|
|
233
154
|
type: 'cobalt:light',
|
|
234
155
|
refs: {
|
|
235
156
|
in: hdrTex,
|
|
@@ -238,6 +159,17 @@ async function main () {
|
|
|
238
159
|
options: {
|
|
239
160
|
}
|
|
240
161
|
})
|
|
162
|
+
|
|
163
|
+
const b = await Cobalt.initNode(Global.renderer, {
|
|
164
|
+
type: 'cobalt:fbBlit',
|
|
165
|
+
refs: {
|
|
166
|
+
in: lightCompositeHdrTex,
|
|
167
|
+
out: 'FRAME_TEXTURE_VIEW',
|
|
168
|
+
},
|
|
169
|
+
options: { }
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
|
|
241
173
|
light.setAmbientLight([0.3, 0.3, 0.3]);
|
|
242
174
|
|
|
243
175
|
const permanentObstacleSegments = [];
|
|
@@ -255,6 +187,22 @@ async function main () {
|
|
|
255
187
|
return segments;
|
|
256
188
|
|
|
257
189
|
};
|
|
190
|
+
|
|
191
|
+
const convertToViewportPosition = (clientPosition /* [number, number] */) /* [number, number] */ => {
|
|
192
|
+
const canvasBox = canvas.getBoundingClientRect();
|
|
193
|
+
return [
|
|
194
|
+
((clientPosition[0] - canvasBox.left) / canvasBox.width) * constants.GAME_WIDTH /Global.renderer.viewport.zoom,
|
|
195
|
+
((clientPosition[1] - canvasBox.top) / canvasBox.height) * constants.GAME_HEIGHT /Global.renderer.viewport.zoom,
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const convertToWorldPosition = (viewportPosition /* [number, number] */) /* [number, number] */ => {
|
|
200
|
+
return [
|
|
201
|
+
viewportPosition[0] + viewportCenter[0] - 0.5 * constants.GAME_WIDTH / Global.renderer.viewport.zoom,
|
|
202
|
+
viewportPosition[1] + viewportCenter[1] - 0.5 * constants.GAME_HEIGHT/ Global.renderer.viewport.zoom,
|
|
203
|
+
];
|
|
204
|
+
};
|
|
205
|
+
|
|
258
206
|
canvas.addEventListener("mouseenter", event => {
|
|
259
207
|
mouseViewportPosition = convertToViewportPosition([event.clientX, event.clientY]);
|
|
260
208
|
});
|
|
@@ -298,7 +246,7 @@ async function main () {
|
|
|
298
246
|
viewportCenter[0] -= movement[0];
|
|
299
247
|
viewportCenter[1] -= movement[1];
|
|
300
248
|
|
|
301
|
-
Cobalt.setViewportPosition(
|
|
249
|
+
Cobalt.setViewportPosition(Global.renderer, viewportCenter)
|
|
302
250
|
}
|
|
303
251
|
|
|
304
252
|
if (temporaryObstacleSegment) {
|
|
@@ -345,33 +293,13 @@ async function main () {
|
|
|
345
293
|
];
|
|
346
294
|
light.setLights(lights)
|
|
347
295
|
|
|
348
|
-
const compositeNode = await Cobalt.initNode(Game.renderer, {
|
|
349
|
-
type: 'cobalt:composite',
|
|
350
|
-
refs: {
|
|
351
|
-
hdr: lightCompositeHdrTex,
|
|
352
|
-
bloom: bloomTex,
|
|
353
|
-
combined: tmpTex,
|
|
354
|
-
},
|
|
355
|
-
options: { }
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
const b = await Cobalt.initNode(Game.renderer, {
|
|
360
|
-
type: 'cobalt:fbBlit',
|
|
361
|
-
refs: {
|
|
362
|
-
in: tmpTex,
|
|
363
|
-
out: 'FRAME_TEXTURE_VIEW',
|
|
364
|
-
},
|
|
365
|
-
options: { }
|
|
366
|
-
})
|
|
367
|
-
|
|
368
296
|
|
|
369
297
|
// use resizeViewport to init values on load:
|
|
370
|
-
resizeViewport(
|
|
298
|
+
resizeViewport(Global.renderer, window.innerWidth, window.innerHeight)
|
|
371
299
|
|
|
372
300
|
// window resize is *expensive* - best to debounce:
|
|
373
301
|
const debouncedResize = debounce(function () {
|
|
374
|
-
resizeViewport(
|
|
302
|
+
resizeViewport(Global.renderer, window.innerWidth, window.innerHeight)
|
|
375
303
|
}, 50)
|
|
376
304
|
|
|
377
305
|
window.addEventListener('resize', debouncedResize, { passive: true })
|
|
@@ -396,13 +324,13 @@ async function main () {
|
|
|
396
324
|
];
|
|
397
325
|
light.setLights(lights)
|
|
398
326
|
|
|
399
|
-
Cobalt.draw(
|
|
327
|
+
Cobalt.draw(Global.renderer)
|
|
400
328
|
}
|
|
401
329
|
|
|
402
330
|
return { onUpdate }
|
|
403
331
|
})
|
|
404
332
|
|
|
405
|
-
Cobalt.setViewportPosition(
|
|
333
|
+
Cobalt.setViewportPosition(Global.renderer, viewportCenter)
|
|
406
334
|
|
|
407
335
|
ECS.addSystem(world, function cameraLoopSystem(world) {
|
|
408
336
|
const onUpdate = function (dt) {
|
|
@@ -410,17 +338,17 @@ async function main () {
|
|
|
410
338
|
// const x = (Math.sin(elapsed / 2000) * 0.5 + 0.5) * 128
|
|
411
339
|
// const y = (Math.sin(elapsed / 5000) * 0.5 + 0.5) * 170
|
|
412
340
|
|
|
413
|
-
// Cobalt.setViewportPosition(
|
|
341
|
+
// Cobalt.setViewportPosition(Global.renderer, [ x, y ])
|
|
414
342
|
}
|
|
415
343
|
|
|
416
344
|
return { onUpdate }
|
|
417
345
|
})
|
|
418
346
|
|
|
419
|
-
|
|
347
|
+
Global.world = world
|
|
420
348
|
|
|
421
349
|
|
|
422
350
|
const gameLoop = function () {
|
|
423
|
-
tick(
|
|
351
|
+
tick(Global)
|
|
424
352
|
requestAnimationFrame(gameLoop)
|
|
425
353
|
}
|
|
426
354
|
|
package/package.json
CHANGED
package/src/cobalt.js
CHANGED
|
@@ -6,17 +6,17 @@ export { default as createTextureFromBuffer } from './create-texture-from-buffer
|
|
|
6
6
|
// built-in run nodes
|
|
7
7
|
import bloomNode from './bloom/bloom.js'
|
|
8
8
|
import compositeNode from './scene-composite/scene-composite.js'
|
|
9
|
-
import
|
|
10
|
-
import
|
|
9
|
+
import spriteHDRNode from './sprite-hdr/sprite.js'
|
|
10
|
+
import tileHDRNode from './tile-hdr/tile.js'
|
|
11
11
|
import displacementNode from './displacement/displacement.js'
|
|
12
|
-
import overlayNode from './overlay/overlay.js'
|
|
13
12
|
import fbBlitNode from './fb-blit/fb-blit.js'
|
|
14
13
|
import primitivesNode from './primitives/primitives.js'
|
|
15
14
|
import lightNode from './light/light.js'
|
|
15
|
+
import spriteNode from './sprite/sprite.js'
|
|
16
16
|
|
|
17
17
|
// built-in resource nodes
|
|
18
|
-
import tileAtlasNode from './tile/atlas.js'
|
|
19
|
-
import spritesheetNode from './
|
|
18
|
+
import tileAtlasNode from './tile-hdr/atlas.js'
|
|
19
|
+
import spritesheetNode from './spritesheet/spritesheet.js'
|
|
20
20
|
import fbTextureNode from './fb-texture/fb-texture.js'
|
|
21
21
|
|
|
22
22
|
|
|
@@ -70,12 +70,12 @@ export async function init (ctx, viewportWidth, viewportHeight) {
|
|
|
70
70
|
'cobalt:fbTexture': fbTextureNode,
|
|
71
71
|
|
|
72
72
|
// builtin run nodes
|
|
73
|
+
'cobalt:sprite': spriteNode,
|
|
73
74
|
'cobalt:bloom': bloomNode,
|
|
74
75
|
'cobalt:composite': compositeNode,
|
|
75
|
-
'cobalt:
|
|
76
|
-
'cobalt:
|
|
76
|
+
'cobalt:spriteHDR': spriteHDRNode,
|
|
77
|
+
'cobalt:tileHDR': tileHDRNode,
|
|
77
78
|
'cobalt:displacement': displacementNode,
|
|
78
|
-
'cobalt:overlay': overlayNode,
|
|
79
79
|
'cobalt:fbBlit': fbBlitNode,
|
|
80
80
|
'cobalt:primitives': primitivesNode,
|
|
81
81
|
'cobalt:light': lightNode,
|