Skip to content

Commit 584a844

Browse files
committed
Merging with alteredq's branch.
1 parent cee17e1 commit 584a844

20 files changed

+743
-695
lines changed

build/Three.js

+167-168
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/ThreeDebug.js

+168-169
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/ThreeExtras.js

+207-206
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/materials_cubemap_balls_reflection.html

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
GeometryUtils.merge( geometry, sphere );
9696

9797
}
98+
geometry.sortFacesByMaterial();
9899

99100
var path = "textures/cube/pisa/";
100101
var format = '.png';

examples/materials_cubemap_balls_refraction.html

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
GeometryUtils.merge( geometry, sphere );
9595

9696
}
97+
geometry.sortFacesByMaterial();
9798

9899
var path = "textures/cube/skybox/";
99100
var format = '.jpg';

examples/materials_gl.html

+16-15
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@
6666

6767
}
6868

69-
// Spheres
70-
71-
var geometry1 = new Sphere( 70, 32, 16, true );
72-
7369
var generatedTexture = new THREE.Texture( generateTexture() );
7470
generatedTexture.image.loaded = 1;
7571

@@ -89,13 +85,15 @@
8985
materials.push( { material: new THREE.MeshBasicMaterial( { map: generatedTexture } ), overdraw: true, doubleSided: false } );
9086
materials.push( { material: new THREE.MeshLambertMaterial( { map: generatedTexture } ), overdraw: true, doubleSided: false } );
9187

92-
// Extra mesh to be broken down for MeshFaceMaterial
93-
94-
var geometry2 = new Sphere( 70, 32, 16, true );
88+
// Spheres geometry
89+
90+
var geometry_smooth = new Sphere( 70, 32, 16, true );
91+
var geometry_flat = new Sphere( 70, 32, 16, false );
92+
var geometry_pieces = new Sphere( 70, 32, 16, true ); // Extra geometry to be broken down for MeshFaceMaterial
9593

96-
for ( var i = 0, l = geometry2.faces.length; i < l; i ++ ) {
94+
for ( var i = 0, l = geometry_pieces.faces.length; i < l; i ++ ) {
9795

98-
var face = geometry2.faces[ i ];
96+
var face = geometry_pieces.faces[ i ];
9997
if ( Math.random() > 0.7 ) face.material = [ materials[ Math.floor( Math.random() * materials.length ) ].material ];
10098

10199
}
@@ -104,16 +102,19 @@
104102

105103
objects = [];
106104

107-
var sphere, geometry;
105+
var sphere, geometry, material;
108106

109107
for ( var i = 0, l = materials.length; i < l; i ++ ) {
110108

111-
geometry = materials[ i ].material instanceof THREE.MeshFaceMaterial ? geometry2 : geometry1;
112-
113-
sphere = new THREE.Mesh( geometry, materials[ i ].material );
109+
material = materials[ i ].material;
110+
111+
geometry = material instanceof THREE.MeshFaceMaterial ? geometry_pieces :
112+
( material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth );
113+
114+
sphere = new THREE.Mesh( geometry, material );
114115

115-
sphere.overdraw = materials[ i ].overdraw;
116-
sphere.doubleSided = materials[ i ].doubleSided;
116+
sphere.overdraw = material.overdraw;
117+
sphere.doubleSided = material.doubleSided;
117118

118119
sphere.position.x = ( i % 4 ) * 200 - 400;
119120
sphere.position.z = Math.floor( i / 4 ) * 200 - 200;

examples/materials_shaders_fresnel.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
GeometryUtils.merge( geometry, sphere );
9393

9494
}
95-
95+
geometry.sortFacesByMaterial();
96+
9697
var path = "textures/cube/Park2/";
9798
var format = '.jpg';
9899
var urls = [

examples/obj/Bird.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ var Bird = function () {
2020
f3( 4, 7, 6 );
2121
f3( 5, 6, 7 );
2222

23+
this.computeCentroids();
24+
this.computeNormals();
25+
this.sortFacesByMaterial();
26+
2327
function v( x, y, z ) {
2428

2529
scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );

examples/obj/Qrcode.js

+1
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ var Qrcode = function () {
14351435

14361436
this.computeCentroids();
14371437
this.computeNormals();
1438+
this.sortFacesByMaterial();
14381439

14391440
function v( x, y, z ) {
14401441

examples/obj/WaltHead.js

+1
Original file line numberDiff line numberDiff line change
@@ -4885,6 +4885,7 @@ var WaltHead = function () {
48854885

48864886
this.computeCentroids();
48874887
this.computeNormals();
4888+
this.sortFacesByMaterial();
48884889

48894890
function v( x, y, z ) {
48904891

examples/uqbiquity_test.html

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175

176176
geometry.computeNormals();
177177
geometry.computeCentroids();
178+
geometry.sortFacesByMaterial();
178179

179180
mesh = new THREE.Mesh( geometry, [ new THREE.MeshFaceMaterial(), new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, wireframe: true, wireframe_linewidth: 10 } ) ] );
180181
mesh.doubleSided = true;

src/core/Geometry.js

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @author mr.doob / http://mrdoob.com/
33
* @author kile / http://kile.stravaganza.org/
4+
* @author alteredq / http://alteredqualia.com/
45
*/
56

67
THREE.Geometry = function () {
@@ -9,6 +10,8 @@ THREE.Geometry = function () {
910
this.faces = [];
1011
this.uvs = [];
1112

13+
this.geometryChunks = {};
14+
1215
};
1316

1417
THREE.Geometry.prototype = {
@@ -89,7 +92,7 @@ THREE.Geometry.prototype = {
8992

9093
if ( !cb.isZero() ) {
9194

92-
cb.normalize();
95+
cb.normalize();
9396

9497
}
9598

@@ -161,6 +164,80 @@ THREE.Geometry.prototype = {
161164

162165
},
163166

167+
sortFacesByMaterial: function () {
168+
169+
// TODO
170+
// Should optimize by grouping faces with ColorFill / ColorStroke materials
171+
// which could then use vertex color attributes instead of each being
172+
// in its separate VBO
173+
174+
var i, l, f, fl, face, material, vertices, mhash, ghash, hash_map = {};
175+
176+
function materialHash( material ) {
177+
178+
var hash_array = [];
179+
180+
for ( i = 0, l = material.length; i < l; i++ ) {
181+
182+
if ( material[ i ] == undefined ) {
183+
184+
hash_array.push( "undefined" );
185+
186+
} else {
187+
188+
hash_array.push( material[ i ].toString() );
189+
190+
}
191+
192+
}
193+
194+
return hash_array.join( '_' );
195+
196+
}
197+
198+
for ( f = 0, fl = this.faces.length; f < fl; f++ ) {
199+
200+
face = this.faces[ f ];
201+
material = face.material;
202+
203+
mhash = materialHash( material );
204+
205+
if ( hash_map[ mhash ] == undefined ) {
206+
207+
hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
208+
209+
}
210+
211+
ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
212+
213+
if ( this.geometryChunks[ ghash ] == undefined ) {
214+
215+
this.geometryChunks[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
216+
217+
}
218+
219+
vertices = face instanceof THREE.Face3 ? 3 : 4;
220+
221+
if ( this.geometryChunks[ ghash ].vertices + vertices > 65535 ) {
222+
223+
hash_map[ mhash ].counter += 1;
224+
ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
225+
226+
if ( this.geometryChunks[ ghash ] == undefined ) {
227+
228+
this.geometryChunks[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
229+
230+
}
231+
232+
}
233+
234+
this.geometryChunks[ ghash ].faces.push( f );
235+
this.geometryChunks[ ghash ].vertices += vertices;
236+
237+
}
238+
239+
},
240+
164241
toString: function () {
165242

166243
return 'THREE.Geometry ( vertices: ' + this.vertices + ', faces: ' + this.faces + ', uvs: ' + this.uvs + ' )';

src/extras/io/Loader.js

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ THREE.Loader.prototype = {
245245

246246
this.computeCentroids();
247247
this.computeNormals();
248+
this.sortFacesByMaterial();
248249

249250
//var e = (new Date).getTime();
250251

@@ -706,6 +707,7 @@ THREE.Loader.prototype = {
706707

707708
this.computeCentroids();
708709
this.computeNormals();
710+
this.sortFacesByMaterial();
709711

710712
function init_vertices() {
711713

src/extras/primitives/Cube.js

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ var Cube = function ( width, height, depth, segments_width, segments_height, mat
158158

159159
this.computeCentroids();
160160
this.computeNormals();
161+
this.sortFacesByMaterial();
161162

162163
}
163164

src/extras/primitives/Cylinder.js

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var Cylinder = function (numSegs, topRad, botRad, height, topOffset, botOffset)
6767

6868
this.computeCentroids();
6969
this.computeNormals();
70+
this.sortFacesByMaterial();
7071

7172
function v(x, y, z) {
7273

src/extras/primitives/Plane.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var Plane = function ( width, height, segments_width, segments_height ) {
5454

5555
this.computeCentroids();
5656
this.computeNormals();
57+
this.sortFacesByMaterial();
5758

5859
}
5960

src/extras/primitives/Sphere.js

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ var Sphere = function ( radius, segments_width, segments_height ) {
105105

106106
this.computeCentroids();
107107
this.computeNormals();
108+
this.sortFacesByMaterial();
109+
108110
}
109111

110112
Sphere.prototype = new THREE.Geometry();

src/objects/Mesh.js

+1-77
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @author mr.doob / http://mrdoob.com/
3+
* @author alteredq / http://alteredqualia.com/
34
*/
45

56
THREE.Mesh = function ( geometry, material, normUVs ) {
@@ -14,8 +15,6 @@ THREE.Mesh = function ( geometry, material, normUVs ) {
1415

1516
this.overdraw = false;
1617

17-
this.materialFaceGroup = {};
18-
this.sortFacesByMaterial();
1918
if ( normUVs ) this.normalizeUVs();
2019

2120
this.geometry.computeBoundingBox();
@@ -25,81 +24,6 @@ THREE.Mesh = function ( geometry, material, normUVs ) {
2524
THREE.Mesh.prototype = new THREE.Object3D();
2625
THREE.Mesh.prototype.constructor = THREE.Mesh;
2726

28-
THREE.Mesh.prototype.sortFacesByMaterial = function () {
29-
30-
// TODO
31-
// Should optimize by grouping faces with ColorFill / ColorStroke materials
32-
// which could then use vertex color attributes instead of each being
33-
// in its separate VBO
34-
35-
var i, l, f, fl, face, material, vertices, mhash, ghash, hash_map = {};
36-
37-
function materialHash( material ) {
38-
39-
var hash_array = [];
40-
41-
for ( i = 0, l = material.length; i < l; i++ ) {
42-
43-
if ( material[ i ] == undefined ) {
44-
45-
hash_array.push( "undefined" );
46-
47-
} else {
48-
49-
hash_array.push( material[ i ].toString() );
50-
51-
}
52-
53-
}
54-
55-
return hash_array.join( '_' );
56-
57-
}
58-
59-
for ( f = 0, fl = this.geometry.faces.length; f < fl; f++ ) {
60-
61-
face = this.geometry.faces[ f ];
62-
material = face.material;
63-
64-
mhash = materialHash( material );
65-
66-
if ( hash_map[ mhash ] == undefined ) {
67-
68-
hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
69-
70-
}
71-
72-
ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
73-
74-
if ( this.materialFaceGroup[ ghash ] == undefined ) {
75-
76-
this.materialFaceGroup[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
77-
78-
}
79-
80-
vertices = face instanceof THREE.Face3 ? 3 : 4;
81-
82-
if ( this.materialFaceGroup[ ghash ].vertices + vertices > 65535 ) {
83-
84-
hash_map[ mhash ].counter += 1;
85-
ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
86-
87-
if ( this.materialFaceGroup[ ghash ] == undefined ) {
88-
89-
this.materialFaceGroup[ ghash ] = { 'faces': [], 'material': material, 'vertices': 0 };
90-
91-
}
92-
93-
}
94-
95-
this.materialFaceGroup[ ghash ].faces.push( f );
96-
this.materialFaceGroup[ ghash ].vertices += vertices;
97-
98-
99-
}
100-
101-
};
102-
10327
THREE.Mesh.prototype.normalizeUVs = function () {
10428

10529
var i, il, j, jl, uvs;

src/objects/Object3D.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
THREE.Object3D = function ( material ) {
66

7+
this.id = THREE.Object3DCounter.value ++;
8+
79
this.position = new THREE.Vector3();
810
this.rotation = new THREE.Vector3();
911
this.scale = new THREE.Vector3( 1, 1, 1 );
@@ -35,3 +37,5 @@ THREE.Object3D = function ( material ) {
3537
};
3638

3739
};
40+
41+
THREE.Object3DCounter = { value: 0 };

0 commit comments

Comments
 (0)