bg2e-js 2.1.2 → 2.2.0

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 (147) hide show
  1. package/dist/bg2e-js.js +7031 -6989
  2. package/dist/bg2e-js.js.map +1 -1
  3. package/package.json +20 -2
  4. package/src/app/AppController.ts +39 -0
  5. package/src/app/Bg2KeyboardEvent.ts +54 -0
  6. package/src/app/Bg2MouseEvent.ts +82 -0
  7. package/src/app/Bg2TouchEvent.ts +18 -0
  8. package/src/app/Canvas.ts +108 -0
  9. package/src/app/EventBase.ts +10 -0
  10. package/src/app/MainLoop.ts +273 -0
  11. package/src/app/index.ts +25 -0
  12. package/src/base/Color.ts +134 -0
  13. package/src/base/Environment.ts +183 -0
  14. package/src/base/Light.ts +192 -0
  15. package/src/base/Material.ts +616 -0
  16. package/src/base/PolyList.ts +365 -0
  17. package/src/base/Texture.ts +620 -0
  18. package/src/base/index.ts +81 -0
  19. package/src/db/Bg2LoaderPlugin.ts +129 -0
  20. package/src/db/DBPluginApi.ts +48 -0
  21. package/src/db/Loader.ts +116 -0
  22. package/src/db/LoaderPlugin.ts +34 -0
  23. package/src/db/MtlParser.ts +7 -0
  24. package/src/db/ObjLoaderPlugin.ts +55 -0
  25. package/src/db/ObjParser.ts +252 -0
  26. package/src/db/ObjWriterPlugin.ts +19 -0
  27. package/src/db/VitscnjLoaderPlugin.ts +100 -0
  28. package/src/db/Writer.ts +52 -0
  29. package/src/db/WriterPlugin.ts +22 -0
  30. package/src/db/index.ts +44 -0
  31. package/src/debug/DebugRenderer.ts +173 -0
  32. package/src/debug/WebGLTextureViewer.ts +75 -0
  33. package/src/debug/index.ts +7 -0
  34. package/src/index.html +11 -0
  35. package/src/index.ts +33 -0
  36. package/src/manipulation/SelectionBuffer.ts +82 -0
  37. package/src/manipulation/SelectionHighlight.ts +85 -0
  38. package/src/manipulation/SelectionIdAssignVisitor.ts +97 -0
  39. package/src/manipulation/SelectionManager.ts +166 -0
  40. package/src/manipulation/SelectionMode.ts +6 -0
  41. package/src/math/Mat3.ts +259 -0
  42. package/src/math/Mat4.ts +706 -0
  43. package/src/math/MatrixStrategy.ts +25 -0
  44. package/src/math/Quat.ts +65 -0
  45. package/src/math/Vec.ts +753 -0
  46. package/src/math/constants.ts +47 -0
  47. package/src/math/functions.ts +103 -0
  48. package/src/math/index.ts +74 -0
  49. package/src/phsics/joint.ts +137 -0
  50. package/src/primitives/arrow.ts +58 -0
  51. package/src/primitives/cone.ts +138 -0
  52. package/src/primitives/cube.ts +60 -0
  53. package/src/primitives/cylinder.ts +216 -0
  54. package/src/primitives/index.ts +13 -0
  55. package/src/primitives/plane.ts +31 -0
  56. package/src/primitives/sphere.ts +809 -0
  57. package/src/render/BRDFIntegrationMap.ts +4 -0
  58. package/src/render/Environment.ts +136 -0
  59. package/src/render/FrameBuffer.ts +35 -0
  60. package/src/render/MaterialRenderer.ts +34 -0
  61. package/src/render/Pipeline.ts +109 -0
  62. package/src/render/PolyListRenderer.ts +47 -0
  63. package/src/render/RenderBuffer.ts +197 -0
  64. package/src/render/RenderQueue.ts +199 -0
  65. package/src/render/RenderState.ts +116 -0
  66. package/src/render/Renderer.ts +248 -0
  67. package/src/render/SceneAppController.ts +238 -0
  68. package/src/render/SceneRenderer.ts +373 -0
  69. package/src/render/Shader.ts +32 -0
  70. package/src/render/ShadowRenderer.ts +176 -0
  71. package/src/render/SkyCube.ts +106 -0
  72. package/src/render/SkySphere.ts +118 -0
  73. package/src/render/TextureMergerRenderer.ts +70 -0
  74. package/src/render/TextureRenderer.ts +34 -0
  75. package/src/render/index.ts +67 -0
  76. package/src/render/webgl/FrameBuffer.ts +10 -0
  77. package/src/render/webgl/MaterialRenderer.ts +113 -0
  78. package/src/render/webgl/Pipeline.ts +89 -0
  79. package/src/render/webgl/PolyListRenderer.ts +260 -0
  80. package/src/render/webgl/RenderBuffer.ts +227 -0
  81. package/src/render/webgl/Renderer.ts +262 -0
  82. package/src/render/webgl/SceneRenderer.ts +68 -0
  83. package/src/render/webgl/ShaderProgram.ts +424 -0
  84. package/src/render/webgl/ShadowRenderer.ts +6 -0
  85. package/src/render/webgl/SkyCube.ts +16 -0
  86. package/src/render/webgl/SkySphere.ts +16 -0
  87. package/src/render/webgl/State.ts +152 -0
  88. package/src/render/webgl/TextureRenderer.ts +167 -0
  89. package/src/render/webgl/VertexBuffer.ts +137 -0
  90. package/src/render/webgl/index.ts +35 -0
  91. package/src/scene/Camera.ts +458 -0
  92. package/src/scene/Chain.ts +44 -0
  93. package/src/scene/ChainJoint.ts +58 -0
  94. package/src/scene/Component.ts +173 -0
  95. package/src/scene/ComponentMap.ts +107 -0
  96. package/src/scene/Drawable.ts +154 -0
  97. package/src/scene/EnvironmentComponent.ts +142 -0
  98. package/src/scene/FindNodeVisitor.ts +60 -0
  99. package/src/scene/LightComponent.ts +155 -0
  100. package/src/scene/MatrixState.ts +46 -0
  101. package/src/scene/Node.ts +314 -0
  102. package/src/scene/NodeVisitor.ts +15 -0
  103. package/src/scene/OrbitCameraController.ts +450 -0
  104. package/src/scene/SmoothOrbitCameraController.ts +99 -0
  105. package/src/scene/Transform.ts +73 -0
  106. package/src/scene/index.ts +57 -0
  107. package/src/shaders/BasicDiffuseColorShader.ts +111 -0
  108. package/src/shaders/BasicPBRLightShader.ts +277 -0
  109. package/src/shaders/DebugRenderShader.ts +98 -0
  110. package/src/shaders/DepthRenderShader.ts +91 -0
  111. package/src/shaders/IrradianceMapCubeShader.ts +116 -0
  112. package/src/shaders/PBRLightIBLShader.ts +487 -0
  113. package/src/shaders/PickSelectionShader.ts +101 -0
  114. package/src/shaders/PresentDebugFramebufferShader.ts +118 -0
  115. package/src/shaders/PresentTextureShader.ts +99 -0
  116. package/src/shaders/SelectionHighlightShader.ts +127 -0
  117. package/src/shaders/ShaderFunction.ts +318 -0
  118. package/src/shaders/SkyCubeShader.ts +94 -0
  119. package/src/shaders/SkySphereShader.ts +102 -0
  120. package/src/shaders/SpecularMapCubeShader.ts +165 -0
  121. package/src/shaders/TextureMergerShader.ts +171 -0
  122. package/src/shaders/index.ts +37 -0
  123. package/src/shaders/webgl/color_correction.glsl +47 -0
  124. package/src/shaders/webgl/constants.glsl +6 -0
  125. package/src/shaders/webgl/index.ts +70 -0
  126. package/src/shaders/webgl/normal_map.glsl +9 -0
  127. package/src/shaders/webgl/pbr.glsl +173 -0
  128. package/src/shaders/webgl/uniforms.glsl +91 -0
  129. package/src/shaders/webgl_shader_lib.ts +213 -0
  130. package/src/tools/BinaryResourceProvider.ts +14 -0
  131. package/src/tools/ImageResourceProvider.ts +66 -0
  132. package/src/tools/MaterialModifier.ts +276 -0
  133. package/src/tools/Resource.ts +203 -0
  134. package/src/tools/ResourceProvider.ts +69 -0
  135. package/src/tools/TextResourceProvider.ts +24 -0
  136. package/src/tools/TextureCache.ts +52 -0
  137. package/src/tools/TextureResourceDatabase.ts +100 -0
  138. package/src/tools/UserAgent.ts +362 -0
  139. package/src/tools/VideoResourceProvider.ts +50 -0
  140. package/src/tools/WriteStrategy.ts +22 -0
  141. package/src/tools/base64.ts +11 -0
  142. package/src/tools/crypto.ts +19 -0
  143. package/src/tools/endiantess.ts +13 -0
  144. package/src/tools/image.ts +18 -0
  145. package/src/tools/index.ts +41 -0
  146. package/src/tools/processType.ts +38 -0
  147. package/src/vite-env.d.ts +12 -0
@@ -0,0 +1,487 @@
1
+ import Shader from "../render/Shader";
2
+ import ShaderFunction from "./ShaderFunction";
3
+ import ShaderProgram from "../render/webgl/ShaderProgram";
4
+ import Mat4 from "../math/Mat4";
5
+ import Vec from "../math/Vec";
6
+ import Light, {
7
+ LightType
8
+ } from "../base/Light";
9
+ import {
10
+ createNormalTexture,
11
+ normalTexture,
12
+ createBRDFIntegrationTexture
13
+ } from "../tools/TextureResourceDatabase";
14
+ import Environment from "../render/Environment";
15
+
16
+ import {
17
+ getColorCorrectionFunctions,
18
+ getNormalMapFunctions,
19
+ getPBRFunctions,
20
+ getUniformsFunctions,
21
+ replaceConstants
22
+ } from "./webgl";
23
+ import Renderer from "../render/Renderer";
24
+ import WebGLRenderer from "../render/webgl/Renderer";
25
+ import PolyListRenderer from "../render/webgl/PolyListRenderer";
26
+ import MaterialRenderer from "../render/webgl/MaterialRenderer";
27
+ import WebGLTextureRenderer from "../render/webgl/TextureRenderer";
28
+ import Texture from "../base/Texture";
29
+
30
+ function getShaderProgramForLights(this: PBRLightIBLShader, renderer: Renderer, numLights: number): ShaderProgram {
31
+ const r = renderer as WebGLRenderer;
32
+
33
+ if (this._programs[numLights]) {
34
+ return this._programs[numLights];
35
+ }
36
+
37
+ const vshader = ShaderFunction.GetShaderCode(`precision mediump float;
38
+ attribute vec3 inPosition;
39
+ attribute vec3 inNormal;
40
+ attribute vec2 inTexCoord;
41
+ attribute vec2 inTexCoord2;
42
+ attribute vec3 inTangent;
43
+
44
+ uniform mat4 uWorld;
45
+ uniform mat4 uView;
46
+ uniform mat4 uViewInverse;
47
+ uniform mat4 uProj;
48
+ uniform mat3 uNormMatrix;
49
+
50
+ varying vec3 fragPos;
51
+ varying vec3 fragViewPos;
52
+ varying vec3 fragNorm;
53
+ varying vec2 fragTexCoord;
54
+ varying vec2 fragTexCoord2;
55
+ varying vec3 fragLightPositions[${numLights}];
56
+
57
+ varying mat3 fragTBN;
58
+
59
+ uniform vec3 uLightPositions[${numLights}];
60
+ uniform mat4 uLightTransforms[${numLights}];
61
+
62
+ // Used to calculate shadow from depth texture
63
+ uniform mat4 uLightPovMvp;
64
+ varying vec4 fragPositionFromLightPov;
65
+
66
+ `,
67
+ [
68
+ new ShaderFunction('void','main','',`{
69
+
70
+
71
+ for (int i = 0; i < ${numLights}; ++i)
72
+ {
73
+ fragLightPositions[i] = (uLightTransforms[i] * vec4(uLightPositions[i], 1.0)).xyz;
74
+ }
75
+
76
+ fragPos = (uWorld * vec4(inPosition, 1.0)).xyz;
77
+
78
+ fragPositionFromLightPov = uLightPovMvp * vec4(fragPos, 1.0);
79
+
80
+ fragViewPos = (uViewInverse * vec4(0.0,0.0,0.0,1.0)).xyz;
81
+ fragTexCoord = inTexCoord;
82
+ fragTexCoord2 = inTexCoord2;
83
+ fragNorm = normalize(uNormMatrix * inNormal);
84
+ gl_Position = uProj * uView * uWorld * vec4(inPosition,1.0);
85
+ fragTBN = TBNMatrix(uWorld, inNormal, inTangent);
86
+ }`, [
87
+ ...getNormalMapFunctions()
88
+ ])
89
+ ]);
90
+
91
+ const deps = [
92
+ ...getColorCorrectionFunctions(),
93
+ ...getNormalMapFunctions(),
94
+ ...getUniformsFunctions(),
95
+ ...getPBRFunctions()
96
+ ];
97
+
98
+ const fshader = replaceConstants(ShaderFunction.GetShaderCode(`precision mediump float;
99
+ varying vec3 fragPos;
100
+ varying vec3 fragNorm;
101
+ varying vec2 fragTexCoord;
102
+ varying vec2 fragTexCoord2;
103
+ varying vec3 fragViewPos;
104
+ varying vec3 fragLightPositions[${numLights}];
105
+
106
+ uniform vec3 uCameraPos;
107
+
108
+ uniform sampler2D uAlbedoTexture;
109
+ uniform sampler2D uNormalTexture;
110
+ uniform sampler2D uMetallicRoughnessHeightAOTexture;
111
+
112
+ uniform vec4 uAlbedo;
113
+ uniform float uMetallic;
114
+ uniform float uRoughness;
115
+ uniform float uLightEmission;
116
+
117
+ uniform vec4 uFresnelTint;
118
+ uniform vec4 uSheenColor;
119
+ uniform float uSheenIntensity;
120
+
121
+ uniform vec2 uAlbedoScale;
122
+ uniform vec2 uNormalScale;
123
+ uniform vec2 uMetallicScale;
124
+ uniform vec2 uRoughnessScale;
125
+ uniform vec2 uLightEmissionScale;
126
+
127
+ uniform float uAlphaTresshold;
128
+
129
+ uniform int uLightTypes[${numLights}];
130
+ uniform vec3 uLightDirections[${numLights}];
131
+ uniform vec3 uLightColors[${numLights}];
132
+ uniform float uLightIntensities[${numLights}];
133
+
134
+
135
+ uniform samplerCube uEnvMap;
136
+ uniform samplerCube uSpecularMap;
137
+ uniform samplerCube uIrradianceMap;
138
+ uniform sampler2D uBRDFIntegrationMap;
139
+ uniform float uAmbientIntensity;
140
+
141
+ uniform sampler2D uShadowMap;
142
+ uniform float uShadowBias;
143
+ varying vec4 fragPositionFromLightPov;
144
+ uniform float uShadowStrength;
145
+
146
+ uniform int uAlbedoMap;
147
+ uniform int uNormalMap;
148
+ uniform int uAOMap;
149
+ uniform int uMetallicMap;
150
+ uniform int uRoughnessMap;
151
+ uniform int uLightemissionMap;
152
+
153
+ varying mat3 fragTBN;
154
+
155
+ uniform float uBrightness;
156
+ uniform float uContrast;
157
+ `,
158
+ [
159
+ new ShaderFunction('void','main','',`{
160
+ float gamma = 2.2;
161
+
162
+ PBRMaterialData mat;
163
+ mat.albedo = uAlbedo;
164
+ mat.albedoScale = uAlbedoScale;
165
+ mat.normalScale = uNormalScale;
166
+ mat.metalnessScale = uMetallicScale;
167
+ mat.roughnessScale = uRoughnessScale;
168
+ mat.lightEmissionScale = uLightEmissionScale;
169
+ mat.metalness = uMetallic;
170
+ mat.roughness = uRoughness;
171
+ mat.lightEmission = uLightEmission;
172
+ mat.albedoUVSet = uAlbedoMap;
173
+ mat.normalUVSet = uNormalMap;
174
+ mat.metalnessUVSet = uMetallicMap;
175
+ mat.roughnessUVSet = uRoughnessMap;
176
+ mat.lightEmissionUVSet = uLightemissionMap;
177
+ mat.aoUVSet = uAOMap;
178
+ mat.fresnelTint = uFresnelTint;
179
+ mat.sheenColor = uSheenColor;
180
+ mat.sheenIntensity = uSheenIntensity;
181
+
182
+ vec4 albedo = sampleAlbedo(uAlbedoTexture, fragTexCoord, fragTexCoord2, mat, gamma);
183
+ float metallic = sampleMetallic(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
184
+ float roughness = sampleRoughness(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
185
+
186
+ // TODO: Light emission
187
+ float ambientOcclussion = sampleAmbientOcclussion(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
188
+
189
+ vec3 normal = sampleNormal(uNormalTexture, fragTexCoord, fragTexCoord2, mat, fragTBN);
190
+ if (!gl_FrontFacing) {
191
+ normal = -normal;
192
+ }
193
+
194
+ vec3 viewDir = normalize(fragViewPos - fragPos);
195
+ vec3 F0 = calcF0(albedo.rgb, mat);
196
+
197
+ if (albedo.a < uAlphaTresshold) {
198
+ discard;
199
+ }
200
+ else {
201
+ vec3 Lo = vec3(0.0);
202
+ for (int i = 0; i < ${numLights}; ++i) {
203
+ Light light;
204
+ light.type = uLightTypes[i];
205
+ light.color = vec4(uLightColors[i], 1.0);
206
+ light.intensity = uLightIntensities[i];
207
+ light.direction = uLightDirections[i];
208
+ light.position = fragLightPositions[i];
209
+
210
+ Lo += calcRadiance(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo.rgb, mat.sheenIntensity, mat.sheenColor.rgb, ambientOcclussion);
211
+
212
+ }
213
+
214
+ vec3 shadowColor = getShadowColor(fragPositionFromLightPov, uShadowMap, uShadowBias, uShadowStrength);
215
+
216
+ Lo = Lo * shadowColor;
217
+
218
+ float ambientIntensity = 2.0;
219
+ vec3 ambient = calcAmbientLight(
220
+ viewDir, normal, F0,
221
+ albedo.rgb, metallic, roughness,
222
+ uIrradianceMap, uSpecularMap, uEnvMap,
223
+ uBRDFIntegrationMap, ambientOcclussion,
224
+ mat.sheenIntensity, mat.sheenColor.rgb,
225
+ shadowColor,
226
+ ambientIntensity
227
+ );
228
+
229
+ vec3 color = ambient + Lo;
230
+
231
+ color = color / (color + vec3(1.0));
232
+ gl_FragColor = lineal2SRGB(vec4(color, 1.0), gamma);
233
+ gl_FragColor = brightnessContrast(vec4(color, albedo.a), uBrightness, uContrast);
234
+ }
235
+ }`, deps)
236
+ ]));
237
+
238
+ this._programs[numLights] = ShaderProgram.Create(r.gl, "PBRLightIBL", vshader, fshader);
239
+ return this._programs[numLights];
240
+ }
241
+
242
+ export default class PBRLightIBLShader extends Shader {
243
+ protected _programs: { [key: number]: ShaderProgram };
244
+ protected _lights: Light[];
245
+ protected _lightTransforms: Mat4[];
246
+ protected _lightTypes: LightType[] = [];
247
+ protected _lightPositions: Vec[] = [];
248
+ protected _lightDirections: Vec[] = [];
249
+ protected _lightColors: Vec[] = [];
250
+ protected _lightIntensities: number[] = [];
251
+ protected _brightness: number;
252
+ protected _contrast: number;
253
+
254
+ protected _cameraPosition: Vec;
255
+ protected _brdfIntegrationTexture: Texture | null = null;
256
+ protected _environment: Environment | null = null;
257
+ protected _program: ShaderProgram | null = null;
258
+
259
+ constructor(renderer: Renderer) {
260
+ super(renderer);
261
+
262
+ this._lights = [];
263
+ this._lightTransforms = [];
264
+
265
+ this._brightness = 0.2;
266
+ this._contrast = 1.1;
267
+
268
+ this._cameraPosition = new Vec(0,0,0);
269
+
270
+ if (renderer.typeId !== "WebGL") {
271
+ throw Error("PresentTextureShader is only compatible with WebGL renderer");
272
+ }
273
+
274
+ this._programs = {};
275
+ }
276
+
277
+ async load() {
278
+ await createNormalTexture(this.renderer);
279
+
280
+ this._brdfIntegrationTexture = await createBRDFIntegrationTexture(this.renderer);
281
+ }
282
+
283
+ get brightness() {
284
+ return this._brightness;
285
+ }
286
+
287
+ get contrast() {
288
+ return this._contrast;
289
+ }
290
+
291
+ set brightness(b) {
292
+ this._brightness = b;
293
+ }
294
+
295
+ set contrast(c) {
296
+ this._contrast = c;
297
+ }
298
+
299
+ set cameraPosition(p: Vec) {
300
+ this._cameraPosition = p;
301
+ }
302
+
303
+ set environment(env: Environment | null) {
304
+ this._environment = env;
305
+ }
306
+
307
+ get environmentMap() {
308
+ return this._environment ? this._environment.environmentMap : null;
309
+ }
310
+
311
+ get specularMap() {
312
+ return this._environment ? this._environment.specularMap : null;
313
+ }
314
+
315
+ get irradianceMap() {
316
+ return this._environment ? this._environment.irradianceMap : null;
317
+ }
318
+
319
+ set lights(l) {
320
+ if (!l.length) {
321
+ throw new Error("BasicPBRLightShader: Invalid light array set.");
322
+ }
323
+ this._lights = l;
324
+ this._lightTypes = [];
325
+ this._lightPositions = [];
326
+ this._lightDirections = [];
327
+ this._lightColors = [];
328
+ this._lightIntensities = [];
329
+ this._lights.forEach((light,i) => {
330
+ this._lightTypes.push(light.type);
331
+ const trx = this._lightTransforms[i];
332
+ const rot = trx && Mat4.GetRotation(trx);
333
+ const position = new Vec(light.position);
334
+ const direction = new Vec(light.direction);
335
+ this._lightPositions.push(trx ? trx.multVector(position).xyz : position);
336
+ this._lightDirections.push(rot ? rot.multVector(direction).xyz.normalize() : direction);
337
+ this._lightColors.push(new Vec(light.color));
338
+ this._lightIntensities.push(light.intensity);
339
+ });
340
+ this._program = getShaderProgramForLights.apply(this, [this.renderer, this._lights.length]);
341
+ }
342
+
343
+ get lights() {
344
+ return this._lights;
345
+ }
346
+
347
+ set lightTransforms(lt) {
348
+ this._lightTransforms = lt;
349
+ }
350
+
351
+ get lightTransforms() {
352
+ return this._lightTransforms;
353
+ }
354
+
355
+ updateLight(light: Light, index: number) {
356
+ if (index >= this._lights.length) {
357
+ throw new Error("BasicPBRLightShader: Invalid light index updating light data");
358
+ }
359
+ this._lightTypes[index] = light.type;
360
+ this._lightPositions[index] = new Vec(light.position);
361
+ this._lightDirections[index] = new Vec(light.direction);
362
+ this._lightColors[index] = new Vec(light.color);
363
+ this._lightIntensities[index] = light.intensity;
364
+ }
365
+
366
+ updateLights(lights: Light[]) {
367
+ if (this._lights.length !== lights.length) {
368
+ this.lights = lights;
369
+ }
370
+ else {
371
+ lights.forEach((l,i) => this.updateLight(l,i));
372
+ }
373
+ }
374
+
375
+ setup(
376
+ plistRenderer: PolyListRenderer,
377
+ materialRenderer: MaterialRenderer,
378
+ modelMatrix: Mat4,
379
+ viewMatrix: Mat4,
380
+ projectionMatrix: Mat4
381
+ ) {
382
+ const rend = this.renderer as WebGLRenderer;
383
+ if (!this._program) {
384
+ throw new Error("BasicPBRLightShader: lights array not specified");
385
+ }
386
+
387
+ const material = materialRenderer.material;
388
+ materialRenderer.mergeTextures();
389
+ rend.state.shaderProgram = this._program;
390
+
391
+ const normMatrix = Mat4.GetNormalMatrix(modelMatrix);
392
+ this._program.bindMatrix('uNormMatrix', normMatrix);
393
+ this._program.bindMatrix('uWorld', modelMatrix);
394
+ this._program.bindMatrix('uView', viewMatrix);
395
+ this._program.bindMatrix('uViewInverse', Mat4.GetInverted(viewMatrix));
396
+ this._program.bindMatrix('uProj', projectionMatrix);
397
+
398
+ if (!this._cameraPosition) {
399
+ console.warn("Serious performance warning: camera position not set in BasicPBRLightShader. Extracting the camera position from the view matrix involves inverting the matrix.");
400
+ this._program.bindVector('uCameraPos',Mat4.GetPosition(Mat4.GetInverted(viewMatrix)));
401
+ }
402
+ else {
403
+ this._program.bindVector('uCameraPos',this._cameraPosition);
404
+ }
405
+
406
+ materialRenderer.bindTexture(this._program, 'albedoTexture', 'uAlbedoTexture', 0);
407
+ materialRenderer.bindTexture(this._program, 'normalTexture', 'uNormalTexture', 1, normalTexture(this.renderer));
408
+ materialRenderer.bindMetalnessRoughnessHeightAOTexture(this._program, 'uMetallicRoughnessHeightAOTexture', 2);
409
+ this._program.uniform1f('uAlphaTresshold', material.alphaCutoff);
410
+
411
+ materialRenderer.bindColor(this._program, 'albedo', 'uAlbedo');
412
+ materialRenderer.bindValue(this._program, 'metalness', 'uMetallic');
413
+ materialRenderer.bindValue(this._program, 'roughness', 'uRoughness');
414
+ materialRenderer.bindValue(this._program, 'lightEmission', 'uLightEmission', 0);
415
+
416
+ this._program.uniform1i('uAlbedoMap', material.albedoUV);
417
+ this._program.uniform1i('uNormalMap', material.normalUV);
418
+ this._program.uniform1i('uAOMap', material.ambientOcclussionUV);
419
+ this._program.uniform1i('uMetallicMap', material.metalnessChannel);
420
+ this._program.uniform1i('uRoughnessMap', material.roughnessChannel);
421
+ this._program.uniform1i('uLightemissionMap', material.lightEmissionChannel);
422
+
423
+ this._program.bindVector("uAlbedoScale", material.albedoScale);
424
+ this._program.bindVector("uNormalScale", material.normalScale);
425
+ this._program.bindVector("uMetallicScale", material.metalnessScale);
426
+ this._program.bindVector("uRoughnessScale", material.roughnessScale);
427
+ this._program.bindVector("uLightEmissionScale", material.lightEmissionScale);
428
+ this._program.bindVector("uFresnelTint", material.fresnelTint);
429
+ this._program.bindVector("uSheenColor", material.sheenColor);
430
+ this._program.uniform1f("uSheenIntensity", material.sheenIntensity);
431
+
432
+ if (!this.irradianceMap || !this.specularMap || !this.environmentMap || !this._brdfIntegrationTexture) {
433
+ throw new Error("PBRLightIBLShader: Environment maps not set.");
434
+ }
435
+ const irMap = this.renderer.factory.texture(this.irradianceMap) as WebGLTextureRenderer;
436
+ const specMap = this.renderer.factory.texture(this.specularMap) as WebGLTextureRenderer;
437
+ const envMap = this.renderer.factory.texture(this.environmentMap) as WebGLTextureRenderer;
438
+ const brdfLut = this.renderer.factory.texture(this._brdfIntegrationTexture) as WebGLTextureRenderer;
439
+
440
+ this._program.bindTexture("uIrradianceMap", irMap, 3);
441
+ this._program.bindTexture("uSpecularMap", specMap, 4);
442
+ this._program.bindTexture("uEnvMap", envMap, 5);
443
+ this._program.bindTexture("uBRDFIntegrationMap", brdfLut, 6);
444
+
445
+ this._program.uniform1f("uBrightness", this._brightness);
446
+ this._program.uniform1f("uContrast", this._contrast);
447
+
448
+ // TODO: Get the ambient intensity from environment
449
+ this._program.uniform1f("uAmbientIntensity", 1.0);
450
+
451
+ let shadowLight: Light | null = this._lights.find(light => light.depthTexture) || null;
452
+ this._lights.forEach((light,i) => {
453
+ if (light.depthTexture) {
454
+ shadowLight = light;
455
+ }
456
+ this._program?.uniform1i(`uLightTypes[${i}]`, this._lightTypes[i]);
457
+ this._program?.bindVector(`uLightPositions[${i}]`, this._lightPositions[i]);
458
+ this._program?.bindVector(`uLightDirections[${i}]`, this._lightDirections[i]);
459
+ this._program?.bindVector(`uLightColors[${i}]`, this._lightColors[i].rgb);
460
+ this._program?.uniform1f(`uLightIntensities[${i}]`, this._lightIntensities[i]);
461
+ this._program?.bindMatrix(`uLightTransforms[${i}]`, this._lightTransforms[i]);
462
+ });
463
+
464
+ if (shadowLight?.depthTexture) {
465
+ const depthTex = this.renderer.factory.texture(shadowLight.depthTexture) as WebGLTextureRenderer;
466
+ this._program.bindTexture("uShadowMap", depthTex, 7);
467
+ const projectionViewMatrix = Mat4.Mult(shadowLight.projection, shadowLight.viewMatrix || Mat4.MakeIdentity());
468
+ this._program.bindMatrix("uLightPovMvp", projectionViewMatrix);
469
+ this._program.uniform1f("uShadowBias", shadowLight.shadowBias * 30);
470
+ this._program.uniform1f("uShadowStrength", shadowLight.shadowStrength);
471
+ }
472
+
473
+ this._program.bindAttribs(plistRenderer, {
474
+ position: "inPosition",
475
+ normal: "inNormal",
476
+ tex0: "inTexCoord",
477
+ tex1: "inTexCoord2",
478
+ tangent: "inTangent"
479
+ });
480
+ }
481
+
482
+ destroy() {
483
+ if (this._program) {
484
+ ShaderProgram.Delete(this._program);
485
+ }
486
+ }
487
+ }
@@ -0,0 +1,101 @@
1
+ import Shader from "../render/Shader";
2
+ import ShaderProgram from "../render/webgl/ShaderProgram";
3
+ import PolyListRenderer from '../render/PolyListRenderer';
4
+ import MaterialRenderer from '../render/MaterialRenderer';
5
+ import Mat4 from "../math/Mat4";
6
+ import Renderer from "../render/Renderer";
7
+ import WebGLRenderer from "../render/webgl/Renderer";
8
+ import WebGLPolyListRenderer from "../render/webgl/PolyListRenderer";
9
+
10
+ const g_code = {
11
+ webgl: {
12
+ vertex: `precision mediump float;
13
+
14
+ attribute vec3 vertPosition;
15
+
16
+ uniform mat4 mWorld;
17
+ uniform mat4 mView;
18
+ uniform mat4 mProj;
19
+
20
+ void main() {
21
+ gl_Position = mProj * mView * mWorld * vec4(vertPosition, 1.0);
22
+ }`,
23
+
24
+ fragment: `precision mediump float;
25
+
26
+ uniform vec4 uPickColor;
27
+ uniform bool uSelected;
28
+
29
+ void main() {
30
+ if (uSelected) {
31
+ gl_FragColor = vec4(uPickColor);
32
+ }
33
+ else {
34
+ discard;
35
+ }
36
+ }`
37
+ }
38
+ }
39
+
40
+ export default class PickSelectionShader extends Shader {
41
+ protected _program: ShaderProgram | null = null;
42
+ protected _forceDraw: boolean = false;
43
+
44
+ constructor(renderer: Renderer) {
45
+ super(renderer);
46
+
47
+ if (renderer.typeId !== "WebGL") {
48
+ throw Error("PickSelectionShader is only compatible with WebGL renderer");
49
+ }
50
+ }
51
+
52
+ async load() {
53
+ const { gl } = (this.renderer as WebGLRenderer);
54
+
55
+ this._program = new ShaderProgram(gl, "PickSelectionShader");
56
+ this._program.attachVertexSource(g_code.webgl.vertex);
57
+ this._program.attachFragmentSource(g_code.webgl.fragment);
58
+ this._program.link();
59
+
60
+ this._forceDraw = true;
61
+ }
62
+
63
+ set forceDraw(d: boolean) {
64
+ this._forceDraw = d;
65
+ }
66
+
67
+ get forceDraw() {
68
+ return this._forceDraw;
69
+ }
70
+
71
+ setup(
72
+ plistRenderer: PolyListRenderer,
73
+ materialRenderer: MaterialRenderer,
74
+ modelMatrix: Mat4,
75
+ viewMatrix: Mat4,
76
+ projectionMatrix: Mat4
77
+ ) {
78
+ if (!this._program) {
79
+ throw new Error("PickSelectionShader: shader program is not loaded");
80
+ }
81
+
82
+ const rend = this.renderer as WebGLRenderer;
83
+ rend.state.shaderProgram = this._program;
84
+ this._program.uniformMatrix4fv('mWorld', false, modelMatrix);
85
+ this._program.uniformMatrix4fv('mView', false, viewMatrix);
86
+ this._program.uniformMatrix4fv('mProj', false, projectionMatrix);
87
+ this._program.uniform1i('uSelected', (this._forceDraw || plistRenderer.polyList.isSelected) ? 1 : 0);
88
+
89
+ const { polyList } = plistRenderer;
90
+ this._program.uniform4fv('uPickColor', polyList.colorCode);
91
+
92
+ this._program.positionAttribPointer((plistRenderer as WebGLPolyListRenderer).positionAttribParams("vertPosition"));
93
+ }
94
+
95
+ destroy() {
96
+ if (this._program) {
97
+ ShaderProgram.Delete(this._program);
98
+ this._program = null;
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,118 @@
1
+
2
+ import { TextureTargetName } from '../base/Texture';
3
+ import Shader from '../render/Shader';
4
+ import ShaderProgram from '../render/webgl/ShaderProgram';
5
+ import ShaderFunction from './ShaderFunction';
6
+ import Vec from '../math/Vec';
7
+ import PolyListRenderer from '../render/PolyListRenderer';
8
+ import MaterialRenderer from '../render/MaterialRenderer';
9
+ import Mat4 from "../math/Mat4";
10
+ import Renderer from "../render/Renderer";
11
+ import WebGLRenderer from "../render/webgl/Renderer";
12
+ import WebGLTextureRenderer from "../render/webgl/TextureRenderer";
13
+ import WebGLPolyListRenderer from "../render/webgl/PolyListRenderer";
14
+
15
+ const g_code = {
16
+ webgl: {
17
+ vertex: `precision mediump float;
18
+
19
+ attribute vec3 position;
20
+ attribute vec2 texCoord;
21
+
22
+ varying vec2 fragTexCoord;
23
+
24
+ void main() {
25
+ fragTexCoord = texCoord;
26
+ gl_Position = vec4(position, 1.0);
27
+ }`,
28
+
29
+ fragment: ShaderFunction.GetShaderCode(`precision mediump float;
30
+ varying vec2 fragTexCoord;
31
+
32
+ uniform sampler2D uTexture;
33
+ `,
34
+ [
35
+ new ShaderFunction('void','main','',`{
36
+ vec3 color = texture2D(uTexture, fragTexCoord).rgb;
37
+ if (color.r!=0.0 || color.g!=0.0 || color.b!=0.0) {
38
+ gl_FragColor = vec4(color, 1.0);
39
+ }
40
+ else {
41
+ discard;
42
+ }
43
+ }`)
44
+ ])
45
+ }
46
+ }
47
+
48
+ export default class PresentDebugFramebufferShader extends Shader {
49
+ protected _program: ShaderProgram | null = null;
50
+ protected _borderWidth!: number;
51
+ protected _borderColor!: Vec;
52
+ protected _convMatrix!: number[];
53
+
54
+ constructor(renderer: Renderer) {
55
+ super(renderer);
56
+
57
+ if (renderer.typeId !== "WebGL") {
58
+ throw Error("PresentDebugFramebufferShader is only compatible with WebGL renderer");
59
+ }
60
+ }
61
+
62
+ async load() {
63
+ const { gl } = (this.renderer as WebGLRenderer);
64
+
65
+ this._program = new ShaderProgram(gl, "PresentDebugFramebufferShader");
66
+ this._program.attachVertexSource(g_code.webgl.vertex);
67
+ this._program.attachFragmentSource(g_code.webgl.fragment);
68
+ this._program.link();
69
+
70
+ this._borderWidth = 3;
71
+ this._borderColor = new Vec([0.0, 0.7, 1, 1.0]);
72
+ this._convMatrix = [
73
+ 0, 1, 0,
74
+ 1,-4, 1,
75
+ 0, 1, 0
76
+ ];
77
+ }
78
+
79
+ setup(
80
+ plistRenderer: PolyListRenderer,
81
+ materialRenderer: MaterialRenderer,
82
+ modelMatrix: Mat4,
83
+ viewMatrix: Mat4,
84
+ projectionMatrix: Mat4
85
+ ) {
86
+ if (!this._program) {
87
+ throw new Error("PresentDebugFramebufferShader: shader program is not loaded");
88
+ }
89
+
90
+ const rend = this.renderer as WebGLRenderer;
91
+ const { gl } = rend;
92
+
93
+ rend.state.shaderProgram = this._program;
94
+
95
+ gl.activeTexture(gl.TEXTURE0);
96
+ this._program.uniform1i('uTexture', 0);
97
+
98
+ const material = materialRenderer.material;
99
+ const webglTexture = (materialRenderer.getTextureRenderer('albedoTexture') as WebGLTextureRenderer)?.getApiObject();
100
+ if (webglTexture && material.albedoTexture) {
101
+ const target = TextureTargetName[material.albedoTexture.target];
102
+ gl.bindTexture((gl as any)[target], webglTexture);
103
+ }
104
+ else {
105
+ throw new Error("PresentDebugFramebufferShader: invalid material setup. The albedoTexture material attribute must to be a texture");
106
+ }
107
+
108
+ this._program.positionAttribPointer((plistRenderer as WebGLPolyListRenderer).positionAttribParams("position"));
109
+ this._program.texCoordAttribPointer((plistRenderer as WebGLPolyListRenderer).texCoord0AttribParams("texCoord"));
110
+ }
111
+
112
+ destroy() {
113
+ if (this._program) {
114
+ ShaderProgram.Delete(this._program);
115
+ this._program = null;
116
+ }
117
+ }
118
+ }