glTFMaterialsCommonExtension.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { GLTFLoaderExtension, GLTFLoaderBase, GLTFLoader } from "./glTFLoader.js";
  2. import { Vector3 } from "@babylonjs/core/Maths/math.vector.js";
  3. import { Color3 } from "@babylonjs/core/Maths/math.color.js";
  4. import { Tools } from "@babylonjs/core/Misc/tools.js";
  5. import { Material } from "@babylonjs/core/Materials/material.js";
  6. import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial.js";
  7. import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight.js";
  8. import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight.js";
  9. import { PointLight } from "@babylonjs/core/Lights/pointLight.js";
  10. import { SpotLight } from "@babylonjs/core/Lights/spotLight.js";
  11. /**
  12. * @internal
  13. * @deprecated
  14. */
  15. export class GLTFMaterialsCommonExtension extends GLTFLoaderExtension {
  16. constructor() {
  17. super("KHR_materials_common");
  18. }
  19. loadRuntimeExtensionsAsync(gltfRuntime) {
  20. if (!gltfRuntime.extensions) {
  21. return false;
  22. }
  23. const extension = gltfRuntime.extensions[this.name];
  24. if (!extension) {
  25. return false;
  26. }
  27. // Create lights
  28. const lights = extension.lights;
  29. if (lights) {
  30. for (const thing in lights) {
  31. const light = lights[thing];
  32. switch (light.type) {
  33. case "ambient": {
  34. const ambientLight = new HemisphericLight(light.name, new Vector3(0, 1, 0), gltfRuntime.scene);
  35. const ambient = light.ambient;
  36. if (ambient) {
  37. ambientLight.diffuse = Color3.FromArray(ambient.color || [1, 1, 1]);
  38. }
  39. break;
  40. }
  41. case "point": {
  42. const pointLight = new PointLight(light.name, new Vector3(10, 10, 10), gltfRuntime.scene);
  43. const point = light.point;
  44. if (point) {
  45. pointLight.diffuse = Color3.FromArray(point.color || [1, 1, 1]);
  46. }
  47. break;
  48. }
  49. case "directional": {
  50. const dirLight = new DirectionalLight(light.name, new Vector3(0, -1, 0), gltfRuntime.scene);
  51. const directional = light.directional;
  52. if (directional) {
  53. dirLight.diffuse = Color3.FromArray(directional.color || [1, 1, 1]);
  54. }
  55. break;
  56. }
  57. case "spot": {
  58. const spot = light.spot;
  59. if (spot) {
  60. const spotLight = new SpotLight(light.name, new Vector3(0, 10, 0), new Vector3(0, -1, 0), spot.fallOffAngle || Math.PI, spot.fallOffExponent || 0.0, gltfRuntime.scene);
  61. spotLight.diffuse = Color3.FromArray(spot.color || [1, 1, 1]);
  62. }
  63. break;
  64. }
  65. default:
  66. Tools.Warn('GLTF Material Common extension: light type "' + light.type + "” not supported");
  67. break;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. loadMaterialAsync(gltfRuntime, id, onSuccess, onError) {
  74. const material = gltfRuntime.materials[id];
  75. if (!material || !material.extensions) {
  76. return false;
  77. }
  78. const extension = material.extensions[this.name];
  79. if (!extension) {
  80. return false;
  81. }
  82. const standardMaterial = new StandardMaterial(id, gltfRuntime.scene);
  83. standardMaterial.sideOrientation = Material.CounterClockWiseSideOrientation;
  84. if (extension.technique === "CONSTANT") {
  85. standardMaterial.disableLighting = true;
  86. }
  87. standardMaterial.backFaceCulling = extension.doubleSided === undefined ? false : !extension.doubleSided;
  88. standardMaterial.alpha = extension.values.transparency === undefined ? 1.0 : extension.values.transparency;
  89. standardMaterial.specularPower = extension.values.shininess === undefined ? 0.0 : extension.values.shininess;
  90. // Ambient
  91. if (typeof extension.values.ambient === "string") {
  92. this._loadTexture(gltfRuntime, extension.values.ambient, standardMaterial, "ambientTexture", onError);
  93. }
  94. else {
  95. standardMaterial.ambientColor = Color3.FromArray(extension.values.ambient || [0, 0, 0]);
  96. }
  97. // Diffuse
  98. if (typeof extension.values.diffuse === "string") {
  99. this._loadTexture(gltfRuntime, extension.values.diffuse, standardMaterial, "diffuseTexture", onError);
  100. }
  101. else {
  102. standardMaterial.diffuseColor = Color3.FromArray(extension.values.diffuse || [0, 0, 0]);
  103. }
  104. // Emission
  105. if (typeof extension.values.emission === "string") {
  106. this._loadTexture(gltfRuntime, extension.values.emission, standardMaterial, "emissiveTexture", onError);
  107. }
  108. else {
  109. standardMaterial.emissiveColor = Color3.FromArray(extension.values.emission || [0, 0, 0]);
  110. }
  111. // Specular
  112. if (typeof extension.values.specular === "string") {
  113. this._loadTexture(gltfRuntime, extension.values.specular, standardMaterial, "specularTexture", onError);
  114. }
  115. else {
  116. standardMaterial.specularColor = Color3.FromArray(extension.values.specular || [0, 0, 0]);
  117. }
  118. return true;
  119. }
  120. _loadTexture(gltfRuntime, id, material, propertyPath, onError) {
  121. // Create buffer from texture url
  122. GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, (buffer) => {
  123. // Create texture from buffer
  124. GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, (texture) => (material[propertyPath] = texture));
  125. }, onError);
  126. }
  127. }
  128. GLTFLoader.RegisterExtension(new GLTFMaterialsCommonExtension());
  129. //# sourceMappingURL=glTFMaterialsCommonExtension.js.map