|
1 | 1 | import type { Nullable } from "core/types";
|
2 |
| -import type { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; |
3 |
| -import type { OpenPBRMaterial } from "core/Materials/PBR/openPbrMaterial"; |
| 2 | +import { PBRMaterial } from "core/Materials/PBR/pbrMaterial"; |
4 | 3 | import type { Material } from "core/Materials/material";
|
5 | 4 | import type { BaseTexture } from "core/Materials/Textures/baseTexture";
|
6 | 5 | import type { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
|
@@ -63,8 +62,6 @@ interface ITransmissionHelperOptions {
|
63 | 62 | clearColor?: Color4;
|
64 | 63 | }
|
65 | 64 |
|
66 |
| -let PBRMaterialClass: typeof PBRMaterial | typeof OpenPBRMaterial; |
67 |
| - |
68 | 65 | /**
|
69 | 66 | * A class to handle setting up the rendering of opaque objects to be shown through transmissive objects.
|
70 | 67 | */
|
@@ -169,7 +166,7 @@ class TransmissionHelper {
|
169 | 166 | if (!material) {
|
170 | 167 | return false;
|
171 | 168 | }
|
172 |
| - if (material instanceof PBRMaterialClass && (material as any).subSurface.isRefractionEnabled) { |
| 169 | + if (material instanceof PBRMaterial && material.subSurface.isRefractionEnabled) { |
173 | 170 | return true;
|
174 | 171 | }
|
175 | 172 | return false;
|
@@ -223,8 +220,8 @@ class TransmissionHelper {
|
223 | 220 | // If the material is transparent, make sure that it's added to the transparent list and removed from the opaque list
|
224 | 221 | const useTransmission = this._shouldRenderAsTransmission(mesh.material);
|
225 | 222 | if (useTransmission) {
|
226 |
| - if (mesh.material instanceof PBRMaterialClass) { |
227 |
| - (mesh.material as any).subSurface.refractionTexture = this._opaqueRenderTarget; |
| 223 | + if (mesh.material instanceof PBRMaterial) { |
| 224 | + mesh.material.subSurface.refractionTexture = this._opaqueRenderTarget; |
228 | 225 | }
|
229 | 226 | if (opaqueIdx !== -1) {
|
230 | 227 | this._opaqueMeshesCache.splice(opaqueIdx, 1);
|
@@ -369,87 +366,60 @@ export class KHR_materials_transmission implements IGLTFLoaderExtension {
|
369 | 366 | * @internal
|
370 | 367 | */
|
371 | 368 | // eslint-disable-next-line no-restricted-syntax
|
372 |
| - public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, useOpenPBR: boolean = false): Nullable<Promise<void>> { |
| 369 | + public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> { |
373 | 370 | return GLTFLoader.LoadExtensionAsync<IKHRMaterialsTransmission>(context, material, this.name, async (extensionContext, extension) => {
|
374 |
| - if (useOpenPBR) { |
375 |
| - const mod = await import("core/Materials/PBR/openPbrMaterial"); |
376 |
| - PBRMaterialClass = mod.OpenPBRMaterial; |
377 |
| - } else { |
378 |
| - const mod = await import("core/Materials/PBR/pbrMaterial"); |
379 |
| - PBRMaterialClass = mod.PBRMaterial; |
380 |
| - } |
381 | 371 | const promises = new Array<Promise<any>>();
|
382 | 372 | promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
|
383 |
| - promises.push(this._loadTransparentPropertiesAsync(extensionContext, material, babylonMaterial, extension, useOpenPBR)); |
| 373 | + promises.push(this._loadTransparentPropertiesAsync(extensionContext, material, babylonMaterial, extension)); |
384 | 374 | // eslint-disable-next-line github/no-then
|
385 | 375 | return await Promise.all(promises).then(() => {});
|
386 | 376 | });
|
387 | 377 | }
|
388 | 378 |
|
389 | 379 | // eslint-disable-next-line no-restricted-syntax, @typescript-eslint/promise-function-async
|
390 |
| - private _loadTransparentPropertiesAsync( |
391 |
| - context: string, |
392 |
| - material: IMaterial, |
393 |
| - babylonMaterial: Material, |
394 |
| - extension: IKHRMaterialsTransmission, |
395 |
| - useOpenPBR: boolean |
396 |
| - ): Promise<void> { |
397 |
| - if (!(babylonMaterial instanceof PBRMaterialClass)) { |
| 380 | + private _loadTransparentPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material, extension: IKHRMaterialsTransmission): Promise<void> { |
| 381 | + if (!(babylonMaterial instanceof PBRMaterial)) { |
398 | 382 | throw new Error(`${context}: Material type not supported`);
|
399 | 383 | }
|
| 384 | + const pbrMaterial = babylonMaterial; |
| 385 | + |
| 386 | + // Enables "refraction" texture which represents transmitted light. |
| 387 | + pbrMaterial.subSurface.isRefractionEnabled = true; |
| 388 | + |
| 389 | + // Since this extension models thin-surface transmission only, we must make IOR = 1.0 |
| 390 | + pbrMaterial.subSurface.volumeIndexOfRefraction = 1.0; |
400 | 391 |
|
401 |
| - let transmissionWeight = 0.0; |
402 |
| - let transmissionWeightTexture: Nullable<BaseTexture> = null; |
| 392 | + // Albedo colour will tint transmission. |
| 393 | + pbrMaterial.subSurface.useAlbedoToTintRefraction = true; |
403 | 394 |
|
404 |
| - const promises = new Array<Promise<any>>(); |
405 | 395 | if (extension.transmissionFactor !== undefined) {
|
406 |
| - transmissionWeight = extension.transmissionFactor; |
| 396 | + pbrMaterial.subSurface.refractionIntensity = extension.transmissionFactor; |
| 397 | + const scene = pbrMaterial.getScene() as unknown as ITransmissionHelperHolder; |
| 398 | + if (pbrMaterial.subSurface.refractionIntensity && !scene._transmissionHelper) { |
| 399 | + new TransmissionHelper({}, pbrMaterial.getScene()); |
| 400 | + } else if (pbrMaterial.subSurface.refractionIntensity && !scene._transmissionHelper?._isRenderTargetValid()) { |
| 401 | + // If the render target is not valid, recreate it. |
| 402 | + scene._transmissionHelper?._setupRenderTargets(); |
| 403 | + } |
407 | 404 | } else {
|
| 405 | + pbrMaterial.subSurface.refractionIntensity = 0.0; |
| 406 | + pbrMaterial.subSurface.isRefractionEnabled = false; |
408 | 407 | return Promise.resolve();
|
409 | 408 | }
|
| 409 | + |
| 410 | + pbrMaterial.subSurface.minimumThickness = 0.0; |
| 411 | + pbrMaterial.subSurface.maximumThickness = 0.0; |
410 | 412 | if (extension.transmissionTexture) {
|
411 | 413 | (extension.transmissionTexture as ITextureInfo).nonColorData = true;
|
412 | 414 | // eslint-disable-next-line github/no-then
|
413 |
| - promises.push( |
414 |
| - this._loader.loadTextureInfoAsync(`${context}/transmissionTexture`, extension.transmissionTexture, (texture: BaseTexture) => { |
415 |
| - texture.name = `${babylonMaterial.name} (Transmission)`; |
416 |
| - transmissionWeightTexture = texture; |
417 |
| - }) |
418 |
| - ); |
| 415 | + return this._loader.loadTextureInfoAsync(`${context}/transmissionTexture`, extension.transmissionTexture, undefined).then((texture: BaseTexture) => { |
| 416 | + texture.name = `${babylonMaterial.name} (Transmission)`; |
| 417 | + pbrMaterial.subSurface.refractionIntensityTexture = texture; |
| 418 | + pbrMaterial.subSurface.useGltfStyleTextures = true; |
| 419 | + }); |
| 420 | + } else { |
| 421 | + return Promise.resolve(); |
419 | 422 | }
|
420 |
| - |
421 |
| - // eslint-disable-next-line github/no-then |
422 |
| - return Promise.all(promises).then(() => { |
423 |
| - if (useOpenPBR) { |
424 |
| - return; |
425 |
| - } |
426 |
| - const pbrMaterial = babylonMaterial as PBRMaterial; |
427 |
| - |
428 |
| - // Enables "refraction" texture which represents transmitted light. |
429 |
| - pbrMaterial.subSurface.isRefractionEnabled = transmissionWeight !== 0; |
430 |
| - |
431 |
| - // Since this extension models thin-surface transmission only, we must make IOR = 1.0 |
432 |
| - pbrMaterial.subSurface.volumeIndexOfRefraction = 1.0; |
433 |
| - |
434 |
| - // Albedo colour will tint transmission. |
435 |
| - pbrMaterial.subSurface.useAlbedoToTintRefraction = true; |
436 |
| - |
437 |
| - pbrMaterial.subSurface.refractionIntensity = transmissionWeight; |
438 |
| - |
439 |
| - if (transmissionWeight) { |
440 |
| - const scene = pbrMaterial.getScene() as unknown as ITransmissionHelperHolder; |
441 |
| - if (pbrMaterial.subSurface.refractionIntensity && !scene._transmissionHelper) { |
442 |
| - new TransmissionHelper({}, pbrMaterial.getScene()); |
443 |
| - } else if (pbrMaterial.subSurface.refractionIntensity && !scene._transmissionHelper?._isRenderTargetValid()) { |
444 |
| - // If the render target is not valid, recreate it. |
445 |
| - scene._transmissionHelper?._setupRenderTargets(); |
446 |
| - } |
447 |
| - } |
448 |
| - pbrMaterial.subSurface.minimumThickness = 0.0; |
449 |
| - pbrMaterial.subSurface.maximumThickness = 0.0; |
450 |
| - pbrMaterial.subSurface.refractionIntensityTexture = transmissionWeightTexture; |
451 |
| - pbrMaterial.subSurface.useGltfStyleTextures = true; |
452 |
| - }); |
453 | 423 | }
|
454 | 424 | }
|
455 | 425 |
|
|
0 commit comments