-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGeometry.ts
377 lines (332 loc) · 11.7 KB
/
Geometry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { Box3 } from '../../math/Box3'
import { throwError, throwWarning } from '../../utils/utils'
import {
GeometryOptions,
GeometryParams,
VertexBuffer,
VertexBufferAttribute,
VertexBufferAttributeParams,
VertexBufferParams
} from '../../types/Geometries'
/**
* Used to create a {@link Geometry} from given parameters like instances count or geometry attributes (vertices, uvs, normals).<br>
* Holds all attributes arrays, bounding box and create as WGSL code snippet for the vertex shader input attributes.
*
* During the {@link Geometry#render | render}, the {@link Geometry} is responsible for setting the {@link Geometry#vertexBuffers | vertexBuffers} and drawing the vertices.
*
* @example
* ```javascript
* const vertices = new Float32Array([
* // first triangle
* 1, 1, 0,
* 1, -1, 0,
* -1, -1, 0,
*
* // second triangle
* 1, 1, 0,
* -1, -1, 0,
* -1, 1, 0
* ])
*
* // create a quad geometry made of 2 triangles
* const geometry = new Geometry()
*
* geometry.setAttribute({
* name: 'position',
* type: 'vec3f',
* bufferFormat: 'float32x3',
* size: 3,
* bufferLength: vertices.length,
* array: vertices,
* })
* ```
*/
export class Geometry {
/** Number of vertices defined by this geometry */
verticesCount: number
/** Vertices order to be drawn by the {@link core/pipelines/RenderPipelineEntry.RenderPipelineEntry | render pipeline} */
verticesOrder: GPUFrontFace
/** {@link https://www.w3.org/TR/webgpu/#enumdef-gpuprimitivetopology | Topology} to use with this {@link Geometry}, i.e. whether to draw triangles or points */
topology: GPUPrimitiveTopology
/** Number of instances of this geometry to draw */
instancesCount: number
/** Array of {@link VertexBuffer | vertex buffers} to use with this geometry */
vertexBuffers: VertexBuffer[]
/** Options used to create this geometry */
options: GeometryOptions
/** The type of the geometry */
type: string
/** The bounding box of the geometry, i.e. two {@link math/Vec3.Vec3 | Vec3} defining the min and max positions to wrap this geometry in a cube */
boundingBox: Box3
/** A string to append to our shaders code describing the WGSL structure representing this geometry attributes */
wgslStructFragment: string
/**
* Geometry constructor
* @param parameters - {@link GeometryParams | parameters} used to create our Geometry
*/
constructor({
verticesOrder = 'ccw',
topology = 'triangle-list',
instancesCount = 1,
vertexBuffers = [],
}: GeometryParams = {}) {
this.verticesCount = 0
this.verticesOrder = verticesOrder
this.topology = topology
this.instancesCount = instancesCount
this.boundingBox = new Box3()
this.type = 'Geometry'
this.vertexBuffers = []
// should contain our vertex position / uv data at least
this.addVertexBuffer({
name: 'attributes',
})
this.options = {
verticesOrder,
instancesCount,
vertexBuffers,
topology,
}
vertexBuffers.forEach((vertexBuffer) => {
this.addVertexBuffer({
stepMode: vertexBuffer.stepMode ?? 'vertex',
name: vertexBuffer.name,
attributes: vertexBuffer.attributes,
})
})
}
/**
* Get whether this Geometry is ready to compute, i.e. if its first vertex buffer array has not been created yet
* @readonly
*/
get shouldCompute(): boolean {
return this.vertexBuffers.length && !this.vertexBuffers[0].array
}
/**
* Get whether this geometry is ready to draw, i.e. it has been computed and all its vertex buffers have been created
* @readonly
*/
get ready(): boolean {
return !this.shouldCompute && !this.vertexBuffers.find((vertexBuffer) => !vertexBuffer.buffer)
}
/**
* Add a vertex buffer to our Geometry, set its attributes and return it
* @param parameters - vertex buffer {@link VertexBufferParams | parameters}
* @returns - newly created {@link VertexBuffer | vertex buffer}
*/
addVertexBuffer({ stepMode = 'vertex', name, attributes = [] }: VertexBufferParams = {}): VertexBuffer {
const vertexBuffer = {
name: name ?? 'attributes' + this.vertexBuffers.length,
stepMode,
arrayStride: 0,
bufferLength: 0,
attributes: [],
buffer: null,
}
// set attributes right away if possible
attributes?.forEach((attribute) => {
this.setAttribute({
vertexBuffer,
...attribute,
} as VertexBufferAttributeParams)
})
this.vertexBuffers.push(vertexBuffer)
return vertexBuffer
}
/**
* Get a vertex buffer by name
* @param name - our vertex buffer name
* @returns - found {@link VertexBuffer | vertex buffer} or null if not found
*/
getVertexBufferByName(name = ''): VertexBuffer | null {
return this.vertexBuffers.find((vertexBuffer) => vertexBuffer.name === name)
}
/**
* Set a vertex buffer attribute
* @param parameters - attributes {@link VertexBufferAttributeParams | parameters}
*/
setAttribute({
vertexBuffer = this.vertexBuffers[0],
name,
type = 'vec3f',
bufferFormat = 'float32x3',
size = 3,
array = new Float32Array(this.verticesCount * size),
verticesStride = 1,
}: VertexBufferAttributeParams) {
const attributes = vertexBuffer.attributes
const attributesLength = attributes.length
if (!name) name = 'geometryAttribute' + attributesLength
if (name === 'position' && (type !== 'vec3f' || bufferFormat !== 'float32x3' || size !== 3)) {
throwWarning(
`Geometry 'position' attribute must have this exact properties set:\n\ttype: 'vec3f',\n\tbufferFormat: 'float32x3',\n\tsize: 3`
)
type = 'vec3f'
bufferFormat = 'float32x3'
size = 3
}
const attributeCount = array.length / size
if (name === 'position') {
this.verticesCount = attributeCount
}
if (
vertexBuffer.stepMode === 'vertex' &&
this.verticesCount &&
this.verticesCount !== attributeCount * verticesStride
) {
throwError(
`Geometry vertex attribute error. Attribute array of size ${size} must be of length: ${
this.verticesCount * size
}, current given: ${array.length}. (${this.verticesCount} vertices).`
)
} else if (vertexBuffer.stepMode === 'instance' && attributeCount !== this.instancesCount) {
throwError(
`Geometry instance attribute error. Attribute array of size ${size} must be of length: ${
this.instancesCount * size
}, current given: ${array.length}. (${this.instancesCount} instances).`
)
}
const attribute = {
name,
type,
bufferFormat,
size,
bufferLength: array.length,
offset: attributesLength
? attributes.reduce((accumulator: number, currentValue) => {
return accumulator + currentValue.bufferLength
}, 0)
: 0,
bufferOffset: attributesLength
? attributes[attributesLength - 1].bufferOffset + attributes[attributesLength - 1].size * 4
: 0,
array,
verticesStride: verticesStride,
}
vertexBuffer.bufferLength += attribute.bufferLength * verticesStride
vertexBuffer.arrayStride += attribute.size
vertexBuffer.attributes.push(attribute)
}
/**
* Get an attribute by name
* @param name - name of the attribute to find
* @returns - found {@link VertexBufferAttribute | attribute} or null if not found
*/
getAttributeByName(name: string): VertexBufferAttribute | null {
let attribute
this.vertexBuffers.forEach((vertexBuffer) => {
attribute = vertexBuffer.attributes.find((attribute) => attribute.name === name)
})
return attribute
}
/**
* Compute a Geometry, which means iterate through all vertex buffers and create the attributes array that will be sent as buffers.
* Also compute the Geometry bounding box.
*/
computeGeometry() {
if (!this.shouldCompute) return
this.vertexBuffers.forEach((vertexBuffer, index) => {
if (index === 0) {
const hasPositionAttribute = vertexBuffer.attributes.find(
(attribute) => attribute.name === 'position'
) as VertexBufferAttribute | null
if (!hasPositionAttribute) {
throwError(`Geometry must have a 'position' attribute`)
}
if (
hasPositionAttribute.type !== 'vec3f' ||
hasPositionAttribute.bufferFormat !== 'float32x3' ||
hasPositionAttribute.size !== 3
) {
throwWarning(
`Geometry 'position' attribute must have this exact properties set:\n\ttype: 'vec3f',\n\tbufferFormat: 'float32x3',\n\tsize: 3`
)
hasPositionAttribute.type = 'vec3f'
hasPositionAttribute.bufferFormat = 'float32x3'
hasPositionAttribute.size = 3
}
}
vertexBuffer.array = new Float32Array(vertexBuffer.bufferLength)
let currentIndex = 0
let attributeIndex = 0
for (let i = 0; i < vertexBuffer.bufferLength; i += vertexBuffer.arrayStride) {
for (let j = 0; j < vertexBuffer.attributes.length; j++) {
const { name, size, array, verticesStride } = vertexBuffer.attributes[j]
for (let s = 0; s < size; s++) {
const attributeValue = array[Math.floor(attributeIndex / verticesStride) * size + s]
vertexBuffer.array[currentIndex] = attributeValue
// compute bounding box
if (name === 'position') {
if (s % 3 === 0) {
// x
if (this.boundingBox.min.x > attributeValue) this.boundingBox.min.x = attributeValue
if (this.boundingBox.max.x < attributeValue) this.boundingBox.max.x = attributeValue
} else if (s % 3 === 1) {
// y
if (this.boundingBox.min.y > attributeValue) this.boundingBox.min.y = attributeValue
if (this.boundingBox.max.y < attributeValue) this.boundingBox.max.y = attributeValue
} else if (s % 3 === 2) {
// z
if (this.boundingBox.min.z > attributeValue) this.boundingBox.min.z = attributeValue
if (this.boundingBox.max.z < attributeValue) this.boundingBox.max.z = attributeValue
}
}
currentIndex++
}
}
attributeIndex++
}
})
this.#setWGSLFragment()
}
/**
* Set the WGSL code snippet that will be appended to the vertex shader.
* @private
*/
#setWGSLFragment() {
let locationIndex = -1
this.wgslStructFragment = `struct Attributes {\n\t@builtin(vertex_index) vertexIndex : u32,\n\t@builtin(instance_index) instanceIndex : u32,${this.vertexBuffers
.map((vertexBuffer) => {
return vertexBuffer.attributes.map((attribute) => {
locationIndex++
return `\n\t@location(${locationIndex}) ${attribute.name}: ${attribute.type}`
})
})
.join(',')}\n};`
}
/** RENDER **/
/**
* Set our render pass geometry vertex buffers
* @param pass - current render pass
*/
setGeometryBuffers(pass: GPURenderPassEncoder) {
this.vertexBuffers.forEach((vertexBuffer, index) => {
pass.setVertexBuffer(index, vertexBuffer.buffer)
})
}
/**
* Draw our geometry
* @param pass - current render pass
*/
drawGeometry(pass: GPURenderPassEncoder) {
pass.draw(this.verticesCount, this.instancesCount)
}
/**
* Set our vertex buffers then draw the geometry
* @param pass - current render pass
*/
render(pass: GPURenderPassEncoder) {
if (!this.ready) return
this.setGeometryBuffers(pass)
this.drawGeometry(pass)
}
/**
* Destroy our geometry vertex buffers
*/
destroy() {
this.vertexBuffers.forEach((vertexBuffer) => {
vertexBuffer.buffer?.destroy()
vertexBuffer.buffer = null
})
}
}