bg2e-js 2.3.11 → 2.3.13

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.
Files changed (148) hide show
  1. package/dist/bg2e-js.js +356 -326
  2. package/dist/bg2e-js.js.map +1 -1
  3. package/package.json +56 -56
  4. package/src/app/AppController.ts +39 -39
  5. package/src/app/Bg2KeyboardEvent.ts +54 -54
  6. package/src/app/Bg2MouseEvent.ts +82 -82
  7. package/src/app/Bg2TouchEvent.ts +18 -18
  8. package/src/app/Canvas.ts +108 -108
  9. package/src/app/EventBase.ts +10 -10
  10. package/src/app/MainLoop.ts +273 -273
  11. package/src/app/index.ts +24 -24
  12. package/src/base/Color.ts +134 -134
  13. package/src/base/Environment.ts +183 -183
  14. package/src/base/Light.ts +192 -192
  15. package/src/base/Material.ts +620 -620
  16. package/src/base/PolyList.ts +365 -365
  17. package/src/base/Texture.ts +620 -620
  18. package/src/base/index.ts +81 -81
  19. package/src/db/Bg2LoaderPlugin.ts +143 -143
  20. package/src/db/DBPluginApi.ts +48 -48
  21. package/src/db/Loader.ts +116 -116
  22. package/src/db/LoaderPlugin.ts +34 -34
  23. package/src/db/MtlParser.ts +7 -7
  24. package/src/db/ObjLoaderPlugin.ts +54 -54
  25. package/src/db/ObjParser.ts +252 -252
  26. package/src/db/ObjWriterPlugin.ts +18 -18
  27. package/src/db/VitscnjLoaderPlugin.ts +112 -112
  28. package/src/db/Writer.ts +52 -52
  29. package/src/db/WriterPlugin.ts +22 -22
  30. package/src/db/index.ts +44 -44
  31. package/src/debug/DebugRenderer.ts +173 -173
  32. package/src/debug/WebGLTextureViewer.ts +75 -75
  33. package/src/debug/index.ts +6 -6
  34. package/src/index.html +11 -11
  35. package/src/index.ts +33 -33
  36. package/src/manipulation/SelectionBuffer.ts +81 -81
  37. package/src/manipulation/SelectionHighlight.ts +105 -84
  38. package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
  39. package/src/manipulation/SelectionManager.ts +196 -188
  40. package/src/manipulation/SelectionMode.ts +6 -6
  41. package/src/math/Mat3.ts +259 -259
  42. package/src/math/Mat4.ts +710 -710
  43. package/src/math/MatrixStrategy.ts +25 -25
  44. package/src/math/Quat.ts +65 -65
  45. package/src/math/Vec.ts +753 -753
  46. package/src/math/constants.ts +46 -46
  47. package/src/math/functions.ts +103 -103
  48. package/src/math/index.ts +74 -74
  49. package/src/phsics/joint.ts +137 -137
  50. package/src/primitives/arrow.ts +57 -57
  51. package/src/primitives/cone.ts +138 -138
  52. package/src/primitives/cube.ts +60 -60
  53. package/src/primitives/cylinder.ts +216 -216
  54. package/src/primitives/index.ts +13 -13
  55. package/src/primitives/plane.ts +31 -31
  56. package/src/primitives/sphere.ts +809 -809
  57. package/src/react/useBg2e.ts +69 -69
  58. package/src/render/BRDFIntegrationMap.ts +4 -4
  59. package/src/render/Environment.ts +135 -135
  60. package/src/render/FrameBuffer.ts +35 -35
  61. package/src/render/MaterialRenderer.ts +34 -34
  62. package/src/render/Pipeline.ts +108 -108
  63. package/src/render/PolyListRenderer.ts +47 -47
  64. package/src/render/RenderBuffer.ts +197 -197
  65. package/src/render/RenderQueue.ts +198 -198
  66. package/src/render/RenderState.ts +116 -116
  67. package/src/render/Renderer.ts +248 -248
  68. package/src/render/SceneAppController.ts +250 -250
  69. package/src/render/SceneRenderer.ts +387 -387
  70. package/src/render/Shader.ts +32 -32
  71. package/src/render/ShadowRenderer.ts +176 -176
  72. package/src/render/SkyCube.ts +105 -105
  73. package/src/render/SkySphere.ts +117 -117
  74. package/src/render/TextureMergerRenderer.ts +70 -70
  75. package/src/render/TextureRenderer.ts +34 -34
  76. package/src/render/index.ts +67 -67
  77. package/src/render/webgl/FrameBuffer.ts +9 -9
  78. package/src/render/webgl/MaterialRenderer.ts +112 -112
  79. package/src/render/webgl/Pipeline.ts +88 -88
  80. package/src/render/webgl/PolyListRenderer.ts +260 -260
  81. package/src/render/webgl/RenderBuffer.ts +226 -226
  82. package/src/render/webgl/Renderer.ts +262 -262
  83. package/src/render/webgl/SceneRenderer.ts +67 -67
  84. package/src/render/webgl/ShaderProgram.ts +424 -424
  85. package/src/render/webgl/ShadowRenderer.ts +6 -6
  86. package/src/render/webgl/SkyCube.ts +15 -15
  87. package/src/render/webgl/SkySphere.ts +15 -15
  88. package/src/render/webgl/State.ts +152 -152
  89. package/src/render/webgl/TextureRenderer.ts +167 -167
  90. package/src/render/webgl/VertexBuffer.ts +137 -137
  91. package/src/render/webgl/index.ts +35 -35
  92. package/src/scene/Camera.ts +458 -458
  93. package/src/scene/Chain.ts +44 -44
  94. package/src/scene/ChainJoint.ts +58 -58
  95. package/src/scene/Component.ts +177 -177
  96. package/src/scene/ComponentMap.ts +106 -106
  97. package/src/scene/Drawable.ts +154 -154
  98. package/src/scene/EnvironmentComponent.ts +141 -141
  99. package/src/scene/FindNodeVisitor.ts +59 -59
  100. package/src/scene/LightComponent.ts +154 -154
  101. package/src/scene/MatrixState.ts +46 -46
  102. package/src/scene/Node.ts +328 -328
  103. package/src/scene/NodeVisitor.ts +15 -15
  104. package/src/scene/OrbitCameraController.ts +450 -450
  105. package/src/scene/SmoothOrbitCameraController.ts +99 -99
  106. package/src/scene/Transform.ts +73 -73
  107. package/src/scene/index.ts +60 -60
  108. package/src/shaders/BasicDiffuseColorShader.ts +111 -111
  109. package/src/shaders/BasicPBRLightShader.ts +276 -276
  110. package/src/shaders/DebugRenderShader.ts +97 -97
  111. package/src/shaders/DepthRenderShader.ts +127 -127
  112. package/src/shaders/IrradianceMapCubeShader.ts +115 -115
  113. package/src/shaders/PBRLightIBLShader.ts +486 -486
  114. package/src/shaders/PickSelectionShader.ts +101 -101
  115. package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
  116. package/src/shaders/PresentTextureShader.ts +99 -99
  117. package/src/shaders/SelectionHighlightShader.ts +143 -127
  118. package/src/shaders/ShaderFunction.ts +318 -318
  119. package/src/shaders/SkyCubeShader.ts +93 -93
  120. package/src/shaders/SkySphereShader.ts +102 -102
  121. package/src/shaders/SpecularMapCubeShader.ts +164 -164
  122. package/src/shaders/TextureMergerShader.ts +171 -171
  123. package/src/shaders/index.ts +36 -36
  124. package/src/shaders/webgl/color_correction.glsl +47 -47
  125. package/src/shaders/webgl/constants.glsl +6 -6
  126. package/src/shaders/webgl/index.ts +70 -70
  127. package/src/shaders/webgl/normal_map.glsl +9 -9
  128. package/src/shaders/webgl/pbr.glsl +173 -173
  129. package/src/shaders/webgl/uniforms.glsl +91 -91
  130. package/src/shaders/webgl_shader_lib.ts +213 -213
  131. package/src/tools/BinaryResourceProvider.ts +14 -14
  132. package/src/tools/ImageResourceProvider.ts +66 -66
  133. package/src/tools/MaterialModifier.ts +446 -446
  134. package/src/tools/Resource.ts +203 -203
  135. package/src/tools/ResourceProvider.ts +69 -69
  136. package/src/tools/TextResourceProvider.ts +24 -24
  137. package/src/tools/TextureCache.ts +51 -51
  138. package/src/tools/TextureResourceDatabase.ts +100 -100
  139. package/src/tools/UserAgent.ts +362 -362
  140. package/src/tools/VideoResourceProvider.ts +50 -50
  141. package/src/tools/WriteStrategy.ts +22 -22
  142. package/src/tools/base64.ts +11 -11
  143. package/src/tools/crypto.ts +19 -19
  144. package/src/tools/endiantess.ts +13 -13
  145. package/src/tools/image.ts +18 -18
  146. package/src/tools/index.ts +41 -41
  147. package/src/tools/processType.ts +39 -39
  148. package/src/vite-env.d.ts +12 -12
@@ -1,32 +1,32 @@
1
- import type Renderer from "./Renderer";
2
- import type PolyListRenderer from "./PolyListRenderer";
3
- import type MaterialRenderer from "./MaterialRenderer";
4
- import Mat4 from "../math/Mat4";
5
-
6
- export default class Shader {
7
- protected _renderer: Renderer;
8
-
9
- constructor(renderer: Renderer) {
10
- this._renderer = renderer;
11
- }
12
-
13
- get renderer(): Renderer { return this._renderer; }
14
-
15
- async load(): Promise<void> {
16
-
17
- }
18
-
19
- setup(
20
- plistRenderer: PolyListRenderer,
21
- materialRenderer: MaterialRenderer,
22
- modelMatrix: Mat4 = Mat4.MakeIdentity(),
23
- viewMatrix: Mat4 = Mat4.MakeIdentity(),
24
- projectionMatrix: Mat4 = Mat4.MakeIdentity()
25
- ): void {
26
- throw new Error("Error: using an abstract implementation of render.Shader.");
27
- }
28
-
29
- destroy(): void {
30
- throw new Error("Error: using an abstract implementation of render.Shader.destroy()");
31
- }
32
- }
1
+ import type Renderer from "./Renderer";
2
+ import type PolyListRenderer from "./PolyListRenderer";
3
+ import type MaterialRenderer from "./MaterialRenderer";
4
+ import Mat4 from "../math/Mat4";
5
+
6
+ export default class Shader {
7
+ protected _renderer: Renderer;
8
+
9
+ constructor(renderer: Renderer) {
10
+ this._renderer = renderer;
11
+ }
12
+
13
+ get renderer(): Renderer { return this._renderer; }
14
+
15
+ async load(): Promise<void> {
16
+
17
+ }
18
+
19
+ setup(
20
+ plistRenderer: PolyListRenderer,
21
+ materialRenderer: MaterialRenderer,
22
+ modelMatrix: Mat4 = Mat4.MakeIdentity(),
23
+ viewMatrix: Mat4 = Mat4.MakeIdentity(),
24
+ projectionMatrix: Mat4 = Mat4.MakeIdentity()
25
+ ): void {
26
+ throw new Error("Error: using an abstract implementation of render.Shader.");
27
+ }
28
+
29
+ destroy(): void {
30
+ throw new Error("Error: using an abstract implementation of render.Shader.destroy()");
31
+ }
32
+ }
@@ -1,176 +1,176 @@
1
- import { RenderLayer } from "../base/PolyList";
2
- import Texture, {
3
- TextureComponentFormat,
4
- TextureRenderTargetAttachment,
5
- TextureWrap
6
- } from "../base/Texture";
7
- import Mat4 from "../math/Mat4";
8
- import Vec from "../math/Vec";
9
- import Color from "../base/Color";
10
- import Camera from "../scene/Camera";
11
- import LightComponent from "../scene/LightComponent";
12
- import Transform from "../scene/Transform";
13
- import DebugRenderer from "../debug/DebugRenderer";
14
- import DepthRenderShader from "../shaders/DepthRenderShader";
15
- import Renderer from "./Renderer";
16
- import Node from "../scene/Node";
17
- import type RenderBuffer from "./RenderBuffer";
18
- import type RenderQueue from "./RenderQueue";
19
-
20
- export default class ShadowRenderer {
21
- _renderer: Renderer;
22
- _size: Vec | null = null;
23
- _texture: Texture | null = null;
24
- _renderBuffer: RenderBuffer | null = null;
25
- _depthTexture: Texture | null = null;
26
- _shader: DepthRenderShader | null = null;
27
- _shadowMapRenderDistance: number;
28
- _debug: boolean;
29
-
30
- constructor(renderer: Renderer) {
31
- this._renderer = renderer;
32
- this._shadowMapRenderDistance = 100;
33
- this._debug = false;
34
- }
35
-
36
- get renderer(): Renderer { return this._renderer; }
37
-
38
- get size(): Vec {
39
- return this._size || new Vec(0, 0);
40
- }
41
-
42
- get shadowMapRenderDistance(): number {
43
- return this._shadowMapRenderDistance;
44
- }
45
-
46
- get debug(): boolean {
47
- return this._debug;
48
- }
49
-
50
- set debug(d: boolean) {
51
- this._debug = d;
52
- }
53
-
54
- set shadowMapRenderDistance(d: number) {
55
- this._shadowMapRenderDistance = d;
56
- }
57
-
58
- // TODO: set size. Update the shadow map size
59
-
60
- get depthTexture(): Texture | null {
61
- return this._depthTexture;
62
- }
63
-
64
- async create(size: Vec = new Vec(1024, 1024)): Promise<void> {
65
- this._size = size;
66
-
67
- this._texture = new Texture();
68
- this._texture.name = `ShadowMap_${ size.width }x${ size.height }`;
69
- this._texture.renderTargetAttachment = TextureRenderTargetAttachment.COLOR_ATTACHMENT_0;
70
- this._texture.componentFormat = TextureComponentFormat.UNSIGNED_BYTE;
71
- this._texture.wrapModeXY = TextureWrap.CLAMP;
72
-
73
- this._renderBuffer = this.renderer.factory.renderBuffer();
74
-
75
- await this._renderBuffer.attachTexture(this._texture);
76
-
77
- this._depthTexture = new Texture();
78
- this._depthTexture.name = `ShadowMapDepth_${ size.width }x${ size.height }`;
79
- this._depthTexture.renderTargetAttachment = TextureRenderTargetAttachment.DEPTH_ATTACHMENT;
80
- this._depthTexture.componentFormat = TextureComponentFormat.UNSIGNED_BYTE;
81
- this._depthTexture.wrapModeXY = TextureWrap.CLAMP;
82
- await this._renderBuffer.attachTexture(this._depthTexture);
83
-
84
- this._renderBuffer.size = this._size;
85
-
86
- this._shader = new DepthRenderShader(this.renderer);
87
- await this._shader.load();
88
- }
89
-
90
- getLightTransform(camera: Camera | Node, light: LightComponent | Node): Mat4 {
91
- let cameraNode: Node | null = null;
92
- let cameraComponent: Camera | null = null;
93
- let lightComponent: LightComponent | null = null;
94
-
95
- if (camera instanceof Camera) {
96
- cameraNode = camera.node;
97
- cameraComponent = camera;
98
- }
99
- else if (camera instanceof Node) {
100
- cameraNode = camera;
101
- cameraComponent = cameraNode.camera ?? null;
102
- }
103
-
104
- if (!cameraNode || !cameraComponent) {
105
- throw Error(`ShadowRenderer.getLightPosition(): invalid camera parameter. Camera must be a Node or a Camera component, and the camera must be added to the scene.`);
106
- }
107
-
108
- let lightNode: Node | null = null;
109
- if (light instanceof LightComponent) {
110
- lightNode = light.node;
111
- lightComponent = light;
112
- }
113
- else if (light instanceof Node) {
114
- lightNode = light;
115
- lightComponent = light.lightComponent || null;
116
- }
117
-
118
- if (!lightNode || !lightComponent) {
119
- throw Error(`ShadowRenderer.getLightPosition(): invalid light. Light must be a Node or a LightComponent`);
120
- }
121
-
122
- // Get the camera focus point
123
- const focus = cameraComponent!.focusDistance;
124
- const cameraTransform = Transform.GetWorldMatrix(cameraNode);
125
- const cameraPos = Vec.Add(Mat4.GetPosition(cameraTransform), Vec.Mult(cameraTransform.forwardVector, -focus));
126
-
127
- // Get the light rotation vector and scale it to the light shadow map render distance
128
- const lightTransform = Transform.GetWorldMatrix(lightNode);
129
- const lightVector = Mat4.GetRotation(lightTransform).forwardVector;
130
- lightVector.scale(this._shadowMapRenderDistance);
131
-
132
- // Get the light render position, adding the camera focus point to the light vector
133
- const lightPos = Vec.Add(cameraPos, lightVector);
134
-
135
- // Set the light render position to the light transform matrix
136
- lightTransform.setPosition(lightPos);
137
-
138
- if (this._debug) {
139
- DebugRenderer.Get(this._renderer).drawSphere({ radius: 0.1, color: Color.Red(), position: cameraPos });
140
- DebugRenderer.Get(this.renderer).drawSphere({ radius: 0.1, color: Color.Blue(), position: lightPos });
141
- DebugRenderer.Get(this.renderer).drawArrow({ length: 0.8, color: Color.Green(), transformMatrix: lightTransform });
142
- }
143
-
144
- return lightTransform;
145
- }
146
-
147
- update(camera: Camera | Node, lightComponent: LightComponent, renderQueue: RenderQueue): void {
148
- const viewMatrix: Mat4 = Mat4.GetInverted(this.getLightTransform(camera, lightComponent));
149
-
150
- this._renderBuffer?.update(() => {
151
- this._renderBuffer!.renderer.state.clear();
152
- const layer: RenderLayer = RenderLayer.OPAQUE_DEFAULT;
153
- const queue = renderQueue.getQueue(layer);
154
- if (queue) {
155
-
156
- if (typeof(queue.beginOperation) === "function") {
157
- queue.beginOperation(layer);
158
- }
159
- queue.queue.forEach(rs => {
160
- rs.draw({
161
- overrideShader: this._shader,
162
- overrideViewMatrix: viewMatrix,
163
- overrideProjectionMatrix: lightComponent.light.projection
164
- });
165
- });
166
- if (typeof(queue.endOperation) === "function") {
167
- queue.endOperation(layer);
168
- }
169
- }
170
- });
171
-
172
- // Set the depthTexture to the light component. If the shader needs the depth texture, it will use it
173
- lightComponent.depthTexture = this._depthTexture;
174
- lightComponent.viewMatrix = viewMatrix;
175
- }
176
- }
1
+ import { RenderLayer } from "../base/PolyList";
2
+ import Texture, {
3
+ TextureComponentFormat,
4
+ TextureRenderTargetAttachment,
5
+ TextureWrap
6
+ } from "../base/Texture";
7
+ import Mat4 from "../math/Mat4";
8
+ import Vec from "../math/Vec";
9
+ import Color from "../base/Color";
10
+ import Camera from "../scene/Camera";
11
+ import LightComponent from "../scene/LightComponent";
12
+ import Transform from "../scene/Transform";
13
+ import DebugRenderer from "../debug/DebugRenderer";
14
+ import DepthRenderShader from "../shaders/DepthRenderShader";
15
+ import Renderer from "./Renderer";
16
+ import Node from "../scene/Node";
17
+ import type RenderBuffer from "./RenderBuffer";
18
+ import type RenderQueue from "./RenderQueue";
19
+
20
+ export default class ShadowRenderer {
21
+ _renderer: Renderer;
22
+ _size: Vec | null = null;
23
+ _texture: Texture | null = null;
24
+ _renderBuffer: RenderBuffer | null = null;
25
+ _depthTexture: Texture | null = null;
26
+ _shader: DepthRenderShader | null = null;
27
+ _shadowMapRenderDistance: number;
28
+ _debug: boolean;
29
+
30
+ constructor(renderer: Renderer) {
31
+ this._renderer = renderer;
32
+ this._shadowMapRenderDistance = 100;
33
+ this._debug = false;
34
+ }
35
+
36
+ get renderer(): Renderer { return this._renderer; }
37
+
38
+ get size(): Vec {
39
+ return this._size || new Vec(0, 0);
40
+ }
41
+
42
+ get shadowMapRenderDistance(): number {
43
+ return this._shadowMapRenderDistance;
44
+ }
45
+
46
+ get debug(): boolean {
47
+ return this._debug;
48
+ }
49
+
50
+ set debug(d: boolean) {
51
+ this._debug = d;
52
+ }
53
+
54
+ set shadowMapRenderDistance(d: number) {
55
+ this._shadowMapRenderDistance = d;
56
+ }
57
+
58
+ // TODO: set size. Update the shadow map size
59
+
60
+ get depthTexture(): Texture | null {
61
+ return this._depthTexture;
62
+ }
63
+
64
+ async create(size: Vec = new Vec(1024, 1024)): Promise<void> {
65
+ this._size = size;
66
+
67
+ this._texture = new Texture();
68
+ this._texture.name = `ShadowMap_${ size.width }x${ size.height }`;
69
+ this._texture.renderTargetAttachment = TextureRenderTargetAttachment.COLOR_ATTACHMENT_0;
70
+ this._texture.componentFormat = TextureComponentFormat.UNSIGNED_BYTE;
71
+ this._texture.wrapModeXY = TextureWrap.CLAMP;
72
+
73
+ this._renderBuffer = this.renderer.factory.renderBuffer();
74
+
75
+ await this._renderBuffer.attachTexture(this._texture);
76
+
77
+ this._depthTexture = new Texture();
78
+ this._depthTexture.name = `ShadowMapDepth_${ size.width }x${ size.height }`;
79
+ this._depthTexture.renderTargetAttachment = TextureRenderTargetAttachment.DEPTH_ATTACHMENT;
80
+ this._depthTexture.componentFormat = TextureComponentFormat.UNSIGNED_BYTE;
81
+ this._depthTexture.wrapModeXY = TextureWrap.CLAMP;
82
+ await this._renderBuffer.attachTexture(this._depthTexture);
83
+
84
+ this._renderBuffer.size = this._size;
85
+
86
+ this._shader = new DepthRenderShader(this.renderer);
87
+ await this._shader.load();
88
+ }
89
+
90
+ getLightTransform(camera: Camera | Node, light: LightComponent | Node): Mat4 {
91
+ let cameraNode: Node | null = null;
92
+ let cameraComponent: Camera | null = null;
93
+ let lightComponent: LightComponent | null = null;
94
+
95
+ if (camera instanceof Camera) {
96
+ cameraNode = camera.node;
97
+ cameraComponent = camera;
98
+ }
99
+ else if (camera instanceof Node) {
100
+ cameraNode = camera;
101
+ cameraComponent = cameraNode.camera ?? null;
102
+ }
103
+
104
+ if (!cameraNode || !cameraComponent) {
105
+ throw Error(`ShadowRenderer.getLightPosition(): invalid camera parameter. Camera must be a Node or a Camera component, and the camera must be added to the scene.`);
106
+ }
107
+
108
+ let lightNode: Node | null = null;
109
+ if (light instanceof LightComponent) {
110
+ lightNode = light.node;
111
+ lightComponent = light;
112
+ }
113
+ else if (light instanceof Node) {
114
+ lightNode = light;
115
+ lightComponent = light.lightComponent || null;
116
+ }
117
+
118
+ if (!lightNode || !lightComponent) {
119
+ throw Error(`ShadowRenderer.getLightPosition(): invalid light. Light must be a Node or a LightComponent`);
120
+ }
121
+
122
+ // Get the camera focus point
123
+ const focus = cameraComponent!.focusDistance;
124
+ const cameraTransform = Transform.GetWorldMatrix(cameraNode);
125
+ const cameraPos = Vec.Add(Mat4.GetPosition(cameraTransform), Vec.Mult(cameraTransform.forwardVector, -focus));
126
+
127
+ // Get the light rotation vector and scale it to the light shadow map render distance
128
+ const lightTransform = Transform.GetWorldMatrix(lightNode);
129
+ const lightVector = Mat4.GetRotation(lightTransform).forwardVector;
130
+ lightVector.scale(this._shadowMapRenderDistance);
131
+
132
+ // Get the light render position, adding the camera focus point to the light vector
133
+ const lightPos = Vec.Add(cameraPos, lightVector);
134
+
135
+ // Set the light render position to the light transform matrix
136
+ lightTransform.setPosition(lightPos);
137
+
138
+ if (this._debug) {
139
+ DebugRenderer.Get(this._renderer).drawSphere({ radius: 0.1, color: Color.Red(), position: cameraPos });
140
+ DebugRenderer.Get(this.renderer).drawSphere({ radius: 0.1, color: Color.Blue(), position: lightPos });
141
+ DebugRenderer.Get(this.renderer).drawArrow({ length: 0.8, color: Color.Green(), transformMatrix: lightTransform });
142
+ }
143
+
144
+ return lightTransform;
145
+ }
146
+
147
+ update(camera: Camera | Node, lightComponent: LightComponent, renderQueue: RenderQueue): void {
148
+ const viewMatrix: Mat4 = Mat4.GetInverted(this.getLightTransform(camera, lightComponent));
149
+
150
+ this._renderBuffer?.update(() => {
151
+ this._renderBuffer!.renderer.state.clear();
152
+ const layer: RenderLayer = RenderLayer.OPAQUE_DEFAULT;
153
+ const queue = renderQueue.getQueue(layer);
154
+ if (queue) {
155
+
156
+ if (typeof(queue.beginOperation) === "function") {
157
+ queue.beginOperation(layer);
158
+ }
159
+ queue.queue.forEach(rs => {
160
+ rs.draw({
161
+ overrideShader: this._shader,
162
+ overrideViewMatrix: viewMatrix,
163
+ overrideProjectionMatrix: lightComponent.light.projection
164
+ });
165
+ });
166
+ if (typeof(queue.endOperation) === "function") {
167
+ queue.endOperation(layer);
168
+ }
169
+ }
170
+ });
171
+
172
+ // Set the depthTexture to the light component. If the shader needs the depth texture, it will use it
173
+ lightComponent.depthTexture = this._depthTexture;
174
+ lightComponent.viewMatrix = viewMatrix;
175
+ }
176
+ }
@@ -1,106 +1,106 @@
1
- import Material from "../base/Material";
2
- import { createCube } from "../primitives";
3
- import RenderState from "./RenderState";
4
- import SkyCubeShader from "../shaders/SkyCubeShader";
5
- import Mat4 from "../math/Mat4";
6
- import { PolyListCullFace } from "../base/PolyList";
7
- import type Renderer from "./Renderer";
8
- import type Texture from "../base/Texture";
9
- import type Shader from "./Shader";
10
- import type PolyListRenderer from "./PolyListRenderer";
11
-
12
- export interface UpdateRenderStateOptions {
13
- viewMatrix: Mat4;
14
- projectionMatrix?: Mat4 | null;
15
- }
16
-
17
- export default class SkyCube {
18
- protected _renderer: Renderer;
19
- protected _texture: Texture | null;
20
- protected _material: Material | null;
21
- protected _shader: Shader | null;
22
- protected _plistRenderer: PolyListRenderer | null;
23
- protected _renderState: RenderState | null;
24
-
25
- constructor(renderer: Renderer) {
26
- this._renderer = renderer;
27
- this._texture = null;
28
- this._material = null;
29
- this._shader = null;
30
- this._plistRenderer = null;
31
- this._renderState = null;
32
- }
33
-
34
- get renderer(): Renderer { return this._renderer; }
35
-
36
- set texture(texture: Texture) {
37
- if (!this._texture) {
38
- throw new Error("SkyCube: setting texture to an uninitialized sky cube. The texture setter is used to change the skyCube texture once created. Use the load() method instead.");
39
- }
40
- if (!this._material) {
41
- throw new Error("SkyCube: internal error, material is null when setting texture.");
42
- }
43
- this._texture = texture;
44
- this._material.albedoTexture = this._texture;
45
- }
46
-
47
- async load(cubemapTexture: Texture, Shader: (new (renderer: Renderer) => any) | null = null, shaderParams: any[] = []): Promise<void> {
48
-
49
- this._texture = cubemapTexture;
50
-
51
- this._material = new Material();
52
- this._material.albedoTexture = this._texture;
53
-
54
- this._shader = Shader ? new Shader(this.renderer) : new SkyCubeShader(this.renderer);
55
- await this._shader?.load();
56
- }
57
-
58
- updateRenderState({ viewMatrix, projectionMatrix = null }: UpdateRenderStateOptions): RenderState {
59
- const rotationMatrix = Mat4.GetRotation(viewMatrix);
60
- if (!this._renderState) {
61
- if (!this._material) {
62
- throw new Error("SkyCube: internal error, material is null when updating render state.");
63
- }
64
-
65
- this._renderState = new RenderState({
66
- shader: this._shader,
67
- polyListRenderer: this.polyListRenderer,
68
- materialRenderer: this.renderer.factory.material(this._material),
69
- viewMatrix: rotationMatrix,
70
- projectionMatrix: projectionMatrix || Mat4.MakeIdentity()
71
- });
72
- }
73
- else {
74
- this._renderState.viewMatrix = rotationMatrix;
75
- if (projectionMatrix) {
76
- this._renderState.projectionMatrix = projectionMatrix;
77
- }
78
- }
79
- return this._renderState;
80
- }
81
-
82
- draw(): void {
83
- throw new Error("SkyCube.draw(): Calling base implementation of SkyCube");
84
- }
85
-
86
- get polyListRenderer(): PolyListRenderer {
87
- if (!this._plistRenderer) {
88
- const cube = createCube(1,1,1);
89
- cube.cullFace = PolyListCullFace.FRONT; // Draw back face
90
- this._plistRenderer = this.renderer.factory.polyList(cube);
91
- }
92
- return this._plistRenderer;
93
- }
94
-
95
- destroy(): void {
96
- this._shader?.destroy();
97
- this._texture?.destroy();
98
- this._plistRenderer?.destroy();
99
- this._material?.destroy();
100
- this._shader = null;
101
- this._texture = null;
102
- this._material = null;
103
- this._plistRenderer = null;
104
- this._renderState = null;
105
- }
1
+ import Material from "../base/Material";
2
+ import { createCube } from "../primitives";
3
+ import RenderState from "./RenderState";
4
+ import SkyCubeShader from "../shaders/SkyCubeShader";
5
+ import Mat4 from "../math/Mat4";
6
+ import { PolyListCullFace } from "../base/PolyList";
7
+ import type Renderer from "./Renderer";
8
+ import type Texture from "../base/Texture";
9
+ import type Shader from "./Shader";
10
+ import type PolyListRenderer from "./PolyListRenderer";
11
+
12
+ export interface UpdateRenderStateOptions {
13
+ viewMatrix: Mat4;
14
+ projectionMatrix?: Mat4 | null;
15
+ }
16
+
17
+ export default class SkyCube {
18
+ protected _renderer: Renderer;
19
+ protected _texture: Texture | null;
20
+ protected _material: Material | null;
21
+ protected _shader: Shader | null;
22
+ protected _plistRenderer: PolyListRenderer | null;
23
+ protected _renderState: RenderState | null;
24
+
25
+ constructor(renderer: Renderer) {
26
+ this._renderer = renderer;
27
+ this._texture = null;
28
+ this._material = null;
29
+ this._shader = null;
30
+ this._plistRenderer = null;
31
+ this._renderState = null;
32
+ }
33
+
34
+ get renderer(): Renderer { return this._renderer; }
35
+
36
+ set texture(texture: Texture) {
37
+ if (!this._texture) {
38
+ throw new Error("SkyCube: setting texture to an uninitialized sky cube. The texture setter is used to change the skyCube texture once created. Use the load() method instead.");
39
+ }
40
+ if (!this._material) {
41
+ throw new Error("SkyCube: internal error, material is null when setting texture.");
42
+ }
43
+ this._texture = texture;
44
+ this._material.albedoTexture = this._texture;
45
+ }
46
+
47
+ async load(cubemapTexture: Texture, Shader: (new (renderer: Renderer) => any) | null = null, shaderParams: any[] = []): Promise<void> {
48
+
49
+ this._texture = cubemapTexture;
50
+
51
+ this._material = new Material();
52
+ this._material.albedoTexture = this._texture;
53
+
54
+ this._shader = Shader ? new Shader(this.renderer) : new SkyCubeShader(this.renderer);
55
+ await this._shader?.load();
56
+ }
57
+
58
+ updateRenderState({ viewMatrix, projectionMatrix = null }: UpdateRenderStateOptions): RenderState {
59
+ const rotationMatrix = Mat4.GetRotation(viewMatrix);
60
+ if (!this._renderState) {
61
+ if (!this._material) {
62
+ throw new Error("SkyCube: internal error, material is null when updating render state.");
63
+ }
64
+
65
+ this._renderState = new RenderState({
66
+ shader: this._shader,
67
+ polyListRenderer: this.polyListRenderer,
68
+ materialRenderer: this.renderer.factory.material(this._material),
69
+ viewMatrix: rotationMatrix,
70
+ projectionMatrix: projectionMatrix || Mat4.MakeIdentity()
71
+ });
72
+ }
73
+ else {
74
+ this._renderState.viewMatrix = rotationMatrix;
75
+ if (projectionMatrix) {
76
+ this._renderState.projectionMatrix = projectionMatrix;
77
+ }
78
+ }
79
+ return this._renderState;
80
+ }
81
+
82
+ draw(): void {
83
+ throw new Error("SkyCube.draw(): Calling base implementation of SkyCube");
84
+ }
85
+
86
+ get polyListRenderer(): PolyListRenderer {
87
+ if (!this._plistRenderer) {
88
+ const cube = createCube(1,1,1);
89
+ cube.cullFace = PolyListCullFace.FRONT; // Draw back face
90
+ this._plistRenderer = this.renderer.factory.polyList(cube);
91
+ }
92
+ return this._plistRenderer;
93
+ }
94
+
95
+ destroy(): void {
96
+ this._shader?.destroy();
97
+ this._texture?.destroy();
98
+ this._plistRenderer?.destroy();
99
+ this._material?.destroy();
100
+ this._shader = null;
101
+ this._texture = null;
102
+ this._material = null;
103
+ this._plistRenderer = null;
104
+ this._renderState = null;
105
+ }
106
106
  }