Browse Source

Fix Draco color attribute bug when using VEC3

Gary Hsu 5 years ago
parent
commit
99890ea15b
1 changed files with 17 additions and 4 deletions
  1. 17 4
      src/Meshes/Compression/dracoCompression.ts

+ 17 - 4
src/Meshes/Compression/dracoCompression.ts

@@ -71,11 +71,24 @@ function decodeMesh(decoderModule: any, dataView: ArrayBufferView, attributes: {
             const dracoData = new decoderModule.DracoFloat32Array();
             const dracoData = new decoderModule.DracoFloat32Array();
             try {
             try {
                 decoder.GetAttributeFloatForAllPoints(geometry, attribute, dracoData);
                 decoder.GetAttributeFloatForAllPoints(geometry, attribute, dracoData);
-                const babylonData = new Float32Array(numPoints * attribute.num_components());
-                for (let i = 0; i < babylonData.length; i++) {
-                    babylonData[i] = dracoData.GetValue(i);
+                const numComponents = attribute.num_components();
+                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);
                 }
                 }
-                onAttributeData(kind, babylonData);
             }
             }
             finally {
             finally {
                 decoderModule.destroy(dracoData);
                 decoderModule.destroy(dracoData);