UNPKG

3.96 kBJavaScriptView Raw
1import { Color3, Vector3 } from "@babylonjs/core/Maths/math";
2import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight";
3import { PointLight } from "@babylonjs/core/Lights/pointLight";
4import { SpotLight } from "@babylonjs/core/Lights/spotLight";
5import { Light } from "@babylonjs/core/Lights/light";
6import { GLTFLoader, ArrayItem } from "../glTFLoader";
7var NAME = "KHR_lights_punctual";
8var LightType;
9(function (LightType) {
10 LightType["DIRECTIONAL"] = "directional";
11 LightType["POINT"] = "point";
12 LightType["SPOT"] = "spot";
13})(LightType || (LightType = {}));
14/**
15 * [Specification](https://github.com/KhronosGroup/glTF/blob/1048d162a44dbcb05aefc1874bfd423cf60135a6/extensions/2.0/Khronos/KHR_lights_punctual/README.md) (Experimental)
16 */
17var KHR_lights = /** @class */ (function () {
18 /** @hidden */
19 function KHR_lights(loader) {
20 /** The name of this extension. */
21 this.name = NAME;
22 /** Defines whether this extension is enabled. */
23 this.enabled = true;
24 this._loader = loader;
25 }
26 /** @hidden */
27 KHR_lights.prototype.dispose = function () {
28 delete this._loader;
29 delete this._lights;
30 };
31 /** @hidden */
32 KHR_lights.prototype.onLoading = function () {
33 var extensions = this._loader.gltf.extensions;
34 if (extensions && extensions[this.name]) {
35 var extension = extensions[this.name];
36 this._lights = extension.lights;
37 }
38 };
39 /** @hidden */
40 KHR_lights.prototype.loadNodeAsync = function (context, node, assign) {
41 var _this = this;
42 return GLTFLoader.LoadExtensionAsync(context, node, this.name, function (extensionContext, extension) {
43 return _this._loader.loadNodeAsync(context, node, function (babylonMesh) {
44 var babylonLight;
45 var light = ArrayItem.Get(extensionContext, _this._lights, extension.light);
46 var name = light.name || babylonMesh.name;
47 switch (light.type) {
48 case LightType.DIRECTIONAL: {
49 babylonLight = new DirectionalLight(name, Vector3.Backward(), _this._loader.babylonScene);
50 break;
51 }
52 case LightType.POINT: {
53 babylonLight = new PointLight(name, Vector3.Zero(), _this._loader.babylonScene);
54 break;
55 }
56 case LightType.SPOT: {
57 var babylonSpotLight = new SpotLight(name, Vector3.Zero(), Vector3.Backward(), 0, 1, _this._loader.babylonScene);
58 babylonSpotLight.angle = ((light.spot && light.spot.outerConeAngle) || Math.PI / 4) * 2;
59 babylonSpotLight.innerAngle = ((light.spot && light.spot.innerConeAngle) || 0) * 2;
60 babylonLight = babylonSpotLight;
61 break;
62 }
63 default: {
64 throw new Error(extensionContext + ": Invalid light type (" + light.type + ")");
65 }
66 }
67 babylonLight.falloffType = Light.FALLOFF_GLTF;
68 babylonLight.diffuse = light.color ? Color3.FromArray(light.color) : Color3.White();
69 babylonLight.intensity = light.intensity == undefined ? 1 : light.intensity;
70 babylonLight.range = light.range == undefined ? Number.MAX_VALUE : light.range;
71 babylonLight.parent = babylonMesh;
72 GLTFLoader.AddPointerMetadata(babylonLight, extensionContext);
73 assign(babylonMesh);
74 });
75 });
76 };
77 return KHR_lights;
78}());
79export { KHR_lights };
80GLTFLoader.RegisterExtension(NAME, function (loader) { return new KHR_lights(loader); });
81//# sourceMappingURL=KHR_lights_punctual.js.map
\No newline at end of file