KHR_materials_sheen.js 3.3 KB

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