1 | import { Scalar } from "@babylonjs/core/Maths/math.scalar";
|
2 | import { SphericalHarmonics, SphericalPolynomial } from "@babylonjs/core/Maths/sphericalPolynomial";
|
3 | import { Quaternion, Matrix } from "@babylonjs/core/Maths/math";
|
4 | import { RawCubeTexture } from "@babylonjs/core/Materials/Textures/rawCubeTexture";
|
5 | import { GLTFLoader, ArrayItem } from "../glTFLoader";
|
6 | var NAME = "EXT_lights_image_based";
|
7 |
|
8 |
|
9 |
|
10 | var EXT_lights_image_based = (function () {
|
11 |
|
12 | function EXT_lights_image_based(loader) {
|
13 |
|
14 | this.name = NAME;
|
15 |
|
16 | this.enabled = true;
|
17 | this._loader = loader;
|
18 | }
|
19 |
|
20 | EXT_lights_image_based.prototype.dispose = function () {
|
21 | delete this._loader;
|
22 | delete this._lights;
|
23 | };
|
24 |
|
25 | EXT_lights_image_based.prototype.onLoading = function () {
|
26 | var extensions = this._loader.gltf.extensions;
|
27 | if (extensions && extensions[this.name]) {
|
28 | var extension = extensions[this.name];
|
29 | this._lights = extension.lights;
|
30 | }
|
31 | };
|
32 |
|
33 | EXT_lights_image_based.prototype.loadSceneAsync = function (context, scene) {
|
34 | var _this = this;
|
35 | return GLTFLoader.LoadExtensionAsync(context, scene, this.name, function (extensionContext, extension) {
|
36 | var promises = new Array();
|
37 | promises.push(_this._loader.loadSceneAsync(context, scene));
|
38 | _this._loader.logOpen("" + extensionContext);
|
39 | var light = ArrayItem.Get(extensionContext + "/light", _this._lights, extension.light);
|
40 | promises.push(_this._loadLightAsync("#/extensions/" + _this.name + "/lights/" + extension.light, light).then(function (texture) {
|
41 | _this._loader.babylonScene.environmentTexture = texture;
|
42 | }));
|
43 | _this._loader.logClose();
|
44 | return Promise.all(promises).then(function () { });
|
45 | });
|
46 | };
|
47 | EXT_lights_image_based.prototype._loadLightAsync = function (context, light) {
|
48 | var _this = this;
|
49 | if (!light._loaded) {
|
50 | var promises = new Array();
|
51 | this._loader.logOpen("" + context);
|
52 | var imageData_1 = new Array(light.specularImages.length);
|
53 | var _loop_1 = function (mipmap) {
|
54 | var faces = light.specularImages[mipmap];
|
55 | imageData_1[mipmap] = new Array(faces.length);
|
56 | var _loop_2 = function (face) {
|
57 | var specularImageContext = context + "/specularImages/" + mipmap + "/" + face;
|
58 | this_1._loader.logOpen("" + specularImageContext);
|
59 | var index = faces[face];
|
60 | var image = ArrayItem.Get(specularImageContext, this_1._loader.gltf.images, index);
|
61 | promises.push(this_1._loader.loadImageAsync("#/images/" + index, image).then(function (data) {
|
62 | imageData_1[mipmap][face] = data;
|
63 | }));
|
64 | this_1._loader.logClose();
|
65 | };
|
66 | for (var face = 0; face < faces.length; face++) {
|
67 | _loop_2(face);
|
68 | }
|
69 | };
|
70 | var this_1 = this;
|
71 | for (var mipmap = 0; mipmap < light.specularImages.length; mipmap++) {
|
72 | _loop_1(mipmap);
|
73 | }
|
74 | this._loader.logClose();
|
75 | light._loaded = Promise.all(promises).then(function () {
|
76 | var babylonTexture = new RawCubeTexture(_this._loader.babylonScene, null, light.specularImageSize);
|
77 | light._babylonTexture = babylonTexture;
|
78 | if (light.intensity != undefined) {
|
79 | babylonTexture.level = light.intensity;
|
80 | }
|
81 | if (light.rotation) {
|
82 | var rotation = Quaternion.FromArray(light.rotation);
|
83 |
|
84 | if (!_this._loader.babylonScene.useRightHandedSystem) {
|
85 | rotation = Quaternion.Inverse(rotation);
|
86 | }
|
87 | Matrix.FromQuaternionToRef(rotation, babylonTexture.getReflectionTextureMatrix());
|
88 | }
|
89 | var sphericalHarmonics = SphericalHarmonics.FromArray(light.irradianceCoefficients);
|
90 | sphericalHarmonics.scale(light.intensity);
|
91 | sphericalHarmonics.convertIrradianceToLambertianRadiance();
|
92 | var sphericalPolynomial = SphericalPolynomial.FromHarmonics(sphericalHarmonics);
|
93 |
|
94 | var lodGenerationScale = (imageData_1.length - 1) / Scalar.Log2(light.specularImageSize);
|
95 | return babylonTexture.updateRGBDAsync(imageData_1, sphericalPolynomial, lodGenerationScale);
|
96 | });
|
97 | }
|
98 | return light._loaded.then(function () {
|
99 | return light._babylonTexture;
|
100 | });
|
101 | };
|
102 | return EXT_lights_image_based;
|
103 | }());
|
104 | export { EXT_lights_image_based };
|
105 | GLTFLoader.RegisterExtension(NAME, function (loader) { return new EXT_lights_image_based(loader); });
|
106 |
|
\ | No newline at end of file |