Преглед изворни кода

Merge pull request #9124 from bghgary/optimize-draco

Improve Draco decoder by using better functions
David Catuhe пре 4 година
родитељ
комит
c5ad71f173

BIN
dist/preview release/draco_decoder_gltf.wasm


Разлика између датотеке није приказан због своје велике величине
+ 104 - 119
dist/preview release/draco_wasm_wrapper_gltf.js


+ 8 - 0
sandbox/public/index-local.html

@@ -35,6 +35,14 @@
         BABYLONDEVTOOLS.Loader
             .require("index.js")
             .load(() => {
+                BABYLON.DracoCompression.Configuration.decoder = {
+                    wasmUrl: "../../dist/preview%20release/draco_wasm_wrapper_gltf.js",
+                    wasmBinaryUrl: "../../dist/preview%20release/draco_decoder_gltf.wasm",
+                    fallbackUrl: "../../dist/preview%20release/draco_decoder_gltf.js"
+                };
+                BABYLON.GLTFValidation.Configuration = {
+                    url: "../../dist/preview%20release/gltf_validator.js"
+                };
             });
     </script>
 </body>

+ 6 - 9
sandbox/public/index.html

@@ -4,8 +4,7 @@
 <head>
     <title>Babylon.js Sandbox - View glTF, glb, obj and babylon files</title>
     <meta name="description" content="Viewer for glTF, glb, obj and babylon files powered by Babylon.js" />
-    <meta name="keywords"
-        content="Babylon.js, Babylon, BabylonJS, glTF, glb, obj, viewer, online viewer, 3D model viewer, 3D, webgl" />
+    <meta name="keywords" content="Babylon.js, Babylon, BabylonJS, glTF, glb, obj, viewer, online viewer, 3D model viewer, 3D, webgl" />
     <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
     <link rel="stylesheet" href="https://use.typekit.net/cta4xsb.css">
     <link rel="shortcut icon" href="https://www.babylonjs.com/favicon.ico">
@@ -20,10 +19,9 @@
     <script src="https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js"></script>
     <script src="https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
 
-    
     <script src="https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js"></script>
     <script src="dist/babylon.sandbox.js"></script>
-    
+
     <style>
         html,
         body {
@@ -36,17 +34,16 @@
 
         #host-element {
             width: 100%;
-            height: 100%;               
+            height: 100%;
             padding: 0;
             margin: 0;
-            overflow: hidden;         
+            overflow: hidden;
         }
     </style>
 </head>
 
-<body>    
-    <div id="host-element">
-    </div>
+<body>
+    <div id="host-element"></div>
     <script src="index.js"></script>
 </body>
 

+ 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);
             }
         };