@babylonjs/loaders
Version:
The Babylon.js file loaders library is an extension you can use to load different 3D file types into a Babylon scene.
106 lines • 5.61 kB
JavaScript
import { Scalar } from "@babylonjs/core/Maths/math.scalar";
import { SphericalHarmonics, SphericalPolynomial } from "@babylonjs/core/Maths/sphericalPolynomial";
import { Quaternion, Matrix } from "@babylonjs/core/Maths/math";
import { RawCubeTexture } from "@babylonjs/core/Materials/Textures/rawCubeTexture";
import { GLTFLoader, ArrayItem } from "../glTFLoader";
var NAME = "EXT_lights_image_based";
/**
* [Specification](https://github.com/KhronosGroup/glTF/blob/eb3e32332042e04691a5f35103f8c261e50d8f1e/extensions/2.0/Khronos/EXT_lights_image_based/README.md) (Experimental)
*/
var EXT_lights_image_based = /** @class */ (function () {
/** @hidden */
function EXT_lights_image_based(loader) {
/** The name of this extension. */
this.name = NAME;
/** Defines whether this extension is enabled. */
this.enabled = true;
this._loader = loader;
}
/** @hidden */
EXT_lights_image_based.prototype.dispose = function () {
delete this._loader;
delete this._lights;
};
/** @hidden */
EXT_lights_image_based.prototype.onLoading = function () {
var extensions = this._loader.gltf.extensions;
if (extensions && extensions[this.name]) {
var extension = extensions[this.name];
this._lights = extension.lights;
}
};
/** @hidden */
EXT_lights_image_based.prototype.loadSceneAsync = function (context, scene) {
var _this = this;
return GLTFLoader.LoadExtensionAsync(context, scene, this.name, function (extensionContext, extension) {
var promises = new Array();
promises.push(_this._loader.loadSceneAsync(context, scene));
_this._loader.logOpen("" + extensionContext);
var light = ArrayItem.Get(extensionContext + "/light", _this._lights, extension.light);
promises.push(_this._loadLightAsync("#/extensions/" + _this.name + "/lights/" + extension.light, light).then(function (texture) {
_this._loader.babylonScene.environmentTexture = texture;
}));
_this._loader.logClose();
return Promise.all(promises).then(function () { });
});
};
EXT_lights_image_based.prototype._loadLightAsync = function (context, light) {
var _this = this;
if (!light._loaded) {
var promises = new Array();
this._loader.logOpen("" + context);
var imageData_1 = new Array(light.specularImages.length);
var _loop_1 = function (mipmap) {
var faces = light.specularImages[mipmap];
imageData_1[mipmap] = new Array(faces.length);
var _loop_2 = function (face) {
var specularImageContext = context + "/specularImages/" + mipmap + "/" + face;
this_1._loader.logOpen("" + specularImageContext);
var index = faces[face];
var image = ArrayItem.Get(specularImageContext, this_1._loader.gltf.images, index);
promises.push(this_1._loader.loadImageAsync("#/images/" + index, image).then(function (data) {
imageData_1[mipmap][face] = data;
}));
this_1._loader.logClose();
};
for (var face = 0; face < faces.length; face++) {
_loop_2(face);
}
};
var this_1 = this;
for (var mipmap = 0; mipmap < light.specularImages.length; mipmap++) {
_loop_1(mipmap);
}
this._loader.logClose();
light._loaded = Promise.all(promises).then(function () {
var babylonTexture = new RawCubeTexture(_this._loader.babylonScene, null, light.specularImageSize);
light._babylonTexture = babylonTexture;
if (light.intensity != undefined) {
babylonTexture.level = light.intensity;
}
if (light.rotation) {
var rotation = Quaternion.FromArray(light.rotation);
// Invert the rotation so that positive rotation is counter-clockwise.
if (!_this._loader.babylonScene.useRightHandedSystem) {
rotation = Quaternion.Inverse(rotation);
}
Matrix.FromQuaternionToRef(rotation, babylonTexture.getReflectionTextureMatrix());
}
var sphericalHarmonics = SphericalHarmonics.FromArray(light.irradianceCoefficients);
sphericalHarmonics.scale(light.intensity);
sphericalHarmonics.convertIrradianceToLambertianRadiance();
var sphericalPolynomial = SphericalPolynomial.FromHarmonics(sphericalHarmonics);
// Compute the lod generation scale to fit exactly to the number of levels available.
var lodGenerationScale = (imageData_1.length - 1) / Scalar.Log2(light.specularImageSize);
return babylonTexture.updateRGBDAsync(imageData_1, sphericalPolynomial, lodGenerationScale);
});
}
return light._loaded.then(function () {
return light._babylonTexture;
});
};
return EXT_lights_image_based;
}());
export { EXT_lights_image_based };
GLTFLoader.RegisterExtension(NAME, function (loader) { return new EXT_lights_image_based(loader); });
//# sourceMappingURL=EXT_lights_image_based.js.map