Przeglądaj źródła

glTF File Loader : got support of lights in materials common extension

Julien Moreau-Mathis 8 lat temu
rodzic
commit
798087f2d8

+ 17 - 0
dist/preview release/loaders/babylon.glTFFileLoader.d.ts

@@ -271,6 +271,7 @@ declare module BABYLON {
     * Runtime
     */
     interface IGLTFRuntime {
+        extensions: Object;
         accessors: Object;
         buffers: Object;
         bufferViews: Object;
@@ -447,6 +448,11 @@ declare module BABYLON {
         */
         loadRuntimeAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): boolean;
         /**
+         * Defines an onverride for creating gltf runtime
+         * Return true to stop further extensions from creating the runtime
+         */
+        loadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): boolean;
+        /**
         * Defines an override for loading buffers
         * Return true to stop further extensions from loading this buffer
         */
@@ -472,6 +478,7 @@ declare module BABYLON {
         */
         loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): boolean;
         static LoadRuntimeAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): void;
+        static LoadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): void;
         static LoadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (bufferView: ArrayBufferView) => void, onError: () => void): void;
         static LoadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => void, onError: () => void): void;
         static LoadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): void;
@@ -494,3 +501,13 @@ declare module BABYLON {
         private _parseBinary(data);
     }
 }
+
+/// <reference path="../../../dist/preview release/babylon.d.ts" />
+declare module BABYLON {
+    class GLTFMaterialCommonExtension extends GLTFFileLoaderExtension {
+        constructor();
+        loadRuntimeExtensionsAsync(gltfRuntime: IGLTFRuntime, onSuccess: () => void, onError: () => void): boolean;
+        loadMaterialAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (material: Material) => void, onError: () => void): boolean;
+        private _loadTexture(gltfRuntime, id, material, propertyPath, onError);
+    }
+}

+ 153 - 14
dist/preview release/loaders/babylon.glTFFileLoader.js

@@ -1063,6 +1063,7 @@ var BABYLON;
         }
         GLTFFileLoaderBase.CreateRuntime = function (parsedData, scene, rootUrl) {
             var gltfRuntime = {
+                extensions: {},
                 accessors: {},
                 buffers: {},
                 bufferViews: {},
@@ -1092,6 +1093,9 @@ var BABYLON;
                 dummyNodes: []
             };
             // Parse
+            if (parsedData.extensions) {
+                parseObject(parsedData.extensions, "extensions", gltfRuntime);
+            }
             if (parsedData.extensionsUsed) {
                 parseObject(parsedData.extensionsUsed, "extensionsUsed", gltfRuntime);
             }
@@ -1421,21 +1425,24 @@ var BABYLON;
             var _this = this;
             scene.useRightHandedSystem = true;
             BABYLON.GLTFFileLoaderExtension.LoadRuntimeAsync(scene, data, rootUrl, function (gltfRuntime) {
-                // Create nodes
-                _this._createNodes(gltfRuntime);
-                // Load buffers, shaders, materials, etc.
-                _this._loadBuffersAsync(gltfRuntime, function () {
-                    _this._loadShadersAsync(gltfRuntime, function () {
-                        importMaterials(gltfRuntime);
-                        postLoad(gltfRuntime);
-                        if (!GLTFFileLoader.IncrementalLoading) {
-                            onSuccess();
-                        }
+                // Load runtime extensios
+                BABYLON.GLTFFileLoaderExtension.LoadRuntimeExtensionsAsync(gltfRuntime, function () {
+                    // Create nodes
+                    _this._createNodes(gltfRuntime);
+                    // Load buffers, shaders, materials, etc.
+                    _this._loadBuffersAsync(gltfRuntime, function () {
+                        _this._loadShadersAsync(gltfRuntime, function () {
+                            importMaterials(gltfRuntime);
+                            postLoad(gltfRuntime);
+                            if (!GLTFFileLoader.IncrementalLoading) {
+                                onSuccess();
+                            }
+                        });
                     });
-                });
-                if (GLTFFileLoader.IncrementalLoading) {
-                    onSuccess();
-                }
+                    if (GLTFFileLoader.IncrementalLoading) {
+                        onSuccess();
+                    }
+                }, onError);
             }, onError);
             return true;
         };
@@ -1811,6 +1818,13 @@ var BABYLON;
             return false;
         };
         /**
+         * Defines an onverride for creating gltf runtime
+         * Return true to stop further extensions from creating the runtime
+         */
+        GLTFFileLoaderExtension.prototype.loadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
+            return false;
+        };
+        /**
         * Defines an override for loading buffers
         * Return true to stop further extensions from loading this buffer
         */
@@ -1857,6 +1871,15 @@ var BABYLON;
                 });
             });
         };
+        GLTFFileLoaderExtension.LoadRuntimeExtensionsAsync = function (gltfRuntime, onSuccess, onError) {
+            GLTFFileLoaderExtension.ApplyExtensions(function (loaderExtension) {
+                return loaderExtension.loadRuntimeExtensionsAsync(gltfRuntime, onSuccess, onError);
+            }, function () {
+                setTimeout(function () {
+                    onSuccess();
+                });
+            });
+        };
         GLTFFileLoaderExtension.LoadBufferAsync = function (gltfRuntime, id, onSuccess, onError) {
             GLTFFileLoaderExtension.ApplyExtensions(function (loaderExtension) {
                 return loaderExtension.loadBufferAsync(gltfRuntime, id, onSuccess, onError);
@@ -2054,3 +2077,119 @@ var BABYLON;
 })(BABYLON || (BABYLON = {}));
 
 //# sourceMappingURL=babylon.glTFBinaryExtension.js.map
+
+/// <reference path="../../../dist/preview release/babylon.d.ts"/>
+var __extends = (this && this.__extends) || function (d, b) {
+    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
+    function __() { this.constructor = d; }
+    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
+};
+var BABYLON;
+(function (BABYLON) {
+    ;
+    ;
+    ;
+    var GLTFMaterialCommonExtension = (function (_super) {
+        __extends(GLTFMaterialCommonExtension, _super);
+        function GLTFMaterialCommonExtension() {
+            return _super.call(this, "KHR_materials_common") || this;
+        }
+        GLTFMaterialCommonExtension.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 BABYLON.HemisphericLight(light.name, new BABYLON.Vector3(0, 1, 0), gltfRuntime.scene);
+                            var ambient = light.ambient;
+                            ambientLight.diffuse = BABYLON.Color3.FromArray(ambient.color || [1, 1, 1]);
+                            break;
+                        case "point":
+                            var pointLight = new BABYLON.PointLight(light.name, new BABYLON.Vector3(10, 10, 10), gltfRuntime.scene);
+                            var point = light.point;
+                            pointLight.diffuse = BABYLON.Color3.FromArray(point.color || [1, 1, 1]);
+                            break;
+                        case "directional":
+                            var dirLight = new BABYLON.DirectionalLight(light.name, new BABYLON.Vector3(0, -1, 0), gltfRuntime.scene);
+                            var directional = light.directional;
+                            dirLight.diffuse = BABYLON.Color3.FromArray(directional.color || [1, 1, 1]);
+                            break;
+                        case "spot":
+                            var spot = light.spot;
+                            var spotLight = new BABYLON.SpotLight(light.name, new BABYLON.Vector3(0, 10, 0), new BABYLON.Vector3(0, -1, 0), light.spot.fallOffAngle || Math.PI, light.spot.fallOffExponent || 0.0, gltfRuntime.scene);
+                            spotLight.diffuse = BABYLON.Color3.FromArray(spot.color || [1, 1, 1]);
+                            break;
+                        default:
+                            BABYLON.Tools.Warn("GLTF Material Common extension: light type \"" + light.type + "\” not supported");
+                            break;
+                    }
+                }
+            }
+            return false;
+        };
+        GLTFMaterialCommonExtension.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 BABYLON.StandardMaterial(id, gltfRuntime.scene);
+            standardMaterial.sideOrientation = BABYLON.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 = BABYLON.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 = BABYLON.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 = BABYLON.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 = BABYLON.Color3.FromArray(extension.values.specular || [0, 0, 0]);
+            }
+            return true;
+        };
+        GLTFMaterialCommonExtension.prototype._loadTexture = function (gltfRuntime, id, material, propertyPath, onError) {
+            // Create buffer from texture url
+            BABYLON.GLTFFileLoaderBase.LoadTextureBufferAsync(gltfRuntime, id, function (buffer) {
+                // Create texture from buffer
+                BABYLON.GLTFFileLoaderBase.CreateTextureAsync(gltfRuntime, id, buffer, function (texture) { return material[propertyPath] = texture; }, onError);
+            }, onError);
+        };
+        return GLTFMaterialCommonExtension;
+    }(BABYLON.GLTFFileLoaderExtension));
+    BABYLON.GLTFMaterialCommonExtension = GLTFMaterialCommonExtension;
+    BABYLON.GLTFFileLoader.RegisterExtension(new GLTFMaterialCommonExtension());
+})(BABYLON || (BABYLON = {}));
+
+//# sourceMappingURL=babylon.glTFMaterialCommonExtension.js.map

Plik diff jest za duży
+ 2 - 2
dist/preview release/loaders/babylon.glTFFileLoader.min.js


+ 36 - 5
loaders/src/glTF/babylon.glTFMaterialCommonExtension.ts

@@ -27,6 +27,8 @@ module BABYLON {
 
         ambient?: IGLTFAmbientLightCommonExtension;
         point?: IGLTFPointLightCommonExtension;
+        directional?: IGLTFDirectionalLightCommonExtension;
+        spot?: IGLTFSpotLightCommonExtension;
     };
 
     interface IGLTFPointLightCommonExtension {
@@ -40,6 +42,19 @@ module BABYLON {
         color: number[];
     }
 
+    interface IGLTFDirectionalLightCommonExtension {
+        color: number[];
+    }
+
+    interface IGLTFSpotLightCommonExtension {
+        color: number[];
+        constantAttenuation: number;
+        fallOffAngle: number;
+        fallOffExponent: number;
+        linearAttenuation: number;
+        quadraticAttenuation: number;
+    }
+
     export class GLTFMaterialCommonExtension extends GLTFFileLoaderExtension {
 
         constructor() {
@@ -62,15 +77,27 @@ module BABYLON {
                         case "ambient":
                             var ambientLight = new HemisphericLight(light.name, new Vector3(0, 1, 0), gltfRuntime.scene);
                             var ambient = light.ambient;
-                            ambientLight.diffuse = Color3.FromArray(ambient.color);
+                            ambientLight.diffuse = Color3.FromArray(ambient.color || [1, 1, 1]);
                             break;
                         case "point":
-                            var pointLight = new PointLight(light.name, Vector3.Zero(), gltfRuntime.scene);
+                            var pointLight = new PointLight(light.name, new Vector3(10, 10, 10), gltfRuntime.scene);
                             var point = light.point;
-                            pointLight.diffuse = Color3.FromArray(point.color);
-                            pointLight.position = new Vector3(10, 10, 10);
+                            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;
+                            dirLight.diffuse = Color3.FromArray(directional.color || [1, 1, 1]);
                             break;
-                        default: Tools.Warn("GLTF Material Common extension: light type \"" + light.type + "\” not supported"); return false;
+                        case "spot":
+                            var spot = light.spot;
+                            var spotLight = new SpotLight(light.name, new Vector3(0, 10, 0), new Vector3(0, -1, 0),
+                                                          light.spot.fallOffAngle || Math.PI,
+                                                          light.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;
                     }
                 }
             }
@@ -88,6 +115,10 @@ module BABYLON {
             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;