@footgun/cobalt 0.7.0 → 0.7.2

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.
@@ -1,7 +1,6 @@
1
1
  import createTextureFromBuffer from '../create-texture-from-buffer.js'
2
- import createTextureFromUrl from '../create-texture-from-url.js'
3
- import getPreferredFormat from '../get-preferred-format.js'
4
-
2
+ import createTextureFromUrl from '../create-texture-from-url.js'
3
+ import getPreferredFormat from '../get-preferred-format.js'
5
4
 
6
5
  /*
7
6
  Tile layers are totally static, and there are usually many of them on several layers.
@@ -16,17 +15,16 @@ Because this processing can happen completely in the fragment shader, there's no
16
15
  Inspired by/ported from https://blog.tojicode.com/2012/07/sprite-tile-maps-on-gpu.html
17
16
  */
18
17
 
19
-
20
18
  export default {
21
19
  type: 'cobalt:tileHDR',
22
20
  refs: [
23
21
  { name: 'tileAtlas', type: 'textureView', format: 'rgba8unorm', access: 'read' },
24
- { name: "hdr", type: "textureView", format: "rgba16float", access: "write", },
22
+ { name: 'hdr', type: 'textureView', format: 'rgba16float', access: 'write' },
25
23
  ],
26
24
 
27
25
  // @params Object cobalt renderer world object
28
26
  // @params Object options optional data passed when initing this node
29
- onInit: async function (cobalt, options={}) {
27
+ onInit: async function (cobalt, options = {}) {
30
28
  return init(cobalt, options)
31
29
  },
32
30
 
@@ -44,12 +42,10 @@ export default {
44
42
  // do whatever you need when the dimensions of the renderer change (resize textures, etc.)
45
43
  },
46
44
 
47
- onViewportPosition: function (cobalt, node) {
48
- },
45
+ onViewportPosition: function (cobalt, node) {},
49
46
 
50
47
  // optional
51
48
  customFunctions: {
52
-
53
49
  setTexture: async function (cobalt, node, texture) {
54
50
  const { canvas, device } = cobalt
55
51
 
@@ -63,8 +59,7 @@ export default {
63
59
  // browser (canvas) path
64
60
  node.options.textureUrl = texture
65
61
  material = await createTextureFromUrl(cobalt, 'tile map', texture, format)
66
- }
67
- else {
62
+ } else {
68
63
  // sdl + gpu path
69
64
  material = await createTextureFromBuffer(cobalt, 'tile map', texture, format)
70
65
  }
@@ -75,18 +70,18 @@ export default {
75
70
  {
76
71
  binding: 0,
77
72
  resource: {
78
- buffer: node.data.uniformBuffer
79
- }
73
+ buffer: node.data.uniformBuffer,
74
+ },
80
75
  },
81
76
  {
82
77
  binding: 1,
83
- resource: material.view
78
+ resource: material.view,
84
79
  },
85
80
  {
86
81
  binding: 2,
87
- resource: material.sampler
82
+ resource: material.sampler,
88
83
  },
89
- ]
84
+ ],
90
85
  })
91
86
 
92
87
  node.data.bindGroup = bindGroup
@@ -95,8 +90,7 @@ export default {
95
90
  },
96
91
  }
97
92
 
98
-
99
- async function init (cobalt, nodeData) {
93
+ async function init(cobalt, nodeData) {
100
94
  const { canvas, device } = cobalt
101
95
 
102
96
  let material
@@ -106,14 +100,23 @@ async function init (cobalt, nodeData) {
106
100
  // build the tile layer and add it to the cobalt data structure
107
101
  if (canvas) {
108
102
  // browser (canvas) path
109
- material = await createTextureFromUrl(cobalt, 'tile map', nodeData.options.textureUrl, format)
110
- }
111
- else {
103
+ material = await createTextureFromUrl(
104
+ cobalt,
105
+ 'tile map',
106
+ nodeData.options.textureUrl,
107
+ format,
108
+ )
109
+ } else {
112
110
  // sdl + gpu path
113
- material = await createTextureFromBuffer(cobalt, 'tile map', nodeData.options.texture, format)
111
+ material = await createTextureFromBuffer(
112
+ cobalt,
113
+ 'tile map',
114
+ nodeData.options.texture,
115
+ format,
116
+ )
114
117
  }
115
118
 
116
- const dat = new Float32Array([ nodeData.options.scrollScale, nodeData.options.scrollScale ])
119
+ const dat = new Float32Array([nodeData.options.scrollScale, nodeData.options.scrollScale])
117
120
 
118
121
  const usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
119
122
 
@@ -121,7 +124,7 @@ async function init (cobalt, nodeData) {
121
124
  size: dat.byteLength,
122
125
  usage,
123
126
  // make this memory space accessible from the CPU (host visible)
124
- mappedAtCreation: true
127
+ mappedAtCreation: true,
125
128
  }
126
129
 
127
130
  const uniformBuffer = device.createBuffer(descriptor)
@@ -134,18 +137,18 @@ async function init (cobalt, nodeData) {
134
137
  {
135
138
  binding: 0,
136
139
  resource: {
137
- buffer: uniformBuffer
138
- }
140
+ buffer: uniformBuffer,
141
+ },
139
142
  },
140
143
  {
141
144
  binding: 1,
142
- resource: material.view
145
+ resource: material.view,
143
146
  },
144
147
  {
145
148
  binding: 2,
146
- resource: material.sampler
149
+ resource: material.sampler,
147
150
  },
148
- ]
151
+ ],
149
152
  })
150
153
 
151
154
  return {
@@ -156,13 +159,10 @@ async function init (cobalt, nodeData) {
156
159
  }
157
160
  }
158
161
 
159
-
160
- function draw (cobalt, nodeData, commandEncoder) {
161
-
162
+ function draw(cobalt, nodeData, commandEncoder) {
162
163
  // calling setTexture can cause the texture to be destroyed followed by this draw command
163
164
  // so check for the undefined texture first and bail for a frame.
164
- if (!nodeData.data.material.texture)
165
- return
165
+ if (!nodeData.data.material.texture) return
166
166
 
167
167
  const { device } = cobalt
168
168
 
@@ -170,17 +170,17 @@ function draw (cobalt, nodeData, commandEncoder) {
170
170
  // otherwise load it, so multiple sprite passes can build up data in the color and emissive textures
171
171
  const loadOp = nodeData.options.loadOp || 'load'
172
172
 
173
- const renderpass = commandEncoder.beginRenderPass({
173
+ const renderpass = commandEncoder.beginRenderPass({
174
174
  label: 'tile',
175
175
  colorAttachments: [
176
176
  {
177
177
  // hdr is passsed as a node || FRAME_TEXTURE_VIEW
178
- view: nodeData.refs.hdr.data?.view || nodeData.refs.hdr,
178
+ view: nodeData.refs.hdr.data?.view || nodeData.refs.hdr,
179
179
  clearValue: cobalt.clearValue,
180
180
  loadOp,
181
- storeOp: 'store'
182
- }
183
- ]
181
+ storeOp: 'store',
182
+ },
183
+ ],
184
184
  })
185
185
 
186
186
  const tileAtlas = nodeData.refs.tileAtlas.data
@@ -197,9 +197,7 @@ function draw (cobalt, nodeData, commandEncoder) {
197
197
  renderpass.end()
198
198
  }
199
199
 
200
-
201
- function destroy (nodeData) {
200
+ function destroy(nodeData) {
202
201
  nodeData.data.material.texture.destroy()
203
202
  nodeData.data.material.texture = undefined
204
203
  }
205
-
package/src/uuid.js CHANGED
@@ -1,3 +1,3 @@
1
- export default function _uuid () {
2
- return Math.ceil(Math.random() * (Number.MAX_SAFE_INTEGER-10)) // super ghetto UUID
1
+ export default function _uuid() {
2
+ return Math.ceil(Math.random() * (Number.MAX_SAFE_INTEGER - 10)) // super ghetto UUID
3
3
  }
@@ -1,60 +0,0 @@
1
- export default function createSpriteQuads (device, spritesheet) {
2
-
3
- // u,v coordinates specify top left as 0,0 bottom right as 1,1
4
-
5
- /*
6
- const vertices = new Float32Array([
7
- // position tex
8
- // x y u v
9
- -0.5, -0.5, 0.0, 0.0,
10
- -0.5, 0.5, 0.0, 1.0,
11
- 0.5, 0.5, 1.0,
12
-
13
- // triangle 2 (2nd half of quad)
14
- -0.5, -0.5, 0.0, 0.0,
15
- 0.5, 0.5, 1.0, 1.0,
16
- 0.5, -0.5, 1.0, 0.0,
17
- ])
18
- */
19
-
20
- const vertices = spritesheet.vertices
21
-
22
- const usage = GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
23
-
24
- const descriptor = {
25
- size: vertices.byteLength,
26
- usage,
27
- // make this memory space accessible from the CPU (host visible)
28
- mappedAtCreation: true
29
- }
30
-
31
- const buffer = device.createBuffer(descriptor)
32
-
33
- new Float32Array(buffer.getMappedRange()).set(vertices)
34
-
35
- buffer.unmap()
36
-
37
- const bufferLayout = {
38
- arrayStride: 16,
39
- stepMode: 'vertex',
40
- attributes: [
41
- // position
42
- {
43
- shaderLocation: 0,
44
- format: 'float32x2',
45
- offset: 0
46
- },
47
- // uv
48
- {
49
- shaderLocation: 1,
50
- format: 'float32x2',
51
- offset: 8
52
- }
53
- ]
54
- }
55
-
56
- return {
57
- buffer,
58
- bufferLayout,
59
- }
60
- }