KHR_lights_punctual.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Vector3 } from "@babylonjs/core/Maths/math.vector.js";
  2. import { Color3 } from "@babylonjs/core/Maths/math.color.js";
  3. import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight.js";
  4. import { PointLight } from "@babylonjs/core/Lights/pointLight.js";
  5. import { SpotLight } from "@babylonjs/core/Lights/spotLight.js";
  6. import { Light } from "@babylonjs/core/Lights/light.js";
  7. import { GLTFLoader, ArrayItem } from "../glTFLoader.js";
  8. const NAME = "KHR_lights_punctual";
  9. /**
  10. * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/README.md)
  11. */
  12. // eslint-disable-next-line @typescript-eslint/naming-convention
  13. export class KHR_lights {
  14. /**
  15. * @internal
  16. */
  17. constructor(loader) {
  18. /**
  19. * The name of this extension.
  20. */
  21. this.name = NAME;
  22. this._loader = loader;
  23. this.enabled = this._loader.isExtensionUsed(NAME);
  24. }
  25. /** @internal */
  26. dispose() {
  27. this._loader = null;
  28. delete this._lights;
  29. }
  30. /** @internal */
  31. onLoading() {
  32. const extensions = this._loader.gltf.extensions;
  33. if (extensions && extensions[this.name]) {
  34. const extension = extensions[this.name];
  35. this._lights = extension.lights;
  36. ArrayItem.Assign(this._lights);
  37. }
  38. }
  39. /**
  40. * @internal
  41. */
  42. loadNodeAsync(context, node, assign) {
  43. return GLTFLoader.LoadExtensionAsync(context, node, this.name, (extensionContext, extension) => {
  44. this._loader._allMaterialsDirtyRequired = true;
  45. return this._loader.loadNodeAsync(context, node, (babylonMesh) => {
  46. let babylonLight;
  47. const light = ArrayItem.Get(extensionContext, this._lights, extension.light);
  48. const name = light.name || babylonMesh.name;
  49. this._loader.babylonScene._blockEntityCollection = !!this._loader._assetContainer;
  50. switch (light.type) {
  51. case "directional" /* KHRLightsPunctual_LightType.DIRECTIONAL */: {
  52. const babylonDirectionalLight = new DirectionalLight(name, Vector3.Backward(), this._loader.babylonScene);
  53. babylonDirectionalLight.position.setAll(0);
  54. babylonLight = babylonDirectionalLight;
  55. break;
  56. }
  57. case "point" /* KHRLightsPunctual_LightType.POINT */: {
  58. babylonLight = new PointLight(name, Vector3.Zero(), this._loader.babylonScene);
  59. break;
  60. }
  61. case "spot" /* KHRLightsPunctual_LightType.SPOT */: {
  62. const babylonSpotLight = new SpotLight(name, Vector3.Zero(), Vector3.Backward(), 0, 1, this._loader.babylonScene);
  63. babylonSpotLight.angle = ((light.spot && light.spot.outerConeAngle) || Math.PI / 4) * 2;
  64. babylonSpotLight.innerAngle = ((light.spot && light.spot.innerConeAngle) || 0) * 2;
  65. babylonLight = babylonSpotLight;
  66. break;
  67. }
  68. default: {
  69. this._loader.babylonScene._blockEntityCollection = false;
  70. throw new Error(`${extensionContext}: Invalid light type (${light.type})`);
  71. }
  72. }
  73. babylonLight._parentContainer = this._loader._assetContainer;
  74. this._loader.babylonScene._blockEntityCollection = false;
  75. light._babylonLight = babylonLight;
  76. babylonLight.falloffType = Light.FALLOFF_GLTF;
  77. babylonLight.diffuse = light.color ? Color3.FromArray(light.color) : Color3.White();
  78. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  79. babylonLight.range = light.range == undefined ? Number.MAX_VALUE : light.range;
  80. babylonLight.parent = babylonMesh;
  81. this._loader._babylonLights.push(babylonLight);
  82. GLTFLoader.AddPointerMetadata(babylonLight, extensionContext);
  83. assign(babylonMesh);
  84. });
  85. });
  86. }
  87. }
  88. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_lights(loader));
  89. //# sourceMappingURL=KHR_lights_punctual.js.map