bg2e-js 2.3.12 → 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 +350 -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 -196
  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,446 +1,446 @@
1
- import {
2
- textureTypeLoader,
3
- colorTypeLoader,
4
- vectorTypeLoader,
5
- primitiveTypeLoader
6
- } from "../base/Material";
7
- import Material from "../base/Material";
8
- import Texture from "../base/Texture";
9
- import Vec from "../math/Vec";
10
- import Color from "../base/Color";
11
- import Drawable from "../scene/Drawable";
12
- import Canvas from "../app/Canvas";
13
- import TextureCache from "./TextureCache";
14
-
15
- const checkImageData = (texture: Texture | Color | number | null): Promise<void> => {
16
- return texture instanceof Texture ? texture.loadImageData() : Promise.resolve();
17
- }
18
-
19
- const loadTexture = async (canvas: Canvas, fileName: string, relativePath: string): Promise<Texture> => {
20
- const texture = new Texture(canvas);
21
- texture.fileName = relativePath + fileName;
22
- await texture.loadImageData();
23
- return texture;
24
- }
25
-
26
- export interface MaterialModifierData {
27
- class?: string;
28
- alphaCutoff?: number;
29
- castShadows?: boolean;
30
- albedo?: [number, number, number, number];
31
- albedoTexture?: string;
32
- albedoScale?: [number, number];
33
- normalTexture?: string;
34
- normalScale?: [number, number];
35
- metalness?: number;
36
- metalnessTexture?: string;
37
- metalnessChannel?: number;
38
- metalnessScale?: [number, number];
39
- roughness?: number;
40
- roughnessTexture?: string;
41
- roughnessChannel?: number;
42
- roughnessScale?: [number, number];
43
- fresnelTint?: [number, number, number, number];
44
- sheenIntensity?: number;
45
- sheenColor?: [number, number, number, number];
46
- lightEmission?: number;
47
- lightEmissionTexture?: string;
48
- lightEmissionChannel?: number;
49
- lightEmissionScale?: [number, number];
50
- isTransparent?: boolean;
51
- unlit?: boolean;
52
-
53
- // Deprecated properties
54
- diffuse?: string | [number, number, number, number];
55
- diffuseScale?: [number, number];
56
- normal?: string;
57
- fresnel?: [number, number, number, number];
58
- metallic?: string | number;
59
- metallicChannel?: number;
60
- metallicScale?: [number, number];
61
- }
62
-
63
- // TODO: Update all material properties (difuse -> albedo, etc)
64
- export default class MaterialModifier {
65
- private _alphaCutoff?: number;
66
- private _castShadows?: boolean;
67
- private _albedo?: [number, number, number, number];
68
- private _albedoTexture?: string;
69
- private _albedoScale?: [number, number];
70
- private _normalTexture?: string;
71
- private _normalScale?: [number, number];
72
- private _metalness?: number;
73
- private _metalnessTexture?: string;
74
- private _metalnessChannel?: number;
75
- private _metalnessScale?: [number, number];
76
- private _roughness?: number;
77
- private _roughnessTexture?: string;
78
- private _roughnessChannel?: number;
79
- private _roughnessScale?: [number, number];
80
- private _fresnelTint?: [number, number, number, number];
81
- private _sheenIntensity?: number;
82
- private _sheenColor?: [number, number, number, number];
83
- private _lightEmission?: [number, number, number, number];
84
- private _lightEmissionTexture?: string;
85
- private _lightEmissionChannel?: number;
86
- private _lightEmissionScale?: [number, number];
87
- private _isTransparent?: boolean;
88
- private _unlit?: boolean;
89
-
90
- private _texturesPath: string = "";
91
-
92
- constructor(jsonData: MaterialModifierData | null = null, texturesPath: string = "") {
93
- this._texturesPath = texturesPath;
94
- if (jsonData) {
95
- if (jsonData['class'] && jsonData['class'] !== "PBRMaterial") {
96
- console.warn(`Could not apply material modifier because of class "${jsonData['class']}". Check the valid material types in bg2 engine v2`);
97
- return;
98
- }
99
- if (jsonData.alphaCutoff !== undefined) {
100
- this.alphaCutoff = jsonData.alphaCutoff;
101
- }
102
-
103
- if (jsonData.castShadows !== undefined) {
104
- this.castShadows = jsonData.castShadows;
105
- }
106
-
107
- if (jsonData.diffuse !== undefined && typeof jsonData.diffuse === "string") {
108
- this.albedoTexture = jsonData.diffuse;
109
- }
110
- else if (jsonData.diffuse !== undefined && Array.isArray(jsonData.diffuse)) {
111
- this.albedo = jsonData.diffuse;
112
- }
113
-
114
- if (jsonData.albedoTexture !== undefined) {
115
- this.albedoTexture = jsonData.albedoTexture;
116
- }
117
-
118
- if (jsonData.albedoScale !== undefined) {
119
- this.albedoScale = jsonData.albedoScale;
120
- }
121
-
122
- if (jsonData.albedo !== undefined) {
123
- this.albedo = jsonData.albedo;
124
- }
125
-
126
- if (jsonData.diffuseScale !== undefined) {
127
- this.albedoScale = jsonData.diffuseScale;
128
- }
129
-
130
- if (jsonData.normal !== undefined) {
131
- this.normalTexture = jsonData.normal;
132
- }
133
-
134
- if (jsonData.normalTexture !== undefined) {
135
- this.normalTexture = jsonData.normalTexture;
136
- }
137
-
138
- if (jsonData.normalScale !== undefined) {
139
- this.normalScale = jsonData.normalScale;
140
- }
141
-
142
- if (jsonData.normalScale !== undefined) {
143
- this.normalScale = jsonData.normalScale;
144
- }
145
-
146
- if (jsonData.metallic !== undefined && typeof jsonData.metallic === "string") {
147
- this.metalnessTexture
148
- = jsonData.metallic;
149
- }
150
-
151
- if (jsonData.metallic !== undefined && typeof jsonData.metallic === "number") {
152
- this.metalness = jsonData.metallic;
153
- }
154
-
155
- if (jsonData.metallicChannel !== undefined) {
156
- this.metalnessChannel = jsonData.metallicChannel;
157
- }
158
-
159
- if (jsonData.metallicScale !== undefined) {
160
- this.metalnessScale = jsonData.metallicScale;
161
- }
162
-
163
- if (jsonData.metalnessTexture !== undefined) {
164
- this.metalnessTexture = jsonData.metalnessTexture;
165
- }
166
-
167
- if (jsonData.metalness !== undefined) {
168
- this.metalness = jsonData.metalness;
169
- }
170
-
171
- if (jsonData.metalnessScale !== undefined) {
172
- this.metalnessScale = jsonData.metalnessScale;
173
- }
174
-
175
- if (jsonData.metalnessChannel !== undefined) {
176
- this.metalnessChannel = jsonData.metalnessChannel;
177
- }
178
-
179
- if (jsonData.roughness !== undefined && typeof jsonData.roughness === "string") {
180
- this.roughnessTexture = jsonData.roughness;
181
- }
182
- else if (jsonData.roughness !== undefined && typeof jsonData.roughness === "number") {
183
- this.roughness = jsonData.roughness;
184
- }
185
-
186
- if (jsonData.roughnessTexture !== undefined) {
187
- this.roughnessTexture = jsonData.roughnessTexture;
188
- }
189
-
190
- if (jsonData.roughnessChannel !== undefined) {
191
- this.roughnessChannel = jsonData.roughnessChannel;
192
- }
193
-
194
- if (jsonData.roughnessScale !== undefined) {
195
- this.roughnessScale = jsonData.roughnessScale;
196
- }
197
-
198
-
199
- if (jsonData.fresnel !== undefined) {
200
- this.fresnelTint = jsonData.fresnel;
201
- }
202
-
203
- if (jsonData.fresnelTint !== undefined) {
204
- this.fresnelTint = jsonData.fresnelTint;
205
- }
206
-
207
- if (jsonData.sheenIntensity !== undefined) {
208
- this.sheenIntensity = jsonData.sheenIntensity;
209
- }
210
-
211
- if (jsonData.sheenColor !== undefined) {
212
- this.sheenColor = jsonData.sheenColor;
213
- }
214
-
215
- if (jsonData.lightEmission !== undefined) {
216
- this.lightEmission = jsonData.lightEmission;
217
- }
218
-
219
- if (jsonData.lightEmissionTexture !== undefined) {
220
- this.lightEmissionTexture = jsonData.lightEmissionTexture;
221
- }
222
-
223
- if (jsonData.lightEmissionChannel !== undefined) {
224
- this.lightEmissionChannel = jsonData.lightEmissionChannel;
225
- }
226
-
227
- if (jsonData.lightEmissionScale !== undefined) {
228
- this.lightEmissionScale = jsonData.lightEmissionScale;
229
- }
230
-
231
- if (jsonData.isTransparent !== undefined) {
232
- this.isTransparent = jsonData.isTransparent;
233
- }
234
-
235
- if (jsonData.unlit !== undefined) {
236
- this.unlit = jsonData.unlit;
237
- }
238
-
239
- }
240
- }
241
-
242
- get alphaCutoff(): number | undefined { return this._alphaCutoff; }
243
- set alphaCutoff(v: number | undefined) { this._alphaCutoff = v; }
244
-
245
- get castShadows(): boolean | undefined { return this._castShadows; }
246
- set castShadows(v: boolean | undefined) { this._castShadows = v; }
247
-
248
- get albedo(): [number, number, number, number] | undefined { return this._albedo; }
249
- set albedo(v: [number, number, number, number] | undefined) { this._albedo = v; }
250
-
251
- get albedoTexture(): string | undefined { return this._albedoTexture; }
252
- set albedoTexture(v: string | undefined) { this._albedoTexture = v; }
253
-
254
- get albedoScale(): [number, number] | undefined { return this._albedoScale; }
255
- set albedoScale(v: [number, number] | undefined) { this._albedoScale = v; }
256
-
257
- get normalTexture(): string | undefined { return this._normalTexture; }
258
- set normalTexture(v: string | undefined) { this._normalTexture = v; }
259
-
260
- get normalScale(): [number, number] | undefined { return this._normalScale; }
261
- set normalScale(v: [number, number] | undefined) { this._normalScale = v; }
262
-
263
- get metalness(): number | undefined { return this._metalness; }
264
- set metalness(v: number | undefined) { this._metalness = v; }
265
-
266
- get metalnessTexture(): string | undefined { return this._metalnessTexture; }
267
- set metalnessTexture(v: string | undefined) { this._metalnessTexture = v; }
268
-
269
- get metalnessChannel(): number | undefined { return this._metalnessChannel; }
270
- set metalnessChannel(v: number | undefined) { this._metalnessChannel = v; }
271
-
272
- get metalnessScale(): [number, number] | undefined { return this._metalnessScale; }
273
- set metalnessScale(v: [number, number] | undefined) { this._metalnessScale = v; }
274
-
275
- get roughness(): number | undefined { return this._roughness; }
276
- set roughness(v: number | undefined) { this._roughness = v; }
277
-
278
- get roughnessTexture(): string | undefined { return this._roughnessTexture; }
279
- set roughnessTexture(v: string | undefined) { this._roughnessTexture = v; }
280
-
281
- get roughnessChannel(): number | undefined { return this._roughnessChannel; }
282
- set roughnessChannel(v: number | undefined) { this._roughnessChannel = v; }
283
-
284
- get roughnessScale(): [number, number] | undefined { return this._roughnessScale; }
285
- set roughnessScale(v: [number, number] | undefined) { this._roughnessScale = v; }
286
-
287
- get fresnelTint(): [number, number, number, number] | undefined { return this._fresnelTint; }
288
- set fresnelTint(v: [number, number, number, number] | undefined) { this._fresnelTint = v; }
289
-
290
- get sheenIntensity(): number | undefined { return this._sheenIntensity; }
291
- set sheenIntensity(v: number | undefined) { this._sheenIntensity = v; }
292
-
293
- get sheenColor(): [number, number, number, number] | undefined { return this._sheenColor; }
294
- set sheenColor(v: [number, number, number, number] | undefined) { this._sheenColor = v; }
295
-
296
- get isTransparent(): boolean | undefined { return this._isTransparent; }
297
- set isTransparent(v: boolean | undefined) { this._isTransparent = v; }
298
-
299
- get lightEmission(): any { return this._lightEmission; }
300
- set lightEmission(v: any) { this._lightEmission = v; }
301
-
302
- get lightEmissionTexture(): string | undefined { return this._lightEmissionTexture; }
303
- set lightEmissionTexture(v: string | undefined) { this._lightEmissionTexture = v; }
304
-
305
- get lightEmissionChannel(): number | undefined { return this._lightEmissionChannel; }
306
- set lightEmissionChannel(v: number | undefined) { this._lightEmissionChannel = v; }
307
-
308
- get lightEmissionScale(): any { return this._lightEmissionScale; }
309
- set lightEmissionScale(v: any) { this._lightEmissionScale = v; }
310
-
311
- get unlit(): boolean | undefined { return this._unlit; }
312
- set unlit(v: boolean | undefined) { this._unlit = v; }
313
-
314
- get texturesPath(): string { return this._texturesPath; }
315
- set texturesPath(v: string) { this._texturesPath = v; }
316
-
317
- async applyDrawable(canvas: Canvas, drawable: Drawable, { groupName, groupRE } : { groupName?: string, groupRE?: RegExp } = {}): Promise<void> {
318
- drawable.items.forEach(item => {
319
- if ( (groupName && item.polyList.groupName === groupName) ||
320
- (groupRE && item.polyList.groupName && groupRE.test(item.polyList.groupName)) ||
321
- (!groupName && !groupRE))
322
- {
323
- this.apply(canvas, item.material);
324
- }
325
- })
326
- }
327
-
328
- async apply(canvas: Canvas, material: Material): Promise<void> {
329
- const relativePath = this._texturesPath;
330
- const promises: Promise<void>[] = [];
331
-
332
- if (this.alphaCutoff !== undefined) {
333
- material.alphaCutoff = this.alphaCutoff;
334
- }
335
-
336
- if (this.castShadows !== undefined) {
337
- material.castShadows = this.castShadows;
338
- }
339
-
340
- if (this.albedo !== undefined) {
341
- material.albedo = new Color(this.albedo);
342
- }
343
-
344
- if (this.albedoTexture !== undefined) {
345
- promises.push(new Promise<void>(async (resolve) => {
346
- material.albedoTexture = await loadTexture(canvas, this.albedoTexture!, relativePath);
347
- resolve();
348
- }));
349
- }
350
-
351
- if (this.albedoScale !== undefined) {
352
- material.albedoScale = new Vec(this.albedoScale);
353
- }
354
-
355
- if (this.normalTexture !== undefined) {
356
- promises.push(new Promise<void>(async (resolve) => {
357
- material.normalTexture = await loadTexture(canvas, this.normalTexture!, relativePath);
358
- resolve();
359
- }));
360
- }
361
-
362
- if (this.normalScale !== undefined) {
363
- material.normalScale = new Vec(this.normalScale);
364
- }
365
-
366
- if (this.metalness !== undefined) {
367
- material.metalness = this.metalness;
368
- }
369
-
370
- if (this.metalnessTexture !== undefined) {
371
- promises.push(new Promise<void>(async (resolve) => {
372
- material.metalnessTexture = await loadTexture(canvas, this.metalnessTexture!, relativePath);
373
- resolve();
374
- }));
375
- }
376
-
377
- if (this.metalnessChannel !== undefined) {
378
- material.metalnessChannel = this.metalnessChannel;
379
- }
380
-
381
- if (this.metalnessScale !== undefined) {
382
- material.metalnessScale = new Vec(this.metalnessScale);
383
- }
384
-
385
- if (this.roughness !== undefined) {
386
- material.roughness = this.roughness;
387
- }
388
-
389
- if (this.roughnessTexture !== undefined) {
390
- promises.push(new Promise<void>(async (resolve) => {
391
- material.roughnessTexture = await loadTexture(canvas, this.roughnessTexture!, relativePath);
392
- resolve();
393
- }));
394
- }
395
-
396
- if (this.roughnessChannel !== undefined) {
397
- material.roughnessChannel = this.roughnessChannel;
398
- }
399
-
400
- if (this.roughnessScale !== undefined) {
401
- material.roughnessScale = new Vec(this.roughnessScale);
402
- }
403
-
404
- if (this.fresnelTint !== undefined) {
405
- material.fresnelTint = new Color(this.fresnelTint);
406
- }
407
-
408
- if (this.sheenIntensity !== undefined) {
409
- material.sheenIntensity = this.sheenIntensity;
410
- }
411
-
412
- if (this.sheenColor !== undefined) {
413
- material.sheenColor = new Color(this.sheenColor);
414
- }
415
-
416
- if (this.isTransparent !== undefined) {
417
- material.isTransparent = this.isTransparent;
418
- }
419
-
420
- if (this.lightEmission !== undefined) {
421
- material.lightEmission = this.lightEmission;
422
- }
423
-
424
- if (this.lightEmissionTexture !== undefined) {
425
- promises.push(new Promise<void>(async (resolve) => {
426
- material.lightEmissionTexture = await loadTexture(canvas, this.lightEmissionTexture!, relativePath);
427
- resolve();
428
- }));
429
- }
430
-
431
- if (this.lightEmissionChannel !== undefined) {
432
- material.lightEmissionChannel = this.lightEmissionChannel;
433
- }
434
-
435
- if (this.lightEmissionScale !== undefined) {
436
- material.lightEmissionScale = new Vec(this.lightEmissionScale);
437
- }
438
-
439
- if (this.unlit !== undefined) {
440
- material.unlit = this.unlit;
441
- }
442
-
443
- await Promise.allSettled(promises);
444
- }
445
-
446
- }
1
+ import {
2
+ textureTypeLoader,
3
+ colorTypeLoader,
4
+ vectorTypeLoader,
5
+ primitiveTypeLoader
6
+ } from "../base/Material";
7
+ import Material from "../base/Material";
8
+ import Texture from "../base/Texture";
9
+ import Vec from "../math/Vec";
10
+ import Color from "../base/Color";
11
+ import Drawable from "../scene/Drawable";
12
+ import Canvas from "../app/Canvas";
13
+ import TextureCache from "./TextureCache";
14
+
15
+ const checkImageData = (texture: Texture | Color | number | null): Promise<void> => {
16
+ return texture instanceof Texture ? texture.loadImageData() : Promise.resolve();
17
+ }
18
+
19
+ const loadTexture = async (canvas: Canvas, fileName: string, relativePath: string): Promise<Texture> => {
20
+ const texture = new Texture(canvas);
21
+ texture.fileName = relativePath + fileName;
22
+ await texture.loadImageData();
23
+ return texture;
24
+ }
25
+
26
+ export interface MaterialModifierData {
27
+ class?: string;
28
+ alphaCutoff?: number;
29
+ castShadows?: boolean;
30
+ albedo?: [number, number, number, number];
31
+ albedoTexture?: string;
32
+ albedoScale?: [number, number];
33
+ normalTexture?: string;
34
+ normalScale?: [number, number];
35
+ metalness?: number;
36
+ metalnessTexture?: string;
37
+ metalnessChannel?: number;
38
+ metalnessScale?: [number, number];
39
+ roughness?: number;
40
+ roughnessTexture?: string;
41
+ roughnessChannel?: number;
42
+ roughnessScale?: [number, number];
43
+ fresnelTint?: [number, number, number, number];
44
+ sheenIntensity?: number;
45
+ sheenColor?: [number, number, number, number];
46
+ lightEmission?: number;
47
+ lightEmissionTexture?: string;
48
+ lightEmissionChannel?: number;
49
+ lightEmissionScale?: [number, number];
50
+ isTransparent?: boolean;
51
+ unlit?: boolean;
52
+
53
+ // Deprecated properties
54
+ diffuse?: string | [number, number, number, number];
55
+ diffuseScale?: [number, number];
56
+ normal?: string;
57
+ fresnel?: [number, number, number, number];
58
+ metallic?: string | number;
59
+ metallicChannel?: number;
60
+ metallicScale?: [number, number];
61
+ }
62
+
63
+ // TODO: Update all material properties (difuse -> albedo, etc)
64
+ export default class MaterialModifier {
65
+ private _alphaCutoff?: number;
66
+ private _castShadows?: boolean;
67
+ private _albedo?: [number, number, number, number];
68
+ private _albedoTexture?: string;
69
+ private _albedoScale?: [number, number];
70
+ private _normalTexture?: string;
71
+ private _normalScale?: [number, number];
72
+ private _metalness?: number;
73
+ private _metalnessTexture?: string;
74
+ private _metalnessChannel?: number;
75
+ private _metalnessScale?: [number, number];
76
+ private _roughness?: number;
77
+ private _roughnessTexture?: string;
78
+ private _roughnessChannel?: number;
79
+ private _roughnessScale?: [number, number];
80
+ private _fresnelTint?: [number, number, number, number];
81
+ private _sheenIntensity?: number;
82
+ private _sheenColor?: [number, number, number, number];
83
+ private _lightEmission?: [number, number, number, number];
84
+ private _lightEmissionTexture?: string;
85
+ private _lightEmissionChannel?: number;
86
+ private _lightEmissionScale?: [number, number];
87
+ private _isTransparent?: boolean;
88
+ private _unlit?: boolean;
89
+
90
+ private _texturesPath: string = "";
91
+
92
+ constructor(jsonData: MaterialModifierData | null = null, texturesPath: string = "") {
93
+ this._texturesPath = texturesPath;
94
+ if (jsonData) {
95
+ if (jsonData['class'] && jsonData['class'] !== "PBRMaterial") {
96
+ console.warn(`Could not apply material modifier because of class "${jsonData['class']}". Check the valid material types in bg2 engine v2`);
97
+ return;
98
+ }
99
+ if (jsonData.alphaCutoff !== undefined) {
100
+ this.alphaCutoff = jsonData.alphaCutoff;
101
+ }
102
+
103
+ if (jsonData.castShadows !== undefined) {
104
+ this.castShadows = jsonData.castShadows;
105
+ }
106
+
107
+ if (jsonData.diffuse !== undefined && typeof jsonData.diffuse === "string") {
108
+ this.albedoTexture = jsonData.diffuse;
109
+ }
110
+ else if (jsonData.diffuse !== undefined && Array.isArray(jsonData.diffuse)) {
111
+ this.albedo = jsonData.diffuse;
112
+ }
113
+
114
+ if (jsonData.albedoTexture !== undefined) {
115
+ this.albedoTexture = jsonData.albedoTexture;
116
+ }
117
+
118
+ if (jsonData.albedoScale !== undefined) {
119
+ this.albedoScale = jsonData.albedoScale;
120
+ }
121
+
122
+ if (jsonData.albedo !== undefined) {
123
+ this.albedo = jsonData.albedo;
124
+ }
125
+
126
+ if (jsonData.diffuseScale !== undefined) {
127
+ this.albedoScale = jsonData.diffuseScale;
128
+ }
129
+
130
+ if (jsonData.normal !== undefined) {
131
+ this.normalTexture = jsonData.normal;
132
+ }
133
+
134
+ if (jsonData.normalTexture !== undefined) {
135
+ this.normalTexture = jsonData.normalTexture;
136
+ }
137
+
138
+ if (jsonData.normalScale !== undefined) {
139
+ this.normalScale = jsonData.normalScale;
140
+ }
141
+
142
+ if (jsonData.normalScale !== undefined) {
143
+ this.normalScale = jsonData.normalScale;
144
+ }
145
+
146
+ if (jsonData.metallic !== undefined && typeof jsonData.metallic === "string") {
147
+ this.metalnessTexture
148
+ = jsonData.metallic;
149
+ }
150
+
151
+ if (jsonData.metallic !== undefined && typeof jsonData.metallic === "number") {
152
+ this.metalness = jsonData.metallic;
153
+ }
154
+
155
+ if (jsonData.metallicChannel !== undefined) {
156
+ this.metalnessChannel = jsonData.metallicChannel;
157
+ }
158
+
159
+ if (jsonData.metallicScale !== undefined) {
160
+ this.metalnessScale = jsonData.metallicScale;
161
+ }
162
+
163
+ if (jsonData.metalnessTexture !== undefined) {
164
+ this.metalnessTexture = jsonData.metalnessTexture;
165
+ }
166
+
167
+ if (jsonData.metalness !== undefined) {
168
+ this.metalness = jsonData.metalness;
169
+ }
170
+
171
+ if (jsonData.metalnessScale !== undefined) {
172
+ this.metalnessScale = jsonData.metalnessScale;
173
+ }
174
+
175
+ if (jsonData.metalnessChannel !== undefined) {
176
+ this.metalnessChannel = jsonData.metalnessChannel;
177
+ }
178
+
179
+ if (jsonData.roughness !== undefined && typeof jsonData.roughness === "string") {
180
+ this.roughnessTexture = jsonData.roughness;
181
+ }
182
+ else if (jsonData.roughness !== undefined && typeof jsonData.roughness === "number") {
183
+ this.roughness = jsonData.roughness;
184
+ }
185
+
186
+ if (jsonData.roughnessTexture !== undefined) {
187
+ this.roughnessTexture = jsonData.roughnessTexture;
188
+ }
189
+
190
+ if (jsonData.roughnessChannel !== undefined) {
191
+ this.roughnessChannel = jsonData.roughnessChannel;
192
+ }
193
+
194
+ if (jsonData.roughnessScale !== undefined) {
195
+ this.roughnessScale = jsonData.roughnessScale;
196
+ }
197
+
198
+
199
+ if (jsonData.fresnel !== undefined) {
200
+ this.fresnelTint = jsonData.fresnel;
201
+ }
202
+
203
+ if (jsonData.fresnelTint !== undefined) {
204
+ this.fresnelTint = jsonData.fresnelTint;
205
+ }
206
+
207
+ if (jsonData.sheenIntensity !== undefined) {
208
+ this.sheenIntensity = jsonData.sheenIntensity;
209
+ }
210
+
211
+ if (jsonData.sheenColor !== undefined) {
212
+ this.sheenColor = jsonData.sheenColor;
213
+ }
214
+
215
+ if (jsonData.lightEmission !== undefined) {
216
+ this.lightEmission = jsonData.lightEmission;
217
+ }
218
+
219
+ if (jsonData.lightEmissionTexture !== undefined) {
220
+ this.lightEmissionTexture = jsonData.lightEmissionTexture;
221
+ }
222
+
223
+ if (jsonData.lightEmissionChannel !== undefined) {
224
+ this.lightEmissionChannel = jsonData.lightEmissionChannel;
225
+ }
226
+
227
+ if (jsonData.lightEmissionScale !== undefined) {
228
+ this.lightEmissionScale = jsonData.lightEmissionScale;
229
+ }
230
+
231
+ if (jsonData.isTransparent !== undefined) {
232
+ this.isTransparent = jsonData.isTransparent;
233
+ }
234
+
235
+ if (jsonData.unlit !== undefined) {
236
+ this.unlit = jsonData.unlit;
237
+ }
238
+
239
+ }
240
+ }
241
+
242
+ get alphaCutoff(): number | undefined { return this._alphaCutoff; }
243
+ set alphaCutoff(v: number | undefined) { this._alphaCutoff = v; }
244
+
245
+ get castShadows(): boolean | undefined { return this._castShadows; }
246
+ set castShadows(v: boolean | undefined) { this._castShadows = v; }
247
+
248
+ get albedo(): [number, number, number, number] | undefined { return this._albedo; }
249
+ set albedo(v: [number, number, number, number] | undefined) { this._albedo = v; }
250
+
251
+ get albedoTexture(): string | undefined { return this._albedoTexture; }
252
+ set albedoTexture(v: string | undefined) { this._albedoTexture = v; }
253
+
254
+ get albedoScale(): [number, number] | undefined { return this._albedoScale; }
255
+ set albedoScale(v: [number, number] | undefined) { this._albedoScale = v; }
256
+
257
+ get normalTexture(): string | undefined { return this._normalTexture; }
258
+ set normalTexture(v: string | undefined) { this._normalTexture = v; }
259
+
260
+ get normalScale(): [number, number] | undefined { return this._normalScale; }
261
+ set normalScale(v: [number, number] | undefined) { this._normalScale = v; }
262
+
263
+ get metalness(): number | undefined { return this._metalness; }
264
+ set metalness(v: number | undefined) { this._metalness = v; }
265
+
266
+ get metalnessTexture(): string | undefined { return this._metalnessTexture; }
267
+ set metalnessTexture(v: string | undefined) { this._metalnessTexture = v; }
268
+
269
+ get metalnessChannel(): number | undefined { return this._metalnessChannel; }
270
+ set metalnessChannel(v: number | undefined) { this._metalnessChannel = v; }
271
+
272
+ get metalnessScale(): [number, number] | undefined { return this._metalnessScale; }
273
+ set metalnessScale(v: [number, number] | undefined) { this._metalnessScale = v; }
274
+
275
+ get roughness(): number | undefined { return this._roughness; }
276
+ set roughness(v: number | undefined) { this._roughness = v; }
277
+
278
+ get roughnessTexture(): string | undefined { return this._roughnessTexture; }
279
+ set roughnessTexture(v: string | undefined) { this._roughnessTexture = v; }
280
+
281
+ get roughnessChannel(): number | undefined { return this._roughnessChannel; }
282
+ set roughnessChannel(v: number | undefined) { this._roughnessChannel = v; }
283
+
284
+ get roughnessScale(): [number, number] | undefined { return this._roughnessScale; }
285
+ set roughnessScale(v: [number, number] | undefined) { this._roughnessScale = v; }
286
+
287
+ get fresnelTint(): [number, number, number, number] | undefined { return this._fresnelTint; }
288
+ set fresnelTint(v: [number, number, number, number] | undefined) { this._fresnelTint = v; }
289
+
290
+ get sheenIntensity(): number | undefined { return this._sheenIntensity; }
291
+ set sheenIntensity(v: number | undefined) { this._sheenIntensity = v; }
292
+
293
+ get sheenColor(): [number, number, number, number] | undefined { return this._sheenColor; }
294
+ set sheenColor(v: [number, number, number, number] | undefined) { this._sheenColor = v; }
295
+
296
+ get isTransparent(): boolean | undefined { return this._isTransparent; }
297
+ set isTransparent(v: boolean | undefined) { this._isTransparent = v; }
298
+
299
+ get lightEmission(): any { return this._lightEmission; }
300
+ set lightEmission(v: any) { this._lightEmission = v; }
301
+
302
+ get lightEmissionTexture(): string | undefined { return this._lightEmissionTexture; }
303
+ set lightEmissionTexture(v: string | undefined) { this._lightEmissionTexture = v; }
304
+
305
+ get lightEmissionChannel(): number | undefined { return this._lightEmissionChannel; }
306
+ set lightEmissionChannel(v: number | undefined) { this._lightEmissionChannel = v; }
307
+
308
+ get lightEmissionScale(): any { return this._lightEmissionScale; }
309
+ set lightEmissionScale(v: any) { this._lightEmissionScale = v; }
310
+
311
+ get unlit(): boolean | undefined { return this._unlit; }
312
+ set unlit(v: boolean | undefined) { this._unlit = v; }
313
+
314
+ get texturesPath(): string { return this._texturesPath; }
315
+ set texturesPath(v: string) { this._texturesPath = v; }
316
+
317
+ async applyDrawable(canvas: Canvas, drawable: Drawable, { groupName, groupRE } : { groupName?: string, groupRE?: RegExp } = {}): Promise<void> {
318
+ drawable.items.forEach(item => {
319
+ if ( (groupName && item.polyList.groupName === groupName) ||
320
+ (groupRE && item.polyList.groupName && groupRE.test(item.polyList.groupName)) ||
321
+ (!groupName && !groupRE))
322
+ {
323
+ this.apply(canvas, item.material);
324
+ }
325
+ })
326
+ }
327
+
328
+ async apply(canvas: Canvas, material: Material): Promise<void> {
329
+ const relativePath = this._texturesPath;
330
+ const promises: Promise<void>[] = [];
331
+
332
+ if (this.alphaCutoff !== undefined) {
333
+ material.alphaCutoff = this.alphaCutoff;
334
+ }
335
+
336
+ if (this.castShadows !== undefined) {
337
+ material.castShadows = this.castShadows;
338
+ }
339
+
340
+ if (this.albedo !== undefined) {
341
+ material.albedo = new Color(this.albedo);
342
+ }
343
+
344
+ if (this.albedoTexture !== undefined) {
345
+ promises.push(new Promise<void>(async (resolve) => {
346
+ material.albedoTexture = await loadTexture(canvas, this.albedoTexture!, relativePath);
347
+ resolve();
348
+ }));
349
+ }
350
+
351
+ if (this.albedoScale !== undefined) {
352
+ material.albedoScale = new Vec(this.albedoScale);
353
+ }
354
+
355
+ if (this.normalTexture !== undefined) {
356
+ promises.push(new Promise<void>(async (resolve) => {
357
+ material.normalTexture = await loadTexture(canvas, this.normalTexture!, relativePath);
358
+ resolve();
359
+ }));
360
+ }
361
+
362
+ if (this.normalScale !== undefined) {
363
+ material.normalScale = new Vec(this.normalScale);
364
+ }
365
+
366
+ if (this.metalness !== undefined) {
367
+ material.metalness = this.metalness;
368
+ }
369
+
370
+ if (this.metalnessTexture !== undefined) {
371
+ promises.push(new Promise<void>(async (resolve) => {
372
+ material.metalnessTexture = await loadTexture(canvas, this.metalnessTexture!, relativePath);
373
+ resolve();
374
+ }));
375
+ }
376
+
377
+ if (this.metalnessChannel !== undefined) {
378
+ material.metalnessChannel = this.metalnessChannel;
379
+ }
380
+
381
+ if (this.metalnessScale !== undefined) {
382
+ material.metalnessScale = new Vec(this.metalnessScale);
383
+ }
384
+
385
+ if (this.roughness !== undefined) {
386
+ material.roughness = this.roughness;
387
+ }
388
+
389
+ if (this.roughnessTexture !== undefined) {
390
+ promises.push(new Promise<void>(async (resolve) => {
391
+ material.roughnessTexture = await loadTexture(canvas, this.roughnessTexture!, relativePath);
392
+ resolve();
393
+ }));
394
+ }
395
+
396
+ if (this.roughnessChannel !== undefined) {
397
+ material.roughnessChannel = this.roughnessChannel;
398
+ }
399
+
400
+ if (this.roughnessScale !== undefined) {
401
+ material.roughnessScale = new Vec(this.roughnessScale);
402
+ }
403
+
404
+ if (this.fresnelTint !== undefined) {
405
+ material.fresnelTint = new Color(this.fresnelTint);
406
+ }
407
+
408
+ if (this.sheenIntensity !== undefined) {
409
+ material.sheenIntensity = this.sheenIntensity;
410
+ }
411
+
412
+ if (this.sheenColor !== undefined) {
413
+ material.sheenColor = new Color(this.sheenColor);
414
+ }
415
+
416
+ if (this.isTransparent !== undefined) {
417
+ material.isTransparent = this.isTransparent;
418
+ }
419
+
420
+ if (this.lightEmission !== undefined) {
421
+ material.lightEmission = this.lightEmission;
422
+ }
423
+
424
+ if (this.lightEmissionTexture !== undefined) {
425
+ promises.push(new Promise<void>(async (resolve) => {
426
+ material.lightEmissionTexture = await loadTexture(canvas, this.lightEmissionTexture!, relativePath);
427
+ resolve();
428
+ }));
429
+ }
430
+
431
+ if (this.lightEmissionChannel !== undefined) {
432
+ material.lightEmissionChannel = this.lightEmissionChannel;
433
+ }
434
+
435
+ if (this.lightEmissionScale !== undefined) {
436
+ material.lightEmissionScale = new Vec(this.lightEmissionScale);
437
+ }
438
+
439
+ if (this.unlit !== undefined) {
440
+ material.unlit = this.unlit;
441
+ }
442
+
443
+ await Promise.allSettled(promises);
444
+ }
445
+
446
+ }