Skip to content

Commit 8edfa99

Browse files
committedSep 29, 2011
Rename SubdivisonGeometry.js to SubdivisionGeometry.js. Fix geometries which use duplicate vertices. SubdivisionGeometry tested with Cube/Sphere/Cylinder/TextGeometry
1 parent 670ebf6 commit 8edfa99

File tree

5 files changed

+267
-421
lines changed

5 files changed

+267
-421
lines changed
 

‎build/Three.js

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

‎examples/canvas_geometry_subdivison.html

+58-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
<script src="../src/core/Geometry.js"></script>
2424
<script src="../src/extras/geometries/CubeGeometry.js"></script>
25-
25+
<script src="../src/extras/geometries/CylinderGeometry.js"></script>
26+
<script src="../src/extras/geometries/SubdivisionGeometry.js"></script>
27+
28+
<script src="fonts/helvetiker_regular.typeface.js"></script>
29+
2630
<script>
2731

2832
var container, stats;
@@ -87,7 +91,22 @@
8791

8892
}
8993

90-
geometry = new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials );
94+
geometry = new THREE.CubeGeometry( 200, 200, 200, 2, 2, 2, materials );
95+
96+
//geometry = new THREE.SphereGeometry( 200, 1, 1);
97+
//geometry = new THREE.CylinderGeometry( 50, 50, 200 );
98+
// geometry = new THREE.TextGeometry( '&', {
99+
// size: 200,
100+
// height: 50,
101+
// curveSegments: 1,
102+
// font: "helvetiker"
103+
//
104+
// });
105+
106+
//PlaneGeometry not supported
107+
108+
// quick fix for duplicated vertices
109+
geometry.checkDupVertices();
91110

92111
smooth = createSubdivision(geometry, 2);
93112

@@ -100,19 +119,48 @@
100119
context.fill();
101120

102121
}
122+
123+
var drawText = function(i) {
124+
125+
return function ( context ) {
126+
127+
context.beginPath();
128+
context.scale(0.1,-0.1);
129+
130+
context.fillText(i, 0,0);
131+
132+
133+
134+
};
135+
136+
137+
}
138+
139+
103140

104141
group = new THREE.Object3D();
105142
group.position.y = 150;
106143
scene.add( group );
107144

108-
for ( var i = 0; i < smooth.vertices.length; i++ ) {
109-
110-
particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
111-
particle.position = smooth.vertices[i].position;
112-
var pos = smooth.vertices.position
113-
particle.scale.x = particle.scale.y = 5;
114-
group.add( particle );
115-
}
145+
// Debug new Points
146+
// for ( var i = 0; i < smooth.vertices.length; i++ ) {
147+
//
148+
// particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
149+
// particle.position = smooth.vertices[i].position;
150+
// var pos = smooth.vertices.position
151+
// particle.scale.x = particle.scale.y = 5;
152+
// group.add( particle );
153+
// }
154+
155+
// Debug original points
156+
// for ( var i = 0; i < geometry.vertices.length; i++ ) {
157+
//
158+
// particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
159+
// particle.position = smooth.vertices[i].position;
160+
// var pos = geometry.vertices.position
161+
// particle.scale.x = particle.scale.y = 30;
162+
// group.add( particle );
163+
// }
116164

117165

118166
cube = new THREE.Mesh( smooth, new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } ) ); //new THREE.MeshFaceMaterial()

‎src/core/Geometry.js

+62-17
Original file line numberDiff line numberDiff line change
@@ -450,19 +450,11 @@ THREE.Geometry.prototype = {
450450
if ( map[ hash ] === undefined ) {
451451

452452
map[ hash ] = { "set": {}, "array": [] };
453-
map[ hash ].set[ i ] = 1;
454-
map[ hash ].array.push( i );
455-
456-
} else {
457-
458-
if( map[ hash ].set[ i ] === undefined ) {
459-
460-
map[ hash ].set[ i ] = 1;
461-
map[ hash ].array.push( i );
462-
463-
}
464-
465-
}
453+
454+
}
455+
456+
map[ hash ].set[ i ] = 1;
457+
map[ hash ].array.push( i );
466458

467459
};
468460

@@ -486,7 +478,7 @@ THREE.Geometry.prototype = {
486478
hash = edge_hash( face.b, face.c );
487479
addToMap( vfMap, hash, i );
488480

489-
hash = edge_hash( face.a, face.c );
481+
hash = edge_hash( face.c, face.a );
490482
addToMap( vfMap, hash, i );
491483

492484
} else if ( face instanceof THREE.Face4 ) {
@@ -504,14 +496,14 @@ THREE.Geometry.prototype = {
504496
hash = edge_hash( face.a, face.b );
505497
addToMap( vfMap, hash, i );
506498

507-
hash = edge_hash( face.a, face.d );
508-
addToMap( vfMap, hash, i );
509-
510499
hash = edge_hash( face.b, face.c );
511500
addToMap( vfMap, hash, i );
512501

513502
hash = edge_hash( face.c, face.d );
514503
addToMap( vfMap, hash, i );
504+
505+
hash = edge_hash( face.d, face.a );
506+
addToMap( vfMap, hash, i );
515507

516508
}
517509

@@ -553,6 +545,59 @@ THREE.Geometry.prototype = {
553545
//
554546
// }
555547

548+
},
549+
550+
checkDupVertices: function() {
551+
var uniqueVertices = {}, patch = {};
552+
var v, key;
553+
var precision = 1;
554+
var i,il, face;
555+
556+
for (i=0,il=this.vertices.length;i<il;i++) {
557+
558+
v = this.vertices[i].position;
559+
//key = [v.x.toFixed(precision), v.y.toFixed(precision), v.z.toFixed(precision)].join('_');
560+
key = [Math.round(v.x * 1000), Math.round(v.y* 1000), Math.round(v.z* 1000)].join('_');
561+
562+
if (uniqueVertices[key]===undefined) {
563+
uniqueVertices[key] = i;
564+
} else {
565+
//console.log('HEY ', i, 'should be using ', uniqueVertices[key]);
566+
patch[i] = uniqueVertices[key];
567+
}
568+
569+
};
570+
571+
// Start to patch.
572+
//console.log(patch);
573+
574+
var runPatch = function(i) {
575+
if (patch[i] !== undefined) {
576+
//console.log('fixing',i, 'to', patch[i]);
577+
return patch[i];
578+
}
579+
return i;
580+
};
581+
for( i = 0, il = this.faces.length; i < il; i ++ ) {
582+
583+
face = this.faces[ i ];
584+
585+
if ( face instanceof THREE.Face3 ) {
586+
face.a = runPatch(face.a);
587+
face.b = runPatch(face.b);
588+
face.c = runPatch(face.c);
589+
590+
} if ( face instanceof THREE.Face4 ) {
591+
592+
face.a = runPatch(face.a);
593+
face.b = runPatch(face.b);
594+
face.c = runPatch(face.c);
595+
face.d = runPatch(face.d);
596+
597+
}
598+
}
599+
600+
//console.log(this);
556601
}
557602

558603
};

‎src/extras/geometries/SubdivisonGeometry.js

-248
This file was deleted.

‎utils/build.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
'extras/geometries/CubeGeometry.js',
103103
'extras/geometries/CylinderGeometry.js',
104104
'extras/geometries/ExtrudeGeometry.js',
105-
'extras/geometries/SubdivisonGeometry.js',
105+
'extras/geometries/SubdivisionGeometry.js',
106106
'extras/geometries/IcosahedronGeometry.js',
107107
'extras/geometries/LatheGeometry.js',
108108
'extras/geometries/PlaneGeometry.js',

0 commit comments

Comments
 (0)
Please sign in to comment.