KHR_materials_sheen.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Nullable } from "babylonjs/types";
  2. import { PBRMaterial } from "babylonjs/Materials/PBR/pbrMaterial";
  3. import { Material } from "babylonjs/Materials/material";
  4. import { IMaterial } from "../glTFLoaderInterfaces";
  5. import { IGLTFLoaderExtension } from "../glTFLoaderExtension";
  6. import { GLTFLoader } from "../glTFLoader";
  7. import { Color3 } from 'babylonjs/Maths/math.color';
  8. import { IKHRMaterialsSheen } from 'babylonjs-gltf2interface';
  9. const NAME = "KHR_materials_sheen";
  10. /**
  11. * [Proposed Specification](https://github.com/KhronosGroup/glTF/pull/1688)
  12. * [Playground Sample](https://www.babylonjs-playground.com/frame.html#BNIZX6#4)
  13. * !!! Experimental Extension Subject to Changes !!!
  14. */
  15. export class KHR_materials_sheen implements IGLTFLoaderExtension {
  16. /**
  17. * The name of this extension.
  18. */
  19. public readonly name = NAME;
  20. /**
  21. * Defines whether this extension is enabled.
  22. */
  23. public enabled: boolean;
  24. /**
  25. * Defines a number that determines the order the extensions are applied.
  26. */
  27. public order = 190;
  28. private _loader: GLTFLoader;
  29. /** @hidden */
  30. constructor(loader: GLTFLoader) {
  31. this._loader = loader;
  32. this.enabled = this._loader.isExtensionUsed(NAME);
  33. }
  34. /** @hidden */
  35. public dispose() {
  36. (this._loader as any) = null;
  37. }
  38. /** @hidden */
  39. public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
  40. return GLTFLoader.LoadExtensionAsync<IKHRMaterialsSheen>(context, material, this.name, (extensionContext, extension) => {
  41. const promises = new Array<Promise<any>>();
  42. promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
  43. promises.push(this._loadSheenPropertiesAsync(extensionContext, extension, babylonMaterial));
  44. return Promise.all(promises).then(() => { });
  45. });
  46. }
  47. private _loadSheenPropertiesAsync(context: string, properties: IKHRMaterialsSheen, babylonMaterial: Material): Promise<void> {
  48. if (!(babylonMaterial instanceof PBRMaterial)) {
  49. throw new Error(`${context}: Material type not supported`);
  50. }
  51. const promises = new Array<Promise<any>>();
  52. babylonMaterial.sheen.isEnabled = true;
  53. babylonMaterial.sheen.intensity = 1;
  54. if (properties.sheenColorFactor != undefined) {
  55. babylonMaterial.sheen.color = Color3.FromArray(properties.sheenColorFactor);
  56. }
  57. else {
  58. babylonMaterial.sheen.color = Color3.Black();
  59. }
  60. if (properties.sheenColorTexture) {
  61. promises.push(this._loader.loadTextureInfoAsync(`${context}/sheenColorTexture`, properties.sheenColorTexture, (texture) => {
  62. texture.name = `${babylonMaterial.name} (Sheen Color)`;
  63. babylonMaterial.sheen.texture = texture;
  64. }));
  65. }
  66. if (properties.sheenRoughnessFactor !== undefined) {
  67. babylonMaterial.sheen.roughness = properties.sheenRoughnessFactor;
  68. } else {
  69. babylonMaterial.sheen.roughness = 0;
  70. }
  71. if (properties.sheenRoughnessTexture) {
  72. promises.push(this._loader.loadTextureInfoAsync(`${context}/sheenRoughnessTexture`, properties.sheenRoughnessTexture, (texture) => {
  73. texture.name = `${babylonMaterial.name} (Sheen Roughness)`;
  74. babylonMaterial.sheen.textureRoughness = texture;
  75. }));
  76. }
  77. babylonMaterial.sheen.albedoScaling = true;
  78. babylonMaterial.sheen.useRoughnessFromMainTexture = false;
  79. return Promise.all(promises).then(() => { });
  80. }
  81. }
  82. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_sheen(loader));