UNPKG

@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.

127 lines 6.56 kB
import * as tslib_1 from "tslib"; import { GLTFLoaderExtension } from "./glTFLoader"; import { GLTFLoaderBase } from "./glTFLoader"; import { Color3, Vector3 } from "@babylonjs/core/Maths/math"; import { Tools } from "@babylonjs/core/Misc/tools"; import { Material } from "@babylonjs/core/Materials/material"; import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial"; import { HemisphericLight } from "@babylonjs/core/Lights/hemisphericLight"; import { DirectionalLight } from "@babylonjs/core/Lights/directionalLight"; import { PointLight } from "@babylonjs/core/Lights/pointLight"; import { SpotLight } from "@babylonjs/core/Lights/spotLight"; import { GLTFLoader } from "./glTFLoader"; /** @hidden */ var GLTFMaterialsCommonExtension = /** @class */ (function (_super) { tslib_1.__extends(GLTFMaterialsCommonExtension, _super); function GLTFMaterialsCommonExtension() { return _super.call(this, "KHR_materials_common") || this; } GLTFMaterialsCommonExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) { if (!gltfRuntime.extensions) { return false; } var extension = gltfRuntime.extensions[this.name]; if (!extension) { return false; } // Create lights var lights = extension.lights; if (lights) { for (var thing in lights) { var light = lights[thing]; switch (light.type) { case "ambient": var ambientLight = new HemisphericLight(light.name, new Vector3(0, 1, 0), gltfRuntime.scene); var ambient = light.ambient; if (ambient) { ambientLight.diffuse = Color3.FromArray(ambient.color || [1, 1, 1]); } break; case "point": var pointLight = new PointLight(light.name, new Vector3(10, 10, 10), gltfRuntime.scene); var point = light.point; if (point) { pointLight.diffuse = Color3.FromArray(point.color || [1, 1, 1]); } break; case "directional": var dirLight = new DirectionalLight(light.name, new Vector3(0, -1, 0), gltfRuntime.scene); var directional = light.directional; if (directional) { dirLight.diffuse = Color3.FromArray(directional.color || [1, 1, 1]); } break; case "spot": var spot = light.spot; if (spot) { var spotLight = new SpotLight(light.name, new Vector3(0, 10, 0), new Vector3(0, -1, 0), spot.fallOffAngle || Math.PI, spot.fallOffExponent || 0.0, gltfRuntime.scene); spotLight.diffuse = Color3.FromArray(spot.color || [1, 1, 1]); } break; default: Tools.Warn("GLTF Material Common extension: light type \"" + light.type + "\” not supported"); break; } } } return false; }; GLTFMaterialsCommonExtension.prototype.loadMaterialAsync = function (gltfRuntime, id, onSuccess, onError) { var material = gltfRuntime.materials[id]; if (!material || !material.extensions) { return false; } var extension = material.extensions[this.name]; if (!extension) { return false; } var standardMaterial = new StandardMaterial(id, gltfRuntime.scene); standardMaterial.sideOrientation = Material.CounterClockWiseSideOrientation; if (extension.technique === "CONSTANT") { standardMaterial.disableLighting = true; } standardMaterial.backFaceCulling = extension.doubleSided === undefined ? false : !extension.doubleSided; standardMaterial.alpha = extension.values.transparency === undefined ? 1.0 : extension.values.transparency; standardMaterial.specularPower = extension.values.shininess === undefined ? 0.0 : extension.values.shininess; // Ambient if (typeof extension.values.ambient === "string") { this._loadTexture(gltfRuntime, extension.values.ambient, standardMaterial, "ambientTexture", onError); } else { standardMaterial.ambientColor = Color3.FromArray(extension.values.ambient || [0, 0, 0]); } // Diffuse if (typeof extension.values.diffuse === "string") { this._loadTexture(gltfRuntime, extension.values.diffuse, standardMaterial, "diffuseTexture", onError); } else { standardMaterial.diffuseColor = Color3.FromArray(extension.values.diffuse || [0, 0, 0]); } // Emission if (typeof extension.values.emission === "string") { this._loadTexture(gltfRuntime, extension.values.emission, standardMaterial, "emissiveTexture", onError); } else { standardMaterial.emissiveColor = Color3.FromArray(extension.values.emission || [0, 0, 0]); } // Specular if (typeof extension.values.specular === "string") { this._loadTexture(gltfRuntime, extension.values.specular, standardMaterial, "specularTexture", onError); } else { standardMaterial.specularColor = Color3.FromArray(extension.values.specular || [0, 0, 0]); } return true; }; GLTFMaterialsCommonExtension.prototype._loadTexture = function (gltfRuntime, id, material, propertyPath, onError) { // Create buffer from texture url GLTFLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) { // Create texture from buffer GLTFLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, function (texture) { return material[propertyPath] = texture; }, onError); }, onError); }; return GLTFMaterialsCommonExtension; }(GLTFLoaderExtension)); export { GLTFMaterialsCommonExtension }; GLTFLoader.RegisterExtension(new GLTFMaterialsCommonExtension()); //# sourceMappingURL=glTFMaterialsCommonExtension.js.map