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,277 +1,277 @@
1
- import Shader from "../render/Shader";
2
-
3
- // TODO: Deprecated. Use ./webgl/index.ts instead
4
- import { pbrPointLight, pbrDirectionalLight } from './webgl_shader_lib';
5
-
6
- import {
7
- getColorCorrectionFunctions,
8
- getNormalMapFunctions,
9
- getPBRFunctions,
10
- getUniformsFunctions,
11
- replaceConstants
12
- } from "./webgl";
13
-
14
- import ShaderFunction from "./ShaderFunction";
15
- import ShaderProgram from "../render/webgl/ShaderProgram";
16
- import Mat4 from "../math/Mat4";
17
- import Vec from "../math/Vec";
18
- import Light, { LightType } from "../base/Light";
19
- import { createNormalTexture, normalTexture } from "../tools/TextureResourceDatabase";
20
- import PolyListRenderer from '../render/PolyListRenderer';
21
- import MaterialRenderer from '../render/MaterialRenderer';
22
- import Renderer from "../render/Renderer";
23
- import WebGLRenderer from "../render/webgl/Renderer";
24
- import WebGLMaterialRenderer from "../render/webgl/MaterialRenderer";
25
- import WebGLPolyListRenderer from "../render/webgl/PolyListRenderer";
26
-
27
- function getShaderProgramForLights(this: BasicPBRLightShader, renderer: Renderer, numLights: number): ShaderProgram {
28
- const r = renderer as WebGLRenderer;
29
-
30
- if (this._programs[numLights]) {
31
- return this._programs[numLights];
32
- }
33
-
34
- const vshader = ShaderFunction.GetShaderCode(`precision mediump float;
35
- attribute vec3 inPosition;
36
- attribute vec3 inNormal;
37
- attribute vec2 inTexCoord;
38
- attribute vec3 inTangent;
39
-
40
- uniform mat4 uWorld;
41
- uniform mat4 uView;
42
- uniform mat4 uProj;
43
- uniform mat3 uNormMatrix;
44
-
45
- varying vec3 fragPos;
46
- varying vec3 fragNorm;
47
- varying vec2 fragTexCoord;
48
- varying vec3 fragTangent;
49
- varying vec3 fragBitangent;
50
- `,
51
- [
52
- new ShaderFunction('void','main','',`{
53
- fragPos = (uWorld * vec4(inPosition, 1.0)).xyz;
54
- fragNorm = normalize(uNormMatrix * inNormal);
55
- fragTangent = normalize(uNormMatrix * inTangent);
56
- fragBitangent = normalize(fragNorm * fragTangent);
57
- fragTexCoord = inTexCoord;
58
- gl_Position = uProj * uView * uWorld * vec4(inPosition,1.0);
59
- }`)
60
- ]);
61
-
62
- const fshader = ShaderFunction.GetShaderCode(`precision mediump float;
63
- varying vec3 fragPos;
64
- varying vec3 fragNorm;
65
- varying vec2 fragTexCoord;
66
- varying vec3 fragTangent;
67
- varying vec3 fragBitangent;
68
-
69
- uniform vec3 uCameraPos;
70
-
71
- uniform sampler2D uAlbedoTexture;
72
- uniform sampler2D uNormalTexture;
73
- uniform sampler2D uMetallicTexture;
74
- uniform sampler2D uRoughnessTexture;
75
-
76
- uniform vec4 uAlbedo;
77
- uniform float uMetallic;
78
- uniform float uRoughness;
79
-
80
- uniform vec2 uAlbedoScale;
81
- uniform vec2 uNormalScale;
82
- uniform vec2 uMetallicScale;
83
- uniform vec2 uRoughnessScale;
84
-
85
- uniform int uLightTypes[${numLights}];
86
- uniform vec3 uLightPositions[${numLights}];
87
- uniform vec3 uLightDirections[${numLights}];
88
- uniform vec3 uLightColors[${numLights}];
89
- uniform float uLightIntensities[${numLights}];
90
- `,
91
- [
92
- new ShaderFunction('void','main','',`{
93
- vec3 N = normalize(fragNorm);
94
- if (!gl_FrontFacing) {
95
- N = -N;
96
- }
97
- vec3 T = normalize(fragTangent);
98
- vec3 B = normalize(fragBitangent);
99
- mat3 TBN = mat3(T,B,N);
100
-
101
- vec3 albedo = texture2D(uAlbedoTexture, fragTexCoord * uAlbedoScale).rgb * uAlbedo.rgb;
102
- vec3 normal = texture2D(uNormalTexture, fragTexCoord * uNormalScale).rgb * 2.0 - 1.0;
103
- float metallic = texture2D(uMetallicTexture, fragTexCoord * uMetallicScale).r * uMetallic;
104
- float roughness = max(texture2D(uRoughnessTexture, fragTexCoord * uRoughnessScale).r * uRoughness, 0.05);
105
-
106
- N = normalize(TBN * normal);
107
- vec3 V = normalize(uCameraPos - fragPos);
108
-
109
- vec3 Lo = vec3(0.0);
110
- for (int i = 0; i < ${numLights}; ++i)
111
- {
112
- if (uLightTypes[i] == ${ LightType.POINT }) {
113
- Lo += pbrPointLight(
114
- uLightPositions[i], uLightColors[i] * uLightIntensities[i], fragPos, N, V,
115
- albedo, roughness, metallic, vec3(1.0));
116
- }
117
- else if (uLightTypes[i] == ${ LightType.DIRECTIONAL }) {
118
- Lo += pbrDirectionalLight(
119
- uLightDirections[i], uLightColors[i] * uLightIntensities[i], fragPos, N, V,
120
- albedo, roughness, metallic, vec3(1.0), vec3(1.0));
121
- }
122
- }
123
-
124
- vec3 ambient = vec3(0.03) * albedo;
125
- vec3 color = ambient + Lo;
126
- color = color / (color + vec3(1.0));
127
- color = pow(color, vec3(1.0/2.2));
128
-
129
- gl_FragColor = vec4(color,1.0);
130
- }`, [pbrPointLight, pbrDirectionalLight])
131
- ]);
132
-
133
- this._programs[numLights] = ShaderProgram.Create(r.gl,"PBRBasicLight",vshader,fshader);
134
- return this._programs[numLights];
135
- }
136
- export default class BasicPBRLightShader extends Shader {
137
- protected _programs: { [key: number]: ShaderProgram };
138
- protected _lights: Light[];
139
- protected _lightTypes: LightType[] = [];
140
- protected _lightPositions: Vec[] = [];
141
- protected _lightDirections: Vec[] = [];
142
- protected _lightColors: Vec[] = [];
143
- protected _lightIntensities: number[] = [];
144
- protected _cameraPosition: Vec | null = null;
145
- protected _program: ShaderProgram | null = null;
146
-
147
- constructor(renderer: Renderer) {
148
- super(renderer);
149
-
150
- this._lights = [];
151
-
152
- if (renderer.typeId !== "WebGL") {
153
- throw Error("BasicPBRLightShader is only compatible with WebGL renderer");
154
- }
155
-
156
- this._programs = {};
157
- }
158
-
159
- async load() {
160
- await createNormalTexture(this.renderer);
161
- }
162
-
163
- set cameraPosition(p: Vec) {
164
- this._cameraPosition = p;
165
- }
166
-
167
- set lights(l: Light[]) {
168
- if (!l.length) {
169
- throw new Error("BasicPBRLightShader: Invalid light array set.");
170
- }
171
- this._lights = l;
172
- this._lightTypes = [];
173
- this._lightPositions = [];
174
- this._lightDirections = [];
175
- this._lightColors = [];
176
- this._lightIntensities = [];
177
- this._lights.forEach(light => {
178
- this._lightTypes.push(light.type);
179
- this._lightPositions.push(new Vec(light.position));
180
- this._lightDirections.push(new Vec(light.direction));
181
- this._lightColors.push(new Vec(light.color));
182
- this._lightIntensities.push(light.intensity);
183
- });
184
- this._program = getShaderProgramForLights.apply(this, [this.renderer, this._lights.length]);
185
- }
186
-
187
- get lights() {
188
- return this._lights;
189
- }
190
-
191
- updateLight(light: Light, index: number) {
192
- if (index >= this._lights.length) {
193
- throw new Error("BasicPBRLightShader: Invalid light index updating light data");
194
- }
195
- this._lightTypes[index] = light.type;
196
- this._lightPositions[index] = new Vec(light.position);
197
- this._lightDirections[index] = new Vec(light.direction);
198
- this._lightColors[index] = new Vec(light.color);
199
- this._lightIntensities[index] = light.intensity;
200
- }
201
-
202
- updateLights(lights: Light[]) {
203
- if (this._lights.length !== lights.length) {
204
- this.lights = lights;
205
- }
206
- else {
207
- lights.forEach((l,i) => this.updateLight(l,i));
208
- }
209
- }
210
-
211
- setup(
212
- plistRenderer: PolyListRenderer,
213
- materialRenderer: MaterialRenderer,
214
- modelMatrix: Mat4,
215
- viewMatrix: Mat4,
216
- projectionMatrix: Mat4
217
- ) {
218
- if (!this._program) {
219
- throw new Error("BasicPBRLightShader: lights array not specified");
220
- }
221
-
222
- const material = materialRenderer.material;
223
- const rend = this.renderer as WebGLRenderer;
224
- rend.state.shaderProgram = this._program;
225
-
226
- const normMatrix = Mat4.GetNormalMatrix(modelMatrix);
227
- this._program.bindMatrix('uNormMatrix', normMatrix);
228
- this._program.bindMatrix('uWorld', modelMatrix);
229
- this._program.bindMatrix('uView', viewMatrix);
230
- this._program.bindMatrix('uProj', projectionMatrix);
231
-
232
- if (!this._cameraPosition) {
233
- console.warn("Serious performance warning: camera position not set in BasicPBRLightShader. Extracting the camera position from the view matrix involves inverting the matrix.");
234
- this._program.bindVector('uCameraPos',Mat4.GetPosition(Mat4.GetInverted(viewMatrix)));
235
- }
236
- else {
237
- this._program.bindVector('uCameraPos',this._cameraPosition);
238
- }
239
-
240
- (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'albedoTexture', 'uAlbedoTexture', 0);
241
- (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'normalTexture', 'uNormalTexture', 1, normalTexture(this.renderer));
242
- (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'metalnessTexture', 'uMetallicTexture', 2);
243
- (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'roughnessTexture', 'uRoughnessTexture', 3);
244
-
245
- (materialRenderer as WebGLMaterialRenderer).bindColor(this._program, 'albedo', 'uAlbedo');
246
- (materialRenderer as WebGLMaterialRenderer).bindValue(this._program, 'metalness', 'uMetallic');
247
- (materialRenderer as WebGLMaterialRenderer).bindValue(this._program, 'roughness', 'uRoughness');
248
-
249
-
250
- this._program.bindVector("uAlbedoScale", material.albedoScale);
251
- this._program.bindVector("uNormalScale", material.normalScale);
252
- this._program.bindVector("uMetallicScale", material.metalnessScale);
253
- this._program.bindVector("uRoughnessScale", material.roughnessScale);
254
-
255
- this._lights.forEach((light,i) => {
256
- this._program?.uniform1i(`uLightTypes[${i}]`, this._lightTypes[i]);
257
- this._program?.bindVector(`uLightPositions[${i}]`, this._lightPositions[i]);
258
- this._program?.bindVector(`uLightDirections[${i}]`, this._lightDirections[i]);
259
- this._program?.bindVector(`uLightColors[${i}]`, this._lightColors[i].rgb);
260
- this._program?.uniform1f(`uLightIntensities[${i}]`, this._lightIntensities[i]);
261
- });
262
-
263
- this._program.bindAttribs(plistRenderer as WebGLPolyListRenderer, {
264
- position: "inPosition",
265
- normal: "inNormal",
266
- tex0: "inTexCoord",
267
- tangent: "inTangent"
268
- });
269
- }
270
-
271
- destroy() {
272
- if (this._program) {
273
- ShaderProgram.Delete(this._program);
274
- this._program = null;
275
- }
276
- }
1
+ import Shader from "../render/Shader";
2
+
3
+ // TODO: Deprecated. Use ./webgl/index.ts instead
4
+ import { pbrPointLight, pbrDirectionalLight } from './webgl_shader_lib';
5
+
6
+ import {
7
+ getColorCorrectionFunctions,
8
+ getNormalMapFunctions,
9
+ getPBRFunctions,
10
+ getUniformsFunctions,
11
+ replaceConstants
12
+ } from "./webgl";
13
+
14
+ import ShaderFunction from "./ShaderFunction";
15
+ import ShaderProgram from "../render/webgl/ShaderProgram";
16
+ import Mat4 from "../math/Mat4";
17
+ import Vec from "../math/Vec";
18
+ import Light, { LightType } from "../base/Light";
19
+ import { createNormalTexture, normalTexture } from "../tools/TextureResourceDatabase";
20
+ import PolyListRenderer from '../render/PolyListRenderer';
21
+ import MaterialRenderer from '../render/MaterialRenderer';
22
+ import Renderer from "../render/Renderer";
23
+ import WebGLRenderer from "../render/webgl/Renderer";
24
+ import WebGLMaterialRenderer from "../render/webgl/MaterialRenderer";
25
+ import WebGLPolyListRenderer from "../render/webgl/PolyListRenderer";
26
+
27
+ function getShaderProgramForLights(this: BasicPBRLightShader, renderer: Renderer, numLights: number): ShaderProgram {
28
+ const r = renderer as WebGLRenderer;
29
+
30
+ if (this._programs[numLights]) {
31
+ return this._programs[numLights];
32
+ }
33
+
34
+ const vshader = ShaderFunction.GetShaderCode(`precision mediump float;
35
+ attribute vec3 inPosition;
36
+ attribute vec3 inNormal;
37
+ attribute vec2 inTexCoord;
38
+ attribute vec3 inTangent;
39
+
40
+ uniform mat4 uWorld;
41
+ uniform mat4 uView;
42
+ uniform mat4 uProj;
43
+ uniform mat3 uNormMatrix;
44
+
45
+ varying vec3 fragPos;
46
+ varying vec3 fragNorm;
47
+ varying vec2 fragTexCoord;
48
+ varying vec3 fragTangent;
49
+ varying vec3 fragBitangent;
50
+ `,
51
+ [
52
+ new ShaderFunction('void','main','',`{
53
+ fragPos = (uWorld * vec4(inPosition, 1.0)).xyz;
54
+ fragNorm = normalize(uNormMatrix * inNormal);
55
+ fragTangent = normalize(uNormMatrix * inTangent);
56
+ fragBitangent = normalize(fragNorm * fragTangent);
57
+ fragTexCoord = inTexCoord;
58
+ gl_Position = uProj * uView * uWorld * vec4(inPosition,1.0);
59
+ }`)
60
+ ]);
61
+
62
+ const fshader = ShaderFunction.GetShaderCode(`precision mediump float;
63
+ varying vec3 fragPos;
64
+ varying vec3 fragNorm;
65
+ varying vec2 fragTexCoord;
66
+ varying vec3 fragTangent;
67
+ varying vec3 fragBitangent;
68
+
69
+ uniform vec3 uCameraPos;
70
+
71
+ uniform sampler2D uAlbedoTexture;
72
+ uniform sampler2D uNormalTexture;
73
+ uniform sampler2D uMetallicTexture;
74
+ uniform sampler2D uRoughnessTexture;
75
+
76
+ uniform vec4 uAlbedo;
77
+ uniform float uMetallic;
78
+ uniform float uRoughness;
79
+
80
+ uniform vec2 uAlbedoScale;
81
+ uniform vec2 uNormalScale;
82
+ uniform vec2 uMetallicScale;
83
+ uniform vec2 uRoughnessScale;
84
+
85
+ uniform int uLightTypes[${numLights}];
86
+ uniform vec3 uLightPositions[${numLights}];
87
+ uniform vec3 uLightDirections[${numLights}];
88
+ uniform vec3 uLightColors[${numLights}];
89
+ uniform float uLightIntensities[${numLights}];
90
+ `,
91
+ [
92
+ new ShaderFunction('void','main','',`{
93
+ vec3 N = normalize(fragNorm);
94
+ if (!gl_FrontFacing) {
95
+ N = -N;
96
+ }
97
+ vec3 T = normalize(fragTangent);
98
+ vec3 B = normalize(fragBitangent);
99
+ mat3 TBN = mat3(T,B,N);
100
+
101
+ vec3 albedo = texture2D(uAlbedoTexture, fragTexCoord * uAlbedoScale).rgb * uAlbedo.rgb;
102
+ vec3 normal = texture2D(uNormalTexture, fragTexCoord * uNormalScale).rgb * 2.0 - 1.0;
103
+ float metallic = texture2D(uMetallicTexture, fragTexCoord * uMetallicScale).r * uMetallic;
104
+ float roughness = max(texture2D(uRoughnessTexture, fragTexCoord * uRoughnessScale).r * uRoughness, 0.05);
105
+
106
+ N = normalize(TBN * normal);
107
+ vec3 V = normalize(uCameraPos - fragPos);
108
+
109
+ vec3 Lo = vec3(0.0);
110
+ for (int i = 0; i < ${numLights}; ++i)
111
+ {
112
+ if (uLightTypes[i] == ${ LightType.POINT }) {
113
+ Lo += pbrPointLight(
114
+ uLightPositions[i], uLightColors[i] * uLightIntensities[i], fragPos, N, V,
115
+ albedo, roughness, metallic, vec3(1.0));
116
+ }
117
+ else if (uLightTypes[i] == ${ LightType.DIRECTIONAL }) {
118
+ Lo += pbrDirectionalLight(
119
+ uLightDirections[i], uLightColors[i] * uLightIntensities[i], fragPos, N, V,
120
+ albedo, roughness, metallic, vec3(1.0), vec3(1.0));
121
+ }
122
+ }
123
+
124
+ vec3 ambient = vec3(0.03) * albedo;
125
+ vec3 color = ambient + Lo;
126
+ color = color / (color + vec3(1.0));
127
+ color = pow(color, vec3(1.0/2.2));
128
+
129
+ gl_FragColor = vec4(color,1.0);
130
+ }`, [pbrPointLight, pbrDirectionalLight])
131
+ ]);
132
+
133
+ this._programs[numLights] = ShaderProgram.Create(r.gl,"PBRBasicLight",vshader,fshader);
134
+ return this._programs[numLights];
135
+ }
136
+ export default class BasicPBRLightShader extends Shader {
137
+ protected _programs: { [key: number]: ShaderProgram };
138
+ protected _lights: Light[];
139
+ protected _lightTypes: LightType[] = [];
140
+ protected _lightPositions: Vec[] = [];
141
+ protected _lightDirections: Vec[] = [];
142
+ protected _lightColors: Vec[] = [];
143
+ protected _lightIntensities: number[] = [];
144
+ protected _cameraPosition: Vec | null = null;
145
+ protected _program: ShaderProgram | null = null;
146
+
147
+ constructor(renderer: Renderer) {
148
+ super(renderer);
149
+
150
+ this._lights = [];
151
+
152
+ if (renderer.typeId !== "WebGL") {
153
+ throw Error("BasicPBRLightShader is only compatible with WebGL renderer");
154
+ }
155
+
156
+ this._programs = {};
157
+ }
158
+
159
+ async load() {
160
+ await createNormalTexture(this.renderer);
161
+ }
162
+
163
+ set cameraPosition(p: Vec) {
164
+ this._cameraPosition = p;
165
+ }
166
+
167
+ set lights(l: Light[]) {
168
+ if (!l.length) {
169
+ throw new Error("BasicPBRLightShader: Invalid light array set.");
170
+ }
171
+ this._lights = l;
172
+ this._lightTypes = [];
173
+ this._lightPositions = [];
174
+ this._lightDirections = [];
175
+ this._lightColors = [];
176
+ this._lightIntensities = [];
177
+ this._lights.forEach(light => {
178
+ this._lightTypes.push(light.type);
179
+ this._lightPositions.push(new Vec(light.position));
180
+ this._lightDirections.push(new Vec(light.direction));
181
+ this._lightColors.push(new Vec(light.color));
182
+ this._lightIntensities.push(light.intensity);
183
+ });
184
+ this._program = getShaderProgramForLights.apply(this, [this.renderer, this._lights.length]);
185
+ }
186
+
187
+ get lights() {
188
+ return this._lights;
189
+ }
190
+
191
+ updateLight(light: Light, index: number) {
192
+ if (index >= this._lights.length) {
193
+ throw new Error("BasicPBRLightShader: Invalid light index updating light data");
194
+ }
195
+ this._lightTypes[index] = light.type;
196
+ this._lightPositions[index] = new Vec(light.position);
197
+ this._lightDirections[index] = new Vec(light.direction);
198
+ this._lightColors[index] = new Vec(light.color);
199
+ this._lightIntensities[index] = light.intensity;
200
+ }
201
+
202
+ updateLights(lights: Light[]) {
203
+ if (this._lights.length !== lights.length) {
204
+ this.lights = lights;
205
+ }
206
+ else {
207
+ lights.forEach((l,i) => this.updateLight(l,i));
208
+ }
209
+ }
210
+
211
+ setup(
212
+ plistRenderer: PolyListRenderer,
213
+ materialRenderer: MaterialRenderer,
214
+ modelMatrix: Mat4,
215
+ viewMatrix: Mat4,
216
+ projectionMatrix: Mat4
217
+ ) {
218
+ if (!this._program) {
219
+ throw new Error("BasicPBRLightShader: lights array not specified");
220
+ }
221
+
222
+ const material = materialRenderer.material;
223
+ const rend = this.renderer as WebGLRenderer;
224
+ rend.state.shaderProgram = this._program;
225
+
226
+ const normMatrix = Mat4.GetNormalMatrix(modelMatrix);
227
+ this._program.bindMatrix('uNormMatrix', normMatrix);
228
+ this._program.bindMatrix('uWorld', modelMatrix);
229
+ this._program.bindMatrix('uView', viewMatrix);
230
+ this._program.bindMatrix('uProj', projectionMatrix);
231
+
232
+ if (!this._cameraPosition) {
233
+ console.warn("Serious performance warning: camera position not set in BasicPBRLightShader. Extracting the camera position from the view matrix involves inverting the matrix.");
234
+ this._program.bindVector('uCameraPos',Mat4.GetPosition(Mat4.GetInverted(viewMatrix)));
235
+ }
236
+ else {
237
+ this._program.bindVector('uCameraPos',this._cameraPosition);
238
+ }
239
+
240
+ (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'albedoTexture', 'uAlbedoTexture', 0);
241
+ (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'normalTexture', 'uNormalTexture', 1, normalTexture(this.renderer));
242
+ (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'metalnessTexture', 'uMetallicTexture', 2);
243
+ (materialRenderer as WebGLMaterialRenderer).bindTexture(this._program, 'roughnessTexture', 'uRoughnessTexture', 3);
244
+
245
+ (materialRenderer as WebGLMaterialRenderer).bindColor(this._program, 'albedo', 'uAlbedo');
246
+ (materialRenderer as WebGLMaterialRenderer).bindValue(this._program, 'metalness', 'uMetallic');
247
+ (materialRenderer as WebGLMaterialRenderer).bindValue(this._program, 'roughness', 'uRoughness');
248
+
249
+
250
+ this._program.bindVector("uAlbedoScale", material.albedoScale);
251
+ this._program.bindVector("uNormalScale", material.normalScale);
252
+ this._program.bindVector("uMetallicScale", material.metalnessScale);
253
+ this._program.bindVector("uRoughnessScale", material.roughnessScale);
254
+
255
+ this._lights.forEach((light,i) => {
256
+ this._program?.uniform1i(`uLightTypes[${i}]`, this._lightTypes[i]);
257
+ this._program?.bindVector(`uLightPositions[${i}]`, this._lightPositions[i]);
258
+ this._program?.bindVector(`uLightDirections[${i}]`, this._lightDirections[i]);
259
+ this._program?.bindVector(`uLightColors[${i}]`, this._lightColors[i].rgb);
260
+ this._program?.uniform1f(`uLightIntensities[${i}]`, this._lightIntensities[i]);
261
+ });
262
+
263
+ this._program.bindAttribs(plistRenderer as WebGLPolyListRenderer, {
264
+ position: "inPosition",
265
+ normal: "inNormal",
266
+ tex0: "inTexCoord",
267
+ tangent: "inTangent"
268
+ });
269
+ }
270
+
271
+ destroy() {
272
+ if (this._program) {
273
+ ShaderProgram.Delete(this._program);
274
+ this._program = null;
275
+ }
276
+ }
277
277
  }