KHR_lights_punctual.ts 4.0 KB

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