From 0a9c9c7b8b0180132ce7afef946f45a86e372023 Mon Sep 17 00:00:00 2001 From: Ondrej Stava Date: Tue, 28 Feb 2017 09:04:51 -0800 Subject: [PATCH] Update to our THREE.js example: DracoModule is now instantiated in DRACOLoader class using closure to prevent multiple instantiations. --- javascript/example/DRACOLoader.js | 57 +++++++++++++--------- javascript/example/webgl_loader_draco.html | 2 - 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/javascript/example/DRACOLoader.js b/javascript/example/DRACOLoader.js index 8254a5f..06a0f8f 100644 --- a/javascript/example/DRACOLoader.js +++ b/javascript/example/DRACOLoader.js @@ -39,32 +39,45 @@ THREE.DRACOLoader.prototype = { this.path = value; }, - decodeDracoFile: function(rawBuffer) { - const scope = this; - /* - * Here is how to use Draco Javascript decoder and get the geometry. - */ - const buffer = new dracoDecoder.DecoderBuffer(); - buffer.Init(new Int8Array(rawBuffer), rawBuffer.byteLength); - const wrapper = new dracoDecoder.WebIDLWrapper(); + decodeDracoFile: ( function() { + let dracoDecoder; - /* - * Determine what type is this file: mesh or point cloud. - */ - const geometryType = wrapper.GetEncodedGeometryType(buffer); - if (geometryType == dracoDecoder.TRIANGULAR_MESH) { - fileDisplayArea.innerText = "Loaded a mesh.\n"; - } else if (geometryType == dracoDecoder.POINT_CLOUD) { - fileDisplayArea.innerText = "Loaded a point cloud.\n"; + if (typeof DracoModule === 'function') { + dracoDecoder = DracoModule(); } else { - const errorMsg = "Error: Unknown geometry type."; - fileDisplayArea.innerText = errorMsg; - throw new Error(errorMsg); + console.error('THREE.DRACOLoader: DracoModule not found.'); + return; } - return scope.convertDracoGeometryTo3JS(wrapper, geometryType, buffer); - }, - convertDracoGeometryTo3JS: function(wrapper, geometryType, buffer) { + return function(rawBuffer) { + const scope = this; + /* + * Here is how to use Draco Javascript decoder and get the geometry. + */ + const buffer = new dracoDecoder.DecoderBuffer(); + buffer.Init(new Int8Array(rawBuffer), rawBuffer.byteLength); + const wrapper = new dracoDecoder.WebIDLWrapper(); + + /* + * Determine what type is this file: mesh or point cloud. + */ + const geometryType = wrapper.GetEncodedGeometryType(buffer); + if (geometryType == dracoDecoder.TRIANGULAR_MESH) { + fileDisplayArea.innerText = "Loaded a mesh.\n"; + } else if (geometryType == dracoDecoder.POINT_CLOUD) { + fileDisplayArea.innerText = "Loaded a point cloud.\n"; + } else { + const errorMsg = "Error: Unknown geometry type."; + fileDisplayArea.innerText = errorMsg; + throw new Error(errorMsg); + } + return scope.convertDracoGeometryTo3JS(wrapper, geometryType, buffer, + dracoDecoder); + } + } )(), + + convertDracoGeometryTo3JS: function(wrapper, geometryType, buffer, + dracoDecoder) { let dracoGeometry; const start_time = performance.now(); if (geometryType == dracoDecoder.TRIANGULAR_MESH) { diff --git a/javascript/example/webgl_loader_draco.html b/javascript/example/webgl_loader_draco.html index 52f9c30..d86aea4 100644 --- a/javascript/example/webgl_loader_draco.html +++ b/javascript/example/webgl_loader_draco.html @@ -47,8 +47,6 @@