@geastack/gea3d 0.0.0-stage → 0.1.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.
@@ -0,0 +1,43 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ // Last values written to a native object: put() records each, and dirty
3
+ // says whether any differed from the previous write (NaN = never written).
4
+ export class ValueCache {
5
+ private readonly values: number[]
6
+ private changed: boolean
7
+
8
+ constructor(size: number) {
9
+ this.values = []
10
+ for (let i = 0; i < size; i++) this.values.push(Number.NaN)
11
+ this.changed = false
12
+ }
13
+
14
+ begin(): void {
15
+ this.changed = false
16
+ }
17
+
18
+ put(index: number, value: number): void {
19
+ if (this.values[index] === value) return
20
+ this.values[index] = value
21
+ this.changed = true
22
+ }
23
+
24
+ get dirty(): boolean {
25
+ return this.changed
26
+ }
27
+
28
+ invalidate(): void {
29
+ for (let i = 0; i < this.values.length; i++) this.values[i] = Number.NaN
30
+ }
31
+ }
32
+
33
+ // Counts renderer frames, so objects shared by several meshes (materials)
34
+ // are written to the native engine at most once per frame.
35
+ let nativeFrameNumber = 0
36
+
37
+ export function advanceNativeFrame(): void {
38
+ nativeFrameNumber++
39
+ }
40
+
41
+ export function currentNativeFrame(): number {
42
+ return nativeFrameNumber
43
+ }
@@ -0,0 +1,120 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ import { Object3D } from './core'
3
+ import { DEG2RAD, Matrix4 } from './math'
4
+ import { cameraProjection, NODE_CAMERA } from '../native/api'
5
+
6
+ export class Camera extends Object3D {
7
+ readonly matrixWorldInverse: Matrix4
8
+ readonly projectionMatrix: Matrix4
9
+ readonly projectionMatrixInverse: Matrix4
10
+ // Clip planes live on the base so the renderer never needs to downcast.
11
+ near: number
12
+ far: number
13
+ isOrthographicCamera: boolean
14
+ // Bumped by updateProjectionMatrix; the native camera follows it.
15
+ projectionVersion: number
16
+ private sentProjectionVersion: number
17
+
18
+ constructor() {
19
+ super()
20
+ this.matrixWorldInverse = new Matrix4()
21
+ this.projectionMatrix = new Matrix4()
22
+ this.projectionMatrixInverse = new Matrix4()
23
+ this.near = 0.1
24
+ this.far = 2000
25
+ this.isOrthographicCamera = false
26
+ this.isCamera = true
27
+ this.nodeKind = NODE_CAMERA
28
+ this.projectionVersion = 0
29
+ this.sentProjectionVersion = -1
30
+ }
31
+
32
+ updateMatrixWorld(force: boolean = false): void {
33
+ super.updateMatrixWorld(force)
34
+ this.matrixWorldInverse.copy(this.matrixWorld).invert()
35
+ }
36
+
37
+ // Recomputes projectionMatrix from the camera's parameters.
38
+ updateProjectionMatrix(): void {
39
+ this.projectionChanged()
40
+ }
41
+
42
+ protected projectionChanged(): void {
43
+ this.projectionMatrixInverse.copy(this.projectionMatrix).invert()
44
+ this.projectionVersion++
45
+ }
46
+
47
+ syncSelf(): void {
48
+ if (this.sentProjectionVersion === this.projectionVersion) return
49
+
50
+ cameraProjection(this.nativeNode.id, this.projectionMatrix.elements, this.near, this.far)
51
+ this.sentProjectionVersion = this.projectionVersion
52
+ }
53
+
54
+ releaseSelf(): void {
55
+ this.sentProjectionVersion = -1
56
+ }
57
+ }
58
+
59
+ export class PerspectiveCamera extends Camera {
60
+ fov: number
61
+ aspect: number
62
+ zoom: number
63
+
64
+ constructor(fov: number = 50, aspect: number = 1, near: number = 0.1, far: number = 2000) {
65
+ super()
66
+ this.fov = fov
67
+ this.aspect = aspect
68
+ this.near = near
69
+ this.far = far
70
+ this.zoom = 1
71
+ this.updateProjectionMatrix()
72
+ }
73
+
74
+ updateProjectionMatrix(): void {
75
+ const near = this.near
76
+ const top = (near * Math.tan(DEG2RAD * 0.5 * this.fov)) / this.zoom
77
+ const height = 2 * top
78
+ const width = this.aspect * height
79
+ const left = -0.5 * width
80
+ this.projectionMatrix.makePerspective(left, left + width, top, top - height, near, this.far)
81
+ this.projectionChanged()
82
+ }
83
+ }
84
+
85
+ export class OrthographicCamera extends Camera {
86
+ left: number
87
+ right: number
88
+ top: number
89
+ bottom: number
90
+ zoom: number
91
+
92
+ constructor(
93
+ left: number = -1,
94
+ right: number = 1,
95
+ top: number = 1,
96
+ bottom: number = -1,
97
+ near: number = 0.1,
98
+ far: number = 2000,
99
+ ) {
100
+ super()
101
+ this.left = left
102
+ this.right = right
103
+ this.top = top
104
+ this.bottom = bottom
105
+ this.near = near
106
+ this.far = far
107
+ this.zoom = 1
108
+ this.isOrthographicCamera = true
109
+ this.updateProjectionMatrix()
110
+ }
111
+
112
+ updateProjectionMatrix(): void {
113
+ const dx = (this.right - this.left) / (2 * this.zoom)
114
+ const dy = (this.top - this.bottom) / (2 * this.zoom)
115
+ const cx = (this.right + this.left) / 2
116
+ const cy = (this.top + this.bottom) / 2
117
+ this.projectionMatrix.makeOrthographic(cx - dx, cx + dx, cy + dy, cy - dy, this.near, this.far)
118
+ this.projectionChanged()
119
+ }
120
+ }