1 | import { Color3, Vector3 } from "@babylonjs/core/Maths/math";
|
2 | import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight";
|
3 | import { PointLight } from "@babylonjs/core/Lights/pointLight";
|
4 | import { SpotLight } from "@babylonjs/core/Lights/spotLight";
|
5 | import { Light } from "@babylonjs/core/Lights/light";
|
6 | import { GLTFLoader, ArrayItem } from "../glTFLoader";
|
7 | var NAME = "KHR_lights_punctual";
|
8 | var LightType;
|
9 | (function (LightType) {
|
10 | LightType["DIRECTIONAL"] = "directional";
|
11 | LightType["POINT"] = "point";
|
12 | LightType["SPOT"] = "spot";
|
13 | })(LightType || (LightType = {}));
|
14 |
|
15 |
|
16 |
|
17 | var KHR_lights = (function () {
|
18 |
|
19 | function KHR_lights(loader) {
|
20 |
|
21 | this.name = NAME;
|
22 |
|
23 | this.enabled = true;
|
24 | this._loader = loader;
|
25 | }
|
26 |
|
27 | KHR_lights.prototype.dispose = function () {
|
28 | delete this._loader;
|
29 | delete this._lights;
|
30 | };
|
31 |
|
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 |
|
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 | }());
|
79 | export { KHR_lights };
|
80 | GLTFLoader.RegisterExtension(NAME, function (loader) { return new KHR_lights(loader); });
|
81 |
|
\ | No newline at end of file |