-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProjectedMeshBaseMixin.ts
456 lines (388 loc) · 15.7 KB
/
ProjectedMeshBaseMixin.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import { CameraRenderer, isCameraRenderer } from '../../renderers/utils'
import { DOMFrustum } from '../../DOM/DOMFrustum'
import {
MeshBaseClass,
MeshBaseMixin,
MeshBaseOptions,
MeshBaseParams,
MeshBaseRenderParams,
MixinConstructor,
} from './MeshBaseMixin'
import { GPUCurtains } from '../../../curtains/GPUCurtains'
import { DOMElementBoundingRect, RectCoords } from '../../DOM/DOMElement'
import { RenderMaterialParams, ShaderOptions } from '../../../types/Materials'
import { ProjectedObject3D } from '../../objects3D/ProjectedObject3D'
import default_projected_vsWgsl from '../../shaders/chunks/default_projected_vs.wgsl'
import default_normal_fsWgsl from '../../shaders/chunks/default_normal_fs.wgsl'
/**
* Base parameters used to create a ProjectedMesh
*/
export interface ProjectedMeshBaseParams {
/** Whether this ProjectedMesh should be frustum culled (not drawn when outside of {@link CameraRenderer#camera | camera} frustum) */
frustumCulled?: boolean
/** Margins (in pixels) to applied to the {@link ProjectedMeshBaseClass#domFrustum | DOM Frustum} to determine if this ProjectedMesh should be frustum culled or not */
DOMFrustumMargins?: RectCoords
}
/** Parameters used to create a ProjectedMesh */
export interface ProjectedMeshParameters extends MeshBaseParams, ProjectedMeshBaseParams {}
export interface ProjectedRenderMaterialParams extends RenderMaterialParams, ProjectedMeshBaseParams {}
/** @const - Default ProjectedMesh parameters to merge with user defined parameters */
const defaultProjectedMeshParams: ProjectedMeshBaseParams = {
// frustum culling and visibility
frustumCulled: true,
DOMFrustumMargins: {
top: 0,
right: 0,
bottom: 0,
left: 0,
},
}
/** Base options used to create this ProjectedMesh */
export interface ProjectedMeshBaseOptions extends MeshBaseOptions, Partial<ProjectedMeshBaseParams> {}
/**
* This class describes the properties and methods to set up a Projected Mesh (i.e. a basic {@link MeshBaseClass | Mesh} with {@link ProjectedObject3D} transformations matrices and a {@link core/camera/Camera.Camera | Camera} to use for projection), implemented in the {@link ProjectedMeshBaseMixin}:
* - Handle the frustum culling (check if the {@link ProjectedObject3D} currently lies inside the {@link core/camera/Camera.Camera | Camera} frustum)
* - Add callbacks for when the Mesh enters or leaves the {@link core/camera/Camera.Camera | Camera} frustum
*/
export declare class ProjectedMeshBaseClass extends MeshBaseClass {
/** The {@link CameraRenderer} used */
renderer: CameraRenderer
/** The ProjectedMesh {@link DOMFrustum} class object */
domFrustum: DOMFrustum
/** Whether this ProjectedMesh should be frustum culled (not drawn when outside of {@link CameraRenderer#camera | camera} frustum) */
frustumCulled: boolean
/** Margins (in pixels) to applied to the {@link ProjectedMeshBaseClass#domFrustum | DOM Frustum} to determine if this ProjectedMesh should be frustum culled or not */
DOMFrustumMargins: RectCoords
// callbacks
/** function assigned to the {@link onReEnterView} callback */
_onReEnterViewCallback: () => void
/** function assigned to the {@link onLeaveView} callback */
_onLeaveViewCallback: () => void
/**
* {@link ProjectedMeshBaseClass} constructor
* @param renderer - our {@link CameraRenderer} class object
* @param element - a DOM HTML Element that can be bound to a Mesh
* @param parameters - {@link ProjectedMeshParameters | Projected Mesh base parameters}
*/
constructor(renderer: CameraRenderer, element: HTMLElement | null, parameters: ProjectedMeshParameters)
/**
* Set default shaders if one or both of them are missing
*/
setShaders(): void
/**
* Override {@link MeshBaseClass} method to add the domFrustum
*/
computeGeometry(): void
/**
* Set a Mesh matrices uniforms inputs then call {@link MeshBaseClass} super method
* @param meshParameters - {@link RenderMaterialParams | RenderMaterial parameters}
*/
setMaterial(meshParameters: ProjectedRenderMaterialParams): void
/**
* Resize our Mesh
* @param boundingRect - the new bounding rectangle
*/
resize(boundingRect: DOMElementBoundingRect | null): void
/**
* Apply scale and resize textures
*/
applyScale(): void
/**
* Get our {@link DOMFrustum} projected bounding rectangle
* @readonly
*/
get projectedBoundingRect(): DOMElementBoundingRect
/**
* At least one of the matrix has been updated, update according uniforms and frustum
*/
onAfterMatrixStackUpdate(): void
/**
* Assign a callback function to _onReEnterViewCallback
* @param callback - callback to run when {@link ProjectedMeshBaseClass} is reentering the view frustum
* @returns - our Mesh
*/
onReEnterView: (callback: () => void) => ProjectedMeshBaseClass
/**
* Assign a callback function to _onLeaveViewCallback
* @param callback - callback to run when {@link ProjectedMeshBaseClass} is leaving the view frustum
* @returns - our Mesh
*/
onLeaveView: (callback: () => void) => ProjectedMeshBaseClass
/**
* Called before rendering the Mesh to update matrices and {@link DOMFrustum}.
* First, we update our matrices to have fresh results. It eventually calls onAfterMatrixStackUpdate() if at least one matrix has been updated.
* Then we check if we need to update the {@link DOMFrustum} projected bounding rectangle.
* Finally we call {@link MeshBaseClass#onBeforeRenderPass | Mesh base onBeforeRenderPass} super
*/
onBeforeRenderPass(): void
/**
* Only render the Mesh if it is in view frustum.
* Since render() is actually called before onRenderPass(), we are sure to have fresh frustum bounding rectangle values here.
* @param pass - current render pass
*/
onRenderPass(pass: GPURenderPassEncoder): void
}
/**
* Used to add the properties and methods defined in {@link ProjectedMeshBaseClass} to the {@link MeshBaseClass} and mix it with a given Base of type {@link ProjectedObject3D} or {@link curtains/objects3D/DOMObject3D.DOMObject3D | DOMObject3D}.
* @exports
* @param Base - the class to mix onto, should be of {@link ProjectedObject3D} or {@link curtains/objects3D/DOMObject3D.DOMObject3D | DOMObject3D} type
* @returns - the mixed classes, creating a Projected Mesh.
*/
function ProjectedMeshBaseMixin<TBase extends MixinConstructor<ProjectedObject3D>>(
Base: TBase
): MixinConstructor<ProjectedMeshBaseClass> & TBase {
/**
* ProjectedMeshBase defines our base properties and methods
*/
return class ProjectedMeshBase extends MeshBaseMixin(Base) {
/** The {@link CameraRenderer} used */
renderer: CameraRenderer
/** The ProjectedMesh {@link DOMFrustum} class object */
domFrustum: DOMFrustum
/** Whether this ProjectedMesh should be frustum culled (not drawn when outside of {@link CameraRenderer#camera | camera} frustum) */
frustumCulled: boolean
/** Margins (in pixels) to applied to the {@link ProjectedMeshBaseClass#domFrustum | DOM Frustum} to determine if this ProjectedMesh should be frustum culled or not */
DOMFrustumMargins: RectCoords
/** Options used to create this {@link ProjectedMeshBaseClass} */
options: ProjectedMeshBaseOptions
// callbacks / events
/** function assigned to the {@link onReEnterView} callback */
_onReEnterViewCallback: () => void = () => {
/* allow empty callback */
}
/** function assigned to the {@link onLeaveView} callback */
_onLeaveViewCallback: () => void = () => {
/* allow empty callback */
}
/**
* ProjectedMeshBase constructor
*
* @typedef MeshBaseArrayParams
* @type {array}
* @property {(CameraRenderer|GPUCurtains)} 0 - our renderer class object
* @property {(string|HTMLElement|null)} 1 - the DOM HTML Element that can be bound to a Mesh
* @property {ProjectedMeshParameters} 2 - Projected Mesh parameters
*
* @param {MeshBaseArrayParams} params - our MeshBaseMixin parameters
*/
constructor(...params: any[]) {
super(
params[0] as CameraRenderer | GPUCurtains,
params[1] as HTMLElement | string,
{ ...defaultProjectedMeshParams, ...params[2], ...{ useProjection: true } } as ProjectedMeshParameters
)
let renderer = params[0]
// force this mesh to use projection!
const parameters = {
...defaultProjectedMeshParams,
...params[2],
...{ useProjection: true },
} as ProjectedMeshParameters
this.type = 'MeshTransformed'
// we could pass our curtains object OR our curtains renderer object
renderer = (renderer && (renderer as GPUCurtains).renderer) || (renderer as CameraRenderer)
isCameraRenderer(renderer, parameters.label ? parameters.label + ' ' + this.type : this.type)
this.renderer = renderer
const { geometry, frustumCulled, DOMFrustumMargins } = parameters
this.options = {
...(this.options ?? {}), // merge possible lower options?
frustumCulled,
DOMFrustumMargins,
}
this.setDOMFrustum()
// explicitly needed for DOM Frustum
this.geometry = geometry
// tell the model and projection matrices to update right away
this.shouldUpdateMatrixStack()
}
/* SHADERS */
/**
* Set default shaders if one or both of them are missing
*/
setShaders() {
let { shaders } = this.options
if (!shaders) {
shaders = {
vertex: {
code: default_projected_vsWgsl,
entryPoint: 'main',
},
fragment: {
code: default_normal_fsWgsl,
entryPoint: 'main',
},
}
} else {
if (!shaders.vertex || !shaders.vertex.code) {
shaders.vertex = {
code: default_projected_vsWgsl,
entryPoint: 'main',
}
}
if (shaders.fragment === undefined || (shaders.fragment && !(shaders.fragment as ShaderOptions).code)) {
shaders.fragment = {
code: default_normal_fsWgsl,
entryPoint: 'main',
}
}
}
}
/* GEOMETRY */
/**
* Set the Mesh frustum culling
*/
setDOMFrustum() {
this.domFrustum = new DOMFrustum({
boundingBox: this.geometry.boundingBox,
modelViewProjectionMatrix: this.modelViewProjectionMatrix,
containerBoundingRect: this.renderer.boundingRect,
DOMFrustumMargins: this.options.DOMFrustumMargins,
onReEnterView: () => {
this._onReEnterViewCallback && this._onReEnterViewCallback()
},
onLeaveView: () => {
this._onLeaveViewCallback && this._onLeaveViewCallback()
},
})
this.DOMFrustumMargins = this.domFrustum.DOMFrustumMargins
this.frustumCulled = this.options.frustumCulled
this.domFrustum.shouldUpdate = this.frustumCulled
}
/* MATERIAL */
/**
* Hook used to clean up parameters before sending them to the material.
* @param parameters - parameters to clean before sending them to the {@link core/materials/RenderMaterial.RenderMaterial | RenderMaterial}
* @returns - cleaned parameters
*/
cleanupRenderMaterialParameters(parameters: ProjectedRenderMaterialParams): MeshBaseRenderParams {
// patch mesh parameters
delete parameters.frustumCulled
delete parameters.DOMFrustumMargins
super.cleanupRenderMaterialParameters(parameters)
return parameters
}
/**
* Set a Mesh matrices uniforms inputs then call {@link MeshBaseClass} super method
* @param meshParameters - {@link RenderMaterialParams | RenderMaterial parameters}
*/
setMaterial(meshParameters: RenderMaterialParams) {
// add matrices uniforms
const matricesUniforms = {
label: 'Matrices',
struct: {
model: {
name: 'model',
type: 'mat4x4f',
value: this.modelMatrix,
},
world: {
name: 'world',
type: 'mat4x4f',
value: this.worldMatrix,
},
modelView: {
// model view matrix (world matrix multiplied by camera view matrix)
name: 'modelView',
type: 'mat4x4f',
value: this.modelViewMatrix,
},
modelViewProjection: {
name: 'modelViewProjection',
type: 'mat4x4f',
value: this.modelViewProjectionMatrix,
},
},
}
if (!meshParameters.uniforms) meshParameters.uniforms = {}
meshParameters.uniforms.matrices = matricesUniforms
super.setMaterial(meshParameters)
}
/* SIZE & TRANSFORMS */
/**
* Resize our {@link ProjectedMeshBaseClass}
* @param boundingRect - the new bounding rectangle
*/
resize(boundingRect?: DOMElementBoundingRect | null) {
if (this.domFrustum) this.domFrustum.setContainerBoundingRect(this.renderer.boundingRect)
super.resize(boundingRect)
}
/**
* Apply scale and resize textures
*/
applyScale() {
super.applyScale()
// resize textures on scale change!
this.textures.forEach((texture) => texture.resize())
}
/**
* Get our {@link DOMFrustum} projected bounding rectangle
* @readonly
*/
get projectedBoundingRect(): DOMElementBoundingRect {
return this.domFrustum?.projectedBoundingRect
}
/**
* At least one of the matrix has been updated, update according uniforms and frustum
*/
onAfterMatrixStackUpdate() {
if (this.material) {
this.material.shouldUpdateInputsBindings('matrices')
}
if (this.domFrustum) this.domFrustum.shouldUpdate = true
}
/* EVENTS */
/**
* Assign a callback function to _onReEnterViewCallback
* @param callback - callback to run when {@link ProjectedMeshBaseClass} is reentering the view frustum
* @returns - our Mesh
*/
onReEnterView(callback: () => void): ProjectedMeshBaseClass {
if (callback) {
this._onReEnterViewCallback = callback
}
return this
}
/**
* Assign a callback function to _onLeaveViewCallback
* @param callback - callback to run when {@link ProjectedMeshBaseClass} is leaving the view frustum
* @returns - our Mesh
*/
onLeaveView(callback: () => void): ProjectedMeshBaseClass {
if (callback) {
this._onLeaveViewCallback = callback
}
return this
}
/* RENDER */
/**
* Called before rendering the Mesh to update matrices and {@link DOMFrustum}.
* First, we update our matrices to have fresh results. It eventually calls onAfterMatrixStackUpdate() if at least one matrix has been updated.
* Then we check if we need to update the {@link DOMFrustum} projected bounding rectangle.
* Finally we call {@link MeshBaseClass#onBeforeRenderPass | Mesh base onBeforeRenderPass} super
*/
onBeforeRenderPass() {
this.updateMatrixStack()
if (this.domFrustum && this.domFrustum.shouldUpdate && this.frustumCulled) {
this.domFrustum.computeProjectedToDocumentCoords()
this.domFrustum.shouldUpdate = false
}
super.onBeforeRenderPass()
}
/**
* Only render the Mesh if it is in view frustum.
* Since render() is actually called before onRenderPass(), we are sure to have fresh frustum bounding rectangle values here.
* @param pass - current render pass
*/
onRenderPass(pass: GPURenderPassEncoder) {
if (!this.material.ready) return
this._onRenderCallback && this._onRenderCallback()
if ((this.domFrustum && this.domFrustum.isIntersecting) || !this.frustumCulled) {
// render ou material
this.material.render(pass)
// then render our geometry
this.geometry.render(pass)
}
}
}
}
export { ProjectedMeshBaseMixin }