From 406063d588d6e83b71bdea52d3f60534baace192 Mon Sep 17 00:00:00 2001 From: alteredq Date: Tue, 2 Nov 2010 19:12:05 +0100 Subject: [PATCH] Added breaking of mesh into chunks of max 64K vertices (done in materials sorting step). --- examples/large_mesh_test.html | 319 ++++++++++++++++++++++++++++++++++ src/objects/Mesh.js | 40 ++++- 2 files changed, 351 insertions(+), 8 deletions(-) create mode 100644 examples/large_mesh_test.html diff --git a/examples/large_mesh_test.html b/examples/large_mesh_test.html new file mode 100644 index 0000000000..e9cbce6116 --- /dev/null +++ b/examples/large_mesh_test.html @@ -0,0 +1,319 @@ + + + + three.js - Large mesh test + + + + + +
+

Large mesh test

+ + 2d canvas renderer + WebGL renderer +
+ +

Lucy model from Stanford 3d scanning repository + (decimated with Meshlab). + +

Please be patient while the mesh is loading. It may take a while, it's 4.5MB file. +

Using a modified version of Three.js by mrdoob. + +
+

Best viewed in Chrome 8/9 or Firefox 4 using WebGL renderer. +

Canvas renderer is very slow for this demo and only diffuse material works there. +

+ +

+
+		
+
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+
+		
+
+		
+
+	
+
diff --git a/src/objects/Mesh.js b/src/objects/Mesh.js
index c77a258b85..d2c8a3e4ec 100644
--- a/src/objects/Mesh.js
+++ b/src/objects/Mesh.js
@@ -32,11 +32,11 @@ THREE.Mesh.prototype.sortFacesByMaterial = function () {
 	// which could then use vertex color attributes instead of each being
 	// in its separate VBO
 
-	var i, l, f, fl, face, material, hash_array;
+	var i, l, f, fl, face, material, vertices, mhash, ghash, hash_map = {};
 
 	function materialHash( material ) {
 
-		hash_array = [];
+		var hash_array = [];
 
 		for ( i = 0, l = material.length; i < l; i++ ) {
 
@@ -60,20 +60,44 @@ THREE.Mesh.prototype.sortFacesByMaterial = function () {
 
 		face = this.geometry.faces[ f ];
 		material = face.material;
+		
+		mhash = materialHash( material );
 
-		hash = materialHash( material);
+		if ( hash_map[ mhash ] == undefined ) {
+			
+			hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
+		}
+
+		ghash = hash_map[ mhash ].hash + "_" + hash_map[ mhash ].counter;
 
-		if ( this.materialFaceGroup[ hash ] == undefined ) {
+		if ( this.materialFaceGroup[ ghash ] == undefined ) {
 
-			this.materialFaceGroup[ hash ] = { 'faces': [], 'material': material };
+			this.materialFaceGroup[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
 
 		}
+		
+		vertices = face instanceof THREE.Face3 ? 3 : 4;
+
+		if ( this.materialFaceGroup[ ghash ].vertices + vertices > 65535 ) {
+			
+			hash_map[ mhash ].counter += 1;
+			ghash = hash_map[ mhash ].hash + "_" + hash_map[ mhash ].counter;
+			
+			if ( this.materialFaceGroup[ ghash ] == undefined ) {
+
+				this.materialFaceGroup[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
 
-		this.materialFaceGroup[ hash ].faces.push( f );
+			}
+			
+		}
+		
+		this.materialFaceGroup[ ghash ].faces.push( f );
+		this.materialFaceGroup[ ghash ].vertices += vertices;
+		
 
 	}
 
-}
+};
 
 THREE.Mesh.prototype.normalizeUVs = function () {
 
@@ -95,4 +119,4 @@ THREE.Mesh.prototype.normalizeUVs = function () {
 
 	}
 
-}
+};
-- 
GitLab