Преглед на файлове

glTFSerializer: fix bug in exporting specular glossiness without textures; normalize normals and tangents

Kacey Coley преди 7 години
родител
ревизия
4e936c5e5c

+ 16 - 16
serializers/src/glTF/2.0/babylon.glTFAnimation.ts

@@ -271,14 +271,14 @@ module BABYLON.GLTF2 {
 
                 // Creates buffer view and accessor for key frames.
                 let byteLength = animationData.inputs.length * 4;
-                bufferView = _GLTFUtilities.CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, `${name}  keyframe data view`);
+                bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, `${name}  keyframe data view`);
                 bufferViews.push(bufferView);
 
                 animationData.inputs.forEach(function (input) {
                     binaryWriter.setFloat32(input);
                 });
 
-                accessor = _GLTFUtilities.CreateAccessor(bufferViews.length - 1, `${name}  keyframes`, AccessorType.SCALAR, AccessorComponentType.FLOAT, animationData.inputs.length, null, [animationData.inputsMin], [animationData.inputsMax]);
+                accessor = _GLTFUtilities._CreateAccessor(bufferViews.length - 1, `${name}  keyframes`, AccessorType.SCALAR, AccessorComponentType.FLOAT, animationData.inputs.length, null, [animationData.inputsMin], [animationData.inputsMax]);
                 accessors.push(accessor);
                 keyframeAccessorIndex = accessors.length - 1;
 
@@ -287,7 +287,7 @@ module BABYLON.GLTF2 {
                 byteLength = dataAccessorType === AccessorType.VEC3 ? animationData.outputs.length * 12 : animationData.outputs.length * 16;
 
                 // check for in and out tangents
-                bufferView = _GLTFUtilities.CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, `${name}  data view`);
+                bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, `${name}  data view`);
                 bufferViews.push(bufferView);
 
                 animationData.outputs.forEach(function (output) {
@@ -296,7 +296,7 @@ module BABYLON.GLTF2 {
                     });
                 });
 
-                accessor = _GLTFUtilities.CreateAccessor(bufferViews.length - 1, `${name}  data`, dataAccessorType, AccessorComponentType.FLOAT, outputLength, null, null, null);
+                accessor = _GLTFUtilities._CreateAccessor(bufferViews.length - 1, `${name}  data`, dataAccessorType, AccessorComponentType.FLOAT, outputLength, null, null, null);
                 accessors.push(accessor);
                 dataAccessorIndex = accessors.length - 1;
 
@@ -447,7 +447,7 @@ module BABYLON.GLTF2 {
                         Quaternion.RotationYawPitchRollToRef(cacheValue.y, cacheValue.x, cacheValue.z, quaternionCache);
                     }
                     if (convertToRightHandedSystem) {
-                        _GLTFUtilities.GetRightHandedQuaternionFromRef(quaternionCache);
+                        _GLTFUtilities._GetRightHandedQuaternionFromRef(quaternionCache);
 
                         if (!babylonTransformNode.parent) {
                             quaternionCache = Quaternion.FromArray([0, 1, 0, 0]).multiply(quaternionCache);
@@ -458,7 +458,7 @@ module BABYLON.GLTF2 {
                 else {
                     cacheValue = value as Vector3;
                     if (convertToRightHandedSystem && (animationChannelTargetPath !== AnimationChannelTargetPath.SCALE)) {
-                        _GLTFUtilities.GetRightHandedPositionVector3FromRef(cacheValue);
+                        _GLTFUtilities._GetRightHandedPositionVector3FromRef(cacheValue);
                         if (!babylonTransformNode.parent) {
                             cacheValue.x *= -1;
                             cacheValue.z *= -1;
@@ -536,7 +536,7 @@ module BABYLON.GLTF2 {
                     if (babylonTransformNode.rotationQuaternion) {
                         basePositionRotationOrScale = babylonTransformNode.rotationQuaternion.asArray();
                         if (convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedQuaternionArrayFromRef(basePositionRotationOrScale);
+                            _GLTFUtilities._GetRightHandedQuaternionArrayFromRef(basePositionRotationOrScale);
                             if (!babylonTransformNode.parent) {
                                 basePositionRotationOrScale = Quaternion.FromArray([0, 1, 0, 0]).multiply(Quaternion.FromArray(basePositionRotationOrScale)).asArray();
                             }
@@ -548,13 +548,13 @@ module BABYLON.GLTF2 {
                 }
                 else {
                     basePositionRotationOrScale = babylonTransformNode.rotation.asArray();
-                    _GLTFUtilities.GetRightHandedNormalArray3FromRef(basePositionRotationOrScale);
+                    _GLTFUtilities._GetRightHandedNormalArray3FromRef(basePositionRotationOrScale);
                 }
             }
             else if (animationChannelTargetPath === AnimationChannelTargetPath.TRANSLATION) {
                 basePositionRotationOrScale = babylonTransformNode.position.asArray();
                 if (convertToRightHandedSystem) {
-                    _GLTFUtilities.GetRightHandedPositionArray3FromRef(basePositionRotationOrScale);
+                    _GLTFUtilities._GetRightHandedPositionArray3FromRef(basePositionRotationOrScale);
                 }
             }
             else { // scale
@@ -583,7 +583,7 @@ module BABYLON.GLTF2 {
                     const array = Vector3.FromArray(value);
                     let rotationQuaternion = Quaternion.RotationYawPitchRoll(array.y, array.x, array.z);
                     if (convertToRightHandedSystem) {
-                        _GLTFUtilities.GetRightHandedQuaternionFromRef(rotationQuaternion);
+                        _GLTFUtilities._GetRightHandedQuaternionFromRef(rotationQuaternion);
 
                         if (!babylonTransformNode.parent) {
                             rotationQuaternion = Quaternion.FromArray([0, 1, 0, 0]).multiply(rotationQuaternion);
@@ -593,7 +593,7 @@ module BABYLON.GLTF2 {
                 }
                 else if (animationChannelTargetPath === AnimationChannelTargetPath.TRANSLATION) {
                     if (convertToRightHandedSystem) {
-                        _GLTFUtilities.GetRightHandedNormalArray3FromRef(value);
+                        _GLTFUtilities._GetRightHandedNormalArray3FromRef(value);
                         if (!babylonTransformNode.parent) {
                             value[0] *= -1;
                             value[2] *= -1;
@@ -609,7 +609,7 @@ module BABYLON.GLTF2 {
                     if (animationChannelTargetPath === AnimationChannelTargetPath.ROTATION) {
                         let posRotScale = useQuaternion ? newPositionRotationOrScale as Quaternion : Quaternion.RotationYawPitchRoll(newPositionRotationOrScale.y, newPositionRotationOrScale.x, newPositionRotationOrScale.z).normalize();
                         if (convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedQuaternionFromRef(posRotScale);
+                            _GLTFUtilities._GetRightHandedQuaternionFromRef(posRotScale);
 
                             if (!babylonTransformNode.parent) {
                                 posRotScale = Quaternion.FromArray([0, 1, 0, 0]).multiply(posRotScale);
@@ -619,7 +619,7 @@ module BABYLON.GLTF2 {
                     }
                     else if (animationChannelTargetPath === AnimationChannelTargetPath.TRANSLATION) {
                         if (convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedNormalVector3FromRef(newPositionRotationOrScale as Vector3);
+                            _GLTFUtilities._GetRightHandedNormalVector3FromRef(newPositionRotationOrScale as Vector3);
 
                             if (!babylonTransformNode.parent) {
                                 newPositionRotationOrScale.x *= -1;
@@ -634,7 +634,7 @@ module BABYLON.GLTF2 {
                 value = (keyFrame.value as Quaternion).normalize().asArray();
 
                 if (convertToRightHandedSystem) {
-                    _GLTFUtilities.GetRightHandedQuaternionArrayFromRef(value);
+                    _GLTFUtilities._GetRightHandedQuaternionArrayFromRef(value);
 
                     if (!babylonTransformNode.parent) {
                         value = Quaternion.FromArray([0, 1, 0, 0]).multiply(Quaternion.FromArray(value)).asArray();
@@ -730,7 +730,7 @@ module BABYLON.GLTF2 {
                         }
 
                         if (convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedQuaternionArrayFromRef(tangent);
+                            _GLTFUtilities._GetRightHandedQuaternionArrayFromRef(tangent);
                             if (!babylonTransformNode.parent) {
                                 tangent = Quaternion.FromArray([0, 1, 0, 0]).multiply(Quaternion.FromArray(tangent)).asArray();
                             }
@@ -745,7 +745,7 @@ module BABYLON.GLTF2 {
                         tangent = (tangentValue as Vector3).scale(frameDelta).asArray();
                         if (convertToRightHandedSystem) {
                             if (animationChannelTargetPath === AnimationChannelTargetPath.TRANSLATION) {
-                                _GLTFUtilities.GetRightHandedPositionArray3FromRef(tangent);
+                                _GLTFUtilities._GetRightHandedPositionArray3FromRef(tangent);
                                 if (!babylonTransformNode.parent) {
                                     tangent[0] *= -1; // x
                                     tangent[2] *= -1; // z

+ 32 - 13
serializers/src/glTF/2.0/babylon.glTFExporter.ts

@@ -437,12 +437,28 @@ module BABYLON.GLTF2 {
             for (let vertex of vertices) {
                 if (this._convertToRightHandedSystem && !(vertexAttributeKind === VertexBuffer.ColorKind) && !(vertex instanceof Vector2)) {
                     if (vertex instanceof Vector3) {
-                        (vertexAttributeKind === VertexBuffer.PositionKind) ? _GLTFUtilities.GetRightHandedPositionVector3FromRef(vertex) : _GLTFUtilities.GetRightHandedNormalVector3FromRef(vertex);
+                        if (vertexAttributeKind === VertexBuffer.NormalKind) {
+                            _GLTFUtilities._GetRightHandedNormalVector3FromRef(vertex);
+                        }
+                        else if (vertexAttributeKind === VertexBuffer.PositionKind) {
+                            _GLTFUtilities._GetRightHandedPositionVector3FromRef(vertex);
+                        }
+                        else {
+                            Tools.Error('Unsupported vertex attribute kind!');
+                        }
                     }
                     else {
-                        _GLTFUtilities.GetRightHandedVector4FromRef(vertex);
+                        
+                        _GLTFUtilities._GetRightHandedVector4FromRef(vertex);   
                     }
                 }
+                if (vertexAttributeKind === VertexBuffer.NormalKind) {
+                    vertex.normalize();
+                }
+                else if (vertexAttributeKind === VertexBuffer.TangentKind && vertex instanceof Vector4) {
+                    _GLTFUtilities._NormalizeTangentFromRef(vertex);
+                }
+
                 for (let component of vertex.asArray()) {
                     binaryWriter.setFloat32(component, byteOffset);
                     byteOffset += 4;
@@ -469,7 +485,7 @@ module BABYLON.GLTF2 {
                         index = k * stride;
                         const vertexData = Vector3.FromArray(meshAttributeArray, index);
                         if (this._convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedPositionVector3FromRef(vertexData);
+                            _GLTFUtilities._GetRightHandedPositionVector3FromRef(vertexData);
                         }
                         vertexAttributes.push(vertexData.asArray());
                     }
@@ -480,8 +496,9 @@ module BABYLON.GLTF2 {
                         index = k * stride;
                         const vertexData = Vector3.FromArray(meshAttributeArray, index);
                         if (this._convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedNormalVector3FromRef(vertexData);
+                            _GLTFUtilities._GetRightHandedNormalVector3FromRef(vertexData);
                         }
+                        vertexData.normalize();
                         vertexAttributes.push(vertexData.asArray());
                     }
                     break;
@@ -491,8 +508,10 @@ module BABYLON.GLTF2 {
                         index = k * stride;
                         const vertexData = Vector4.FromArray(meshAttributeArray, index);
                         if (this._convertToRightHandedSystem) {
-                            _GLTFUtilities.GetRightHandedVector4FromRef(vertexData);
+                            _GLTFUtilities._GetRightHandedVector4FromRef(vertexData);
                         }
+                        _GLTFUtilities._NormalizeTangentFromRef(vertexData);
+
                         vertexAttributes.push(vertexData.asArray());
                     }
                     break;
@@ -584,7 +603,7 @@ module BABYLON.GLTF2 {
                         if (image.uri) {
                             imageData = this._imageData[image.uri];
                             imageName = image.uri.split('.')[0] + " image";
-                            bufferView = _GLTFUtilities.CreateBufferView(0, byteOffset, imageData.data.length, undefined, imageName);
+                            bufferView = _GLTFUtilities._CreateBufferView(0, byteOffset, imageData.data.length, undefined, imageName);
                             byteOffset += imageData.data.buffer.byteLength;
                             this._bufferViews.push(bufferView);
                             image.bufferView = this._bufferViews.length - 1;
@@ -762,7 +781,7 @@ module BABYLON.GLTF2 {
          */
         private setNodeTransformation(node: INode, babylonTransformNode: TransformNode): void {
             if (!babylonTransformNode.position.equalsToFloats(0, 0, 0)) {
-                node.translation = this._convertToRightHandedSystem ? _GLTFUtilities.GetRightHandedPositionVector3(babylonTransformNode.position).asArray() : babylonTransformNode.position.asArray();
+                node.translation = this._convertToRightHandedSystem ? _GLTFUtilities._GetRightHandedPositionVector3(babylonTransformNode.position).asArray() : babylonTransformNode.position.asArray();
             }
 
             if (!babylonTransformNode.scaling.equalsToFloats(1, 1, 1)) {
@@ -775,7 +794,7 @@ module BABYLON.GLTF2 {
             }
             if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
                 if (this._convertToRightHandedSystem) {
-                    _GLTFUtilities.GetRightHandedQuaternionFromRef(rotationQuaternion);
+                    _GLTFUtilities._GetRightHandedQuaternionFromRef(rotationQuaternion);
 
                 }
                 node.rotation = rotationQuaternion.normalize().asArray();
@@ -808,7 +827,7 @@ module BABYLON.GLTF2 {
 
                 if (vertexData) {
                     const byteLength = vertexData.length * 4;
-                    const bufferView = _GLTFUtilities.CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, kind + " - " + bufferMesh.name);
+                    const bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, kind + " - " + bufferMesh.name);
                     this._bufferViews.push(bufferView);
 
                     this.writeAttributeData(
@@ -963,7 +982,7 @@ module BABYLON.GLTF2 {
                     const indices = bufferMesh.getIndices();
                     if (indices) {
                         const byteLength = indices.length * 4;
-                        bufferView = _GLTFUtilities.CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, "Indices - " + bufferMesh.name);
+                        bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, "Indices - " + bufferMesh.name);
                         this._bufferViews.push(bufferView);
                         indexBufferViewIndex = this._bufferViews.length - 1;
 
@@ -1026,9 +1045,9 @@ module BABYLON.GLTF2 {
                                     if (bufferViewIndex != undefined) { // check to see if bufferviewindex has a numeric value assigned.
                                         minMax = { min: null, max: null };
                                         if (attributeKind == VertexBuffer.PositionKind) {
-                                            minMax = _GLTFUtilities.CalculateMinMaxPositions(vertexData, 0, vertexData.length / stride, this._convertToRightHandedSystem);
+                                            minMax = _GLTFUtilities._CalculateMinMaxPositions(vertexData, 0, vertexData.length / stride, this._convertToRightHandedSystem);
                                         }
-                                        const accessor = _GLTFUtilities.CreateAccessor(bufferViewIndex, attributeKind + " - " + babylonTransformNode.name, attribute.accessorType, AccessorComponentType.FLOAT, vertexData.length / stride, 0, minMax.min, minMax.max);
+                                        const accessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, attributeKind + " - " + babylonTransformNode.name, attribute.accessorType, AccessorComponentType.FLOAT, vertexData.length / stride, 0, minMax.min, minMax.max);
                                         this._accessors.push(accessor);
                                         this.setAttributeKind(meshPrimitive, attributeKind);
                                         if (meshPrimitive.attributes.TEXCOORD_0 != null || meshPrimitive.attributes.TEXCOORD_1 != null) {
@@ -1040,7 +1059,7 @@ module BABYLON.GLTF2 {
                         }
                         if (indexBufferViewIndex) {
                             // Create accessor
-                            const accessor = _GLTFUtilities.CreateAccessor(indexBufferViewIndex, "indices - " + babylonTransformNode.name, AccessorType.SCALAR, AccessorComponentType.UNSIGNED_INT, submesh.indexCount, submesh.indexStart * 4, null, null);
+                            const accessor = _GLTFUtilities._CreateAccessor(indexBufferViewIndex, "indices - " + babylonTransformNode.name, AccessorType.SCALAR, AccessorComponentType.UNSIGNED_INT, submesh.indexCount, submesh.indexStart * 4, null, null);
                             this._accessors.push(accessor);
                             meshPrimitive.indices = this._accessors.length - 1;
                         }

+ 38 - 32
serializers/src/glTF/2.0/babylon.glTFMaterialExporter.ts

@@ -933,40 +933,46 @@ module BABYLON.GLTF2 {
          * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
          * @returns glTF PBR Metallic Roughness factors
          */
-        private _convertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, glTFPbrMetallicRoughness: IMaterialPbrMetallicRoughness, hasTextureCoords: boolean): Promise<_IPBRMetallicRoughness> {
-            const samplers = this._exporter._samplers;
-            const textures = this._exporter._textures;
-            const specGloss: _IPBRSpecularGlossiness = {
-                diffuseColor: babylonPBRMaterial.albedoColor || Color3.White(),
-                specularColor: babylonPBRMaterial.reflectivityColor || Color3.White(),
-                glossiness: babylonPBRMaterial.microSurface || 1,
-            };
-            let samplerIndex: Nullable<number> = null;
-            const sampler = this._getGLTFTextureSampler(babylonPBRMaterial.albedoTexture);
-            if (sampler.magFilter != null && sampler.minFilter != null && sampler.wrapS != null && sampler.wrapT != null) {
-                samplers.push(sampler);
-                samplerIndex = samplers.length - 1;
-            }
-            if (babylonPBRMaterial.reflectivityTexture && !babylonPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha) {
-                return Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported");
-            }
+        private _convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial: PBRMaterial, mimeType: ImageMimeType, glTFPbrMetallicRoughness: IMaterialPbrMetallicRoughness, hasTextureCoords: boolean): Promise<_IPBRMetallicRoughness> {
+            return Promise.resolve().then(() => {
+                const samplers = this._exporter._samplers;
+                const textures = this._exporter._textures;
+                const specGloss: _IPBRSpecularGlossiness = {
+                    diffuseColor: babylonPBRMaterial.albedoColor || Color3.White(),
+                    specularColor: babylonPBRMaterial.reflectivityColor || Color3.White(),
+                    glossiness: babylonPBRMaterial.microSurface || 1,
+                };
+                let samplerIndex: Nullable<number> = null;
+                const sampler = this._getGLTFTextureSampler(babylonPBRMaterial.albedoTexture);
+                if (sampler.magFilter != null && sampler.minFilter != null && sampler.wrapS != null && sampler.wrapT != null) {
+                    samplers.push(sampler);
+                    samplerIndex = samplers.length - 1;
+                }
+                if (babylonPBRMaterial.reflectivityTexture && !babylonPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha) {
+                    return Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported");
+                }
+                if (babylonPBRMaterial.albedoTexture || babylonPBRMaterial.reflectivityTexture) {
+                    return this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(babylonPBRMaterial.albedoTexture, babylonPBRMaterial.reflectivityTexture, specGloss, mimeType).then(metallicRoughnessFactors => {
+                        if (hasTextureCoords) {
+                            if (metallicRoughnessFactors.baseColorTextureBase64) {
+                                const glTFBaseColorTexture = this._getTextureInfoFromBase64(metallicRoughnessFactors.baseColorTextureBase64, "bjsBaseColorTexture_" + (textures.length) + ".png", mimeType, babylonPBRMaterial.albedoTexture ? babylonPBRMaterial.albedoTexture.coordinatesIndex : null, samplerIndex);
+                                if (glTFBaseColorTexture) {
+                                    glTFPbrMetallicRoughness.baseColorTexture = glTFBaseColorTexture;
+                                }
+                            }
+                            if (metallicRoughnessFactors.metallicRoughnessTextureBase64) {
+                                const glTFMRColorTexture = this._getTextureInfoFromBase64(metallicRoughnessFactors.metallicRoughnessTextureBase64, "bjsMetallicRoughnessTexture_" + (textures.length) + ".png", mimeType, babylonPBRMaterial.reflectivityTexture ? babylonPBRMaterial.reflectivityTexture.coordinatesIndex : null, samplerIndex);
+                                if (glTFMRColorTexture) {
+                                    glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFMRColorTexture;
+                                }
+                            }
 
-            return this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(babylonPBRMaterial.albedoTexture, babylonPBRMaterial.reflectivityTexture, specGloss, mimeType).then(metallicRoughnessFactors => {
-                if (hasTextureCoords) {
-                    if (metallicRoughnessFactors.baseColorTextureBase64) {
-                        const glTFBaseColorTexture = this._getTextureInfoFromBase64(metallicRoughnessFactors.baseColorTextureBase64, "bjsBaseColorTexture_" + (textures.length) + ".png", mimeType, babylonPBRMaterial.albedoTexture ? babylonPBRMaterial.albedoTexture.coordinatesIndex : null, samplerIndex);
-                        if (glTFBaseColorTexture) {
-                            glTFPbrMetallicRoughness.baseColorTexture = glTFBaseColorTexture;
+                            return metallicRoughnessFactors;
                         }
-                    }
-                    if (metallicRoughnessFactors.metallicRoughnessTextureBase64) {
-                        const glTFMRColorTexture = this._getTextureInfoFromBase64(metallicRoughnessFactors.metallicRoughnessTextureBase64, "bjsMetallicRoughnessTexture_" + (textures.length) + ".png", mimeType, babylonPBRMaterial.reflectivityTexture ? babylonPBRMaterial.reflectivityTexture.coordinatesIndex : null, samplerIndex);
-                        if (glTFMRColorTexture) {
-                            glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFMRColorTexture;
+                        else {
+                            return this._convertSpecularGlossinessToMetallicRoughness(specGloss);
                         }
-                    }
-
-                    return metallicRoughnessFactors;
+                    });
                 }
                 else {
                     return this._convertSpecularGlossinessToMetallicRoughness(specGloss);
@@ -1005,7 +1011,7 @@ module BABYLON.GLTF2 {
                 });
             }
             else {
-                return this._convertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords).then(metallicRoughness => {
+                return this._convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords).then(metallicRoughness => {
                     return this.setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
                 });
             }

+ 21 - 14
serializers/src/glTF/2.0/babylon.glTFUtilities.ts

@@ -14,7 +14,7 @@ module BABYLON.GLTF2 {
          * @param name name of the buffer view
          * @returns bufferView for glTF
          */
-        public static CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView {
+        public static _CreateBufferView(bufferIndex: number, byteOffset: number, byteLength: number, byteStride?: number, name?: string): IBufferView {
             let bufferview: IBufferView = { buffer: bufferIndex, byteLength: byteLength };
             if (byteOffset) {
                 bufferview.byteOffset = byteOffset;
@@ -41,7 +41,7 @@ module BABYLON.GLTF2 {
          * @param max Maximum value of each component in this attribute
          * @returns accessor for glTF 
          */
-        public static CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor {
+        public static _CreateAccessor(bufferviewIndex: number, name: string, type: AccessorType, componentType: AccessorComponentType, count: number, byteOffset: Nullable<number>, min: Nullable<number[]>, max: Nullable<number[]>): IAccessor {
             let accessor: IAccessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
 
             if (min != null) {
@@ -64,7 +64,7 @@ module BABYLON.GLTF2 {
          * @param vertexCount Number of vertices to check for min and max values
          * @returns min number array and max number array
          */
-        public static CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): { min: number[], max: number[] } {
+        public static _CalculateMinMaxPositions(positions: FloatArray, vertexStart: number, vertexCount: number, convertToRightHandedSystem: boolean): { min: number[], max: number[] } {
             const min = [Infinity, Infinity, Infinity];
             const max = [-Infinity, -Infinity, -Infinity];
             const positionStrideSize = 3;
@@ -78,7 +78,7 @@ module BABYLON.GLTF2 {
 
                     position = Vector3.FromArray(positions, indexOffset);
                     if (convertToRightHandedSystem) {
-                        _GLTFUtilities.GetRightHandedPositionVector3FromRef(position);
+                        _GLTFUtilities._GetRightHandedPositionVector3FromRef(position);
                     }
                     vector = position.asArray();
 
@@ -102,7 +102,7 @@ module BABYLON.GLTF2 {
          * @param vector vector3 array
          * @returns right-handed Vector3
          */
-        public static GetRightHandedPositionVector3(vector: Vector3): Vector3 {
+        public static _GetRightHandedPositionVector3(vector: Vector3): Vector3 {
             return new Vector3(vector.x, vector.y, -vector.z);
         }
 
@@ -110,7 +110,7 @@ module BABYLON.GLTF2 {
          * Converts a Vector3 to right-handed
          * @param vector Vector3 to convert to right-handed
          */
-        public static GetRightHandedPositionVector3FromRef(vector: Vector3) {
+        public static _GetRightHandedPositionVector3FromRef(vector: Vector3) {
             vector.z *= -1;
         }
 
@@ -118,7 +118,7 @@ module BABYLON.GLTF2 {
          * Converts a three element number array to right-handed
          * @param vector number array to convert to right-handed
          */
-        public static GetRightHandedPositionArray3FromRef(vector: number[]) {
+        public static _GetRightHandedPositionArray3FromRef(vector: number[]) {
             vector[2] *= -1;
         }
 
@@ -127,7 +127,7 @@ module BABYLON.GLTF2 {
          * @param vector vector3 array
          * @returns right-handed Vector3
          */
-        public static GetRightHandedNormalVector3(vector: Vector3): Vector3 {
+        public static _GetRightHandedNormalVector3(vector: Vector3): Vector3 {
             return new Vector3(vector.x, vector.y, -vector.z);
         }
 
@@ -135,7 +135,7 @@ module BABYLON.GLTF2 {
          * Converts a Vector3 to right-handed
          * @param vector Vector3 to convert to right-handed
          */
-        public static GetRightHandedNormalVector3FromRef(vector: Vector3) {
+        public static _GetRightHandedNormalVector3FromRef(vector: Vector3) {
             vector.z *= -1;
         }
 
@@ -143,7 +143,7 @@ module BABYLON.GLTF2 {
          * Converts a three element number array to right-handed
          * @param vector number array to convert to right-handed
          */
-        public static GetRightHandedNormalArray3FromRef(vector: number[]) {
+        public static _GetRightHandedNormalArray3FromRef(vector: number[]) {
             vector[2] *= -1;
         }
 
@@ -151,7 +151,7 @@ module BABYLON.GLTF2 {
          * Converts a Vector4 to right-handed
          * @param vector Vector4 to convert to right-handed
          */
-        public static GetRightHandedVector4FromRef(vector: Vector4) {
+        public static _GetRightHandedVector4FromRef(vector: Vector4) {
             vector.z *= -1;
             vector.w *= -1;
         }
@@ -160,7 +160,7 @@ module BABYLON.GLTF2 {
          * Converts a Vector4 to right-handed
          * @param vector Vector4 to convert to right-handed
          */
-        public static GetRightHandedArray4FromRef(vector: number[]) {
+        public static _GetRightHandedArray4FromRef(vector: number[]) {
             vector[2] *= -1;
             vector[3] *= -1;
         }
@@ -169,7 +169,7 @@ module BABYLON.GLTF2 {
          * Converts a Quaternion to right-handed
          * @param quaternion Source quaternion to convert to right-handed
          */
-        public static GetRightHandedQuaternionFromRef(quaternion: Quaternion) {
+        public static _GetRightHandedQuaternionFromRef(quaternion: Quaternion) {
              quaternion.x *= -1;
              quaternion.y *= -1;
         }
@@ -178,9 +178,16 @@ module BABYLON.GLTF2 {
          * Converts a Quaternion to right-handed
          * @param quaternion Source quaternion to convert to right-handed
          */
-        public static GetRightHandedQuaternionArrayFromRef(quaternion: number[]) {
+        public static _GetRightHandedQuaternionArrayFromRef(quaternion: number[]) {
              quaternion[0] *= -1;
              quaternion[1] *= -1;
         }
+
+        public static _NormalizeTangentFromRef(tangent: Vector4) {
+            const normalizedTangentComponent = new Vector3(tangent.x, tangent.y, tangent.z).normalize();
+            tangent.x = normalizedTangentComponent.x;
+            tangent.y = normalizedTangentComponent.y;
+            tangent.z = normalizedTangentComponent.z;
+        }
     }
 }