-
Notifications
You must be signed in to change notification settings - Fork 46
/
standard-material.ts
388 lines (349 loc) · 14 KB
/
standard-material.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { Renderer, Shader } from "@pixi/core"
import { DEG_TO_RAD } from "@pixi/math"
import { LightType } from "../../lighting/light-type"
import { StandardMaterialFeatureSet } from "./standard-material-feature-set"
import { StandardShader } from "./standard-shader"
import { Material } from "../material"
import { Camera } from "../../camera/camera"
import { LightingEnvironment } from "../../lighting/lighting-environment"
import { Mesh3D } from "../../mesh/mesh"
import { StandardMaterialAlphaMode } from "./standard-material-alpha-mode"
import { StandardMaterialDebugMode } from "./standard-material-debug-mode"
import { ShadowCastingLight } from "../../shadow/shadow-casting-light"
import { StandardMaterialSkinUniforms } from "./standard-material-skin-uniforms"
import { Color } from "../../color"
import { InstancedStandardMaterial } from "./instanced-standard-material"
import { StandardMaterialOcclusionTexture } from "./standard-material-occlusion-texture"
import { StandardMaterialNormalTexture } from "./standard-material-normal-texture"
import { StandardMaterialTexture } from "./standard-material-texture"
import { StandardMaterialFactory } from "./standard-material-factory"
import { ImageBasedLighting } from "../../lighting/image-based-lighting"
import { TextureTransform } from "../../texture/texture-transform"
const shaders: { [features: string]: StandardShader } = {}
const getLightingEnvironmentConfigId = (env?: LightingEnvironment) => {
return env ? (env.lights.length + (env.imageBasedLighting ? 0.5 : 0)) : 0
}
/**
* The standard material is using Physically-Based Rendering (PBR) which makes
* it suitable to represent a wide range of different surfaces. It's the default
* material when loading models from file.
*/
export class StandardMaterial extends Material {
private _lightingEnvironment?: LightingEnvironment
private _lightingEnvironmentConfigId = 0
private _unlit = false
private _alphaMode = StandardMaterialAlphaMode.blend
private _debugMode?: StandardMaterialDebugMode
private _baseColorTexture?: StandardMaterialTexture
private _baseColorFactor = new Float32Array(4)
private _normalTexture?: StandardMaterialNormalTexture
private _occlusionTexture?: StandardMaterialOcclusionTexture
private _emissiveTexture?: StandardMaterialTexture
private _metallicRoughnessTexture?: StandardMaterialTexture
private _shadowCastingLight?: ShadowCastingLight
private _instancingEnabled = false
private _skinUniforms = new StandardMaterialSkinUniforms()
/** The roughness of the material. */
roughness = 1
/** The metalness of the material. */
metallic = 1
/** The base color of the material. */
baseColor = new Color(1, 1, 1, 1)
/** The cutoff threshold when alpha mode is set to "mask". */
alphaCutoff = 0.5
/** The emissive color of the material. */
emissive = new Color(0, 0, 0)
/** The exposure (brightness) of the material. */
exposure = 1
/** The base color texture. */
get baseColorTexture() {
return this._baseColorTexture
}
set baseColorTexture(value: StandardMaterialTexture | undefined) {
if (value !== this._baseColorTexture) {
this.invalidateShader()
if (!value?.transform && value?.frame && !value?.noFrame) {
value.transform = TextureTransform.fromTexture(value)
}
this._baseColorTexture = value
}
}
/** The metallic-roughness texture. */
get metallicRoughnessTexture() {
return this._metallicRoughnessTexture
}
set metallicRoughnessTexture(value: StandardMaterialTexture | undefined) {
if (value !== this._metallicRoughnessTexture) {
this.invalidateShader()
if (!value?.transform && value?.frame && !value?.noFrame) {
value.transform = TextureTransform.fromTexture(value)
}
this._metallicRoughnessTexture = value
}
}
/** The normal map texture. */
get normalTexture() {
return this._normalTexture
}
set normalTexture(value: StandardMaterialNormalTexture | undefined) {
if (value !== this._normalTexture) {
this.invalidateShader()
if (!value?.transform && value?.frame && !value?.noFrame) {
value.transform = TextureTransform.fromTexture(value)
}
this._normalTexture = value
}
}
/** The occlusion map texture. */
get occlusionTexture() {
return this._occlusionTexture
}
set occlusionTexture(value: StandardMaterialOcclusionTexture | undefined) {
if (value !== this._occlusionTexture) {
this.invalidateShader()
if (!value?.transform && value?.frame && !value?.noFrame) {
value.transform = TextureTransform.fromTexture(value)
}
this._occlusionTexture = value
}
}
/** The emissive map texture. */
get emissiveTexture() {
return this._emissiveTexture
}
set emissiveTexture(value: StandardMaterialTexture | undefined) {
if (value !== this._emissiveTexture) {
this.invalidateShader()
if (!value?.transform && value?.frame && !value?.noFrame) {
value.transform = TextureTransform.fromTexture(value)
}
this._emissiveTexture = value
}
}
/** The alpha rendering mode of the material. */
get alphaMode() {
return this._alphaMode
}
set alphaMode(value: StandardMaterialAlphaMode) {
if (this._alphaMode !== value) {
this._alphaMode = value
this.invalidateShader()
}
}
/** The shadow casting light of the material. */
get shadowCastingLight() {
return this._shadowCastingLight
}
set shadowCastingLight(value: ShadowCastingLight | undefined) {
if (value !== this._shadowCastingLight) {
this.invalidateShader()
this._shadowCastingLight = value
}
}
/** The debug rendering mode of the material. */
get debugMode() {
return this._debugMode
}
set debugMode(value: StandardMaterialDebugMode | undefined) {
if (this._debugMode !== value) {
this.invalidateShader()
this._debugMode = value
}
}
/**
* The camera used when rendering a mesh. If this value is not set, the main
* camera will be used by default.
*/
camera?: Camera
/**
* Lighting environment used when rendering a mesh. If this value is not set,
* the main lighting environment will be used by default.
*/
get lightingEnvironment() {
return this._lightingEnvironment
}
set lightingEnvironment(value: LightingEnvironment | undefined) {
if (value !== this._lightingEnvironment) {
this.invalidateShader()
this._lightingEnvironmentConfigId = getLightingEnvironmentConfigId(value)
this._lightingEnvironment = value
}
}
/**
* Value indicating if the material is unlit. If this value if set to true,
* all lighting is disabled and only the base color will be used.
*/
get unlit() {
return this._unlit
}
set unlit(value: boolean) {
if (this._unlit !== value) {
this._unlit = value
this.invalidateShader()
}
}
destroy() {
this._baseColorTexture?.destroy()
this._normalTexture?.destroy()
this._emissiveTexture?.destroy()
this._occlusionTexture?.destroy()
this._metallicRoughnessTexture?.destroy()
this._skinUniforms.destroy()
}
/**
* Invalidates the shader so it can be rebuilt with the current features.
*/
invalidateShader() {
this._shader = undefined
}
/**
* Creates a new standard material from the specified source.
* @param source Source from which the material is created.
*/
static create(source: unknown) {
return new StandardMaterialFactory().create(source)
}
render(mesh: Mesh3D, renderer: Renderer) {
if (!this._instancingEnabled && mesh.instances.length > 0) {
// Invalidate shader when instancing was enabled.
this.invalidateShader()
this._instancingEnabled = true
}
if (this._instancingEnabled && mesh.instances.length === 0) {
// Invalidate shader when instancing was disabled.
this.invalidateShader()
this._instancingEnabled = false
}
let lighting = this.lightingEnvironment || LightingEnvironment.main
let configId = getLightingEnvironmentConfigId(lighting)
if (configId !== this._lightingEnvironmentConfigId) {
// Invalidate shader when the lighting config has changed.
this.invalidateShader()
this._lightingEnvironmentConfigId = configId
}
super.render(mesh, renderer)
}
get isInstancingSupported() {
return true
}
createInstance() {
return new InstancedStandardMaterial(this)
}
createShader(mesh: Mesh3D, renderer: Renderer) {
if (renderer.context.webGLVersion === 1) {
let extensions = ["EXT_shader_texture_lod", "OES_standard_derivatives"]
for (let ext of extensions) {
if (!renderer.gl.getExtension(ext)) {
// Log warning?
}
}
}
let lightingEnvironment = this.lightingEnvironment || LightingEnvironment.main
let features = StandardMaterialFeatureSet.build(renderer, mesh, mesh.geometry, this, lightingEnvironment)
if (!features) {
// The shader features couldn't be built, some resources may still be
// loading. Don't worry, we will retry creating shader at next render.
return undefined
}
if (mesh.skin && StandardMaterialFeatureSet.hasSkinningTextureFeature(features)) {
this._skinUniforms.enableJointMatrixTextures(mesh.skin.joints.length)
}
let checksum = features.join(",")
if (!shaders[checksum]) {
shaders[checksum] = StandardShader.build(renderer, features)
}
return shaders[checksum]
}
updateUniforms(mesh: Mesh3D, shader: Shader) {
for (let i = 0; i < 3; i++) {
this._baseColorFactor[i] = this.baseColor.rgba[i]
}
this._baseColorFactor[3] = this.baseColor.a * mesh.worldAlpha
let camera = this.camera || Camera.main
if (mesh.skin) {
this._skinUniforms.update(mesh, shader)
}
shader.uniforms.u_Camera = camera.worldTransform.position.array
shader.uniforms.u_ViewProjectionMatrix = camera.viewProjection.array
shader.uniforms.u_Exposure = this.exposure
shader.uniforms.u_MetallicFactor = this.metallic
shader.uniforms.u_RoughnessFactor = this.roughness
shader.uniforms.u_BaseColorFactor = this._baseColorFactor
shader.uniforms.u_ModelMatrix = mesh.worldTransform.array
shader.uniforms.u_NormalMatrix = mesh.transform.normalTransform.array
if (this._alphaMode === StandardMaterialAlphaMode.mask) {
shader.uniforms.u_AlphaCutoff = this.alphaCutoff
}
if (mesh.targetWeights && mesh.targetWeights.length > 0) {
shader.uniforms.u_morphWeights = mesh.targetWeights
}
if (this.baseColorTexture?.valid) {
shader.uniforms.u_BaseColorSampler = this.baseColorTexture
shader.uniforms.u_BaseColorUVSet = this.baseColorTexture.uvSet || 0
if (this.baseColorTexture.transform) {
shader.uniforms.u_BaseColorUVTransform = this.baseColorTexture.transform.array
}
}
let lightingEnvironment = this.lightingEnvironment || LightingEnvironment.main
for (let i = 0; i < lightingEnvironment.lights.length; i++) {
let light = lightingEnvironment.lights[i]
let type = 0
switch (light.type) {
case LightType.point: type = 1; break
case LightType.directional: type = 0; break
case LightType.spot: type = 2; break
}
shader.uniforms[`u_Lights[${i}].type`] = type
shader.uniforms[`u_Lights[${i}].position`] = light.worldTransform.position.array
shader.uniforms[`u_Lights[${i}].direction`] = light.worldTransform.forward.array
shader.uniforms[`u_Lights[${i}].range`] = light.range
shader.uniforms[`u_Lights[${i}].color`] = light.color.rgb
shader.uniforms[`u_Lights[${i}].intensity`] = light.intensity
shader.uniforms[`u_Lights[${i}].innerConeCos`] = Math.cos(light.innerConeAngle * DEG_TO_RAD)
shader.uniforms[`u_Lights[${i}].outerConeCos`] = Math.cos(light.outerConeAngle * DEG_TO_RAD)
}
if (this._shadowCastingLight) {
shader.uniforms.u_ShadowSampler = this._shadowCastingLight.shadowTexture
shader.uniforms.u_LightViewProjectionMatrix = this._shadowCastingLight.lightViewProjection
shader.uniforms.u_ShadowLightIndex = lightingEnvironment.lights.indexOf(this._shadowCastingLight.light)
}
let imageBasedLighting = lightingEnvironment.imageBasedLighting
if (imageBasedLighting?.valid) {
shader.uniforms.u_DiffuseEnvSampler = imageBasedLighting.diffuse
shader.uniforms.u_SpecularEnvSampler = imageBasedLighting.specular
shader.uniforms.u_brdfLUT = imageBasedLighting.lookupBrdf || ImageBasedLighting.defaultLookupBrdf
shader.uniforms.u_MipCount = imageBasedLighting.specular.levels - 1
}
if (this.emissiveTexture?.valid) {
shader.uniforms.u_EmissiveSampler = this.emissiveTexture
shader.uniforms.u_EmissiveUVSet = this.emissiveTexture.uvSet || 0
shader.uniforms.u_EmissiveFactor = this.emissive.rgb
if (this.emissiveTexture.transform) {
shader.uniforms.u_EmissiveUVTransform = this.emissiveTexture.transform.array
}
}
if (this.normalTexture?.valid) {
shader.uniforms.u_NormalSampler = this.normalTexture
shader.uniforms.u_NormalScale = this.normalTexture.scale || 1
shader.uniforms.u_NormalUVSet = this.normalTexture.uvSet || 0
if (this.normalTexture.transform) {
shader.uniforms.u_NormalUVTransform = this.normalTexture.transform.array
}
}
if (this.metallicRoughnessTexture?.valid) {
shader.uniforms.u_MetallicRoughnessSampler = this.metallicRoughnessTexture
shader.uniforms.u_MetallicRoughnessUVSet = this.metallicRoughnessTexture.uvSet || 0
if (this.metallicRoughnessTexture.transform) {
shader.uniforms.u_MetallicRoughnessUVTransform = this.metallicRoughnessTexture.transform.array
}
}
if (this.occlusionTexture?.valid) {
shader.uniforms.u_OcclusionSampler = this.occlusionTexture
shader.uniforms.u_OcclusionStrength = this.occlusionTexture.strength || 1
shader.uniforms.u_OcclusionUVSet = this.occlusionTexture.uvSet || 0
if (this.occlusionTexture.transform) {
shader.uniforms.u_OcclusionUVTransform = this.occlusionTexture.transform.array
}
}
}
}