KHR_lights_punctual.ts 3.9 KB

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