KHR_lights_punctual.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Nullable } from "babylonjs/types";
  2. import { Vector3 } from "babylonjs/Maths/math.vector";
  3. import { Color3 } from 'babylonjs/Maths/math.color';
  4. import { DirectionalLight } from "babylonjs/Lights/directionalLight";
  5. import { PointLight } from "babylonjs/Lights/pointLight";
  6. import { SpotLight } from "babylonjs/Lights/spotLight";
  7. import { Light } from "babylonjs/Lights/light";
  8. import { TransformNode } from "babylonjs/Meshes/transformNode";
  9. import { IChildRootProperty } from "babylonjs-gltf2interface";
  10. import { INode } from "../glTFLoaderInterfaces";
  11. import { IGLTFLoaderExtension } from "../glTFLoaderExtension";
  12. import { GLTFLoader, ArrayItem } from "../glTFLoader";
  13. const NAME = "KHR_lights_punctual";
  14. /** @hidden */
  15. export enum LightType {
  16. DIRECTIONAL = "directional",
  17. POINT = "point",
  18. SPOT = "spot"
  19. }
  20. /** @hidden */
  21. export interface ILightReference {
  22. light: number;
  23. }
  24. /** @hidden */
  25. export interface ILight extends IChildRootProperty {
  26. type: LightType;
  27. color?: number[];
  28. intensity?: number;
  29. range?: number;
  30. spot?: {
  31. innerConeAngle?: number;
  32. outerConeAngle?: number;
  33. };
  34. }
  35. /** @hidden */
  36. export interface ILights {
  37. lights: ILight[];
  38. }
  39. /**
  40. * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_lights_punctual)
  41. */
  42. export class KHR_lights implements IGLTFLoaderExtension {
  43. /**
  44. * The name of this extension.
  45. */
  46. public readonly name = NAME;
  47. /**
  48. * Defines whether this extension is enabled.
  49. */
  50. public enabled: boolean;
  51. private _loader: GLTFLoader;
  52. private _lights?: ILight[];
  53. /** @hidden */
  54. constructor(loader: GLTFLoader) {
  55. this._loader = loader;
  56. this.enabled = this._loader.isExtensionUsed(NAME);
  57. }
  58. /** @hidden */
  59. public dispose() {
  60. delete this._loader;
  61. delete this._lights;
  62. }
  63. /** @hidden */
  64. public onLoading(): void {
  65. const extensions = this._loader.gltf.extensions;
  66. if (extensions && extensions[this.name]) {
  67. const extension = extensions[this.name] as ILights;
  68. this._lights = extension.lights;
  69. }
  70. }
  71. /** @hidden */
  72. public loadNodeAsync(context: string, node: INode, assign: (babylonTransformNode: TransformNode) => void): Nullable<Promise<TransformNode>> {
  73. return GLTFLoader.LoadExtensionAsync<ILightReference, TransformNode>(context, node, this.name, (extensionContext, extension) => {
  74. return this._loader.loadNodeAsync(context, node, (babylonMesh) => {
  75. let babylonLight: Light;
  76. const light = ArrayItem.Get(extensionContext, this._lights, extension.light);
  77. const name = light.name || babylonMesh.name;
  78. this._loader.babylonScene._blockEntityCollection = this._loader._forAssetContainer;
  79. switch (light.type) {
  80. case LightType.DIRECTIONAL: {
  81. babylonLight = new DirectionalLight(name, Vector3.Backward(), this._loader.babylonScene);
  82. break;
  83. }
  84. case LightType.POINT: {
  85. babylonLight = new PointLight(name, Vector3.Zero(), this._loader.babylonScene);
  86. break;
  87. }
  88. case LightType.SPOT: {
  89. const babylonSpotLight = new SpotLight(name, Vector3.Zero(), Vector3.Backward(), 0, 1, this._loader.babylonScene);
  90. babylonSpotLight.angle = ((light.spot && light.spot.outerConeAngle) || Math.PI / 4) * 2;
  91. babylonSpotLight.innerAngle = ((light.spot && light.spot.innerConeAngle) || 0) * 2;
  92. babylonLight = babylonSpotLight;
  93. break;
  94. }
  95. default: {
  96. this._loader.babylonScene._blockEntityCollection = false;
  97. throw new Error(`${extensionContext}: Invalid light type (${light.type})`);
  98. }
  99. }
  100. this._loader.babylonScene._blockEntityCollection = false;
  101. babylonLight.falloffType = Light.FALLOFF_GLTF;
  102. babylonLight.diffuse = light.color ? Color3.FromArray(light.color) : Color3.White();
  103. babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
  104. babylonLight.range = light.range == undefined ? Number.MAX_VALUE : light.range;
  105. babylonLight.parent = babylonMesh;
  106. this._loader._babylonLights.push(babylonLight);
  107. GLTFLoader.AddPointerMetadata(babylonLight, extensionContext);
  108. assign(babylonMesh);
  109. });
  110. });
  111. }
  112. }
  113. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_lights(loader));