فهرست منبع

Parsing and Int32Array for indices

Indices are now either number[] or Int32Array
Parse functions for everything except Actions.
Raanan Weber 9 سال پیش
والد
کامیت
e603176c2b

+ 1 - 1
src/Collisions/babylon.collider.ts

@@ -315,7 +315,7 @@
             }
         }
 
-        public _collide(trianglePlaneArray: Array<Plane>, pts: Vector3[], indices: number[], indexStart: number, indexEnd: number, decal: number, hasMaterial: boolean): void {
+        public _collide(trianglePlaneArray: Array<Plane>, pts: Vector3[], indices: number[] | Int32Array, indexStart: number, indexEnd: number, decal: number, hasMaterial: boolean): void {
             for (var i = indexStart; i < indexEnd; i += 3) {
                 var p1 = pts[indices[i] - decal];
                 var p2 = pts[indices[i + 1] - decal];

+ 18 - 509
src/Loading/Plugins/babylon.babylonFileLoader.js

@@ -30,274 +30,6 @@ var BABYLON;
             var id = parsedGeometry.id;
             return scene.getGeometryByID(id);
         };
-        var parseBox = function (parsedBox, scene) {
-            if (parseGeometry(parsedBox, scene)) {
-                return null; // null since geometry could be something else than a box...
-            }
-            var box = new BABYLON.Geometry.Primitives.Box(parsedBox.id, scene, parsedBox.size, parsedBox.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(box, parsedBox.tags);
-            scene.pushGeometry(box, true);
-            return box;
-        };
-        var parseSphere = function (parsedSphere, scene) {
-            if (parseGeometry(parsedSphere, scene)) {
-                return null; // null since geometry could be something else than a sphere...
-            }
-            var sphere = new BABYLON.Geometry.Primitives.Sphere(parsedSphere.id, scene, parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(sphere, parsedSphere.tags);
-            scene.pushGeometry(sphere, true);
-            return sphere;
-        };
-        var parseCylinder = function (parsedCylinder, scene) {
-            if (parseGeometry(parsedCylinder, scene)) {
-                return null; // null since geometry could be something else than a cylinder...
-            }
-            var cylinder = new BABYLON.Geometry.Primitives.Cylinder(parsedCylinder.id, scene, parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.subdivisions, parsedCylinder.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(cylinder, parsedCylinder.tags);
-            scene.pushGeometry(cylinder, true);
-            return cylinder;
-        };
-        var parseTorus = function (parsedTorus, scene) {
-            if (parseGeometry(parsedTorus, scene)) {
-                return null; // null since geometry could be something else than a torus...
-            }
-            var torus = new BABYLON.Geometry.Primitives.Torus(parsedTorus.id, scene, parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(torus, parsedTorus.tags);
-            scene.pushGeometry(torus, true);
-            return torus;
-        };
-        var parseGround = function (parsedGround, scene) {
-            if (parseGeometry(parsedGround, scene)) {
-                return null; // null since geometry could be something else than a ground...
-            }
-            var ground = new BABYLON.Geometry.Primitives.Ground(parsedGround.id, scene, parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(ground, parsedGround.tags);
-            scene.pushGeometry(ground, true);
-            return ground;
-        };
-        var parsePlane = function (parsedPlane, scene) {
-            if (parseGeometry(parsedPlane, scene)) {
-                return null; // null since geometry could be something else than a plane...
-            }
-            var plane = new BABYLON.Geometry.Primitives.Plane(parsedPlane.id, scene, parsedPlane.size, parsedPlane.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(plane, parsedPlane.tags);
-            scene.pushGeometry(plane, true);
-            return plane;
-        };
-        var parseTorusKnot = function (parsedTorusKnot, scene) {
-            if (parseGeometry(parsedTorusKnot, scene)) {
-                return null; // null since geometry could be something else than a torusKnot...
-            }
-            var torusKnot = new BABYLON.Geometry.Primitives.TorusKnot(parsedTorusKnot.id, scene, parsedTorusKnot.radius, parsedTorusKnot.tube, parsedTorusKnot.radialSegments, parsedTorusKnot.tubularSegments, parsedTorusKnot.p, parsedTorusKnot.q, parsedTorusKnot.canBeRegenerated, null);
-            BABYLON.Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
-            scene.pushGeometry(torusKnot, true);
-            return torusKnot;
-        };
-        var parseVertexData = function (parsedVertexData, scene, rootUrl) {
-            if (parseGeometry(parsedVertexData, scene)) {
-                return null; // null since geometry could be a primitive
-            }
-            var geometry = new BABYLON.Geometry(parsedVertexData.id, scene);
-            BABYLON.Tags.AddTagsTo(geometry, parsedVertexData.tags);
-            if (parsedVertexData.delayLoadingFile) {
-                geometry.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
-                geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
-                geometry._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
-                geometry._delayInfo = [];
-                if (parsedVertexData.hasUVs) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.UVKind);
-                }
-                if (parsedVertexData.hasUVs2) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
-                }
-                if (parsedVertexData.hasUVs3) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV3Kind);
-                }
-                if (parsedVertexData.hasUVs4) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV4Kind);
-                }
-                if (parsedVertexData.hasUVs5) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV5Kind);
-                }
-                if (parsedVertexData.hasUVs6) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV6Kind);
-                }
-                if (parsedVertexData.hasColors) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
-                }
-                if (parsedVertexData.hasMatricesIndices) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
-                }
-                if (parsedVertexData.hasMatricesWeights) {
-                    geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
-                }
-                geometry._delayLoadingFunction = importVertexData;
-            }
-            else {
-                importVertexData(parsedVertexData, geometry);
-            }
-            scene.pushGeometry(geometry, true);
-            return geometry;
-        };
-        var parseMesh = function (parsedMesh, scene, rootUrl) {
-            var mesh = new BABYLON.Mesh(parsedMesh.name, scene);
-            mesh.id = parsedMesh.id;
-            BABYLON.Tags.AddTagsTo(mesh, parsedMesh.tags);
-            mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position);
-            if (parsedMesh.rotationQuaternion) {
-                mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion);
-            }
-            else if (parsedMesh.rotation) {
-                mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation);
-            }
-            mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling);
-            if (parsedMesh.localMatrix) {
-                mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix));
-            }
-            else if (parsedMesh.pivotMatrix) {
-                mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.pivotMatrix));
-            }
-            mesh.setEnabled(parsedMesh.isEnabled);
-            mesh.isVisible = parsedMesh.isVisible;
-            mesh.infiniteDistance = parsedMesh.infiniteDistance;
-            mesh.showBoundingBox = parsedMesh.showBoundingBox;
-            mesh.showSubMeshesBoundingBox = parsedMesh.showSubMeshesBoundingBox;
-            if (parsedMesh.applyFog !== undefined) {
-                mesh.applyFog = parsedMesh.applyFog;
-            }
-            if (parsedMesh.pickable !== undefined) {
-                mesh.isPickable = parsedMesh.pickable;
-            }
-            if (parsedMesh.alphaIndex !== undefined) {
-                mesh.alphaIndex = parsedMesh.alphaIndex;
-            }
-            mesh.receiveShadows = parsedMesh.receiveShadows;
-            mesh.billboardMode = parsedMesh.billboardMode;
-            if (parsedMesh.visibility !== undefined) {
-                mesh.visibility = parsedMesh.visibility;
-            }
-            mesh.checkCollisions = parsedMesh.checkCollisions;
-            mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading;
-            // freezeWorldMatrix
-            if (parsedMesh.freezeWorldMatrix) {
-                mesh._waitingFreezeWorldMatrix = parsedMesh.freezeWorldMatrix;
-            }
-            // Parent
-            if (parsedMesh.parentId) {
-                mesh._waitingParentId = parsedMesh.parentId;
-            }
-            // Actions
-            if (parsedMesh.actions !== undefined) {
-                mesh._waitingActions = parsedMesh.actions;
-            }
-            // Geometry
-            mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
-            if (parsedMesh.delayLoadingFile) {
-                mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
-                mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
-                mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum));
-                if (parsedMesh._binaryInfo) {
-                    mesh._binaryInfo = parsedMesh._binaryInfo;
-                }
-                mesh._delayInfo = [];
-                if (parsedMesh.hasUVs) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind);
-                }
-                if (parsedMesh.hasUVs2) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
-                }
-                if (parsedMesh.hasUVs3) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV3Kind);
-                }
-                if (parsedMesh.hasUVs4) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV4Kind);
-                }
-                if (parsedMesh.hasUVs5) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV5Kind);
-                }
-                if (parsedMesh.hasUVs6) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV6Kind);
-                }
-                if (parsedMesh.hasColors) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
-                }
-                if (parsedMesh.hasMatricesIndices) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
-                }
-                if (parsedMesh.hasMatricesWeights) {
-                    mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
-                }
-                mesh._delayLoadingFunction = importGeometry;
-                if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) {
-                    mesh._checkDelayState();
-                }
-            }
-            else {
-                importGeometry(parsedMesh, mesh);
-            }
-            // Material
-            if (parsedMesh.materialId) {
-                mesh.setMaterialByID(parsedMesh.materialId);
-            }
-            else {
-                mesh.material = null;
-            }
-            // Skeleton
-            if (parsedMesh.skeletonId > -1) {
-                mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
-                if (parsedMesh.numBoneInfluencers) {
-                    mesh.numBoneInfluencers = parsedMesh.numBoneInfluencers;
-                }
-            }
-            // Physics
-            if (parsedMesh.physicsImpostor) {
-                if (!scene.isPhysicsEnabled()) {
-                    scene.enablePhysics();
-                }
-                mesh.setPhysicsState({ impostor: parsedMesh.physicsImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
-            }
-            // Animations
-            if (parsedMesh.animations) {
-                for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
-                    var parsedAnimation = parsedMesh.animations[animationIndex];
-                    mesh.animations.push(BABYLON.Animation.ParseAnimation(parsedAnimation));
-                }
-            }
-            if (parsedMesh.autoAnimate) {
-                scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
-            }
-            // Layer Mask
-            if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) {
-                mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask));
-            }
-            else {
-                mesh.layerMask = 0x0FFFFFFF;
-            }
-            // Instances
-            if (parsedMesh.instances) {
-                for (var index = 0; index < parsedMesh.instances.length; index++) {
-                    var parsedInstance = parsedMesh.instances[index];
-                    var instance = mesh.createInstance(parsedInstance.name);
-                    BABYLON.Tags.AddTagsTo(instance, parsedInstance.tags);
-                    instance.position = BABYLON.Vector3.FromArray(parsedInstance.position);
-                    if (parsedInstance.rotationQuaternion) {
-                        instance.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedInstance.rotationQuaternion);
-                    }
-                    else if (parsedInstance.rotation) {
-                        instance.rotation = BABYLON.Vector3.FromArray(parsedInstance.rotation);
-                    }
-                    instance.scaling = BABYLON.Vector3.FromArray(parsedInstance.scaling);
-                    instance.checkCollisions = mesh.checkCollisions;
-                    if (parsedMesh.animations) {
-                        for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
-                            parsedAnimation = parsedMesh.animations[animationIndex];
-                            instance.animations.push(BABYLON.Animation.ParseAnimation(parsedAnimation));
-                        }
-                    }
-                }
-            }
-            return mesh;
-        };
         var parseActions = function (parsedActions, object, scene) {
             var actionManager = new BABYLON.ActionManager(scene);
             if (object === null)
@@ -494,229 +226,6 @@ var BABYLON;
             }
             return false;
         };
-        var importVertexData = function (parsedVertexData, geometry) {
-            var vertexData = new BABYLON.VertexData();
-            // positions
-            var positions = parsedVertexData.positions;
-            if (positions) {
-                vertexData.set(positions, BABYLON.VertexBuffer.PositionKind);
-            }
-            // normals
-            var normals = parsedVertexData.normals;
-            if (normals) {
-                vertexData.set(normals, BABYLON.VertexBuffer.NormalKind);
-            }
-            // uvs
-            var uvs = parsedVertexData.uvs;
-            if (uvs) {
-                vertexData.set(uvs, BABYLON.VertexBuffer.UVKind);
-            }
-            // uv2s
-            var uv2s = parsedVertexData.uv2s;
-            if (uv2s) {
-                vertexData.set(uv2s, BABYLON.VertexBuffer.UV2Kind);
-            }
-            // uv3s
-            var uv3s = parsedVertexData.uv3s;
-            if (uv3s) {
-                vertexData.set(uv3s, BABYLON.VertexBuffer.UV3Kind);
-            }
-            // uv4s
-            var uv4s = parsedVertexData.uv4s;
-            if (uv4s) {
-                vertexData.set(uv4s, BABYLON.VertexBuffer.UV4Kind);
-            }
-            // uv5s
-            var uv5s = parsedVertexData.uv5s;
-            if (uv5s) {
-                vertexData.set(uv5s, BABYLON.VertexBuffer.UV5Kind);
-            }
-            // uv6s
-            var uv6s = parsedVertexData.uv6s;
-            if (uv6s) {
-                vertexData.set(uv6s, BABYLON.VertexBuffer.UV6Kind);
-            }
-            // colors
-            var colors = parsedVertexData.colors;
-            if (colors) {
-                vertexData.set(checkColors4(colors, positions.length / 3), BABYLON.VertexBuffer.ColorKind);
-            }
-            // matricesIndices
-            var matricesIndices = parsedVertexData.matricesIndices;
-            if (matricesIndices) {
-                vertexData.set(matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind);
-            }
-            // matricesWeights
-            var matricesWeights = parsedVertexData.matricesWeights;
-            if (matricesWeights) {
-                vertexData.set(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind);
-            }
-            // indices
-            var indices = parsedVertexData.indices;
-            if (indices) {
-                vertexData.indices = indices;
-            }
-            geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
-        };
-        var importGeometry = function (parsedGeometry, mesh) {
-            var scene = mesh.getScene();
-            // Geometry
-            var geometryId = parsedGeometry.geometryId;
-            if (geometryId) {
-                var geometry = scene.getGeometryByID(geometryId);
-                if (geometry) {
-                    geometry.applyToMesh(mesh);
-                }
-            }
-            else if (parsedGeometry instanceof ArrayBuffer) {
-                var binaryInfo = mesh._binaryInfo;
-                if (binaryInfo.positionsAttrDesc && binaryInfo.positionsAttrDesc.count > 0) {
-                    var positionsData = new Float32Array(parsedGeometry, binaryInfo.positionsAttrDesc.offset, binaryInfo.positionsAttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positionsData, false);
-                }
-                if (binaryInfo.normalsAttrDesc && binaryInfo.normalsAttrDesc.count > 0) {
-                    var normalsData = new Float32Array(parsedGeometry, binaryInfo.normalsAttrDesc.offset, binaryInfo.normalsAttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normalsData, false);
-                }
-                if (binaryInfo.uvsAttrDesc && binaryInfo.uvsAttrDesc.count > 0) {
-                    var uvsData = new Float32Array(parsedGeometry, binaryInfo.uvsAttrDesc.offset, binaryInfo.uvsAttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, uvsData, false);
-                }
-                if (binaryInfo.uvs2AttrDesc && binaryInfo.uvs2AttrDesc.count > 0) {
-                    var uvs2Data = new Float32Array(parsedGeometry, binaryInfo.uvs2AttrDesc.offset, binaryInfo.uvs2AttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, uvs2Data, false);
-                }
-                if (binaryInfo.uvs3AttrDesc && binaryInfo.uvs3AttrDesc.count > 0) {
-                    var uvs3Data = new Float32Array(parsedGeometry, binaryInfo.uvs3AttrDesc.offset, binaryInfo.uvs3AttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV3Kind, uvs3Data, false);
-                }
-                if (binaryInfo.uvs4AttrDesc && binaryInfo.uvs4AttrDesc.count > 0) {
-                    var uvs4Data = new Float32Array(parsedGeometry, binaryInfo.uvs4AttrDesc.offset, binaryInfo.uvs4AttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV4Kind, uvs4Data, false);
-                }
-                if (binaryInfo.uvs5AttrDesc && binaryInfo.uvs5AttrDesc.count > 0) {
-                    var uvs5Data = new Float32Array(parsedGeometry, binaryInfo.uvs5AttrDesc.offset, binaryInfo.uvs5AttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV5Kind, uvs5Data, false);
-                }
-                if (binaryInfo.uvs6AttrDesc && binaryInfo.uvs6AttrDesc.count > 0) {
-                    var uvs6Data = new Float32Array(parsedGeometry, binaryInfo.uvs6AttrDesc.offset, binaryInfo.uvs6AttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV6Kind, uvs6Data, false);
-                }
-                if (binaryInfo.colorsAttrDesc && binaryInfo.colorsAttrDesc.count > 0) {
-                    var colorsData = new Float32Array(parsedGeometry, binaryInfo.colorsAttrDesc.offset, binaryInfo.colorsAttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, colorsData, false, binaryInfo.colorsAttrDesc.stride);
-                }
-                if (binaryInfo.matricesIndicesAttrDesc && binaryInfo.matricesIndicesAttrDesc.count > 0) {
-                    var matricesIndicesData = new Int32Array(parsedGeometry, binaryInfo.matricesIndicesAttrDesc.offset, binaryInfo.matricesIndicesAttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, matricesIndicesData, false);
-                }
-                if (binaryInfo.matricesWeightsAttrDesc && binaryInfo.matricesWeightsAttrDesc.count > 0) {
-                    var matricesWeightsData = new Float32Array(parsedGeometry, binaryInfo.matricesWeightsAttrDesc.offset, binaryInfo.matricesWeightsAttrDesc.count);
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, matricesWeightsData, false);
-                }
-                if (binaryInfo.indicesAttrDesc && binaryInfo.indicesAttrDesc.count > 0) {
-                    var indicesData = new Int32Array(parsedGeometry, binaryInfo.indicesAttrDesc.offset, binaryInfo.indicesAttrDesc.count);
-                    mesh.setIndices(indicesData);
-                }
-                if (binaryInfo.subMeshesAttrDesc && binaryInfo.subMeshesAttrDesc.count > 0) {
-                    var subMeshesData = new Int32Array(parsedGeometry, binaryInfo.subMeshesAttrDesc.offset, binaryInfo.subMeshesAttrDesc.count * 5);
-                    mesh.subMeshes = [];
-                    for (var i = 0; i < binaryInfo.subMeshesAttrDesc.count; i++) {
-                        var materialIndex = subMeshesData[(i * 5) + 0];
-                        var verticesStart = subMeshesData[(i * 5) + 1];
-                        var verticesCount = subMeshesData[(i * 5) + 2];
-                        var indexStart = subMeshesData[(i * 5) + 3];
-                        var indexCount = subMeshesData[(i * 5) + 4];
-                        var subMesh = new BABYLON.SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh);
-                    }
-                }
-            }
-            else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, parsedGeometry.positions, false);
-                mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, parsedGeometry.normals, false);
-                if (parsedGeometry.uvs) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, parsedGeometry.uvs, false);
-                }
-                if (parsedGeometry.uvs2) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
-                }
-                if (parsedGeometry.uvs3) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV3Kind, parsedGeometry.uvs3, false);
-                }
-                if (parsedGeometry.uvs4) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV4Kind, parsedGeometry.uvs4, false);
-                }
-                if (parsedGeometry.uvs5) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV5Kind, parsedGeometry.uvs5, false);
-                }
-                if (parsedGeometry.uvs6) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.UV6Kind, parsedGeometry.uvs6, false);
-                }
-                if (parsedGeometry.colors) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, checkColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
-                }
-                if (parsedGeometry.matricesIndices) {
-                    if (!parsedGeometry.matricesIndices._isExpanded) {
-                        var floatIndices = [];
-                        for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
-                            var matricesIndex = parsedGeometry.matricesIndices[i];
-                            floatIndices.push(matricesIndex & 0x000000FF);
-                            floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
-                            floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
-                            floatIndices.push(matricesIndex >> 24);
-                        }
-                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, floatIndices, false);
-                    }
-                    else {
-                        delete parsedGeometry.matricesIndices._isExpanded;
-                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
-                    }
-                }
-                if (parsedGeometry.matricesIndicesExtra) {
-                    if (!parsedGeometry.matricesIndicesExtra._isExpanded) {
-                        var floatIndices = [];
-                        for (var i = 0; i < parsedGeometry.matricesIndicesExtra.length; i++) {
-                            var matricesIndex = parsedGeometry.matricesIndicesExtra[i];
-                            floatIndices.push(matricesIndex & 0x000000FF);
-                            floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
-                            floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
-                            floatIndices.push(matricesIndex >> 24);
-                        }
-                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind, floatIndices, false);
-                    }
-                    else {
-                        delete parsedGeometry.matricesIndices._isExpanded;
-                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind, parsedGeometry.matricesIndicesExtra, false);
-                    }
-                }
-                if (parsedGeometry.matricesWeights) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
-                }
-                if (parsedGeometry.matricesWeightsExtra) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsExtraKind, parsedGeometry.matricesWeightsExtra, false);
-                }
-                mesh.setIndices(parsedGeometry.indices);
-            }
-            // SubMeshes
-            if (parsedGeometry.subMeshes) {
-                mesh.subMeshes = [];
-                for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
-                    var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
-                    var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
-                }
-            }
-            // Flat shading
-            if (mesh._shouldGenerateFlatShading) {
-                mesh.convertToFlatShadedMesh();
-                delete mesh._shouldGenerateFlatShading;
-            }
-            // Update
-            mesh.computeWorldMatrix(true);
-            // Octree
-            if (scene._selectionOctree) {
-                scene._selectionOctree.addMesh(mesh);
-            }
-        };
         BABYLON.SceneLoader.RegisterPlugin({
             extensions: ".babylon",
             importMesh: function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
@@ -747,28 +256,28 @@ var BABYLON;
                                             if (parsedGeometryData.id == parsedMesh.geometryId) {
                                                 switch (geometryType) {
                                                     case "boxes":
-                                                        parseBox(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.Box.ParseBox(parsedGeometryData, scene);
                                                         break;
                                                     case "spheres":
-                                                        parseSphere(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.Sphere.ParseSphere(parsedGeometryData, scene);
                                                         break;
                                                     case "cylinders":
-                                                        parseCylinder(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.Cylinder.ParseCylinder(parsedGeometryData, scene);
                                                         break;
                                                     case "toruses":
-                                                        parseTorus(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.Torus.ParseTorus(parsedGeometryData, scene);
                                                         break;
                                                     case "grounds":
-                                                        parseGround(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.Ground.ParseGround(parsedGeometryData, scene);
                                                         break;
                                                     case "planes":
-                                                        parsePlane(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.Plane.ParsePlane(parsedGeometryData, scene);
                                                         break;
                                                     case "torusKnots":
-                                                        parseTorusKnot(parsedGeometryData, scene);
+                                                        BABYLON.Geometry.Primitives.TorusKnot.ParseTorusKnot(parsedGeometryData, scene);
                                                         break;
                                                     case "vertexData":
-                                                        parseVertexData(parsedGeometryData, scene, rootUrl);
+                                                        BABYLON.Geometry.ParseGeometry(parsedGeometryData, scene, rootUrl);
                                                         break;
                                                 }
                                                 found = true;
@@ -820,7 +329,7 @@ var BABYLON;
                                 }
                             }
                         }
-                        var mesh = parseMesh(parsedMesh, scene, rootUrl);
+                        var mesh = mesh.ParseMesh(parsedMesh, scene, rootUrl);
                         meshes.push(mesh);
                     }
                 }
@@ -922,7 +431,7 @@ var BABYLON;
                     if (boxes) {
                         for (index = 0; index < boxes.length; index++) {
                             var parsedBox = boxes[index];
-                            parseBox(parsedBox, scene);
+                            BABYLON.Geometry.Primitives.Box.ParseBox(parsedBox, scene);
                         }
                     }
                     // Spheres
@@ -930,7 +439,7 @@ var BABYLON;
                     if (spheres) {
                         for (index = 0; index < spheres.length; index++) {
                             var parsedSphere = spheres[index];
-                            parseSphere(parsedSphere, scene);
+                            BABYLON.Geometry.Primitives.Sphere.ParseSphere(parsedSphere, scene);
                         }
                     }
                     // Cylinders
@@ -938,7 +447,7 @@ var BABYLON;
                     if (cylinders) {
                         for (index = 0; index < cylinders.length; index++) {
                             var parsedCylinder = cylinders[index];
-                            parseCylinder(parsedCylinder, scene);
+                            BABYLON.Geometry.Primitives.Cylinder.ParseCylinder(parsedCylinder, scene);
                         }
                     }
                     // Toruses
@@ -946,7 +455,7 @@ var BABYLON;
                     if (toruses) {
                         for (index = 0; index < toruses.length; index++) {
                             var parsedTorus = toruses[index];
-                            parseTorus(parsedTorus, scene);
+                            BABYLON.Geometry.Primitives.Torus.ParseTorus(parsedTorus, scene);
                         }
                     }
                     // Grounds
@@ -954,7 +463,7 @@ var BABYLON;
                     if (grounds) {
                         for (index = 0; index < grounds.length; index++) {
                             var parsedGround = grounds[index];
-                            parseGround(parsedGround, scene);
+                            BABYLON.Geometry.Primitives.Ground.ParseGround(parsedGround, scene);
                         }
                     }
                     // Planes
@@ -962,7 +471,7 @@ var BABYLON;
                     if (planes) {
                         for (index = 0; index < planes.length; index++) {
                             var parsedPlane = planes[index];
-                            parsePlane(parsedPlane, scene);
+                            BABYLON.Geometry.Primitives.Plane.ParsePlane(parsedPlane, scene);
                         }
                     }
                     // TorusKnots
@@ -970,7 +479,7 @@ var BABYLON;
                     if (torusKnots) {
                         for (index = 0; index < torusKnots.length; index++) {
                             var parsedTorusKnot = torusKnots[index];
-                            parseTorusKnot(parsedTorusKnot, scene);
+                            BABYLON.Geometry.Primitives.TorusKnot.ParseTorusKnot(parsedTorusKnot, scene);
                         }
                     }
                     // VertexData
@@ -978,14 +487,14 @@ var BABYLON;
                     if (vertexData) {
                         for (index = 0; index < vertexData.length; index++) {
                             var parsedVertexData = vertexData[index];
-                            parseVertexData(parsedVertexData, scene, rootUrl);
+                            BABYLON.Geometry.ParseGeometry(parsedVertexData, scene, rootUrl);
                         }
                     }
                 }
                 // Meshes
                 for (index = 0; index < parsedData.meshes.length; index++) {
                     var parsedMesh = parsedData.meshes[index];
-                    parseMesh(parsedMesh, scene, rootUrl);
+                    BABYLON.Mesh.ParseMesh(parsedMesh, scene, rootUrl);
                 }
                 // Cameras
                 for (index = 0; index < parsedData.cameras.length; index++) {

+ 18 - 647
src/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -34,361 +34,6 @@
         return scene.getGeometryByID(id);
     };
 
-    var parseBox = (parsedBox, scene) => {
-        if (parseGeometry(parsedBox, scene)) {
-            return null; // null since geometry could be something else than a box...
-        }
-
-        var box = new BABYLON.Geometry.Primitives.Box(parsedBox.id, scene, parsedBox.size, parsedBox.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(box, parsedBox.tags);
-
-        scene.pushGeometry(box, true);
-
-        return box;
-    };
-
-    var parseSphere = (parsedSphere, scene) => {
-        if (parseGeometry(parsedSphere, scene)) {
-            return null; // null since geometry could be something else than a sphere...
-        }
-
-        var sphere = new BABYLON.Geometry.Primitives.Sphere(parsedSphere.id, scene, parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(sphere, parsedSphere.tags);
-
-        scene.pushGeometry(sphere, true);
-
-        return sphere;
-    };
-
-    var parseCylinder = (parsedCylinder, scene) => {
-        if (parseGeometry(parsedCylinder, scene)) {
-            return null; // null since geometry could be something else than a cylinder...
-        }
-
-        var cylinder = new BABYLON.Geometry.Primitives.Cylinder(parsedCylinder.id, scene, parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.subdivisions, parsedCylinder.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(cylinder, parsedCylinder.tags);
-
-        scene.pushGeometry(cylinder, true);
-
-        return cylinder;
-    };
-
-    var parseTorus = (parsedTorus, scene) => {
-        if (parseGeometry(parsedTorus, scene)) {
-            return null; // null since geometry could be something else than a torus...
-        }
-
-        var torus = new BABYLON.Geometry.Primitives.Torus(parsedTorus.id, scene, parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(torus, parsedTorus.tags);
-
-        scene.pushGeometry(torus, true);
-
-        return torus;
-    };
-
-    var parseGround = (parsedGround, scene) => {
-        if (parseGeometry(parsedGround, scene)) {
-            return null; // null since geometry could be something else than a ground...
-        }
-
-        var ground = new BABYLON.Geometry.Primitives.Ground(parsedGround.id, scene, parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(ground, parsedGround.tags);
-
-        scene.pushGeometry(ground, true);
-
-        return ground;
-    };
-
-    var parsePlane = (parsedPlane, scene) => {
-        if (parseGeometry(parsedPlane, scene)) {
-            return null; // null since geometry could be something else than a plane...
-        }
-
-        var plane = new BABYLON.Geometry.Primitives.Plane(parsedPlane.id, scene, parsedPlane.size, parsedPlane.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(plane, parsedPlane.tags);
-
-        scene.pushGeometry(plane, true);
-
-        return plane;
-    };
-
-    var parseTorusKnot = (parsedTorusKnot, scene) => {
-        if (parseGeometry(parsedTorusKnot, scene)) {
-            return null; // null since geometry could be something else than a torusKnot...
-        }
-
-        var torusKnot = new BABYLON.Geometry.Primitives.TorusKnot(parsedTorusKnot.id, scene, parsedTorusKnot.radius, parsedTorusKnot.tube, parsedTorusKnot.radialSegments, parsedTorusKnot.tubularSegments, parsedTorusKnot.p, parsedTorusKnot.q, parsedTorusKnot.canBeRegenerated, null);
-        BABYLON.Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
-
-        scene.pushGeometry(torusKnot, true);
-
-        return torusKnot;
-    };
-
-    var parseVertexData = (parsedVertexData, scene, rootUrl) => {
-        if (parseGeometry(parsedVertexData, scene)) {
-            return null; // null since geometry could be a primitive
-        }
-
-        var geometry = new BABYLON.Geometry(parsedVertexData.id, scene);
-
-        BABYLON.Tags.AddTagsTo(geometry, parsedVertexData.tags);
-
-        if (parsedVertexData.delayLoadingFile) {
-            geometry.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
-            geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
-            geometry._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
-
-            geometry._delayInfo = [];
-            if (parsedVertexData.hasUVs) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.UVKind);
-            }
-
-            if (parsedVertexData.hasUVs2) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
-            }
-
-            if (parsedVertexData.hasUVs3) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.UV3Kind);
-            }
-
-            if (parsedVertexData.hasUVs4) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.UV4Kind);
-            }
-
-            if (parsedVertexData.hasUVs5) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.UV5Kind);
-            }
-
-            if (parsedVertexData.hasUVs6) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.UV6Kind);
-            }
-
-            if (parsedVertexData.hasColors) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
-            }
-
-            if (parsedVertexData.hasMatricesIndices) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
-            }
-
-            if (parsedVertexData.hasMatricesWeights) {
-                geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
-            }
-
-            geometry._delayLoadingFunction = importVertexData;
-        } else {
-            importVertexData(parsedVertexData, geometry);
-        }
-
-        scene.pushGeometry(geometry, true);
-
-        return geometry;
-    };
-
-    var parseMesh = (parsedMesh, scene, rootUrl) => {
-        var mesh = new BABYLON.Mesh(parsedMesh.name, scene);
-        mesh.id = parsedMesh.id;
-
-        BABYLON.Tags.AddTagsTo(mesh, parsedMesh.tags);
-
-        mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position);
-
-        if (parsedMesh.rotationQuaternion) {
-            mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion);
-        } else if (parsedMesh.rotation) {
-            mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation);
-        }
-
-        mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling);
-
-        if (parsedMesh.localMatrix) {
-            mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix));
-        } else if (parsedMesh.pivotMatrix) {
-            mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.pivotMatrix));
-        }
-
-        mesh.setEnabled(parsedMesh.isEnabled);
-        mesh.isVisible = parsedMesh.isVisible;
-        mesh.infiniteDistance = parsedMesh.infiniteDistance;
-
-        mesh.showBoundingBox = parsedMesh.showBoundingBox;
-        mesh.showSubMeshesBoundingBox = parsedMesh.showSubMeshesBoundingBox;
-
-        if (parsedMesh.applyFog !== undefined) {
-            mesh.applyFog = parsedMesh.applyFog;
-        }
-
-        if (parsedMesh.pickable !== undefined) {
-            mesh.isPickable = parsedMesh.pickable;
-        }
-
-        if (parsedMesh.alphaIndex !== undefined) {
-            mesh.alphaIndex = parsedMesh.alphaIndex;
-        }
-
-        mesh.receiveShadows = parsedMesh.receiveShadows;
-
-        mesh.billboardMode = parsedMesh.billboardMode;
-
-        if (parsedMesh.visibility !== undefined) {
-            mesh.visibility = parsedMesh.visibility;
-        }
-
-        mesh.checkCollisions = parsedMesh.checkCollisions;
-        mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading;
-
-        // freezeWorldMatrix
-        if (parsedMesh.freezeWorldMatrix) {
-            mesh._waitingFreezeWorldMatrix = parsedMesh.freezeWorldMatrix;
-        }
-
-        // Parent
-        if (parsedMesh.parentId) {
-            mesh._waitingParentId = parsedMesh.parentId;
-        }
-
-        // Actions
-        if (parsedMesh.actions !== undefined) {
-            mesh._waitingActions = parsedMesh.actions;
-        }
-
-        // Geometry
-        mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
-
-        if (parsedMesh.delayLoadingFile) {
-            mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
-            mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
-            mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum));
-
-            if (parsedMesh._binaryInfo) {
-                mesh._binaryInfo = parsedMesh._binaryInfo;
-            }
-
-            mesh._delayInfo = [];
-            if (parsedMesh.hasUVs) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind);
-            }
-
-            if (parsedMesh.hasUVs2) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
-            }
-
-            if (parsedMesh.hasUVs3) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.UV3Kind);
-            }
-
-            if (parsedMesh.hasUVs4) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.UV4Kind);
-            }
-
-            if (parsedMesh.hasUVs5) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.UV5Kind);
-            }
-
-            if (parsedMesh.hasUVs6) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.UV6Kind);
-            }
-
-            if (parsedMesh.hasColors) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
-            }
-
-            if (parsedMesh.hasMatricesIndices) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
-            }
-
-            if (parsedMesh.hasMatricesWeights) {
-                mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
-            }
-
-            mesh._delayLoadingFunction = importGeometry;
-
-            if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) {
-                mesh._checkDelayState();
-            }
-
-        } else {
-            importGeometry(parsedMesh, mesh);
-        }
-
-        // Material
-        if (parsedMesh.materialId) {
-            mesh.setMaterialByID(parsedMesh.materialId);
-        } else {
-            mesh.material = null;
-        }
-
-        // Skeleton
-        if (parsedMesh.skeletonId > -1) {
-            mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
-            if (parsedMesh.numBoneInfluencers) {
-                mesh.numBoneInfluencers = parsedMesh.numBoneInfluencers;
-            }
-        }
-
-        // Physics
-        if (parsedMesh.physicsImpostor) {
-            if (!scene.isPhysicsEnabled()) {
-                scene.enablePhysics();
-            }
-
-            mesh.setPhysicsState({ impostor: parsedMesh.physicsImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
-        }
-
-        // Animations
-        if (parsedMesh.animations) {
-            for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
-                var parsedAnimation = parsedMesh.animations[animationIndex];
-
-                mesh.animations.push(Animation.ParseAnimation(parsedAnimation));
-            }
-        }
-
-        if (parsedMesh.autoAnimate) {
-            scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
-        }
-
-        // Layer Mask
-        if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) {
-            mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask));
-        } else {
-            mesh.layerMask = 0x0FFFFFFF;
-        }
-
-        // Instances
-        if (parsedMesh.instances) {
-            for (var index = 0; index < parsedMesh.instances.length; index++) {
-                var parsedInstance = parsedMesh.instances[index];
-                var instance = mesh.createInstance(parsedInstance.name);
-
-                BABYLON.Tags.AddTagsTo(instance, parsedInstance.tags);
-
-                instance.position = BABYLON.Vector3.FromArray(parsedInstance.position);
-
-                if (parsedInstance.rotationQuaternion) {
-                    instance.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedInstance.rotationQuaternion);
-                } else if (parsedInstance.rotation) {
-                    instance.rotation = BABYLON.Vector3.FromArray(parsedInstance.rotation);
-                }
-
-                instance.scaling = BABYLON.Vector3.FromArray(parsedInstance.scaling);
-
-                instance.checkCollisions = mesh.checkCollisions;
-
-                if (parsedMesh.animations) {
-                    for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
-                        parsedAnimation = parsedMesh.animations[animationIndex];
-
-                        instance.animations.push(Animation.ParseAnimation(parsedAnimation));
-                    }
-                }
-            }
-        }
-
-        return mesh;
-    };
-
     var parseActions = (parsedActions: any, object: AbstractMesh, scene: Scene) => {
         var actionManager = new BABYLON.ActionManager(scene);
         if (object === null)
@@ -621,280 +266,6 @@
         return false;
     };
 
-    var importVertexData = (parsedVertexData, geometry) => {
-        var vertexData = new BABYLON.VertexData();
-
-        // positions
-        var positions = parsedVertexData.positions;
-        if (positions) {
-            vertexData.set(positions, BABYLON.VertexBuffer.PositionKind);
-        }
-
-        // normals
-        var normals = parsedVertexData.normals;
-        if (normals) {
-            vertexData.set(normals, BABYLON.VertexBuffer.NormalKind);
-        }
-
-        // uvs
-        var uvs = parsedVertexData.uvs;
-        if (uvs) {
-            vertexData.set(uvs, BABYLON.VertexBuffer.UVKind);
-        }
-
-        // uv2s
-        var uv2s = parsedVertexData.uv2s;
-        if (uv2s) {
-            vertexData.set(uv2s, BABYLON.VertexBuffer.UV2Kind);
-        }
-
-        // uv3s
-        var uv3s = parsedVertexData.uv3s;
-        if (uv3s) {
-            vertexData.set(uv3s, BABYLON.VertexBuffer.UV3Kind);
-        }
-
-        // uv4s
-        var uv4s = parsedVertexData.uv4s;
-        if (uv4s) {
-            vertexData.set(uv4s, BABYLON.VertexBuffer.UV4Kind);
-        }
-
-        // uv5s
-        var uv5s = parsedVertexData.uv5s;
-        if (uv5s) {
-            vertexData.set(uv5s, BABYLON.VertexBuffer.UV5Kind);
-        }
-
-        // uv6s
-        var uv6s = parsedVertexData.uv6s;
-        if (uv6s) {
-            vertexData.set(uv6s, BABYLON.VertexBuffer.UV6Kind);
-        }
-
-        // colors
-        var colors = parsedVertexData.colors;
-        if (colors) {
-            vertexData.set(checkColors4(colors, positions.length / 3), BABYLON.VertexBuffer.ColorKind);
-        }
-
-        // matricesIndices
-        var matricesIndices = parsedVertexData.matricesIndices;
-        if (matricesIndices) {
-            vertexData.set(matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind);
-        }
-
-        // matricesWeights
-        var matricesWeights = parsedVertexData.matricesWeights;
-        if (matricesWeights) {
-            vertexData.set(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind);
-        }
-
-        // indices
-        var indices = parsedVertexData.indices;
-        if (indices) {
-            vertexData.indices = indices;
-        }
-
-        geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
-    };
-
-    var importGeometry = (parsedGeometry, mesh) => {
-        var scene = mesh.getScene();
-
-        // Geometry
-        var geometryId = parsedGeometry.geometryId;
-        if (geometryId) {
-            var geometry = scene.getGeometryByID(geometryId);
-            if (geometry) {
-                geometry.applyToMesh(mesh);
-            }
-        } else if (parsedGeometry instanceof ArrayBuffer) {
-
-            var binaryInfo = mesh._binaryInfo;
-
-            if (binaryInfo.positionsAttrDesc && binaryInfo.positionsAttrDesc.count > 0) {
-                var positionsData = new Float32Array(parsedGeometry, binaryInfo.positionsAttrDesc.offset, binaryInfo.positionsAttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positionsData, false);
-            }
-
-            if (binaryInfo.normalsAttrDesc && binaryInfo.normalsAttrDesc.count > 0) {
-                var normalsData = new Float32Array(parsedGeometry, binaryInfo.normalsAttrDesc.offset, binaryInfo.normalsAttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normalsData, false);
-            }
-
-            if (binaryInfo.uvsAttrDesc && binaryInfo.uvsAttrDesc.count > 0) {
-                var uvsData = new Float32Array(parsedGeometry, binaryInfo.uvsAttrDesc.offset, binaryInfo.uvsAttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, uvsData, false);
-            }
-
-            if (binaryInfo.uvs2AttrDesc && binaryInfo.uvs2AttrDesc.count > 0) {
-                var uvs2Data = new Float32Array(parsedGeometry, binaryInfo.uvs2AttrDesc.offset, binaryInfo.uvs2AttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, uvs2Data, false);
-            }
-
-            if (binaryInfo.uvs3AttrDesc && binaryInfo.uvs3AttrDesc.count > 0) {
-                var uvs3Data = new Float32Array(parsedGeometry, binaryInfo.uvs3AttrDesc.offset, binaryInfo.uvs3AttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV3Kind, uvs3Data, false);
-            }
-
-            if (binaryInfo.uvs4AttrDesc && binaryInfo.uvs4AttrDesc.count > 0) {
-                var uvs4Data = new Float32Array(parsedGeometry, binaryInfo.uvs4AttrDesc.offset, binaryInfo.uvs4AttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV4Kind, uvs4Data, false);
-            }
-
-            if (binaryInfo.uvs5AttrDesc && binaryInfo.uvs5AttrDesc.count > 0) {
-                var uvs5Data = new Float32Array(parsedGeometry, binaryInfo.uvs5AttrDesc.offset, binaryInfo.uvs5AttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV5Kind, uvs5Data, false);
-            }
-
-            if (binaryInfo.uvs6AttrDesc && binaryInfo.uvs6AttrDesc.count > 0) {
-                var uvs6Data = new Float32Array(parsedGeometry, binaryInfo.uvs6AttrDesc.offset, binaryInfo.uvs6AttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV6Kind, uvs6Data, false);
-            }
-
-            if (binaryInfo.colorsAttrDesc && binaryInfo.colorsAttrDesc.count > 0) {
-                var colorsData = new Float32Array(parsedGeometry, binaryInfo.colorsAttrDesc.offset, binaryInfo.colorsAttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, colorsData, false, binaryInfo.colorsAttrDesc.stride);
-            }
-
-            if (binaryInfo.matricesIndicesAttrDesc && binaryInfo.matricesIndicesAttrDesc.count > 0) {
-                var matricesIndicesData = new Int32Array(parsedGeometry, binaryInfo.matricesIndicesAttrDesc.offset, binaryInfo.matricesIndicesAttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, matricesIndicesData, false);
-            }
-
-            if (binaryInfo.matricesWeightsAttrDesc && binaryInfo.matricesWeightsAttrDesc.count > 0) {
-                var matricesWeightsData = new Float32Array(parsedGeometry, binaryInfo.matricesWeightsAttrDesc.offset, binaryInfo.matricesWeightsAttrDesc.count);
-                mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, matricesWeightsData, false);
-            }
-
-            if (binaryInfo.indicesAttrDesc && binaryInfo.indicesAttrDesc.count > 0) {
-                var indicesData = new Int32Array(parsedGeometry, binaryInfo.indicesAttrDesc.offset, binaryInfo.indicesAttrDesc.count);
-                mesh.setIndices(indicesData);
-            }
-
-            if (binaryInfo.subMeshesAttrDesc && binaryInfo.subMeshesAttrDesc.count > 0) {
-                var subMeshesData = new Int32Array(parsedGeometry, binaryInfo.subMeshesAttrDesc.offset, binaryInfo.subMeshesAttrDesc.count * 5);
-
-                mesh.subMeshes = [];
-                for (var i = 0; i < binaryInfo.subMeshesAttrDesc.count; i++) {
-                    var materialIndex = subMeshesData[(i * 5) + 0];
-                    var verticesStart = subMeshesData[(i * 5) + 1];
-                    var verticesCount = subMeshesData[(i * 5) + 2];
-                    var indexStart = subMeshesData[(i * 5) + 3];
-                    var indexCount = subMeshesData[(i * 5) + 4];
-
-                    var subMesh = new BABYLON.SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh);
-                }
-            }
-        } else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
-            mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, parsedGeometry.positions, false);
-            mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, parsedGeometry.normals, false);
-
-            if (parsedGeometry.uvs) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, parsedGeometry.uvs, false);
-            }
-
-            if (parsedGeometry.uvs2) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
-            }
-
-            if (parsedGeometry.uvs3) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV3Kind, parsedGeometry.uvs3, false);
-            }
-
-            if (parsedGeometry.uvs4) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV4Kind, parsedGeometry.uvs4, false);
-            }
-
-            if (parsedGeometry.uvs5) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV5Kind, parsedGeometry.uvs5, false);
-            }
-
-            if (parsedGeometry.uvs6) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.UV6Kind, parsedGeometry.uvs6, false);
-            }
-
-            if (parsedGeometry.colors) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, checkColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
-            }
-
-            if (parsedGeometry.matricesIndices) {
-                if (!parsedGeometry.matricesIndices._isExpanded) {
-                    var floatIndices = [];
-
-                    for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
-                        var matricesIndex = parsedGeometry.matricesIndices[i];
-
-                        floatIndices.push(matricesIndex & 0x000000FF);
-                        floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
-                        floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
-                        floatIndices.push(matricesIndex >> 24);
-                    }
-
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, floatIndices, false);
-                } else {
-                    delete parsedGeometry.matricesIndices._isExpanded;
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
-                }
-            }
-
-            if (parsedGeometry.matricesIndicesExtra) {
-                if (!parsedGeometry.matricesIndicesExtra._isExpanded) {
-                    var floatIndices = [];
-
-                    for (var i = 0; i < parsedGeometry.matricesIndicesExtra.length; i++) {
-                        var matricesIndex = parsedGeometry.matricesIndicesExtra[i];
-
-                        floatIndices.push(matricesIndex & 0x000000FF);
-                        floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
-                        floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
-                        floatIndices.push(matricesIndex >> 24);
-                    }
-
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind, floatIndices, false);
-                } else {
-                    delete parsedGeometry.matricesIndices._isExpanded;
-                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind, parsedGeometry.matricesIndicesExtra, false);
-                }
-            }
-
-            if (parsedGeometry.matricesWeights) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
-            }
-
-            if (parsedGeometry.matricesWeightsExtra) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsExtraKind, parsedGeometry.matricesWeightsExtra, false);
-            }
-
-            mesh.setIndices(parsedGeometry.indices);
-        }
-
-        // SubMeshes
-        if (parsedGeometry.subMeshes) {
-            mesh.subMeshes = [];
-            for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
-                var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
-
-                var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
-            }
-        }
-
-        // Flat shading
-        if (mesh._shouldGenerateFlatShading) {
-            mesh.convertToFlatShadedMesh();
-            delete mesh._shouldGenerateFlatShading;
-        }
-
-        // Update
-        mesh.computeWorldMatrix(true);
-
-        // Octree
-        if (scene._selectionOctree) {
-            scene._selectionOctree.addMesh(mesh);
-        }
-    };
-
     BABYLON.SceneLoader.RegisterPlugin({
         extensions: ".babylon",
         importMesh: (meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]): boolean => {
@@ -927,28 +298,28 @@
                                         if (parsedGeometryData.id == parsedMesh.geometryId) {
                                             switch (geometryType) {
                                                 case "boxes":
-                                                    parseBox(parsedGeometryData, scene);
+                                                    Geometry.Primitives.Box.ParseBox(parsedGeometryData, scene);
                                                     break;
                                                 case "spheres":
-                                                    parseSphere(parsedGeometryData, scene);
+                                                    Geometry.Primitives.Sphere.ParseSphere(parsedGeometryData, scene);
                                                     break;
                                                 case "cylinders":
-                                                    parseCylinder(parsedGeometryData, scene);
+                                                    Geometry.Primitives.Cylinder.ParseCylinder(parsedGeometryData, scene);
                                                     break;
                                                 case "toruses":
-                                                    parseTorus(parsedGeometryData, scene);
+                                                    Geometry.Primitives.Torus.ParseTorus(parsedGeometryData, scene);
                                                     break;
                                                 case "grounds":
-                                                    parseGround(parsedGeometryData, scene);
+                                                    Geometry.Primitives.Ground.ParseGround(parsedGeometryData, scene);
                                                     break;
                                                 case "planes":
-                                                    parsePlane(parsedGeometryData, scene);
+                                                    Geometry.Primitives.Plane.ParsePlane(parsedGeometryData, scene);
                                                     break;
                                                 case "torusKnots":
-                                                    parseTorusKnot(parsedGeometryData, scene);
+                                                    Geometry.Primitives.TorusKnot.ParseTorusKnot(parsedGeometryData, scene);
                                                     break;
                                                 case "vertexData":
-                                                    parseVertexData(parsedGeometryData, scene, rootUrl);
+                                                    Geometry.ParseGeometry(parsedGeometryData, scene, rootUrl);
                                                     break;
                                             }
                                             found = true;
@@ -1009,7 +380,7 @@
                         }
                     }
 
-                    var mesh = parseMesh(parsedMesh, scene, rootUrl);
+                    var mesh = mesh.ParseMesh(parsedMesh, scene, rootUrl);
                     meshes.push(mesh);
                 }
             }
@@ -1124,7 +495,7 @@
                 if (boxes) {
                     for (index = 0; index < boxes.length; index++) {
                         var parsedBox = boxes[index];
-                        parseBox(parsedBox, scene);
+                        Geometry.Primitives.Box.ParseBox(parsedBox, scene);
                     }
                 }
 
@@ -1133,7 +504,7 @@
                 if (spheres) {
                     for (index = 0; index < spheres.length; index++) {
                         var parsedSphere = spheres[index];
-                        parseSphere(parsedSphere, scene);
+                        Geometry.Primitives.Sphere.ParseSphere(parsedSphere, scene);
                     }
                 }
 
@@ -1142,7 +513,7 @@
                 if (cylinders) {
                     for (index = 0; index < cylinders.length; index++) {
                         var parsedCylinder = cylinders[index];
-                        parseCylinder(parsedCylinder, scene);
+                        Geometry.Primitives.Cylinder.ParseCylinder(parsedCylinder, scene);
                     }
                 }
 
@@ -1151,7 +522,7 @@
                 if (toruses) {
                     for (index = 0; index < toruses.length; index++) {
                         var parsedTorus = toruses[index];
-                        parseTorus(parsedTorus, scene);
+                        Geometry.Primitives.Torus.ParseTorus(parsedTorus, scene);
                     }
                 }
 
@@ -1160,7 +531,7 @@
                 if (grounds) {
                     for (index = 0; index < grounds.length; index++) {
                         var parsedGround = grounds[index];
-                        parseGround(parsedGround, scene);
+                        Geometry.Primitives.Ground.ParseGround(parsedGround, scene);
                     }
                 }
 
@@ -1169,7 +540,7 @@
                 if (planes) {
                     for (index = 0; index < planes.length; index++) {
                         var parsedPlane = planes[index];
-                        parsePlane(parsedPlane, scene);
+                        Geometry.Primitives.Plane.ParsePlane(parsedPlane, scene);
                     }
                 }
 
@@ -1178,7 +549,7 @@
                 if (torusKnots) {
                     for (index = 0; index < torusKnots.length; index++) {
                         var parsedTorusKnot = torusKnots[index];
-                        parseTorusKnot(parsedTorusKnot, scene);
+                        Geometry.Primitives.TorusKnot.ParseTorusKnot(parsedTorusKnot, scene);
                     }
                 }
 
@@ -1187,7 +558,7 @@
                 if (vertexData) {
                     for (index = 0; index < vertexData.length; index++) {
                         var parsedVertexData = vertexData[index];
-                        parseVertexData(parsedVertexData, scene, rootUrl);
+                        Geometry.ParseGeometry(parsedVertexData, scene, rootUrl);
                     }
                 }
             }
@@ -1195,7 +566,7 @@
             // Meshes
             for (index = 0; index < parsedData.meshes.length; index++) {
                 var parsedMesh = parsedData.meshes[index];
-                parseMesh(parsedMesh, scene, rootUrl);
+                Mesh.ParseMesh(parsedMesh, scene, rootUrl);
             }
 
             // Cameras

+ 15 - 0
src/Math/babylon.math.js

@@ -260,6 +260,21 @@ var BABYLON;
         Color4.FromInts = function (r, g, b, a) {
             return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
         };
+        Color4.CheckColors4 = function (colors, count) {
+            // Check if color3 was used
+            if (colors.length === count * 3) {
+                var colors4 = [];
+                for (var index = 0; index < colors.length; index += 3) {
+                    var newIndex = (index / 3) * 4;
+                    colors4[newIndex] = colors[index];
+                    colors4[newIndex + 1] = colors[index + 1];
+                    colors4[newIndex + 2] = colors[index + 2];
+                    colors4[newIndex + 3] = 1.0;
+                }
+                return colors4;
+            }
+            return colors;
+        };
         return Color4;
     })();
     BABYLON.Color4 = Color4;

+ 19 - 1
src/Math/babylon.math.ts

@@ -322,6 +322,24 @@
         public static FromInts(r: number, g: number, b: number, a: number): Color4 {
             return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
         }
+
+        public static CheckColors4(colors: number[], count: number): number[] {
+            // Check if color3 was used
+            if (colors.length === count * 3) {
+                var colors4 = [];
+                for (var index = 0; index < colors.length; index += 3) {
+                    var newIndex = (index / 3) * 4;
+                    colors4[newIndex] = colors[index];
+                    colors4[newIndex + 1] = colors[index + 1];
+                    colors4[newIndex + 2] = colors[index + 2];
+                    colors4[newIndex + 3] = 1.0;
+                }
+
+                return colors4;
+            }
+
+            return colors;
+        }
     }
 
     export class Vector2 {
@@ -2429,7 +2447,7 @@
             var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
 
             return Matrix.Compose(resultScale, resultRotation, resultTranslation);
-        }    
+        }
 
         public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
             var result = Matrix.Zero();

+ 1 - 1
src/Mesh/babylon.abstractMesh.ts

@@ -162,7 +162,7 @@
             return 0;
         }
 
-        public getIndices(): number[] {
+        public getIndices(): number[] | Int32Array {
             return null;
         }
 

+ 268 - 0
src/Mesh/babylon.geometry.js

@@ -394,6 +394,211 @@ var BABYLON;
                 return v.toString(16);
             });
         };
+        Geometry.ImportGeometry = function (parsedGeometry, mesh) {
+            var scene = mesh.getScene();
+            // Geometry
+            var geometryId = parsedGeometry.geometryId;
+            if (geometryId) {
+                var geometry = scene.getGeometryByID(geometryId);
+                if (geometry) {
+                    geometry.applyToMesh(mesh);
+                }
+            }
+            else if (parsedGeometry instanceof ArrayBuffer) {
+                var binaryInfo = mesh._binaryInfo;
+                if (binaryInfo.positionsAttrDesc && binaryInfo.positionsAttrDesc.count > 0) {
+                    var positionsData = new Float32Array(parsedGeometry, binaryInfo.positionsAttrDesc.offset, binaryInfo.positionsAttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positionsData, false);
+                }
+                if (binaryInfo.normalsAttrDesc && binaryInfo.normalsAttrDesc.count > 0) {
+                    var normalsData = new Float32Array(parsedGeometry, binaryInfo.normalsAttrDesc.offset, binaryInfo.normalsAttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normalsData, false);
+                }
+                if (binaryInfo.uvsAttrDesc && binaryInfo.uvsAttrDesc.count > 0) {
+                    var uvsData = new Float32Array(parsedGeometry, binaryInfo.uvsAttrDesc.offset, binaryInfo.uvsAttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, uvsData, false);
+                }
+                if (binaryInfo.uvs2AttrDesc && binaryInfo.uvs2AttrDesc.count > 0) {
+                    var uvs2Data = new Float32Array(parsedGeometry, binaryInfo.uvs2AttrDesc.offset, binaryInfo.uvs2AttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, uvs2Data, false);
+                }
+                if (binaryInfo.uvs3AttrDesc && binaryInfo.uvs3AttrDesc.count > 0) {
+                    var uvs3Data = new Float32Array(parsedGeometry, binaryInfo.uvs3AttrDesc.offset, binaryInfo.uvs3AttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV3Kind, uvs3Data, false);
+                }
+                if (binaryInfo.uvs4AttrDesc && binaryInfo.uvs4AttrDesc.count > 0) {
+                    var uvs4Data = new Float32Array(parsedGeometry, binaryInfo.uvs4AttrDesc.offset, binaryInfo.uvs4AttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV4Kind, uvs4Data, false);
+                }
+                if (binaryInfo.uvs5AttrDesc && binaryInfo.uvs5AttrDesc.count > 0) {
+                    var uvs5Data = new Float32Array(parsedGeometry, binaryInfo.uvs5AttrDesc.offset, binaryInfo.uvs5AttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV5Kind, uvs5Data, false);
+                }
+                if (binaryInfo.uvs6AttrDesc && binaryInfo.uvs6AttrDesc.count > 0) {
+                    var uvs6Data = new Float32Array(parsedGeometry, binaryInfo.uvs6AttrDesc.offset, binaryInfo.uvs6AttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV6Kind, uvs6Data, false);
+                }
+                if (binaryInfo.colorsAttrDesc && binaryInfo.colorsAttrDesc.count > 0) {
+                    var colorsData = new Float32Array(parsedGeometry, binaryInfo.colorsAttrDesc.offset, binaryInfo.colorsAttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, colorsData, false, binaryInfo.colorsAttrDesc.stride);
+                }
+                if (binaryInfo.matricesIndicesAttrDesc && binaryInfo.matricesIndicesAttrDesc.count > 0) {
+                    var matricesIndicesData = new Int32Array(parsedGeometry, binaryInfo.matricesIndicesAttrDesc.offset, binaryInfo.matricesIndicesAttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, matricesIndicesData, false);
+                }
+                if (binaryInfo.matricesWeightsAttrDesc && binaryInfo.matricesWeightsAttrDesc.count > 0) {
+                    var matricesWeightsData = new Float32Array(parsedGeometry, binaryInfo.matricesWeightsAttrDesc.offset, binaryInfo.matricesWeightsAttrDesc.count);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, matricesWeightsData, false);
+                }
+                if (binaryInfo.indicesAttrDesc && binaryInfo.indicesAttrDesc.count > 0) {
+                    var indicesData = new Int32Array(parsedGeometry, binaryInfo.indicesAttrDesc.offset, binaryInfo.indicesAttrDesc.count);
+                    mesh.setIndices(indicesData);
+                }
+                if (binaryInfo.subMeshesAttrDesc && binaryInfo.subMeshesAttrDesc.count > 0) {
+                    var subMeshesData = new Int32Array(parsedGeometry, binaryInfo.subMeshesAttrDesc.offset, binaryInfo.subMeshesAttrDesc.count * 5);
+                    mesh.subMeshes = [];
+                    for (var i = 0; i < binaryInfo.subMeshesAttrDesc.count; i++) {
+                        var materialIndex = subMeshesData[(i * 5) + 0];
+                        var verticesStart = subMeshesData[(i * 5) + 1];
+                        var verticesCount = subMeshesData[(i * 5) + 2];
+                        var indexStart = subMeshesData[(i * 5) + 3];
+                        var indexCount = subMeshesData[(i * 5) + 4];
+                        var subMesh = new BABYLON.SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh);
+                    }
+                }
+            }
+            else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
+                mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, parsedGeometry.positions, false);
+                mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, parsedGeometry.normals, false);
+                if (parsedGeometry.uvs) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UVKind, parsedGeometry.uvs, false);
+                }
+                if (parsedGeometry.uvs2) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
+                }
+                if (parsedGeometry.uvs3) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV3Kind, parsedGeometry.uvs3, false);
+                }
+                if (parsedGeometry.uvs4) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV4Kind, parsedGeometry.uvs4, false);
+                }
+                if (parsedGeometry.uvs5) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV5Kind, parsedGeometry.uvs5, false);
+                }
+                if (parsedGeometry.uvs6) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.UV6Kind, parsedGeometry.uvs6, false);
+                }
+                if (parsedGeometry.colors) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, BABYLON.Color4.CheckColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
+                }
+                if (parsedGeometry.matricesIndices) {
+                    if (!parsedGeometry.matricesIndices._isExpanded) {
+                        var floatIndices = [];
+                        for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
+                            var matricesIndex = parsedGeometry.matricesIndices[i];
+                            floatIndices.push(matricesIndex & 0x000000FF);
+                            floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
+                            floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
+                            floatIndices.push(matricesIndex >> 24);
+                        }
+                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, floatIndices, false);
+                    }
+                    else {
+                        delete parsedGeometry.matricesIndices._isExpanded;
+                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
+                    }
+                }
+                if (parsedGeometry.matricesIndicesExtra) {
+                    if (!parsedGeometry.matricesIndicesExtra._isExpanded) {
+                        var floatIndices = [];
+                        for (var i = 0; i < parsedGeometry.matricesIndicesExtra.length; i++) {
+                            var matricesIndex = parsedGeometry.matricesIndicesExtra[i];
+                            floatIndices.push(matricesIndex & 0x000000FF);
+                            floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
+                            floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
+                            floatIndices.push(matricesIndex >> 24);
+                        }
+                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind, floatIndices, false);
+                    }
+                    else {
+                        delete parsedGeometry.matricesIndices._isExpanded;
+                        mesh.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesExtraKind, parsedGeometry.matricesIndicesExtra, false);
+                    }
+                }
+                if (parsedGeometry.matricesWeights) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
+                }
+                if (parsedGeometry.matricesWeightsExtra) {
+                    mesh.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsExtraKind, parsedGeometry.matricesWeightsExtra, false);
+                }
+                mesh.setIndices(parsedGeometry.indices);
+            }
+            // SubMeshes
+            if (parsedGeometry.subMeshes) {
+                mesh.subMeshes = [];
+                for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
+                    var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
+                    var subMesh = new BABYLON.SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
+                }
+            }
+            // Flat shading
+            if (mesh._shouldGenerateFlatShading) {
+                mesh.convertToFlatShadedMesh();
+                delete mesh._shouldGenerateFlatShading;
+            }
+            // Update
+            mesh.computeWorldMatrix(true);
+            // Octree
+            if (scene['_selectionOctree']) {
+                scene['_selectionOctree'].addMesh(mesh);
+            }
+        };
+        Geometry.ParseGeometry = function (parsedVertexData, scene, rootUrl) {
+            if (scene.getGeometryByID(parsedVertexData.id)) {
+                return null; // null since geometry could be something else than a box...
+            }
+            var geometry = new Geometry(parsedVertexData.id, scene);
+            BABYLON.Tags.AddTagsTo(geometry, parsedVertexData.tags);
+            if (parsedVertexData.delayLoadingFile) {
+                geometry.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
+                geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
+                geometry._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
+                geometry._delayInfo = [];
+                if (parsedVertexData.hasUVs) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.UVKind);
+                }
+                if (parsedVertexData.hasUVs2) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
+                }
+                if (parsedVertexData.hasUVs3) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV3Kind);
+                }
+                if (parsedVertexData.hasUVs4) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV4Kind);
+                }
+                if (parsedVertexData.hasUVs5) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV5Kind);
+                }
+                if (parsedVertexData.hasUVs6) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.UV6Kind);
+                }
+                if (parsedVertexData.hasColors) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
+                }
+                if (parsedVertexData.hasMatricesIndices) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
+                }
+                if (parsedVertexData.hasMatricesWeights) {
+                    geometry._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
+                }
+                geometry._delayLoadingFunction = BABYLON.VertexData.ImportVertexData;
+            }
+            else {
+                BABYLON.VertexData.ImportVertexData(parsedVertexData, geometry);
+            }
+            scene.pushGeometry(geometry, true);
+            return geometry;
+        };
         return Geometry;
     })();
     BABYLON.Geometry = Geometry;
@@ -483,6 +688,15 @@ var BABYLON;
                 Box.prototype.copy = function (id) {
                     return new Box(id, this.getScene(), this.size, this.canBeRegenerated(), null, this.side);
                 };
+                Box.ParseBox = function (parsedBox, scene) {
+                    if (scene.getGeometryByID(parsedBox.id)) {
+                        return null; // null since geometry could be something else than a box...
+                    }
+                    var box = new Geometry.Primitives.Box(parsedBox.id, scene, parsedBox.size, parsedBox.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(box, parsedBox.tags);
+                    scene.pushGeometry(box, true);
+                    return box;
+                };
                 return Box;
             })(_Primitive);
             Primitives.Box = Box;
@@ -501,6 +715,15 @@ var BABYLON;
                 Sphere.prototype.copy = function (id) {
                     return new Sphere(id, this.getScene(), this.segments, this.diameter, this.canBeRegenerated(), null, this.side);
                 };
+                Sphere.ParseSphere = function (parsedSphere, scene) {
+                    if (scene.getGeometryByID(parsedSphere.id)) {
+                        return null; // null since geometry could be something else than a sphere...
+                    }
+                    var sphere = new Geometry.Primitives.Sphere(parsedSphere.id, scene, parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(sphere, parsedSphere.tags);
+                    scene.pushGeometry(sphere, true);
+                    return sphere;
+                };
                 return Sphere;
             })(_Primitive);
             Primitives.Sphere = Sphere;
@@ -541,6 +764,15 @@ var BABYLON;
                 Cylinder.prototype.copy = function (id) {
                     return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions, this.canBeRegenerated(), null, this.side);
                 };
+                Cylinder.ParseCylinder = function (parsedCylinder, scene) {
+                    if (scene.getGeometryByID(parsedCylinder.id)) {
+                        return null; // null since geometry could be something else than a cylinder...
+                    }
+                    var cylinder = new Geometry.Primitives.Cylinder(parsedCylinder.id, scene, parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.subdivisions, parsedCylinder.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(cylinder, parsedCylinder.tags);
+                    scene.pushGeometry(cylinder, true);
+                    return cylinder;
+                };
                 return Cylinder;
             })(_Primitive);
             Primitives.Cylinder = Cylinder;
@@ -560,6 +792,15 @@ var BABYLON;
                 Torus.prototype.copy = function (id) {
                     return new Torus(id, this.getScene(), this.diameter, this.thickness, this.tessellation, this.canBeRegenerated(), null, this.side);
                 };
+                Torus.ParseTorus = function (parsedTorus, scene) {
+                    if (scene.getGeometryByID(parsedTorus.id)) {
+                        return null; // null since geometry could be something else than a torus...
+                    }
+                    var torus = new Geometry.Primitives.Torus(parsedTorus.id, scene, parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(torus, parsedTorus.tags);
+                    scene.pushGeometry(torus, true);
+                    return torus;
+                };
                 return Torus;
             })(_Primitive);
             Primitives.Torus = Torus;
@@ -577,6 +818,15 @@ var BABYLON;
                 Ground.prototype.copy = function (id) {
                     return new Ground(id, this.getScene(), this.width, this.height, this.subdivisions, this.canBeRegenerated(), null);
                 };
+                Ground.ParseGround = function (parsedGround, scene) {
+                    if (scene.getGeometryByID(parsedGround.id)) {
+                        return null; // null since geometry could be something else than a ground...
+                    }
+                    var ground = new Geometry.Primitives.Ground(parsedGround.id, scene, parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(ground, parsedGround.tags);
+                    scene.pushGeometry(ground, true);
+                    return ground;
+                };
                 return Ground;
             })(_Primitive);
             Primitives.Ground = Ground;
@@ -614,6 +864,15 @@ var BABYLON;
                 Plane.prototype.copy = function (id) {
                     return new Plane(id, this.getScene(), this.size, this.canBeRegenerated(), null, this.side);
                 };
+                Plane.ParsePlane = function (parsedPlane, scene) {
+                    if (scene.getGeometryByID(parsedPlane.id)) {
+                        return null; // null since geometry could be something else than a ground...
+                    }
+                    var plane = new Geometry.Primitives.Plane(parsedPlane.id, scene, parsedPlane.size, parsedPlane.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(plane, parsedPlane.tags);
+                    scene.pushGeometry(plane, true);
+                    return plane;
+                };
                 return Plane;
             })(_Primitive);
             Primitives.Plane = Plane;
@@ -636,6 +895,15 @@ var BABYLON;
                 TorusKnot.prototype.copy = function (id) {
                     return new TorusKnot(id, this.getScene(), this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q, this.canBeRegenerated(), null, this.side);
                 };
+                TorusKnot.ParseTorusKnot = function (parsedTorusKnot, scene) {
+                    if (scene.getGeometryByID(parsedTorusKnot.id)) {
+                        return null; // null since geometry could be something else than a ground...
+                    }
+                    var torusKnot = new Geometry.Primitives.TorusKnot(parsedTorusKnot.id, scene, parsedTorusKnot.radius, parsedTorusKnot.tube, parsedTorusKnot.radialSegments, parsedTorusKnot.tubularSegments, parsedTorusKnot.p, parsedTorusKnot.q, parsedTorusKnot.canBeRegenerated, null);
+                    BABYLON.Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
+                    scene.pushGeometry(torusKnot, true);
+                    return torusKnot;
+                };
                 return TorusKnot;
             })(_Primitive);
             Primitives.TorusKnot = TorusKnot;

+ 354 - 6
src/Mesh/babylon.geometry.ts

@@ -11,7 +11,7 @@
         private _engine: Engine;
         private _meshes: Mesh[];
         private _totalVertices = 0;
-        private _indices: number[];
+        private _indices: number[] | Int32Array;
         private _vertexBuffers;
         private _isDisposed = false;
         private _extend: { minimum: Vector3, maximum: Vector3 };
@@ -209,7 +209,7 @@
             return result;
         }
 
-        public setIndices(indices: number[], totalVertices?: number): void {
+        public setIndices(indices: number[] | Int32Array, totalVertices?: number): void {
             if (this._indexBuffer) {
                 this._engine._releaseBuffer(this._indexBuffer);
             }
@@ -239,7 +239,7 @@
             return this._indices.length;
         }
 
-        public getIndices(copyWhenShared?: boolean): number[] {
+        public getIndices(copyWhenShared?: boolean): number[] | Int32Array {
             if (!this.isReady()) {
                 return null;
             }
@@ -420,7 +420,7 @@
             this._delayLoadingFunction = null;
             this._delayInfo = [];
 
-            this._boundingInfo = null; 
+            this._boundingInfo = null;
 
             this._scene.removeGeometry(this);
             this._isDisposed = true;
@@ -433,7 +433,7 @@
 
             var indices = this.getIndices();
             for (var index = 0; index < indices.length; index++) {
-                vertexData.indices.push(indices[index]);
+                (<number[]>vertexData.indices).push(indices[index]);
             }
 
             var updatable = false;
@@ -490,6 +490,263 @@
                 return v.toString(16);
             });
         }
+
+        public static ImportGeometry(parsedGeometry: any, mesh: Mesh): void {
+            var scene = mesh.getScene();
+
+            // Geometry
+            var geometryId = parsedGeometry.geometryId;
+            if (geometryId) {
+                var geometry = scene.getGeometryByID(geometryId);
+                if (geometry) {
+                    geometry.applyToMesh(mesh);
+                }
+            } else if (parsedGeometry instanceof ArrayBuffer) {
+
+                var binaryInfo = mesh._binaryInfo;
+
+                if (binaryInfo.positionsAttrDesc && binaryInfo.positionsAttrDesc.count > 0) {
+                    var positionsData = new Float32Array(parsedGeometry, binaryInfo.positionsAttrDesc.offset, binaryInfo.positionsAttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.PositionKind, positionsData, false);
+                }
+
+                if (binaryInfo.normalsAttrDesc && binaryInfo.normalsAttrDesc.count > 0) {
+                    var normalsData = new Float32Array(parsedGeometry, binaryInfo.normalsAttrDesc.offset, binaryInfo.normalsAttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.NormalKind, normalsData, false);
+                }
+
+                if (binaryInfo.uvsAttrDesc && binaryInfo.uvsAttrDesc.count > 0) {
+                    var uvsData = new Float32Array(parsedGeometry, binaryInfo.uvsAttrDesc.offset, binaryInfo.uvsAttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.UVKind, uvsData, false);
+                }
+
+                if (binaryInfo.uvs2AttrDesc && binaryInfo.uvs2AttrDesc.count > 0) {
+                    var uvs2Data = new Float32Array(parsedGeometry, binaryInfo.uvs2AttrDesc.offset, binaryInfo.uvs2AttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.UV2Kind, uvs2Data, false);
+                }
+
+                if (binaryInfo.uvs3AttrDesc && binaryInfo.uvs3AttrDesc.count > 0) {
+                    var uvs3Data = new Float32Array(parsedGeometry, binaryInfo.uvs3AttrDesc.offset, binaryInfo.uvs3AttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.UV3Kind, uvs3Data, false);
+                }
+
+                if (binaryInfo.uvs4AttrDesc && binaryInfo.uvs4AttrDesc.count > 0) {
+                    var uvs4Data = new Float32Array(parsedGeometry, binaryInfo.uvs4AttrDesc.offset, binaryInfo.uvs4AttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.UV4Kind, uvs4Data, false);
+                }
+
+                if (binaryInfo.uvs5AttrDesc && binaryInfo.uvs5AttrDesc.count > 0) {
+                    var uvs5Data = new Float32Array(parsedGeometry, binaryInfo.uvs5AttrDesc.offset, binaryInfo.uvs5AttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.UV5Kind, uvs5Data, false);
+                }
+
+                if (binaryInfo.uvs6AttrDesc && binaryInfo.uvs6AttrDesc.count > 0) {
+                    var uvs6Data = new Float32Array(parsedGeometry, binaryInfo.uvs6AttrDesc.offset, binaryInfo.uvs6AttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.UV6Kind, uvs6Data, false);
+                }
+
+                if (binaryInfo.colorsAttrDesc && binaryInfo.colorsAttrDesc.count > 0) {
+                    var colorsData = new Float32Array(parsedGeometry, binaryInfo.colorsAttrDesc.offset, binaryInfo.colorsAttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.ColorKind, colorsData, false, binaryInfo.colorsAttrDesc.stride);
+                }
+
+                if (binaryInfo.matricesIndicesAttrDesc && binaryInfo.matricesIndicesAttrDesc.count > 0) {
+                    var matricesIndicesData = new Int32Array(parsedGeometry, binaryInfo.matricesIndicesAttrDesc.offset, binaryInfo.matricesIndicesAttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.MatricesIndicesKind, matricesIndicesData, false);
+                }
+
+                if (binaryInfo.matricesWeightsAttrDesc && binaryInfo.matricesWeightsAttrDesc.count > 0) {
+                    var matricesWeightsData = new Float32Array(parsedGeometry, binaryInfo.matricesWeightsAttrDesc.offset, binaryInfo.matricesWeightsAttrDesc.count);
+                    mesh.setVerticesData(VertexBuffer.MatricesWeightsKind, matricesWeightsData, false);
+                }
+
+                if (binaryInfo.indicesAttrDesc && binaryInfo.indicesAttrDesc.count > 0) {
+                    var indicesData = new Int32Array(parsedGeometry, binaryInfo.indicesAttrDesc.offset, binaryInfo.indicesAttrDesc.count);
+                    mesh.setIndices(indicesData);
+                }
+
+                if (binaryInfo.subMeshesAttrDesc && binaryInfo.subMeshesAttrDesc.count > 0) {
+                    var subMeshesData = new Int32Array(parsedGeometry, binaryInfo.subMeshesAttrDesc.offset, binaryInfo.subMeshesAttrDesc.count * 5);
+
+                    mesh.subMeshes = [];
+                    for (var i = 0; i < binaryInfo.subMeshesAttrDesc.count; i++) {
+                        var materialIndex = subMeshesData[(i * 5) + 0];
+                        var verticesStart = subMeshesData[(i * 5) + 1];
+                        var verticesCount = subMeshesData[(i * 5) + 2];
+                        var indexStart = subMeshesData[(i * 5) + 3];
+                        var indexCount = subMeshesData[(i * 5) + 4];
+
+                        var subMesh = new SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh);
+                    }
+                }
+            } else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
+                mesh.setVerticesData(VertexBuffer.PositionKind, parsedGeometry.positions, false);
+                mesh.setVerticesData(VertexBuffer.NormalKind, parsedGeometry.normals, false);
+
+                if (parsedGeometry.uvs) {
+                    mesh.setVerticesData(VertexBuffer.UVKind, parsedGeometry.uvs, false);
+                }
+
+                if (parsedGeometry.uvs2) {
+                    mesh.setVerticesData(VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
+                }
+
+                if (parsedGeometry.uvs3) {
+                    mesh.setVerticesData(VertexBuffer.UV3Kind, parsedGeometry.uvs3, false);
+                }
+
+                if (parsedGeometry.uvs4) {
+                    mesh.setVerticesData(VertexBuffer.UV4Kind, parsedGeometry.uvs4, false);
+                }
+
+                if (parsedGeometry.uvs5) {
+                    mesh.setVerticesData(VertexBuffer.UV5Kind, parsedGeometry.uvs5, false);
+                }
+
+                if (parsedGeometry.uvs6) {
+                    mesh.setVerticesData(VertexBuffer.UV6Kind, parsedGeometry.uvs6, false);
+                }
+
+                if (parsedGeometry.colors) {
+                    mesh.setVerticesData(VertexBuffer.ColorKind, Color4.CheckColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
+                }
+
+                if (parsedGeometry.matricesIndices) {
+                    if (!parsedGeometry.matricesIndices._isExpanded) {
+                        var floatIndices = [];
+
+                        for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
+                            var matricesIndex = parsedGeometry.matricesIndices[i];
+
+                            floatIndices.push(matricesIndex & 0x000000FF);
+                            floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
+                            floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
+                            floatIndices.push(matricesIndex >> 24);
+                        }
+
+                        mesh.setVerticesData(VertexBuffer.MatricesIndicesKind, floatIndices, false);
+                    } else {
+                        delete parsedGeometry.matricesIndices._isExpanded;
+                        mesh.setVerticesData(VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
+                    }
+                }
+
+                if (parsedGeometry.matricesIndicesExtra) {
+                    if (!parsedGeometry.matricesIndicesExtra._isExpanded) {
+                        var floatIndices = [];
+
+                        for (var i = 0; i < parsedGeometry.matricesIndicesExtra.length; i++) {
+                            var matricesIndex = parsedGeometry.matricesIndicesExtra[i];
+
+                            floatIndices.push(matricesIndex & 0x000000FF);
+                            floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
+                            floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
+                            floatIndices.push(matricesIndex >> 24);
+                        }
+
+                        mesh.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, floatIndices, false);
+                    } else {
+                        delete parsedGeometry.matricesIndices._isExpanded;
+                        mesh.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, parsedGeometry.matricesIndicesExtra, false);
+                    }
+                }
+
+                if (parsedGeometry.matricesWeights) {
+                    mesh.setVerticesData(VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
+                }
+
+                if (parsedGeometry.matricesWeightsExtra) {
+                    mesh.setVerticesData(VertexBuffer.MatricesWeightsExtraKind, parsedGeometry.matricesWeightsExtra, false);
+                }
+
+                mesh.setIndices(parsedGeometry.indices);
+            }
+
+            // SubMeshes
+            if (parsedGeometry.subMeshes) {
+                mesh.subMeshes = [];
+                for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
+                    var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
+
+                    var subMesh = new SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
+                }
+            }
+
+            // Flat shading
+            if (mesh._shouldGenerateFlatShading) {
+                mesh.convertToFlatShadedMesh();
+                delete mesh._shouldGenerateFlatShading;
+            }
+
+            // Update
+            mesh.computeWorldMatrix(true);
+
+            // Octree
+            if (scene['_selectionOctree']) {
+                scene['_selectionOctree'].addMesh(mesh);
+            }
+        }
+
+        public static ParseGeometry(parsedVertexData: any, scene: Scene, rootUrl: string): Geometry {
+            if (scene.getGeometryByID(parsedVertexData.id)) {
+                return null; // null since geometry could be something else than a box...
+            }
+
+            var geometry = new Geometry(parsedVertexData.id, scene);
+
+            Tags.AddTagsTo(geometry, parsedVertexData.tags);
+
+            if (parsedVertexData.delayLoadingFile) {
+                geometry.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
+                geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
+                geometry._boundingInfo = new BoundingInfo(Vector3.FromArray(parsedVertexData.boundingBoxMinimum), Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
+
+                geometry._delayInfo = [];
+                if (parsedVertexData.hasUVs) {
+                    geometry._delayInfo.push(VertexBuffer.UVKind);
+                }
+
+                if (parsedVertexData.hasUVs2) {
+                    geometry._delayInfo.push(VertexBuffer.UV2Kind);
+                }
+
+                if (parsedVertexData.hasUVs3) {
+                    geometry._delayInfo.push(VertexBuffer.UV3Kind);
+                }
+
+                if (parsedVertexData.hasUVs4) {
+                    geometry._delayInfo.push(VertexBuffer.UV4Kind);
+                }
+
+                if (parsedVertexData.hasUVs5) {
+                    geometry._delayInfo.push(VertexBuffer.UV5Kind);
+                }
+
+                if (parsedVertexData.hasUVs6) {
+                    geometry._delayInfo.push(VertexBuffer.UV6Kind);
+                }
+
+                if (parsedVertexData.hasColors) {
+                    geometry._delayInfo.push(VertexBuffer.ColorKind);
+                }
+
+                if (parsedVertexData.hasMatricesIndices) {
+                    geometry._delayInfo.push(VertexBuffer.MatricesIndicesKind);
+                }
+
+                if (parsedVertexData.hasMatricesWeights) {
+                    geometry._delayInfo.push(VertexBuffer.MatricesWeightsKind);
+                }
+
+                geometry._delayLoadingFunction = VertexData.ImportVertexData;
+            } else {
+                VertexData.ImportVertexData(parsedVertexData, geometry);
+            }
+
+            scene.pushGeometry(geometry, true);
+
+            return geometry;
+        }
     }
 
     /////// Primitives //////////////////////////////////////////////
@@ -533,7 +790,7 @@
                 super.setAllVerticesData(vertexData, false);
             }
 
-            public setVerticesData(kind: string, data: number[], updatable?: boolean): void {
+            public setVerticesData(kind: string, data: number[] | Int32Array | Float32Array, updatable?: boolean): void {
                 if (!this._beingRegenerated) {
                     return;
                 }
@@ -597,6 +854,19 @@
             public copy(id: string): Geometry {
                 return new Box(id, this.getScene(), this.size, this.canBeRegenerated(), null, this.side);
             }
+
+            public static ParseBox(parsedBox: any, scene: Scene): Geometry.Primitives.Box {
+                if (scene.getGeometryByID(parsedBox.id)) {
+                    return null; // null since geometry could be something else than a box...
+                }
+
+                var box = new Geometry.Primitives.Box(parsedBox.id, scene, parsedBox.size, parsedBox.canBeRegenerated, null);
+                Tags.AddTagsTo(box, parsedBox.tags);
+
+                scene.pushGeometry(box, true);
+
+                return box;
+            }
         }
 
         export class Sphere extends _Primitive {
@@ -620,6 +890,19 @@
             public copy(id: string): Geometry {
                 return new Sphere(id, this.getScene(), this.segments, this.diameter, this.canBeRegenerated(), null, this.side);
             }
+
+            public static ParseSphere(parsedSphere: any, scene: Scene): Geometry.Primitives.Sphere {
+                if (scene.getGeometryByID(parsedSphere.id)) {
+                    return null; // null since geometry could be something else than a sphere...
+                }
+
+                var sphere = new Geometry.Primitives.Sphere(parsedSphere.id, scene, parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
+                Tags.AddTagsTo(sphere, parsedSphere.tags);
+
+                scene.pushGeometry(sphere, true);
+
+                return sphere;
+            }
         }
 
         export class Disc extends _Primitive {
@@ -673,6 +956,19 @@
             public copy(id: string): Geometry {
                 return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions, this.canBeRegenerated(), null, this.side);
             }
+
+            public static ParseCylinder(parsedCylinder: any, scene: Scene): Geometry.Primitives.Cylinder {
+                if (scene.getGeometryByID(parsedCylinder.id)) {
+                    return null; // null since geometry could be something else than a cylinder...
+                }
+
+                var cylinder = new Geometry.Primitives.Cylinder(parsedCylinder.id, scene, parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.subdivisions, parsedCylinder.canBeRegenerated, null);
+                Tags.AddTagsTo(cylinder, parsedCylinder.tags);
+
+                scene.pushGeometry(cylinder, true);
+
+                return cylinder;
+            }
         }
 
         export class Torus extends _Primitive {
@@ -698,6 +994,19 @@
             public copy(id: string): Geometry {
                 return new Torus(id, this.getScene(), this.diameter, this.thickness, this.tessellation, this.canBeRegenerated(), null, this.side);
             }
+
+            public static ParseTorus(parsedTorus: any, scene: Scene): Geometry.Primitives.Torus {
+                if (scene.getGeometryByID(parsedTorus.id)) {
+                    return null; // null since geometry could be something else than a torus...
+                }
+
+                var torus = new Geometry.Primitives.Torus(parsedTorus.id, scene, parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
+                Tags.AddTagsTo(torus, parsedTorus.tags);
+
+                scene.pushGeometry(torus, true);
+
+                return torus;
+            }
         }
 
         export class Ground extends _Primitive {
@@ -721,6 +1030,19 @@
             public copy(id: string): Geometry {
                 return new Ground(id, this.getScene(), this.width, this.height, this.subdivisions, this.canBeRegenerated(), null);
             }
+
+            public static ParseGround(parsedGround: any, scene: Scene): Geometry.Primitives.Ground {
+                if (scene.getGeometryByID(parsedGround.id)) {
+                    return null; // null since geometry could be something else than a ground...
+                }
+
+                var ground = new Geometry.Primitives.Ground(parsedGround.id, scene, parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
+                Tags.AddTagsTo(ground, parsedGround.tags);
+
+                scene.pushGeometry(ground, true);
+
+                return ground;
+            }
         }
 
         export class TiledGround extends _Primitive {
@@ -771,6 +1093,19 @@
             public copy(id: string): Geometry {
                 return new Plane(id, this.getScene(), this.size, this.canBeRegenerated(), null, this.side);
             }
+
+            public static ParsePlane(parsedPlane: any, scene: Scene): Geometry.Primitives.Plane {
+                if (scene.getGeometryByID(parsedPlane.id)) {
+                    return null; // null since geometry could be something else than a ground...
+                }
+
+                var plane = new Geometry.Primitives.Plane(parsedPlane.id, scene, parsedPlane.size, parsedPlane.canBeRegenerated, null);
+                Tags.AddTagsTo(plane, parsedPlane.tags);
+
+                scene.pushGeometry(plane, true);
+
+                return plane;
+            }
         }
 
         export class TorusKnot extends _Primitive {
@@ -802,6 +1137,19 @@
             public copy(id: string): Geometry {
                 return new TorusKnot(id, this.getScene(), this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q, this.canBeRegenerated(), null, this.side);
             }
+
+            public static ParseTorusKnot(parsedTorusKnot: any, scene: Scene): Geometry.Primitives.TorusKnot {
+                if (scene.getGeometryByID(parsedTorusKnot.id)) {
+                    return null; // null since geometry could be something else than a ground...
+                }
+
+                var torusKnot = new Geometry.Primitives.TorusKnot(parsedTorusKnot.id, scene, parsedTorusKnot.radius, parsedTorusKnot.tube, parsedTorusKnot.radialSegments, parsedTorusKnot.tubularSegments, parsedTorusKnot.p, parsedTorusKnot.q, parsedTorusKnot.canBeRegenerated, null);
+                Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
+
+                scene.pushGeometry(torusKnot, true);
+
+                return torusKnot;
+            }
         }
     }
 }

+ 1 - 1
src/Mesh/babylon.instancedMesh.ts

@@ -63,7 +63,7 @@
             return this._sourceMesh.isVerticesDataPresent(kind);
         }
 
-        public getIndices(): number[] {
+        public getIndices(): number[] | Int32Array {
             return this._sourceMesh.getIndices();
         }
 

+ 163 - 4
src/Mesh/babylon.mesh.js

@@ -154,8 +154,8 @@ var BABYLON;
         /**
          * Add a mesh as LOD level triggered at the given distance.
          * @param {number} distance - the distance from the center of the object to show this level
-         * @param {BABYLON.Mesh} mesh - the mesh to be added as LOD level
-         * @return {BABYLON.Mesh} this mesh (for chaining)
+         * @param {Mesh} mesh - the mesh to be added as LOD level
+         * @return {Mesh} this mesh (for chaining)
          */
         Mesh.prototype.addLODLevel = function (distance, mesh) {
             if (mesh && mesh._masterMesh) {
@@ -181,8 +181,8 @@ var BABYLON;
         };
         /**
          * Remove a mesh from the LOD array
-         * @param {BABYLON.Mesh} mesh - the mesh to be removed.
-         * @return {BABYLON.Mesh} this mesh (for chaining)
+         * @param {Mesh} mesh - the mesh to be removed.
+         * @return {Mesh} this mesh (for chaining)
          */
         Mesh.prototype.removeLODLevel = function (mesh) {
             for (var index = 0; index < this._LODLevels.length; index++) {
@@ -1059,6 +1059,165 @@ var BABYLON;
             });
         };
         // Statics
+        Mesh.ParseMesh = function (parsedMesh, scene, rootUrl) {
+            var mesh = new Mesh(parsedMesh.name, scene);
+            mesh.id = parsedMesh.id;
+            BABYLON.Tags.AddTagsTo(mesh, parsedMesh.tags);
+            mesh.position = BABYLON.Vector3.FromArray(parsedMesh.position);
+            if (parsedMesh.rotationQuaternion) {
+                mesh.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedMesh.rotationQuaternion);
+            }
+            else if (parsedMesh.rotation) {
+                mesh.rotation = BABYLON.Vector3.FromArray(parsedMesh.rotation);
+            }
+            mesh.scaling = BABYLON.Vector3.FromArray(parsedMesh.scaling);
+            if (parsedMesh.localMatrix) {
+                mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.localMatrix));
+            }
+            else if (parsedMesh.pivotMatrix) {
+                mesh.setPivotMatrix(BABYLON.Matrix.FromArray(parsedMesh.pivotMatrix));
+            }
+            mesh.setEnabled(parsedMesh.isEnabled);
+            mesh.isVisible = parsedMesh.isVisible;
+            mesh.infiniteDistance = parsedMesh.infiniteDistance;
+            mesh.showBoundingBox = parsedMesh.showBoundingBox;
+            mesh.showSubMeshesBoundingBox = parsedMesh.showSubMeshesBoundingBox;
+            if (parsedMesh.applyFog !== undefined) {
+                mesh.applyFog = parsedMesh.applyFog;
+            }
+            if (parsedMesh.pickable !== undefined) {
+                mesh.isPickable = parsedMesh.pickable;
+            }
+            if (parsedMesh.alphaIndex !== undefined) {
+                mesh.alphaIndex = parsedMesh.alphaIndex;
+            }
+            mesh.receiveShadows = parsedMesh.receiveShadows;
+            mesh.billboardMode = parsedMesh.billboardMode;
+            if (parsedMesh.visibility !== undefined) {
+                mesh.visibility = parsedMesh.visibility;
+            }
+            mesh.checkCollisions = parsedMesh.checkCollisions;
+            mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading;
+            // freezeWorldMatrix
+            if (parsedMesh.freezeWorldMatrix) {
+                mesh._waitingFreezeWorldMatrix = parsedMesh.freezeWorldMatrix;
+            }
+            // Parent
+            if (parsedMesh.parentId) {
+                mesh._waitingParentId = parsedMesh.parentId;
+            }
+            // Actions
+            if (parsedMesh.actions !== undefined) {
+                mesh._waitingActions = parsedMesh.actions;
+            }
+            // Geometry
+            mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
+            if (parsedMesh.delayLoadingFile) {
+                mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
+                mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
+                mesh._boundingInfo = new BABYLON.BoundingInfo(BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMinimum), BABYLON.Vector3.FromArray(parsedMesh.boundingBoxMaximum));
+                if (parsedMesh._binaryInfo) {
+                    mesh._binaryInfo = parsedMesh._binaryInfo;
+                }
+                mesh._delayInfo = [];
+                if (parsedMesh.hasUVs) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.UVKind);
+                }
+                if (parsedMesh.hasUVs2) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV2Kind);
+                }
+                if (parsedMesh.hasUVs3) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV3Kind);
+                }
+                if (parsedMesh.hasUVs4) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV4Kind);
+                }
+                if (parsedMesh.hasUVs5) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV5Kind);
+                }
+                if (parsedMesh.hasUVs6) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.UV6Kind);
+                }
+                if (parsedMesh.hasColors) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.ColorKind);
+                }
+                if (parsedMesh.hasMatricesIndices) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesIndicesKind);
+                }
+                if (parsedMesh.hasMatricesWeights) {
+                    mesh._delayInfo.push(BABYLON.VertexBuffer.MatricesWeightsKind);
+                }
+                mesh._delayLoadingFunction = BABYLON.Geometry.ImportGeometry;
+                if (BABYLON.SceneLoader.ForceFullSceneLoadingForIncremental) {
+                    mesh._checkDelayState();
+                }
+            }
+            else {
+                BABYLON.Geometry.ImportGeometry(parsedMesh, mesh);
+            }
+            // Material
+            if (parsedMesh.materialId) {
+                mesh.setMaterialByID(parsedMesh.materialId);
+            }
+            else {
+                mesh.material = null;
+            }
+            // Skeleton
+            if (parsedMesh.skeletonId > -1) {
+                mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
+                if (parsedMesh.numBoneInfluencers) {
+                    mesh.numBoneInfluencers = parsedMesh.numBoneInfluencers;
+                }
+            }
+            // Physics
+            if (parsedMesh.physicsImpostor) {
+                if (!scene.isPhysicsEnabled()) {
+                    scene.enablePhysics();
+                }
+                mesh.setPhysicsState({ impostor: parsedMesh.physicsImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
+            }
+            // Animations
+            if (parsedMesh.animations) {
+                for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
+                    var parsedAnimation = parsedMesh.animations[animationIndex];
+                    mesh.animations.push(BABYLON.Animation.ParseAnimation(parsedAnimation));
+                }
+            }
+            if (parsedMesh.autoAnimate) {
+                scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
+            }
+            // Layer Mask
+            if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) {
+                mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask));
+            }
+            else {
+                mesh.layerMask = 0x0FFFFFFF;
+            }
+            // Instances
+            if (parsedMesh.instances) {
+                for (var index = 0; index < parsedMesh.instances.length; index++) {
+                    var parsedInstance = parsedMesh.instances[index];
+                    var instance = mesh.createInstance(parsedInstance.name);
+                    BABYLON.Tags.AddTagsTo(instance, parsedInstance.tags);
+                    instance.position = BABYLON.Vector3.FromArray(parsedInstance.position);
+                    if (parsedInstance.rotationQuaternion) {
+                        instance.rotationQuaternion = BABYLON.Quaternion.FromArray(parsedInstance.rotationQuaternion);
+                    }
+                    else if (parsedInstance.rotation) {
+                        instance.rotation = BABYLON.Vector3.FromArray(parsedInstance.rotation);
+                    }
+                    instance.scaling = BABYLON.Vector3.FromArray(parsedInstance.scaling);
+                    instance.checkCollisions = mesh.checkCollisions;
+                    if (parsedMesh.animations) {
+                        for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
+                            parsedAnimation = parsedMesh.animations[animationIndex];
+                            instance.animations.push(BABYLON.Animation.ParseAnimation(parsedAnimation));
+                        }
+                    }
+                }
+            }
+            return mesh;
+        };
         Mesh.CreateRibbon = function (name, pathArray, closeArray, closePath, offset, scene, updatable, sideOrientation, instance) {
             return BABYLON.MeshBuilder.CreateRibbon(name, {
                 pathArray: pathArray,

+ 213 - 9
src/Mesh/babylon.mesh.ts

@@ -150,8 +150,8 @@
         /**
          * Add a mesh as LOD level triggered at the given distance.
          * @param {number} distance - the distance from the center of the object to show this level
-         * @param {BABYLON.Mesh} mesh - the mesh to be added as LOD level
-         * @return {BABYLON.Mesh} this mesh (for chaining)
+         * @param {Mesh} mesh - the mesh to be added as LOD level
+         * @return {Mesh} this mesh (for chaining)
          */
         public addLODLevel(distance: number, mesh: Mesh): Mesh {
             if (mesh && mesh._masterMesh) {
@@ -184,8 +184,8 @@
 
         /**
          * Remove a mesh from the LOD array
-         * @param {BABYLON.Mesh} mesh - the mesh to be removed.
-         * @return {BABYLON.Mesh} this mesh (for chaining)
+         * @param {Mesh} mesh - the mesh to be removed.
+         * @return {Mesh} this mesh (for chaining)
          */
         public removeLODLevel(mesh: Mesh): Mesh {
 
@@ -293,7 +293,7 @@
             return this._geometry.getTotalIndices();
         }
 
-        public getIndices(copyWhenShared?: boolean): number[] {
+        public getIndices(copyWhenShared?: boolean): number[] | Int32Array {
             if (!this._geometry) {
                 return [];
             }
@@ -485,7 +485,7 @@
             geometry.applyToMesh(this);
         }
 
-        public setIndices(indices: number[], totalVertices?: number): void {
+        public setIndices(indices: number[] | Int32Array, totalVertices?: number): void {
             if (!this._geometry) {
                 var vertexData = new VertexData();
                 vertexData.indices = indices;
@@ -1240,6 +1240,210 @@
         }
 
         // Statics
+        
+        public static ParseMesh(parsedMesh: any, scene: Scene, rootUrl: string): Mesh {
+            var mesh = new Mesh(parsedMesh.name, scene);
+            mesh.id = parsedMesh.id;
+
+            Tags.AddTagsTo(mesh, parsedMesh.tags);
+
+            mesh.position = Vector3.FromArray(parsedMesh.position);
+
+            if (parsedMesh.rotationQuaternion) {
+                mesh.rotationQuaternion = Quaternion.FromArray(parsedMesh.rotationQuaternion);
+            } else if (parsedMesh.rotation) {
+                mesh.rotation = Vector3.FromArray(parsedMesh.rotation);
+            }
+
+            mesh.scaling = Vector3.FromArray(parsedMesh.scaling);
+
+            if (parsedMesh.localMatrix) {
+                mesh.setPivotMatrix(Matrix.FromArray(parsedMesh.localMatrix));
+            } else if (parsedMesh.pivotMatrix) {
+                mesh.setPivotMatrix(Matrix.FromArray(parsedMesh.pivotMatrix));
+            }
+
+            mesh.setEnabled(parsedMesh.isEnabled);
+            mesh.isVisible = parsedMesh.isVisible;
+            mesh.infiniteDistance = parsedMesh.infiniteDistance;
+
+            mesh.showBoundingBox = parsedMesh.showBoundingBox;
+            mesh.showSubMeshesBoundingBox = parsedMesh.showSubMeshesBoundingBox;
+
+            if (parsedMesh.applyFog !== undefined) {
+                mesh.applyFog = parsedMesh.applyFog;
+            }
+
+            if (parsedMesh.pickable !== undefined) {
+                mesh.isPickable = parsedMesh.pickable;
+            }
+
+            if (parsedMesh.alphaIndex !== undefined) {
+                mesh.alphaIndex = parsedMesh.alphaIndex;
+            }
+
+            mesh.receiveShadows = parsedMesh.receiveShadows;
+
+            mesh.billboardMode = parsedMesh.billboardMode;
+
+            if (parsedMesh.visibility !== undefined) {
+                mesh.visibility = parsedMesh.visibility;
+            }
+
+            mesh.checkCollisions = parsedMesh.checkCollisions;
+            mesh._shouldGenerateFlatShading = parsedMesh.useFlatShading;
+
+            // freezeWorldMatrix
+            if (parsedMesh.freezeWorldMatrix) {
+                mesh._waitingFreezeWorldMatrix = parsedMesh.freezeWorldMatrix;
+            }
+
+            // Parent
+            if (parsedMesh.parentId) {
+                mesh._waitingParentId = parsedMesh.parentId;
+            }
+
+            // Actions
+            if (parsedMesh.actions !== undefined) {
+                mesh._waitingActions = parsedMesh.actions;
+            }
+
+            // Geometry
+            mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
+
+            if (parsedMesh.delayLoadingFile) {
+                mesh.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
+                mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
+                mesh._boundingInfo = new BoundingInfo(Vector3.FromArray(parsedMesh.boundingBoxMinimum), Vector3.FromArray(parsedMesh.boundingBoxMaximum));
+
+                if (parsedMesh._binaryInfo) {
+                    mesh._binaryInfo = parsedMesh._binaryInfo;
+                }
+
+                mesh._delayInfo = [];
+                if (parsedMesh.hasUVs) {
+                    mesh._delayInfo.push(VertexBuffer.UVKind);
+                }
+
+                if (parsedMesh.hasUVs2) {
+                    mesh._delayInfo.push(VertexBuffer.UV2Kind);
+                }
+
+                if (parsedMesh.hasUVs3) {
+                    mesh._delayInfo.push(VertexBuffer.UV3Kind);
+                }
+
+                if (parsedMesh.hasUVs4) {
+                    mesh._delayInfo.push(VertexBuffer.UV4Kind);
+                }
+
+                if (parsedMesh.hasUVs5) {
+                    mesh._delayInfo.push(VertexBuffer.UV5Kind);
+                }
+
+                if (parsedMesh.hasUVs6) {
+                    mesh._delayInfo.push(VertexBuffer.UV6Kind);
+                }
+
+                if (parsedMesh.hasColors) {
+                    mesh._delayInfo.push(VertexBuffer.ColorKind);
+                }
+
+                if (parsedMesh.hasMatricesIndices) {
+                    mesh._delayInfo.push(VertexBuffer.MatricesIndicesKind);
+                }
+
+                if (parsedMesh.hasMatricesWeights) {
+                    mesh._delayInfo.push(VertexBuffer.MatricesWeightsKind);
+                }
+
+                mesh._delayLoadingFunction = Geometry.ImportGeometry;
+
+                if (SceneLoader.ForceFullSceneLoadingForIncremental) {
+                    mesh._checkDelayState();
+                }
+
+            } else {
+                Geometry.ImportGeometry(parsedMesh, mesh);
+            }
+
+            // Material
+            if (parsedMesh.materialId) {
+                mesh.setMaterialByID(parsedMesh.materialId);
+            } else {
+                mesh.material = null;
+            }
+
+            // Skeleton
+            if (parsedMesh.skeletonId > -1) {
+                mesh.skeleton = scene.getLastSkeletonByID(parsedMesh.skeletonId);
+                if (parsedMesh.numBoneInfluencers) {
+                    mesh.numBoneInfluencers = parsedMesh.numBoneInfluencers;
+                }
+            }
+
+            // Physics
+            if (parsedMesh.physicsImpostor) {
+                if (!scene.isPhysicsEnabled()) {
+                    scene.enablePhysics();
+                }
+
+                mesh.setPhysicsState({ impostor: parsedMesh.physicsImpostor, mass: parsedMesh.physicsMass, friction: parsedMesh.physicsFriction, restitution: parsedMesh.physicsRestitution });
+            }
+
+            // Animations
+            if (parsedMesh.animations) {
+                for (var animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
+                    var parsedAnimation = parsedMesh.animations[animationIndex];
+
+                    mesh.animations.push(Animation.ParseAnimation(parsedAnimation));
+                }
+            }
+
+            if (parsedMesh.autoAnimate) {
+                scene.beginAnimation(mesh, parsedMesh.autoAnimateFrom, parsedMesh.autoAnimateTo, parsedMesh.autoAnimateLoop, 1.0);
+            }
+
+            // Layer Mask
+            if (parsedMesh.layerMask && (!isNaN(parsedMesh.layerMask))) {
+                mesh.layerMask = Math.abs(parseInt(parsedMesh.layerMask));
+            } else {
+                mesh.layerMask = 0x0FFFFFFF;
+            }
+
+            // Instances
+            if (parsedMesh.instances) {
+                for (var index = 0; index < parsedMesh.instances.length; index++) {
+                    var parsedInstance = parsedMesh.instances[index];
+                    var instance = mesh.createInstance(parsedInstance.name);
+
+                    Tags.AddTagsTo(instance, parsedInstance.tags);
+
+                    instance.position = Vector3.FromArray(parsedInstance.position);
+
+                    if (parsedInstance.rotationQuaternion) {
+                        instance.rotationQuaternion = Quaternion.FromArray(parsedInstance.rotationQuaternion);
+                    } else if (parsedInstance.rotation) {
+                        instance.rotation = Vector3.FromArray(parsedInstance.rotation);
+                    }
+
+                    instance.scaling = Vector3.FromArray(parsedInstance.scaling);
+
+                    instance.checkCollisions = mesh.checkCollisions;
+
+                    if (parsedMesh.animations) {
+                        for (animationIndex = 0; animationIndex < parsedMesh.animations.length; animationIndex++) {
+                            parsedAnimation = parsedMesh.animations[animationIndex];
+
+                            instance.animations.push(Animation.ParseAnimation(parsedAnimation));
+                        }
+                    }
+                }
+            }
+
+            return mesh;
+        }
+
         public static CreateRibbon(name: string, pathArray: Vector3[][], closeArray: boolean, closePath: boolean, offset: number, scene: Scene, updatable?: boolean, sideOrientation?: number, instance?: Mesh): Mesh {
             return MeshBuilder.CreateRibbon(name, {
                 pathArray: pathArray,
@@ -1571,7 +1775,7 @@
             var tempMatrix = new Matrix();
 
             var matWeightIdx = 0;
-            var inf : number;
+            var inf: number;
             for (var index = 0; index < positionsData.length; index += 3, matWeightIdx += 4) {
                 var weight: number;
                 for (inf = 0; inf < 4; inf++) {
@@ -1589,10 +1793,10 @@
                             Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesExtraData[matWeightIdx + inf] * 16, weight, tempMatrix);
                             finalMatrix.addToSelf(tempMatrix);
 
-                        } else break;           
+                        } else break;
                     }
                 }
-                
+
                 Vector3.TransformCoordinatesFromFloatsToRef(this._sourcePositions[index], this._sourcePositions[index + 1], this._sourcePositions[index + 2], finalMatrix, tempVector3);
                 tempVector3.toArray(positionsData, index);
 

+ 65 - 0
src/Mesh/babylon.mesh.vertexData.js

@@ -178,6 +178,7 @@ var BABYLON;
                 }
                 var offset = this.positions ? this.positions.length / 3 : 0;
                 for (index = 0; index < other.indices.length; index++) {
+                    //TODO check type - if Int32Array!
                     this.indices.push(other.indices[index] + offset);
                 }
             }
@@ -1767,6 +1768,70 @@ var BABYLON;
                     break;
             }
         };
+        VertexData.ImportVertexData = function (parsedVertexData, geometry) {
+            var vertexData = new VertexData();
+            // positions
+            var positions = parsedVertexData.positions;
+            if (positions) {
+                vertexData.set(positions, BABYLON.VertexBuffer.PositionKind);
+            }
+            // normals
+            var normals = parsedVertexData.normals;
+            if (normals) {
+                vertexData.set(normals, BABYLON.VertexBuffer.NormalKind);
+            }
+            // uvs
+            var uvs = parsedVertexData.uvs;
+            if (uvs) {
+                vertexData.set(uvs, BABYLON.VertexBuffer.UVKind);
+            }
+            // uv2s
+            var uv2s = parsedVertexData.uv2s;
+            if (uv2s) {
+                vertexData.set(uv2s, BABYLON.VertexBuffer.UV2Kind);
+            }
+            // uv3s
+            var uv3s = parsedVertexData.uv3s;
+            if (uv3s) {
+                vertexData.set(uv3s, BABYLON.VertexBuffer.UV3Kind);
+            }
+            // uv4s
+            var uv4s = parsedVertexData.uv4s;
+            if (uv4s) {
+                vertexData.set(uv4s, BABYLON.VertexBuffer.UV4Kind);
+            }
+            // uv5s
+            var uv5s = parsedVertexData.uv5s;
+            if (uv5s) {
+                vertexData.set(uv5s, BABYLON.VertexBuffer.UV5Kind);
+            }
+            // uv6s
+            var uv6s = parsedVertexData.uv6s;
+            if (uv6s) {
+                vertexData.set(uv6s, BABYLON.VertexBuffer.UV6Kind);
+            }
+            // colors
+            var colors = parsedVertexData.colors;
+            if (colors) {
+                vertexData.set(BABYLON.Color4.CheckColors4(colors, positions.length / 3), BABYLON.VertexBuffer.ColorKind);
+            }
+            // matricesIndices
+            var matricesIndices = parsedVertexData.matricesIndices;
+            if (matricesIndices) {
+                vertexData.set(matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind);
+            }
+            // matricesWeights
+            var matricesWeights = parsedVertexData.matricesWeights;
+            if (matricesWeights) {
+                vertexData.set(matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind);
+            }
+            // indices
+            var indices = parsedVertexData.indices;
+            if (indices) {
+                vertexData.indices = indices;
+            }
+            geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
+        };
         return VertexData;
     })();
     BABYLON.VertexData = VertexData;

+ 84 - 5
src/Mesh/babylon.mesh.vertexData.ts

@@ -1,11 +1,11 @@
 module BABYLON {
     export interface IGetSetVerticesData {
         isVerticesDataPresent(kind: string): boolean;
-        getVerticesData(kind: string, copyWhenShared?: boolean): number[] | Float32Array;
-        getIndices(copyWhenShared?: boolean): number[];
+        getVerticesData(kind: string, copyWhenShared?: boolean): number[] | Int32Array |Float32Array;
+        getIndices(copyWhenShared?: boolean): number[] | Int32Array;
         setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean): void;
         updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): void;
-        setIndices(indices: number[] | Float32Array): void;
+        setIndices(indices: number[] | Int32Array): void;
     }
 
     export class VertexData {
@@ -22,7 +22,7 @@
         public matricesWeights: number[] | Float32Array;
         public matricesIndicesExtra: number[] | Float32Array;
         public matricesWeightsExtra: number[] | Float32Array;
-        public indices: number[];
+        public indices: number[] | Int32Array;
 
         public set(data: number[] | Float32Array, kind: string) {
             switch (kind) {
@@ -239,7 +239,8 @@
 
                 var offset = this.positions ? this.positions.length / 3 : 0;
                 for (index = 0; index < other.indices.length; index++) {
-                    this.indices.push(other.indices[index] + offset);
+                    //TODO check type - if Int32Array!
+                    (<number[]>this.indices).push(other.indices[index] + offset);
                 }
             }
 
@@ -2098,5 +2099,83 @@
                     break;
             }
         }
+
+        public static ImportVertexData(parsedVertexData: any, geometry: Geometry) {
+            var vertexData = new VertexData();
+
+            // positions
+            var positions = parsedVertexData.positions;
+            if (positions) {
+                vertexData.set(positions, VertexBuffer.PositionKind);
+            }
+
+            // normals
+            var normals = parsedVertexData.normals;
+            if (normals) {
+                vertexData.set(normals, VertexBuffer.NormalKind);
+            }
+
+            // uvs
+            var uvs = parsedVertexData.uvs;
+            if (uvs) {
+                vertexData.set(uvs, VertexBuffer.UVKind);
+            }
+
+            // uv2s
+            var uv2s = parsedVertexData.uv2s;
+            if (uv2s) {
+                vertexData.set(uv2s, VertexBuffer.UV2Kind);
+            }
+
+            // uv3s
+            var uv3s = parsedVertexData.uv3s;
+            if (uv3s) {
+                vertexData.set(uv3s, VertexBuffer.UV3Kind);
+            }
+
+            // uv4s
+            var uv4s = parsedVertexData.uv4s;
+            if (uv4s) {
+                vertexData.set(uv4s, VertexBuffer.UV4Kind);
+            }
+
+            // uv5s
+            var uv5s = parsedVertexData.uv5s;
+            if (uv5s) {
+                vertexData.set(uv5s, VertexBuffer.UV5Kind);
+            }
+
+            // uv6s
+            var uv6s = parsedVertexData.uv6s;
+            if (uv6s) {
+                vertexData.set(uv6s, VertexBuffer.UV6Kind);
+            }
+
+            // colors
+            var colors = parsedVertexData.colors;
+            if (colors) {
+                vertexData.set(Color4.CheckColors4(colors, positions.length / 3), VertexBuffer.ColorKind);
+            }
+
+            // matricesIndices
+            var matricesIndices = parsedVertexData.matricesIndices;
+            if (matricesIndices) {
+                vertexData.set(matricesIndices, VertexBuffer.MatricesIndicesKind);
+            }
+
+            // matricesWeights
+            var matricesWeights = parsedVertexData.matricesWeights;
+            if (matricesWeights) {
+                vertexData.set(matricesWeights, VertexBuffer.MatricesWeightsKind);
+            }
+
+            // indices
+            var indices = parsedVertexData.indices;
+            if (indices) {
+                vertexData.indices = indices;
+            }
+
+            geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
+        }
     }
 }

+ 1 - 0
src/Mesh/babylon.meshBuilder.js

@@ -583,6 +583,7 @@ var BABYLON;
                 // Add UVs and get back to world
                 for (var vIndex = 0; vIndex < faceVertices.length; vIndex++) {
                     var vertex = faceVertices[vIndex];
+                    //TODO check for Int32Array
                     vertexData.indices.push(currentVertexDataIndex);
                     vertex.position.toArray(vertexData.positions, currentVertexDataIndex * 3);
                     vertex.normal.toArray(vertexData.normals, currentVertexDataIndex * 3);

+ 2 - 1
src/Mesh/babylon.meshBuilder.ts

@@ -690,7 +690,8 @@
                 for (var vIndex = 0; vIndex < faceVertices.length; vIndex++) {
                     var vertex = faceVertices[vIndex];
 
-                    vertexData.indices.push(currentVertexDataIndex);
+                    //TODO check for Int32Array
+                    (<number[]>vertexData.indices).push(currentVertexDataIndex);
                     vertex.position.toArray(vertexData.positions, currentVertexDataIndex * 3);
                     vertex.normal.toArray(vertexData.normals, currentVertexDataIndex * 3);
                     (<number[]>vertexData.uvs).push(0.5 + vertex.position.x / size.x);

+ 1 - 1
src/Mesh/babylon.meshSimplification.ts

@@ -536,7 +536,7 @@
             var submeshesArray = this._reconstructedMesh.subMeshes;
             this._reconstructedMesh.subMeshes = [];
 
-            var newIndicesArray: Array<number> = this._reconstructedMesh.getIndices(); //[];
+            var newIndicesArray: number[] = <number[]>this._reconstructedMesh.getIndices(); //[];
             var originalIndices = this._mesh.getIndices();
             for (i = 0; i < newTriangles.length; ++i) {
                 t = newTriangles[i]; //now get the new referencing point for each vertex

+ 2 - 2
src/Mesh/babylon.subMesh.ts

@@ -98,7 +98,7 @@
             this._renderingMesh.render(this, enableAlphaMode);
         }
 
-        public getLinesIndexBuffer(indices: number[], engine): WebGLBuffer {
+        public getLinesIndexBuffer(indices: number[] | Int32Array, engine): WebGLBuffer {
             if (!this._linesIndexBuffer) {
                 var linesIndices = [];
 
@@ -118,7 +118,7 @@
             return ray.intersectsBox(this._boundingInfo.boundingBox);
         }
 
-        public intersects(ray: Ray, positions: Vector3[], indices: number[], fastCheck?: boolean): IntersectionInfo {
+        public intersects(ray: Ray, positions: Vector3[], indices: number[] | Int32Array, fastCheck?: boolean): IntersectionInfo {
             var intersectInfo: IntersectionInfo = null;
 
             // Triangles test

+ 1 - 1
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -145,7 +145,7 @@
             return returnValue;
         }
 
-        private _createConvexPolyhedron(rawVerts: number[] | Float32Array, rawFaces: number[], mesh: AbstractMesh): any {
+        private _createConvexPolyhedron(rawVerts: number[] | Float32Array, rawFaces: number[] | Int32Array, mesh: AbstractMesh): any {
             var verts = [], faces = [];
 
             mesh.computeWorldMatrix(true);

+ 1 - 1
src/Tools/babylon.tools.ts

@@ -131,7 +131,7 @@
             return "data:image/png;base64," + output;
         }
 
-        public static ExtractMinAndMaxIndexed(positions: number[] | Float32Array, indices: number[], indexStart: number, indexCount: number): { minimum: Vector3; maximum: Vector3 } {
+        public static ExtractMinAndMaxIndexed(positions: number[] | Float32Array, indices: number[] | Int32Array, indexStart: number, indexCount: number): { minimum: Vector3; maximum: Vector3 } {
             var minimum = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
             var maximum = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
 

+ 1 - 1
src/babylon.engine.ts

@@ -1070,7 +1070,7 @@
             this._cachedIndexBuffer = null;
         }
 
-        public createIndexBuffer(indices: number[]): WebGLBuffer {
+        public createIndexBuffer(indices: number[] | Int32Array): WebGLBuffer {
             var vbo = this._gl.createBuffer();
             this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);