Переглянути джерело

Use faster functions for loading draco data

Gary Hsu 4 роки тому
батько
коміт
cbe176596a
1 змінених файлів з 30 додано та 33 видалено
  1. 30 33
      src/Meshes/Compression/dracoCompression.ts

+ 30 - 33
src/Meshes/Compression/dracoCompression.ts

@@ -46,54 +46,51 @@ function decodeMesh(decoderModule: any, dataView: ArrayBufferView, attributes: {
             throw new Error(status.error_msg());
         }
 
-        const numPoints = geometry.num_points();
-
         if (type === decoderModule.TRIANGULAR_MESH) {
             const numFaces = geometry.num_faces();
-            const faceIndices = new decoderModule.DracoInt32Array();
+            const numIndices = numFaces * 3;
+            const byteLength = numIndices * 4;
+
+            const ptr = decoderModule._malloc(byteLength);
             try {
-                const indices = new Uint32Array(numFaces * 3);
-                for (let i = 0; i < numFaces; i++) {
-                    decoder.GetFaceFromMesh(geometry, i, faceIndices);
-                    const offset = i * 3;
-                    indices[offset + 0] = faceIndices.GetValue(0);
-                    indices[offset + 1] = faceIndices.GetValue(1);
-                    indices[offset + 2] = faceIndices.GetValue(2);
-                }
+                decoder.GetTrianglesUInt32Array(geometry, byteLength, ptr);
+                const indices = new Uint32Array(numIndices);
+                indices.set(new Uint32Array(decoderModule.HEAPF32.buffer, ptr, numIndices));
                 onIndicesData(indices);
             }
             finally {
-                decoderModule.destroy(faceIndices);
+                decoderModule._free(ptr);
             }
         }
 
         const processAttribute = (kind: string, attribute: any) => {
-            const dracoData = new decoderModule.DracoFloat32Array();
+            var numComponents = attribute.num_components();
+            var numPoints = geometry.num_points();
+            var numValues = numPoints * numComponents;
+            var byteLength = numValues * Float32Array.BYTES_PER_ELEMENT;
+
+            var ptr = decoderModule._malloc(byteLength);
             try {
-                decoder.GetAttributeFloatForAllPoints(geometry, attribute, dracoData);
-                const numComponents = attribute.num_components();
-                if (numComponents) {
-                    if (kind === "color" && numComponents === 3) {
-                        const babylonData = new Float32Array(numPoints * 4);
-                        for (let i = 0, j = 0; i < babylonData.length; i += 4, j += numComponents) {
-                            babylonData[i + 0] = dracoData.GetValue(j + 0);
-                            babylonData[i + 1] = dracoData.GetValue(j + 1);
-                            babylonData[i + 2] = dracoData.GetValue(j + 2);
-                            babylonData[i + 3] = 1;
-                        }
-                        onAttributeData(kind, babylonData);
-                    }
-                    else {
-                        const babylonData = new Float32Array(numPoints * numComponents);
-                        for (let i = 0; i < babylonData.length; i++) {
-                            babylonData[i] = dracoData.GetValue(i);
-                        }
-                        onAttributeData(kind, babylonData);
+                decoder.GetAttributeDataArrayForAllPoints(geometry, attribute, decoderModule.DT_FLOAT32, byteLength, ptr);
+                const values = new Float32Array(decoderModule.HEAPF32.buffer, ptr, numValues);
+                if (kind === "color" && numComponents === 3) {
+                    const babylonData = new Float32Array(numPoints * 4);
+                    for (let i = 0, j = 0; i < babylonData.length; i += 4, j += numComponents) {
+                        babylonData[i + 0] = values[j + 0];
+                        babylonData[i + 1] = values[j + 1];
+                        babylonData[i + 2] = values[j + 2];
+                        babylonData[i + 3] = 1;
                     }
+                    onAttributeData(kind, babylonData);
+                }
+                else {
+                    const babylonData = new Float32Array(numValues);
+                    babylonData.set(new Float32Array(decoderModule.HEAPF32.buffer, ptr, numValues));
+                    onAttributeData(kind, babylonData);
                 }
             }
             finally {
-                decoderModule.destroy(dracoData);
+                decoderModule._free(ptr);
             }
         };