@footgun/cobalt 0.4.0 → 0.5.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@footgun/cobalt",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "type": "module",
5
5
  "main": "bundle.js",
6
6
  "description": "A 2D WebGpu renderer",
@@ -0,0 +1,11 @@
1
+ > Why do I have a folder named ".vercel" in my project?
2
+ The ".vercel" folder is created when you link a directory to a Vercel project.
3
+
4
+ > What does the "project.json" file contain?
5
+ The "project.json" file contains:
6
+ - The ID of the Vercel project that you linked ("projectId")
7
+ - The ID of the user or team your Vercel project is owned by ("orgId")
8
+
9
+ > Should I commit the ".vercel" folder?
10
+ No, you should not share the ".vercel" folder with anyone.
11
+ Upon creation, it will be automatically added to your ".gitignore" file.
@@ -0,0 +1 @@
1
+ {"projectId":"prj_jJXTHkZlQjFtTW7K7D94nIPjxcOX","orgId":"team_b8j5PhQ0qhPb9qIqUQzkxmra"}
@@ -142,7 +142,7 @@ async function init (cobalt, nodeData) {
142
142
  })
143
143
 
144
144
  const pipeline = device.createRenderPipeline({
145
- label: 'overlay',
145
+ label: 'overlaysprite',
146
146
  vertex: {
147
147
  module: device.createShaderModule({
148
148
  code: overlayWGSL
@@ -57,7 +57,8 @@ async function init (cobalt, node) {
57
57
  // Define vertices and indices for your line represented as two triangles (a rectangle)
58
58
  // For example, this could represent a line segment from (10, 10) to (100, 10) with a thickness of 10 units
59
59
  // Updated vertices in normalized device coordinates (NDC)
60
- const vertices = new Float32Array(300000)
60
+
61
+ const vertices = new Float32Array(1024)
61
62
 
62
63
  const vertexBuffer = device.createBuffer({
63
64
  size: vertices.byteLength,
@@ -65,10 +66,10 @@ async function init (cobalt, node) {
65
66
  //mappedAtCreation: true,
66
67
  })
67
68
 
68
-
69
69
  //new Float32Array(vertexBuffer.getMappedRange()).set(vertices);
70
70
  //vertexBuffer.unmap()
71
71
 
72
+
72
73
  const uniformBuffer = device.createBuffer({
73
74
  size: 64 * 2, // 4x4 matrix with 4 bytes per float32, times 2 matrices (view, projection)
74
75
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
@@ -107,6 +108,7 @@ async function init (cobalt, node) {
107
108
 
108
109
  // Create render pipeline
109
110
  const pipeline = device.createRenderPipeline({
111
+ label: "primitives",
110
112
  layout: pipelineLayout,
111
113
  vertex: {
112
114
  module: shaderModule,
@@ -186,9 +188,18 @@ function draw (cobalt, node, commandEncoder) {
186
188
  node.data.dirty = false
187
189
  const stride = 6 * Float32Array.BYTES_PER_ELEMENT // 2 floats per vertex position + 4 floats per vertex color
188
190
 
191
+ // if node.data.vertices has been re-sized, re-create the buffer
192
+ if (node.data.vertices.buffer.byteLength > node.data.vertexBuffer.size) {
193
+ node.data.vertexBuffer.destroy()
194
+ node.data.vertexBuffer = device.createBuffer({
195
+ size: node.data.vertices.byteLength,
196
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
197
+ })
198
+ }
199
+
189
200
  let byteCount = node.data.vertexCount * stride
190
201
  if (byteCount > node.data.vertexBuffer.size) {
191
- console.warn('too many primitives, bailing')
202
+ console.error('too many primitives, bailing')
192
203
  return
193
204
  }
194
205
 
@@ -47,6 +47,12 @@ export default {
47
47
 
48
48
  let i = node.data.vertexCount * 6 // 2 floats position + 4 floats color per vertex
49
49
 
50
+
51
+ const currentElementCount = node.data.vertexCount * 6
52
+ const floatsToAdd = triangles.length * 3 * 6
53
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
54
+
55
+
50
56
  const pos = vec2.create()
51
57
 
52
58
  for (const tri of triangles) {
@@ -123,6 +129,11 @@ export default {
123
129
  // angle between each segment
124
130
  const deltaAngle = 2 * Math.PI / numSegments
125
131
 
132
+ const currentElementCount = node.data.vertexCount * 6
133
+ const floatsToAdd = numSegments * 3 * 6
134
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
135
+
136
+
126
137
  const m = node.data.transforms.at(-1)
127
138
 
128
139
  // Generate points for the ellipsoid
@@ -218,6 +229,12 @@ export default {
218
229
  const bottomLeft = vec2.transformMat3([ x - halfWidth, y + halfHeight ], m)
219
230
  const bottomRight = vec2.transformMat3([ x + halfWidth, y + halfHeight ], m)
220
231
 
232
+
233
+ const currentElementCount = node.data.vertexCount * 6
234
+ const floatsToAdd = 6 * 6
235
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
236
+
237
+
221
238
  let i = node.data.vertexCount * 6 // 2 floats position + 4 floats color per vertex
222
239
 
223
240
 
@@ -314,6 +331,11 @@ function line (cobalt, node, start, end, color, lineWidth=1) {
314
331
 
315
332
  let i = node.data.vertexCount * 6 // 2 floats position + 4 floats color per vertex
316
333
 
334
+
335
+ const currentElementCount = node.data.vertexCount * 6
336
+ const floatsToAdd = 6 * 6
337
+ node.data.vertices = handleArrayResize(Float32Array, node.data.vertices, currentElementCount, floatsToAdd)
338
+
317
339
  // triangle 1
318
340
  // pt 1
319
341
  node.data.vertices[i + 0] = start[0] + perp[0] * halfLineWidth
@@ -383,6 +405,28 @@ function line (cobalt, node, start, end, color, lineWidth=1) {
383
405
  }
384
406
 
385
407
 
408
+ // if the new elements won't fit in the existing vertices array, resize it
409
+ // @param ArrayType ArrayType one of the TypedArray types (Float32Array, Uint8Array, etc.)
410
+ // @param TypedArray arr
411
+ function handleArrayResize (ArrayType, arr, currentElementCount, elementsToAdd) {
412
+ // if the new vertices fit in the existing vertices array bail
413
+ if ((currentElementCount + elementsToAdd) <= arr.length)
414
+ return arr
415
+
416
+ // attempt to double the existing array size when we need more capacity
417
+ const newSize = arr.length * 2
418
+
419
+ const MAX_LENGTH = 16 * 1024 * 1024 / arr.BYTES_PER_ELEMENT
420
+
421
+ if (newSize > MAX_LENGTH)
422
+ throw new Error('vertices exceed max array size')
423
+
424
+ const newArray = new ArrayType(newSize)
425
+ newArray.set(arr)
426
+ return newArray
427
+ }
428
+
429
+
386
430
  // return component of vector perpendicular to a unit basis vector
387
431
  // (IMPORTANT NOTE: assumes "basis" has unit magnitude (length==1))
388
432
  function perpendicularComponent (inp) {
@@ -55,6 +55,7 @@ function init (cobalt, node) {
55
55
  params_buf.unmap()
56
56
 
57
57
  const pipeline = device.createRenderPipeline({
58
+ label: 'scenecomposite',
58
59
  layout: 'auto',
59
60
  vertex: {
60
61
  module: device.createShaderModule({
@@ -113,7 +113,7 @@ async function init (cobalt, node) {
113
113
  })
114
114
 
115
115
  const pipeline = device.createRenderPipeline({
116
- label: 'sprite',
116
+ label: 'spritesheet',
117
117
  vertex: {
118
118
  module: device.createShaderModule({
119
119
  code: spriteWGSL
package/src/tile/atlas.js CHANGED
@@ -111,7 +111,7 @@ async function init (cobalt, nodeData) {
111
111
  })
112
112
 
113
113
  const pipeline = device.createRenderPipeline({
114
- label: 'tile',
114
+ label: 'tileatlas',
115
115
  vertex: {
116
116
  module: device.createShaderModule({
117
117
  code: tileWGSL