KHR_materials_pbrSpecularGlossiness.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GLTF2.Extensions {
  3. interface IKHRMaterialsPbrSpecularGlossiness {
  4. diffuseFactor: number[];
  5. diffuseTexture: IGLTFTextureInfo;
  6. specularFactor: number[];
  7. glossinessFactor: number;
  8. specularGlossinessTexture: IGLTFTextureInfo;
  9. }
  10. export class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  11. public get name(): string {
  12. return "KHR_materials_pbrSpecularGlossiness";
  13. }
  14. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
  15. return this._loadExtension<IKHRMaterialsPbrSpecularGlossiness>(material, (extension, onComplete) => {
  16. loader._createPbrMaterial(material);
  17. loader._loadMaterialBaseProperties(context, material);
  18. this._loadSpecularGlossinessProperties(loader, context, material, extension);
  19. if (material.babylonMaterial) {
  20. assign(material.babylonMaterial, true);
  21. }
  22. });
  23. }
  24. private _loadSpecularGlossinessProperties(loader: GLTFLoader, context: string, material: IGLTFMaterial, properties: IKHRMaterialsPbrSpecularGlossiness): void {
  25. const babylonMaterial = material.babylonMaterial as PBRMaterial;
  26. babylonMaterial.albedoColor = properties.diffuseFactor ? Color3.FromArray(properties.diffuseFactor) : new Color3(1, 1, 1);
  27. babylonMaterial.reflectivityColor = properties.specularFactor ? Color3.FromArray(properties.specularFactor) : new Color3(1, 1, 1);
  28. babylonMaterial.microSurface = properties.glossinessFactor == null ? 1 : properties.glossinessFactor;
  29. if (loader._gltf.textures) {
  30. if (properties.diffuseTexture) {
  31. const texture = GLTFUtils.GetArrayItem(loader._gltf.textures, properties.diffuseTexture.index);
  32. if (!texture) {
  33. throw new Error(context + ": Failed to find diffuse texture " + properties.diffuseTexture.index);
  34. }
  35. if (properties.diffuseTexture.texCoord) {
  36. babylonMaterial.albedoTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.diffuseTexture.texCoord);
  37. }
  38. }
  39. if (properties.specularGlossinessTexture) {
  40. const texture = GLTFUtils.GetArrayItem(loader._gltf.textures, properties.specularGlossinessTexture.index);
  41. if (!texture) {
  42. throw new Error(context + ": Failed to find diffuse texture " + properties.specularGlossinessTexture.index);
  43. }
  44. if (properties.specularGlossinessTexture.texCoord) {
  45. babylonMaterial.reflectivityTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.specularGlossinessTexture.texCoord);
  46. }
  47. babylonMaterial.reflectivityTexture.hasAlpha = true;
  48. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  49. }
  50. }
  51. loader._loadMaterialAlphaProperties(context, material, properties.diffuseFactor);
  52. }
  53. }
  54. GLTFLoader.RegisterExtension(new KHRMaterialsPbrSpecularGlossiness());
  55. }