KHR_materials_clearcoat.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
  2. import { GLTFLoader } from "../glTFLoader.js";
  3. const NAME = "KHR_materials_clearcoat";
  4. /**
  5. * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_clearcoat/README.md)
  6. * [Playground Sample](https://www.babylonjs-playground.com/frame.html#7F7PN6#8)
  7. */
  8. // eslint-disable-next-line @typescript-eslint/naming-convention
  9. export class KHR_materials_clearcoat {
  10. /**
  11. * @internal
  12. */
  13. constructor(loader) {
  14. /**
  15. * The name of this extension.
  16. */
  17. this.name = NAME;
  18. /**
  19. * Defines a number that determines the order the extensions are applied.
  20. */
  21. this.order = 190;
  22. this._loader = loader;
  23. this.enabled = this._loader.isExtensionUsed(NAME);
  24. }
  25. /** @internal */
  26. dispose() {
  27. this._loader = null;
  28. }
  29. /**
  30. * @internal
  31. */
  32. loadMaterialPropertiesAsync(context, material, babylonMaterial) {
  33. return GLTFLoader.LoadExtensionAsync(context, material, this.name, (extensionContext, extension) => {
  34. const promises = new Array();
  35. promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
  36. promises.push(this._loadClearCoatPropertiesAsync(extensionContext, extension, babylonMaterial));
  37. return Promise.all(promises).then(() => { });
  38. });
  39. }
  40. _loadClearCoatPropertiesAsync(context, properties, babylonMaterial) {
  41. if (!(babylonMaterial instanceof PBRMaterial)) {
  42. throw new Error(`${context}: Material type not supported`);
  43. }
  44. const promises = new Array();
  45. babylonMaterial.clearCoat.isEnabled = true;
  46. babylonMaterial.clearCoat.useRoughnessFromMainTexture = false;
  47. babylonMaterial.clearCoat.remapF0OnInterfaceChange = false;
  48. if (properties.clearcoatFactor != undefined) {
  49. babylonMaterial.clearCoat.intensity = properties.clearcoatFactor;
  50. }
  51. else {
  52. babylonMaterial.clearCoat.intensity = 0;
  53. }
  54. if (properties.clearcoatTexture) {
  55. promises.push(this._loader.loadTextureInfoAsync(`${context}/clearcoatTexture`, properties.clearcoatTexture, (texture) => {
  56. texture.name = `${babylonMaterial.name} (ClearCoat Intensity)`;
  57. babylonMaterial.clearCoat.texture = texture;
  58. }));
  59. }
  60. if (properties.clearcoatRoughnessFactor != undefined) {
  61. babylonMaterial.clearCoat.roughness = properties.clearcoatRoughnessFactor;
  62. }
  63. else {
  64. babylonMaterial.clearCoat.roughness = 0;
  65. }
  66. if (properties.clearcoatRoughnessTexture) {
  67. properties.clearcoatRoughnessTexture.nonColorData = true;
  68. promises.push(this._loader.loadTextureInfoAsync(`${context}/clearcoatRoughnessTexture`, properties.clearcoatRoughnessTexture, (texture) => {
  69. texture.name = `${babylonMaterial.name} (ClearCoat Roughness)`;
  70. babylonMaterial.clearCoat.textureRoughness = texture;
  71. }));
  72. }
  73. if (properties.clearcoatNormalTexture) {
  74. properties.clearcoatNormalTexture.nonColorData = true;
  75. promises.push(this._loader.loadTextureInfoAsync(`${context}/clearcoatNormalTexture`, properties.clearcoatNormalTexture, (texture) => {
  76. texture.name = `${babylonMaterial.name} (ClearCoat Normal)`;
  77. babylonMaterial.clearCoat.bumpTexture = texture;
  78. }));
  79. babylonMaterial.invertNormalMapX = !babylonMaterial.getScene().useRightHandedSystem;
  80. babylonMaterial.invertNormalMapY = babylonMaterial.getScene().useRightHandedSystem;
  81. if (properties.clearcoatNormalTexture.scale != undefined) {
  82. babylonMaterial.clearCoat.bumpTexture.level = properties.clearcoatNormalTexture.scale;
  83. }
  84. }
  85. return Promise.all(promises).then(() => { });
  86. }
  87. }
  88. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_clearcoat(loader));
  89. //# sourceMappingURL=KHR_materials_clearcoat.js.map