From 85b97d1ae66d522ce6ff7aa2b233e8bdc2bb1cc3 Mon Sep 17 00:00:00 2001 From: Brian Chirls Date: Mon, 1 Dec 2014 15:09:23 -0500 Subject: [PATCH] Optimization: use buffer geometry for building models --- src/Blueprint/BlueprintOutputBuildingTiles.js | 55 +++++-------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/src/Blueprint/BlueprintOutputBuildingTiles.js b/src/Blueprint/BlueprintOutputBuildingTiles.js index cf24011..1557e9e 100644 --- a/src/Blueprint/BlueprintOutputBuildingTiles.js +++ b/src/Blueprint/BlueprintOutputBuildingTiles.js @@ -152,30 +152,13 @@ // Load buildings in a Web Worker self.worker(self.world.origin, self.world.originZoom, buildings).then(function(result) { - var model = result.model; var offset = result.offset; + var geom = new THREE.BufferGeometry(); + geom.addAttribute('position', new THREE.BufferAttribute(result.position, 3)); + geom.addAttribute('normal', new THREE.BufferAttribute(result.normal, 3)); + geom.addAttribute('uv', new THREE.BufferAttribute(result.uv, 2)); - // Convert typed data back to arrays - model.vertices = Array.apply( [], model.vertices ); - model.normals = Array.apply( [], model.normals ); - // Wrap UVs within an array - // https://github.com/mrdoob/three.js/blob/master/examples/js/exporters/GeometryExporter.js#L231 - model.uvs = [ Array.apply( [], model.uvs ) ]; - - // Keep getting a "Maximum call stack size exceeded" error here - //model.faces = Array.apply( [], model.faces ); - var faces = []; - _.each(model.faces, function(face) { - faces.push(face); - }); - - model.faces = faces; - - // TODO: Stop this locking up the browser - // No visible lock up at all when removed - var geom = loader.parse(model); - - var mesh = new THREE.Mesh(geom.geometry, material); + var mesh = new THREE.Mesh(geom, material); // Use previously calculated offset to return merged mesh to correct position // This allows frustum culling to work correctly @@ -383,30 +366,22 @@ // Move merged geom to 0,0 and return offset var offset = combinedGeom.center(); - var exportedGeom = combinedGeom.toJSON(); - - // Convert exported geom into a typed array - var verticesArray = new Float64Array( exportedGeom.data.vertices ); - var normalsArray = new Float64Array( exportedGeom.data.normals ); - // var colorsArray = new Float64Array( exportedGeom.colors ); - // Seems to be manually set to have 1 array in the uvs array - // https://github.com/mrdoob/three.js/blob/master/examples/js/exporters/GeometryExporter.js#L231 - var uvsArray = new Float64Array( exportedGeom.data.uvs[0] ); - var facesArray = new Float64Array( exportedGeom.data.faces ); + //TODO: save a more compact model using indices. Requires replacing fromGeometry with custom code + var exportedGeom = new THREE.BufferGeometry(); + exportedGeom.fromGeometry(combinedGeom); // Store geom typed array as Three.js model object var model = { - metadata: exportedGeom.metadata, - colors: exportedGeom.colors, - vertices: verticesArray, - normals: normalsArray, - uvs: uvsArray, - faces: facesArray + offset: offset }; - var data = {model: model, offset: offset}; + var transfers = []; + exportedGeom.attributesKeys.forEach(function (key) { + model[key] = exportedGeom.attributes[key].array; + transfers.push(model[key].buffer); + }); - deferred.transferResolve(data, [model.vertices.buffer, model.normals.buffer, model.uvs.buffer, model.faces.buffer]); + deferred.transferResolve(model, transfers); }; VIZI.BlueprintOutputBuildingTiles.prototype.onAdd = function(world) {