/
KHR_materials_transmission.ts
375 lines (326 loc) · 14.9 KB
/
KHR_materials_transmission.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
import { Nullable } from "babylonjs/types";
import { PBRMaterial } from "babylonjs/Materials/PBR/pbrMaterial";
import { Material } from "babylonjs/Materials/material";
import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
import { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
import { IGLTFLoaderExtension } from "../glTFLoaderExtension";
import { GLTFLoader } from "../glTFLoader";
import { IKHRMaterialsTransmission } from 'babylonjs-gltf2interface';
import { Scene } from "babylonjs/scene";
import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
import { Texture } from "babylonjs/Materials/Textures/texture";
import { RenderTargetTexture } from "babylonjs/Materials/Textures/renderTargetTexture";
import { Observable, Observer } from "babylonjs/Misc/observable";
import { Constants } from "babylonjs/Engines/constants";
import { Tools } from "babylonjs/Misc/tools";
import { Color4 } from "babylonjs/Maths/math.color";
interface ITransmissionHelperHolder {
/**
* @hidden
*/
_transmissionHelper: TransmissionHelper | undefined;
}
interface ITransmissionHelperOptions {
/**
* The size of the render buffers (default: 1024)
*/
renderSize: number;
/**
* The number of samples to use when generating the render target texture for opaque meshes (default: 4)
*/
samples: number;
/**
* Scale to apply when selecting the LOD level to sample the refraction texture (default: 1)
*/
lodGenerationScale: number;
/**
* Offset to apply when selecting the LOD level to sample the refraction texture (default: -4)
*/
lodGenerationOffset: number;
/**
* Type of the refraction render target texture (default: TEXTURETYPE_HALF_FLOAT)
*/
renderTargetTextureType: number;
/**
* Defines if the mipmaps for the refraction render target texture must be generated (default: true)
*/
generateMipmaps: boolean;
/**
* Clear color of the opaque texture. If not provided, use the scene clear color (which will be converted to linear space).
* If provided, should be in linear space
*/
clearColor?: Color4;
}
/**
* A class to handle setting up the rendering of opaque objects to be shown through transmissive objects.
*/
class TransmissionHelper {
/**
* Creates the default options for the helper.
*/
private static _getDefaultOptions(): ITransmissionHelperOptions {
return {
renderSize: 1024,
samples: 4,
lodGenerationScale: 1,
lodGenerationOffset: -4,
renderTargetTextureType: Constants.TEXTURETYPE_HALF_FLOAT,
generateMipmaps: true,
};
}
/**
* Stores the creation options.
*/
private readonly _scene: Scene & ITransmissionHelperHolder;
private _options: ITransmissionHelperOptions;
private _opaqueRenderTarget: Nullable<RenderTargetTexture> = null;
private _opaqueMeshesCache: AbstractMesh[] = [];
private _transparentMeshesCache: AbstractMesh[] = [];
private _materialObservers: { [id: string]: Nullable<Observer<AbstractMesh>> } = {};
/**
* This observable will be notified with any error during the creation of the environment,
* mainly texture creation errors.
*/
public onErrorObservable: Observable<{ message?: string, exception?: any }>;
/**
* constructor
* @param options Defines the options we want to customize the helper
* @param scene The scene to add the material to
*/
constructor(options: Partial<ITransmissionHelperOptions>, scene: Scene) {
this._options = {
...TransmissionHelper._getDefaultOptions(),
...options
};
this._scene = scene as any;
this._scene._transmissionHelper = this;
this.onErrorObservable = new Observable();
this._scene.onDisposeObservable.addOnce((scene) => {
this.dispose();
});
this._parseScene();
this._setupRenderTargets();
}
/**
* Updates the background according to the new options
* @param options
*/
public updateOptions(options: Partial<ITransmissionHelperOptions>) {
// First check if any options are actually being changed. If not, exit.
const newValues = Object.keys(options).filter((key: string) => (this._options as any)[key] !== (options as any)[key]);
if (!newValues.length) {
return;
}
const newOptions = {
...this._options,
...options
};
const oldOptions = this._options;
this._options = newOptions;
// If size changes, recreate everything
if (newOptions.renderSize !== oldOptions.renderSize || newOptions.renderTargetTextureType !== oldOptions.renderTargetTextureType || newOptions.generateMipmaps !== oldOptions.generateMipmaps || !this._opaqueRenderTarget) {
this._setupRenderTargets();
} else {
this._opaqueRenderTarget.samples = newOptions.samples;
this._opaqueRenderTarget.lodGenerationScale = newOptions.lodGenerationScale;
this._opaqueRenderTarget.lodGenerationOffset = newOptions.lodGenerationOffset;
}
}
public getOpaqueTarget(): Nullable<Texture> {
return this._opaqueRenderTarget;
}
private shouldRenderAsTransmission(material: Nullable<Material>): boolean {
if (!material) {
return false;
}
if (material instanceof PBRMaterial && (material.subSurface.isRefractionEnabled)) {
return true;
}
return false;
}
private _addMesh(mesh: AbstractMesh): void {
this._materialObservers[mesh.uniqueId] = mesh.onMaterialChangedObservable.add(this._onMeshMaterialChanged.bind(this));
// we need to defer the processing because _addMesh may be called as part as an instance mesh creation, in which case some
// internal properties are not setup yet, like _sourceMesh (needed when doing mesh.material below)
Tools.SetImmediate(() => {
if (this.shouldRenderAsTransmission(mesh.material)) {
(mesh.material as PBRMaterial).refractionTexture = this._opaqueRenderTarget;
this._transparentMeshesCache.push(mesh);
} else {
this._opaqueMeshesCache.push(mesh);
}
});
}
private _removeMesh(mesh: AbstractMesh): void {
mesh.onMaterialChangedObservable.remove(this._materialObservers[mesh.uniqueId]);
delete this._materialObservers[mesh.uniqueId];
let idx = this._transparentMeshesCache.indexOf(mesh);
if (idx !== -1) {
this._transparentMeshesCache.splice(idx, 1);
}
idx = this._opaqueMeshesCache.indexOf(mesh);
if (idx !== -1) {
this._opaqueMeshesCache.splice(idx, 1);
}
}
private _parseScene(): void {
this._scene.meshes.forEach(this._addMesh.bind(this));
// Listen for when a mesh is added to the scene and add it to our cache lists.
this._scene.onNewMeshAddedObservable.add(this._addMesh.bind(this));
// Listen for when a mesh is removed from to the scene and remove it from our cache lists.
this._scene.onMeshRemovedObservable.add(this._removeMesh.bind(this));
}
// When one of the meshes in the scene has its material changed, make sure that it's in the correct cache list.
private _onMeshMaterialChanged(mesh: AbstractMesh) {
const transparentIdx = this._transparentMeshesCache.indexOf(mesh);
const opaqueIdx = this._opaqueMeshesCache.indexOf(mesh);
// If the material is transparent, make sure that it's added to the transparent list and removed from the opaque list
const useTransmission = this.shouldRenderAsTransmission(mesh.material);
if (useTransmission) {
if (mesh.material instanceof PBRMaterial) {
mesh.material.subSurface.refractionTexture = this._opaqueRenderTarget;
}
if (opaqueIdx !== -1) {
this._opaqueMeshesCache.splice(opaqueIdx, 1);
this._transparentMeshesCache.push(mesh);
} else if (transparentIdx === -1) {
this._transparentMeshesCache.push(mesh);
}
// If the material is opaque, make sure that it's added to the opaque list and removed from the transparent list
} else {
if (transparentIdx !== -1) {
this._transparentMeshesCache.splice(transparentIdx, 1);
this._opaqueMeshesCache.push(mesh);
} else if (opaqueIdx === -1) {
this._opaqueMeshesCache.push(mesh);
}
}
}
/**
* Setup the render targets according to the specified options.
*/
private _setupRenderTargets(): void {
if (this._opaqueRenderTarget) {
this._opaqueRenderTarget.dispose();
}
this._opaqueRenderTarget = new RenderTargetTexture("opaqueSceneTexture", this._options.renderSize, this._scene, this._options.generateMipmaps, undefined, this._options.renderTargetTextureType);
this._opaqueRenderTarget.ignoreCameraViewport = true;
this._opaqueRenderTarget.renderList = this._opaqueMeshesCache;
this._opaqueRenderTarget.clearColor = this._options.clearColor?.clone() ?? this._scene.clearColor.clone();
this._opaqueRenderTarget.gammaSpace = false;
this._opaqueRenderTarget.lodGenerationScale = this._options.lodGenerationScale;
this._opaqueRenderTarget.lodGenerationOffset = this._options.lodGenerationOffset;
this._opaqueRenderTarget.samples = this._options.samples;
let sceneImageProcessingapplyByPostProcess: boolean;
let saveSceneEnvIntensity: number;
this._opaqueRenderTarget.onBeforeBindObservable.add((opaqueRenderTarget) => {
saveSceneEnvIntensity = this._scene.environmentIntensity;
this._scene.environmentIntensity = 1.0;
sceneImageProcessingapplyByPostProcess = this._scene.imageProcessingConfiguration.applyByPostProcess;
if (!this._options.clearColor) {
this._scene.clearColor.toLinearSpaceToRef(opaqueRenderTarget.clearColor);
} else {
opaqueRenderTarget.clearColor.copyFrom(this._options.clearColor);
}
this._scene.imageProcessingConfiguration.applyByPostProcess = true;
});
this._opaqueRenderTarget.onAfterUnbindObservable.add(() => {
this._scene.environmentIntensity = saveSceneEnvIntensity;
this._scene.imageProcessingConfiguration.applyByPostProcess = sceneImageProcessingapplyByPostProcess;
});
this._transparentMeshesCache.forEach((mesh: AbstractMesh) => {
if (this.shouldRenderAsTransmission(mesh.material)) {
(mesh.material as PBRMaterial).refractionTexture = this._opaqueRenderTarget;
}
});
}
/**
* Dispose all the elements created by the Helper.
*/
public dispose(): void {
this._scene._transmissionHelper = undefined;
if (this._opaqueRenderTarget) {
this._opaqueRenderTarget.dispose();
this._opaqueRenderTarget = null;
}
this._transparentMeshesCache = [];
this._opaqueMeshesCache = [];
}
}
const NAME = "KHR_materials_transmission";
/**
* [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_transmission/README.md)
*/
export class KHR_materials_transmission implements IGLTFLoaderExtension {
/**
* The name of this extension.
*/
public readonly name = NAME;
/**
* Defines whether this extension is enabled.
*/
public enabled: boolean;
/**
* Defines a number that determines the order the extensions are applied.
*/
public order = 175;
private _loader: GLTFLoader;
/** @hidden */
constructor(loader: GLTFLoader) {
this._loader = loader;
this.enabled = this._loader.isExtensionUsed(NAME);
if (this.enabled) {
loader.parent.transparencyAsCoverage = true;
}
}
/** @hidden */
public dispose() {
(this._loader as any) = null;
}
/** @hidden */
public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
return GLTFLoader.LoadExtensionAsync<IKHRMaterialsTransmission>(context, material, this.name, (extensionContext, extension) => {
const promises = new Array<Promise<any>>();
promises.push(this._loader.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
promises.push(this._loadTransparentPropertiesAsync(extensionContext, material, babylonMaterial, extension));
return Promise.all(promises).then(() => { });
});
}
private _loadTransparentPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, extension: IKHRMaterialsTransmission): Promise<void> {
if (!(babylonMaterial instanceof PBRMaterial)) {
throw new Error(`${context}: Material type not supported`);
}
let pbrMaterial = babylonMaterial as PBRMaterial;
// Enables "refraction" texture which represents transmitted light.
pbrMaterial.subSurface.isRefractionEnabled = true;
// Since this extension models thin-surface transmission only, we must make IOR = 1.0
pbrMaterial.subSurface.volumeIndexOfRefraction = 1.0;
// Albedo colour will tint transmission.
pbrMaterial.subSurface.useAlbedoToTintRefraction = true;
if (extension.transmissionFactor !== undefined) {
pbrMaterial.subSurface.refractionIntensity = extension.transmissionFactor;
const scene = pbrMaterial.getScene() as unknown as ITransmissionHelperHolder;
if (pbrMaterial.subSurface.refractionIntensity && !scene._transmissionHelper) {
new TransmissionHelper({}, pbrMaterial.getScene());
}
} else {
pbrMaterial.subSurface.refractionIntensity = 0.0;
pbrMaterial.subSurface.isRefractionEnabled = false;
return Promise.resolve();
}
pbrMaterial.subSurface.minimumThickness = 0.0;
pbrMaterial.subSurface.maximumThickness = 0.0;
if (extension.transmissionTexture) {
(extension.transmissionTexture as ITextureInfo).nonColorData = true;
return this._loader.loadTextureInfoAsync(`${context}/transmissionTexture`, extension.transmissionTexture, undefined)
.then((texture: BaseTexture) => {
pbrMaterial.subSurface.refractionIntensityTexture = texture;
pbrMaterial.subSurface.useGltfStyleTextures = true;
});
} else {
return Promise.resolve();
}
}
}
GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_transmission(loader));