-
Notifications
You must be signed in to change notification settings - Fork 4
/
CameraComponent.ts
799 lines (698 loc) · 20 KB
/
CameraComponent.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
import { ComponentRepository } from '../../core/ComponentRepository';
import { Component } from '../../core/Component';
import {applyMixins, EntityRepository} from '../../core/EntityRepository';
import {WellKnownComponentTIDs} from '../WellKnownComponentTIDs';
import { Vector3 } from '../../math/Vector3';
import { Vector4 } from '../../math/Vector4';
import {CameraTypeEnum, CameraType} from '../../definitions/CameraType';
import { Matrix44 } from '../../math/Matrix44';
import { SceneGraphComponent } from '../SceneGraph/SceneGraphComponent';
import {BufferUse} from '../../definitions/BufferUse';
import {ComponentType} from '../../definitions/ComponentType';
import { MutableMatrix44 } from '../../math/MutableMatrix44';
import {ProcessStage} from '../../definitions/ProcessStage';
import { MutableVector4 } from '../../math/MutableVector4';
import { MutableVector3 } from '../../math/MutableVector3';
import {Frustum} from '../../geometry/Frustum';
import {Config} from '../../core/Config';
import {
ComponentTID,
ComponentSID,
EntityUID,
} from '../../../types/CommonTypes';
import { GlobalDataRepository } from '../../core/GlobalDataRepository';
import {ShaderSemantics} from '../../definitions/ShaderSemantics';
import {MathUtil} from '../../math/MathUtil';
import { CameraControllerComponent } from '../CameraController/CameraControllerComponent';
import { ModuleManager } from '../../system/ModuleManager';
import {RnXR} from '../../../xr/main';
import { RenderPass } from '../../renderer/RenderPass';
import {ICameraEntity} from '../../helpers/EntityHelper';
import {IEntity} from '../../core/Entity';
import {ComponentToComponentMethods} from '../ComponentTypes';
import { Is } from '../../misc/Is';
import { MiscUtil } from '../../misc';
/**
* The Component that represents a camera.
*/
export class CameraComponent extends Component {
private static readonly _eye: Vector3 = Vector3.zero();
private _eyeInner: MutableVector3 = MutableVector3.dummy();
private _direction: MutableVector3 = MutableVector3.dummy();
private _directionInner: MutableVector3 = MutableVector3.dummy();
private _up: MutableVector3 = MutableVector3.dummy();
private _upInner: MutableVector3 = MutableVector3.dummy();
private _filmWidth = 36; // mili meter
private _filmHeight = 24; // mili meter
private _focalLength = 20;
private primitiveMode = false;
// x: left, y:right, z:top, w: bottom
private _corner: MutableVector4 = MutableVector4.dummy();
private _cornerInner: MutableVector4 = MutableVector4.dummy();
// x: zNear, y: zFar,
// if perspective, z: fovy, w: aspect
// if ortho, z: xmag, w: ymag
private _parameters: MutableVector4 = MutableVector4.dummy();
private _parametersInner: MutableVector4 = MutableVector4.dummy();
private __type: CameraTypeEnum = CameraType.Perspective;
private __sceneGraphComponent?: SceneGraphComponent;
private _projectionMatrix: MutableMatrix44 = MutableMatrix44.dummy();
private __isProjectionMatrixUpToDate = false;
private _viewMatrix: MutableMatrix44 = MutableMatrix44.dummy();
private __isViewMatrixUpToDate = false;
private static __current: ComponentSID = -1;
private static returnVector3 = MutableVector3.zero();
private static __globalDataRepository = GlobalDataRepository.getInstance();
private static __tmpVector3_0: MutableVector3 = MutableVector3.zero();
private static __tmpVector3_1: MutableVector3 = MutableVector3.zero();
private static __tmpVector3_2: MutableVector3 = MutableVector3.zero();
private static __tmpMatrix44_0 = MutableMatrix44.zero();
private static __tmpMatrix44_1 = MutableMatrix44.zero();
private static __biasMatrix = Matrix44.fromCopy16ColumnMajor(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0
);
_xrLeft = false;
_xrRight = false;
public isSyncToLight = false;
private __frustum = new Frustum();
constructor(
entityUid: EntityUID,
componentSid: ComponentSID,
entityRepository: EntityRepository
) {
super(entityUid, componentSid, entityRepository);
this._setMaxNumberOfComponent(
Math.max(10, Math.floor(Config.maxEntityNumber / 100))
);
this.registerMember(
BufferUse.CPUGeneric,
'eyeInner',
MutableVector3,
ComponentType.Float,
[0, 0, 0]
);
this.registerMember(
BufferUse.CPUGeneric,
'direction',
MutableVector3,
ComponentType.Float,
[0, 0, -1]
);
this.registerMember(
BufferUse.CPUGeneric,
'up',
MutableVector3,
ComponentType.Float,
[0, 1, 0]
);
this.registerMember(
BufferUse.CPUGeneric,
'directionInner',
MutableVector3,
ComponentType.Float,
[0, 0, -1]
);
this.registerMember(
BufferUse.CPUGeneric,
'upInner',
MutableVector3,
ComponentType.Float,
[0, 1, 0]
);
this.registerMember(
BufferUse.CPUGeneric,
'corner',
MutableVector4,
ComponentType.Float,
[-1, 1, 1, -1]
);
this.registerMember(
BufferUse.CPUGeneric,
'cornerInner',
MutableVector4,
ComponentType.Float,
[-1, 1, 1, -1]
);
this.registerMember(
BufferUse.CPUGeneric,
'parameters',
MutableVector4,
ComponentType.Float,
[0.1, 10000, 90, 1]
);
this.registerMember(
BufferUse.CPUGeneric,
'parametersInner',
MutableVector4,
ComponentType.Float,
[0.1, 10000, 90, 1]
);
this.registerMember(
BufferUse.CPUGeneric,
'projectionMatrix',
MutableMatrix44,
ComponentType.Float,
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
);
this.registerMember(
BufferUse.CPUGeneric,
'viewMatrix',
MutableMatrix44,
ComponentType.Float,
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
);
this.submitToAllocation(Config.maxCameraNumber);
const globalDataRepository = GlobalDataRepository.getInstance();
globalDataRepository.takeOne(ShaderSemantics.ViewMatrix);
globalDataRepository.takeOne(ShaderSemantics.ProjectionMatrix);
globalDataRepository.takeOne(ShaderSemantics.ViewPosition);
this.setFovyAndChangeFocalLength(90);
if (CameraComponent.current === -1) {
CameraComponent.current = componentSid;
}
}
static set current(componentSID: ComponentSID) {
this.__current = componentSID;
}
static get current() {
return this.__current;
}
set type(type: CameraTypeEnum) {
this.__type = type;
if (type === CameraType.Orthographic) {
this._parameters.z = 1;
this._parameters.w = 1;
this._parametersInner.z = 1;
this._parametersInner.w = 1;
} else {
this.setFovyAndChangeFocalLength(90);
this._parameters.w = 1;
this._parametersInner.z = 90;
this._parametersInner.w = 1;
}
}
get type() {
return this.__type;
}
get eye() {
// In Rhodonite, eye is always (0,0,0). Use TransformComponent for Camera positioning
return CameraComponent._eye;
}
set eye(noUseVec: Vector3) {
throw Error(
'In Rhodonite, eye is always (0,0,0). Use TransformComponent for Camera positioning.'
);
}
get eyeInner() {
return this._eyeInner;
}
/**
* @internal
*/
set eyeInner(vec: Vector3) {
this._eyeInner.copyComponents(vec);
}
set upInner(vec: Vector3) {
this._upInner.copyComponents(vec);
}
set up(vec: Vector3) {
this._up.copyComponents(vec);
}
get up() {
return this._up.clone();
}
get upInner() {
return this._upInner;
}
set direction(vec: Vector3) {
const oldDirection = this._direction;
const newDirection = vec;
const oldUp = this._up;
const orthogonalVectorNewDirectionAndOldUp = MutableVector3.crossTo(
newDirection,
oldUp,
CameraComponent.__tmpVector3_0
);
const isOrthogonalNewDirectionAndOldUp =
orthogonalVectorNewDirectionAndOldUp.length() === 0.0;
let newUpNonNormalize;
if (isOrthogonalNewDirectionAndOldUp) {
const relativeXaxis = MutableVector3.crossTo(
oldDirection,
oldUp,
CameraComponent.__tmpVector3_1
);
newUpNonNormalize = MutableVector3.crossTo(
relativeXaxis,
newDirection,
CameraComponent.__tmpVector3_2
);
} else {
const newDirectionComponentInOldUp = MutableVector3.multiplyTo(
newDirection,
newDirection.dot(oldUp),
CameraComponent.__tmpVector3_1
);
newUpNonNormalize = MutableVector3.subtractTo(
oldUp,
newDirectionComponentInOldUp,
CameraComponent.__tmpVector3_2
);
}
this._up.copyComponents(newUpNonNormalize).normalize();
this._direction.copyComponents(newDirection);
}
set directionInner(vec: Vector3) {
this._directionInner.copyComponents(vec);
}
get direction() {
return this._direction.clone();
}
get directionInner() {
return this._directionInner;
}
set corner(vec: Vector4) {
this._corner.copyComponents(vec);
}
get corner(): Vector4 {
return this._corner.clone();
}
set left(value: number) {
this._corner.x = value;
}
set leftInner(value: number) {
this._cornerInner.x = value;
}
get left() {
return this._corner.x;
}
set right(value: number) {
this._corner.y = value;
}
set rightInner(value: number) {
this._cornerInner.y = value;
}
get right() {
return this._corner.y;
}
set top(value: number) {
this._corner.z = value;
}
set topInner(value: number) {
this._cornerInner.z = value;
}
get top() {
return this._corner.z;
}
set bottom(value: number) {
this._corner.w = value;
}
set bottomInner(value: number) {
this._cornerInner.w = value;
}
get bottom() {
return this._corner.w;
}
set cornerInner(vec: Vector4) {
this._cornerInner.copyComponents(vec);
}
get cornerInner() {
return this._cornerInner;
}
// set parameters(vec: Vector4) {
// this._parameters.copyComponents(vec);
// }
set parametersInner(vec: Vector4) {
this._parametersInner.copyComponents(vec);
}
get parametersInner() {
return this._parametersInner;
}
get parameters(): Vector4 {
return this._parameters.clone();
}
set zNear(val: number) {
this._parameters.x = val;
}
set zNearInner(val: number) {
this._parametersInner.x = val;
}
get zNearInner() {
return this._parametersInner.x;
}
get zNear() {
return this._parameters.x;
}
set focalLength(val: number) {
this._focalLength = val;
this._parameters.z =
2 * MathUtil.radianToDegree(Math.atan(this._filmHeight / (val * 2)));
}
get focalLength() {
return this._focalLength;
}
set zFar(val: number) {
this._parameters.y = val;
}
set zFarInner(val: number) {
this._parametersInner.y = val;
}
get zFarInner() {
return this._parametersInner.y;
}
get zFar() {
return this._parameters.y;
}
setFovyAndChangeFilmSize(degree: number) {
this._parameters.z = degree;
this._filmHeight =
2 * this.focalLength * Math.tan(MathUtil.degreeToRadian(degree) / 2);
this._filmWidth = this._filmHeight * this.aspect;
}
setFovyAndChangeFocalLength(degree: number) {
this._parameters.z = degree;
this._focalLength =
this._filmHeight / 2 / Math.tan(MathUtil.degreeToRadian(degree) / 2);
}
get fovy() {
return this._parameters.z;
}
set fovyInner(val: number) {
this._parametersInner.z = val;
}
set aspect(val: number) {
this._parameters.w = val;
this._filmWidth = this._filmHeight * val;
}
set aspectInner(val: number) {
this._parametersInner.w = val;
}
get aspectInner() {
return this._parametersInner.w;
}
get aspect() {
return this._parameters.w;
}
set xMag(val: number) {
this._parameters.z = val;
}
get xMag() {
return this._parameters.z;
}
set yMag(val: number) {
this._parameters.w = val;
}
get yMag() {
return this._parameters.w;
}
static get componentTID(): ComponentTID {
return WellKnownComponentTIDs.CameraComponentTID;
}
calcProjectionMatrix() {
const zNear = this._parametersInner.x;
const zFar = this._parametersInner.y;
if (this.type === CameraType.Perspective) {
const fovy = this._parametersInner.z;
const aspect = this._parametersInner.w;
const yscale = 1.0 / Math.tan((0.5 * fovy * Math.PI) / 180);
const xscale = yscale / aspect;
this._projectionMatrix.setComponents(
xscale,
0,
0,
0,
0,
yscale,
0,
0,
0,
0,
-(zFar + zNear) / (zFar - zNear),
-(2.0 * zFar * zNear) / (zFar - zNear),
0,
0,
-1,
0
);
} else if (this.type === CameraType.Orthographic) {
const xmag = this._parametersInner.z;
const ymag = this._parametersInner.w;
this._projectionMatrix.setComponents(
1 / xmag,
0.0,
0.0,
0,
0.0,
1 / ymag,
0.0,
0,
0.0,
0.0,
-2 / (zFar - zNear),
-(zFar + zNear) / (zFar - zNear),
0.0,
0.0,
0.0,
1.0
);
} else {
const left = this._cornerInner.x;
const right = this._cornerInner.y;
const top = this._cornerInner.z;
const bottom = this._cornerInner.w;
this._projectionMatrix.setComponents(
(2 * zNear) / (right - left),
0.0,
(right + left) / (right - left),
0.0,
0.0,
(2 * zNear) / (top - bottom),
(top + bottom) / (top - bottom),
0.0,
0.0,
0.0,
-(zFar + zNear) / (zFar - zNear),
(-1 * 2 * zFar * zNear) / (zFar - zNear),
0.0,
0.0,
-1.0,
0.0
);
}
return this._projectionMatrix;
}
calcViewMatrix() {
const eye = this.eyeInner;
const f = MutableVector3.subtractTo(
this._directionInner,
eye,
CameraComponent.__tmpVector3_0
).normalize();
const s = MutableVector3.crossTo(
f,
this._upInner,
CameraComponent.__tmpVector3_1
).normalize();
const u = MutableVector3.crossTo(s, f, CameraComponent.__tmpVector3_2);
this._viewMatrix.setComponents(
s.x,
s.y,
s.z,
-Vector3.dot(s, eye),
u.x,
u.y,
u.z,
-Vector3.dot(u, eye),
-f.x,
-f.y,
-f.z,
Vector3.dot(f, eye),
0,
0,
0,
1
);
if (!this.primitiveMode) {
const invertWorldMatrix = MutableMatrix44.invertTo(
this.__sceneGraphComponent!.worldMatrixInner,
CameraComponent.__tmpMatrix44_0
);
this._viewMatrix.multiply(invertWorldMatrix);
}
return this._viewMatrix;
}
get viewMatrix() {
return this._viewMatrix;
}
set viewMatrix(viewMatrix: Matrix44) {
this._viewMatrix.copyComponents(viewMatrix);
}
get projectionMatrix() {
if (this._xrLeft || this._xrRight) {
const rnXRModule = ModuleManager.getInstance().getModule('xr') as RnXR;
if (rnXRModule?.WebXRSystem.getInstance().isWebXRMode) {
const webXRSystem = rnXRModule.WebXRSystem.getInstance();
if (this._xrLeft) {
return webXRSystem.leftProjectionMatrix;
} else if (this._xrRight) {
return webXRSystem.rightProjectionMatrix;
}
}
}
return this._projectionMatrix;
}
set projectionMatrix(projectionMatrix: Matrix44) {
this._projectionMatrix.copyComponents(projectionMatrix);
}
get viewProjectionMatrix() {
return MutableMatrix44.multiplyTo(
this._projectionMatrix,
this._viewMatrix,
CameraComponent.__tmpMatrix44_0
);
}
get biasViewProjectionMatrix() {
MutableMatrix44.multiplyTo(
this._projectionMatrix,
this._viewMatrix,
CameraComponent.__tmpMatrix44_0
);
return MutableMatrix44.multiplyTo(
CameraComponent.__biasMatrix,
CameraComponent.__tmpMatrix44_0,
CameraComponent.__tmpMatrix44_1
);
}
setValuesToGlobalDataRepositoryOnlyMatrices() {
CameraComponent.__globalDataRepository.setValue(
ShaderSemantics.ViewMatrix,
this.componentSID,
this.viewMatrix
);
CameraComponent.__globalDataRepository.setValue(
ShaderSemantics.ProjectionMatrix,
this.componentSID,
this.projectionMatrix
);
}
setValuesToGlobalDataRepository() {
CameraComponent.__globalDataRepository.setValue(
ShaderSemantics.ViewMatrix,
this.componentSID,
this.viewMatrix
);
CameraComponent.__globalDataRepository.setValue(
ShaderSemantics.ProjectionMatrix,
this.componentSID,
this.projectionMatrix
);
CameraComponent.__globalDataRepository.setValue(
ShaderSemantics.ViewPosition,
this.componentSID,
this.worldPosition
);
}
get worldPosition() {
this.__sceneGraphComponent!.worldMatrixInner.multiplyVector3To(
this.eyeInner,
CameraComponent.returnVector3
);
return CameraComponent.returnVector3;
}
updateFrustum() {
this.__frustum.update(this.viewMatrix, this.projectionMatrix);
}
get frustum() {
return this.__frustum;
}
$create() {
this.__sceneGraphComponent = EntityRepository.getComponentOfEntity(
this.__entityUid,
SceneGraphComponent
) as SceneGraphComponent;
this.moveStageTo(ProcessStage.Logic);
}
$logic({renderPass}: {renderPass: RenderPass}) {
const lightComponent = this.entity.tryToGetLight();
if (this.isSyncToLight && Is.exist(lightComponent)) {
this._eyeInner.copyComponents(CameraComponent._eye);
this._directionInner.copyComponents(this._direction);
// this._directionInner.copyComponents(lightComponent.direction);
this._upInner.copyComponents(this._up);
this.setFovyAndChangeFilmSize(
MathUtil.radianToDegree(lightComponent.outerConeAngle)
);
this._cornerInner.copyComponents(this._corner);
this.aspect = 1;
this.zNear = 0.1;
this.zFar = lightComponent.range !== -1 ? lightComponent.range : 10000;
this._parametersInner.copyComponents(this._parameters);
} else {
const cameraControllerComponent = this.entity.tryToGetCameraController();
if (Is.exist(cameraControllerComponent)) {
this._parametersInner.w = this._parameters.w;
} else {
if (!this.primitiveMode) {
this._eyeInner.copyComponents(CameraComponent._eye);
this._directionInner.copyComponents(this._direction);
this._upInner.copyComponents(this._up);
this._cornerInner.copyComponents(this._corner);
this._parametersInner.copyComponents(this._parameters);
}
}
}
this.calcViewMatrix();
if (!this._xrLeft && !this._xrRight) {
this.calcProjectionMatrix();
}
this.setValuesToGlobalDataRepository();
}
static getCurrentCameraEntity() {
const currentCameraComponent = ComponentRepository.getComponent(
this,
this.current
) as CameraComponent;
return currentCameraComponent.entity;
}
/**
* get the entity which has this component.
* @returns the entity which has this component
*/
get entity(): ICameraEntity {
return EntityRepository.getEntity(
this.__entityUid
) as unknown as ICameraEntity;
}
/**
* @override
* Add this component to the entity
* @param base the target entity
* @param _componentClass the component class to add
*/
addThisComponentToEntity<
EntityBaseClass extends IEntity,
SomeComponentClass extends typeof Component
>(base: EntityBaseClass, _componentClass: SomeComponentClass) {
class CameraEntity extends (base.constructor as any) {
constructor(
entityUID: EntityUID,
isAlive: Boolean,
components?: Map<ComponentTID, Component>
) {
super(entityUID, isAlive, components);
}
getCamera() {
return this.getComponentByComponentTID(
WellKnownComponentTIDs.CameraComponentTID
) as CameraComponent;
}
}
applyMixins(base, CameraEntity);
return base as unknown as ComponentToComponentMethods<SomeComponentClass> &
EntityBaseClass;
}
}
ComponentRepository.registerComponentClass(CameraComponent);