Browse Source

Merge pull request #279 from r2d2Proton/master

Changes to support binary file formats for vertex data
deltakosh 11 years ago
parent
commit
a42bad9dc3

+ 65 - 0
Babylon/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -607,6 +607,10 @@
             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);
@@ -794,6 +798,67 @@
             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.colorsAttrDesc && binaryInfo.colorsAttrDesc.count > 0) {
+                var colorsData = new Float32Array(parsedGeometry, binaryInfo.colorsAttrDesc.offset, binaryInfo.colorsAttrDesc.count);
+                mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, colorsData, false);
+            }
+
+            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);
+                }
+            }
+
+            return;
+
         } else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
             mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, parsedGeometry.positions, false);
             mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, parsedGeometry.normals, false);

+ 12 - 2
Babylon/Mesh/babylon.mesh.ts

@@ -10,6 +10,7 @@
         public delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
         public instances = new Array<InstancedMesh>();
         public delayLoadingFile: string;
+        public _binaryInfo : BinaryAttrData;
 
         // Private
         public _geometry: Geometry;
@@ -518,11 +519,20 @@
 
                 scene._addPendingData(that);
 
+                var getBinaryData = (this.delayLoadingFile.indexOf(".babylonbinarymeshdata") !== -1) ? true : false;
+
                 BABYLON.Tools.LoadFile(this.delayLoadingFile, data => {
-                    this._delayLoadingFunction(JSON.parse(data), this);
+
+                    if (data instanceof ArrayBuffer) {
+                        this._delayLoadingFunction(data, this);
+                    }
+                    else {
+                        this._delayLoadingFunction(JSON.parse(data), this);
+                    }
+
                     this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
                     scene._removePendingData(this);
-                }, () => { }, scene.database);
+                }, () => { }, scene.database, getBinaryData);
             }
         }
 

+ 48 - 14
Exporters/BinaryConverter/BabylonLodMesh.cs

@@ -162,6 +162,9 @@ namespace BabylonBinaryConverter
         public AttrDesc matricesWeightsAttrDesc { get; set; }
 
         [DataMember]
+        public AttrDesc subMeshesAttrDesc { get; set; }
+
+        [DataMember]
         public AttrDesc combinedAttrDesc { get; set; }
 
         [DataMember]
@@ -187,6 +190,8 @@ namespace BabylonBinaryConverter
             matricesIndicesAttrDesc = new AttrDesc();
             matricesWeightsAttrDesc = new AttrDesc();
 
+            subMeshesAttrDesc = new AttrDesc();
+
             combinedAttrDesc = new AttrDesc();
             vertexAttributes = VertexAttributes.Undefined;
         }
@@ -201,20 +206,21 @@ namespace BabylonBinaryConverter
                 LoadMeshData(Path.Combine(srcPath, delayLoadingFile));
 
             if (string.IsNullOrEmpty(delayLoadingFile))
+                //delayLoadingFile = name + fileEXT;
                 delayLoadingFile = id + fileEXT;
             else
                 delayLoadingFile = Path.GetFileNameWithoutExtension(delayLoadingFile) + fileEXT;
 
+            
             string fullPath = Path.Combine(dstPath, delayLoadingFile);
 
             if (localMatrix == null)
                 localMatrix = Matrix.Identity.ToArray();
 
-            if (boundingBoxMinimum == null || boundingBoxMaximum == null)
-                CalculateBoundingBox();
+            CalculateBoundingBox();
 
             if (!_combined)
-                MultipleBuffers(fullPath);
+                WriteMultipleBuffers(fullPath);
 
             else
                 VertexBuffer(fullPath);
@@ -227,19 +233,19 @@ namespace BabylonBinaryConverter
             {
                 string filename = WebUtility.UrlDecode(srcFilename);
 
-                IncrMeshData meshData = JsonConvert.DeserializeObject<IncrMeshData>(File.ReadAllText(filename));
+                IncrMeshData incrMeshData = JsonConvert.DeserializeObject<IncrMeshData>(File.ReadAllText(filename));
 
-                positions = meshData.positions;
-                colors = meshData.colors;
-                normals = meshData.normals;
-                uvs = meshData.uvs;
-                uvs2 = meshData.uvs2;
-                indices = meshData.indices;
+                positions = incrMeshData.positions;
+                colors = incrMeshData.colors;
+                normals = incrMeshData.normals;
+                uvs = incrMeshData.uvs;
+                uvs2 = incrMeshData.uvs2;
+                indices = incrMeshData.indices;
 
-                matricesIndices = meshData.matricesIndices;
-                matricesWeights = meshData.matricesWeights;
+                matricesIndices = incrMeshData.matricesIndices;
+                matricesWeights = incrMeshData.matricesWeights;
 
-                subMeshes = meshData.subMeshes;
+                subMeshes = incrMeshData.subMeshes;
             }
             catch (Exception ex)
             {
@@ -253,7 +259,7 @@ namespace BabylonBinaryConverter
         }
         
         
-        private void MultipleBuffers(string fullPath)
+        private void WriteMultipleBuffers(string fullPath)
         {
             try
             {
@@ -314,6 +320,8 @@ namespace BabylonBinaryConverter
 
                         hasUVs = true;
 
+                        writer.Flush();
+
                         uvs = null;
                     }
 
@@ -329,6 +337,8 @@ namespace BabylonBinaryConverter
 
                         hasUVs2 = true;
 
+                        writer.Flush();
+
                         uvs2 = null;
                     }
 
@@ -374,6 +384,30 @@ namespace BabylonBinaryConverter
 
                         matricesWeights = null;
                     }
+
+                    if(subMeshes != null && subMeshes.Length > 0)
+                    {
+                        subMeshesAttrDesc.count = subMeshes.Length;
+                        subMeshesAttrDesc.stride = 5;
+                        subMeshesAttrDesc.offset = stream.Length;
+                        subMeshesAttrDesc.dataType = DataType.Int32;
+
+                        int[] smData = new int[5];
+
+                        for (int x = 0; x < subMeshes.Length; x++)
+                        {
+                            smData[0] = subMeshes[x].materialIndex;
+                            smData[1] = subMeshes[x].verticesStart;
+                            smData[2] = subMeshes[x].verticesCount;
+                            smData[3] = subMeshes[x].indexStart;
+                            smData[4] = subMeshes[x].indexCount;
+
+                            for (int y = 0; y < smData.Length; y++)
+                                writer.Write(smData[y]);
+                        }
+
+                        subMeshes = null;
+                    }
                 }
             }
             catch (Exception ex)

+ 3 - 0
Exporters/BinaryConverter/BabylonLodScene.cs

@@ -12,6 +12,9 @@ namespace BabylonBinaryConverter
         [DataMember]
         public new BabylonLodMesh[] meshes { get; set; }
 
+        [DataMember]
+        public bool useDelayedTextureLoading { get; set; }
+
 
         public BabylonLodScene(string outputPath) : base(outputPath)
         {

+ 3 - 1
Exporters/BinaryConverter/Program.cs

@@ -96,7 +96,9 @@ namespace BabylonBinaryConverter
             try
             {
                 BabylonLodScene scene = JsonConvert.DeserializeObject<BabylonLodScene>(File.ReadAllText(WebUtility.UrlDecode(srcFilename)));
-                
+                scene.autoClear = true;
+                scene.useDelayedTextureLoading = true;
+
                 scene.Convert(srcPath, dstPath);
 
                 File.WriteAllText(WebUtility.UrlDecode(dstFilename), JsonConvert.SerializeObject(scene, (formatted ? Formatting.Indented : Formatting.None)));