KHR_materials_unlit.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GLTF2.Extensions {
  3. const NAME = "KHR_materials_unlit";
  4. /**
  5. * [Specification](https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit)
  6. */
  7. export class KHR_materials_unlit extends GLTFLoaderExtension {
  8. public readonly name = NAME;
  9. protected _loadMaterialPropertiesAsync(context: string, material: _ILoaderMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
  10. return this._loadExtensionAsync<{}>(context, material, () => {
  11. return this._loadUnlitPropertiesAsync(context, material, babylonMaterial as PBRMaterial);
  12. });
  13. }
  14. private _loadUnlitPropertiesAsync(context: string, material: _ILoaderMaterial, babylonMaterial: PBRMaterial): Promise<void> {
  15. const promises = new Array<Promise<void>>();
  16. babylonMaterial.unlit = true;
  17. // Ensure metallic workflow
  18. babylonMaterial.metallic = 1;
  19. babylonMaterial.roughness = 1;
  20. const properties = material.pbrMetallicRoughness;
  21. if (properties) {
  22. if (properties.baseColorFactor) {
  23. babylonMaterial.albedoColor = Color3.FromArray(properties.baseColorFactor);
  24. babylonMaterial.alpha = properties.baseColorFactor[3];
  25. }
  26. else {
  27. babylonMaterial.albedoColor = Color3.White();
  28. }
  29. if (properties.baseColorTexture) {
  30. promises.push(this._loader._loadTextureAsync(`${context}/baseColorTexture`, properties.baseColorTexture, texture => {
  31. babylonMaterial.albedoTexture = texture;
  32. }));
  33. }
  34. }
  35. if (material.doubleSided) {
  36. babylonMaterial.backFaceCulling = false;
  37. babylonMaterial.twoSidedLighting = true;
  38. }
  39. this._loader._loadMaterialAlphaProperties(context, material, babylonMaterial);
  40. return Promise.all(promises).then(() => {});
  41. }
  42. }
  43. GLTFLoader._Register(NAME, loader => new KHR_materials_unlit(loader));
  44. }