提交 d961ee52 编写于 作者: M Mr.doob

Started GeometryUtils class.

MeshUtils.merge( object1, object2 [ geometry | mesh ] ): Merges object2 into object1. Implemented in materials_cubemap_sky.
上级 2be2673d
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
<script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script> <script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
<script type="text/javascript" src="../src/extras/primitives/Plane.js"></script> <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
<script type="text/javascript" src="../src/extras/GeometryUtils.js"></script>
<script type="text/javascript" src="js/Stats.js"></script> <script type="text/javascript" src="js/Stats.js"></script>
...@@ -76,27 +77,31 @@ ...@@ -76,27 +77,31 @@
scene = new THREE.Scene(); scene = new THREE.Scene();
sceneCube = new THREE.Scene(); sceneCube = new THREE.Scene();
var r = "textures/skymap_"; var geometry = new THREE.Geometry();
var sphere = new THREE.Mesh( new Sphere( 100, 32, 16 ) );
for ( var i = 0; i < 200; i ++ ) {
sphere.position.x = Math.random() * 10000 - 5000;
sphere.position.y = Math.random() * 10000 - 5000;
sphere.position.z = Math.random() * 10000 - 5000;
sphere.scale.x = sphere.scale.y = sphere.scale.z = Math.random() * 4 + 1;
// Merging spheres in a single geometry
GeometryUtils.merge( geometry, sphere );
}
var r = "textures/skymap_";
var urls = [ r + "right1024.jpg", r + "left1024.jpg", var urls = [ r + "right1024.jpg", r + "left1024.jpg",
r + "top1024.jpg", r + "bottom1024.jpg", r + "top1024.jpg", r + "bottom1024.jpg",
r + "front1024.jpg", r + "back1024.jpg" ]; r + "front1024.jpg", r + "back1024.jpg" ];
var images = loadImageArray( urls ); var images = loadImageArray( urls );
var material = new THREE.MeshBasicMaterial( { color: 0xffffff, env_map: new THREE.TextureCube( images ) } ); var material = new THREE.MeshBasicMaterial( { color: 0xffffff, env_map: new THREE.TextureCube( images ) } );
var geometry = new Sphere( 100, 32, 16, true );
for ( var i = 0; i < 200; i ++ ) {
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = Math.random() * 10000 - 5000;
mesh.position.y = Math.random() * 10000 - 5000;
mesh.position.z = Math.random() * 10000 - 5000;
mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 1;
scene.addObject( mesh );
} var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = 100;
scene.addObject( mesh );
createCube( 100000, images ); createCube( 100000, images );
......
...@@ -10,7 +10,7 @@ THREE.Face3 = function ( a, b, c, normal, material ) { ...@@ -10,7 +10,7 @@ THREE.Face3 = function ( a, b, c, normal, material ) {
this.centroid = new THREE.Vector3(); this.centroid = new THREE.Vector3();
this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3(); this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
this.vertexNormals = normal instanceof Array ? normal : []; this.vertexNormals = normal instanceof Array ? normal : [];
this.material = material instanceof Array ? material : [ material ]; this.material = material instanceof Array ? material : [ material ];
......
...@@ -11,7 +11,7 @@ THREE.Face4 = function ( a, b, c, d, normal, material ) { ...@@ -11,7 +11,7 @@ THREE.Face4 = function ( a, b, c, d, normal, material ) {
this.centroid = new THREE.Vector3(); this.centroid = new THREE.Vector3();
this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3(); this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
this.vertexNormals = normal instanceof Array ? normal : []; this.vertexNormals = normal instanceof Array ? normal : [];
this.material = material instanceof Array ? material : [ material ]; this.material = material instanceof Array ? material : [ material ];
......
var GeometryUtils = {
merge: function ( object1, object2 ) {
var isMesh = object2 instanceof THREE.Mesh;
var geometry1 = object1,
vertexPosition = geometry1.vertices.length,
facePosition = geometry1.faces.length,
uvPosition = geometry1.uvs.length,
geometry2 = isMesh ? object2.geometry : object2;
if ( isMesh ) object2.updateMatrix();
for ( var i = 0, il = geometry2.vertices.length; i < il; i ++ ) {
var vertex = geometry2.vertices[ i ];
var vertexCopy = new THREE.Vertex( vertex.position.clone() );
if ( isMesh ) object2.matrix.transform( vertexCopy.position );
geometry1.vertices.push( vertexCopy );
}
for ( var i = 0, il = geometry2.faces.length; i < il; i ++ ) {
var face = geometry2.faces[ i ];
if ( face instanceof THREE.Face3 ) {
var faceCopy = new THREE.Face3();
faceCopy.a = face.a + vertexPosition;
faceCopy.b = face.b + vertexPosition;
faceCopy.c = face.c + vertexPosition;
} else if ( face instanceof THREE.Face4 ) {
var faceCopy = new THREE.Face4();
faceCopy.a = face.a + vertexPosition;
faceCopy.b = face.b + vertexPosition;
faceCopy.c = face.c + vertexPosition;
faceCopy.d = face.d + vertexPosition;
}
for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
var normal = face.vertexNormals[ j ];
faceCopy.vertexNormals.push( normal.clone() );
}
geometry1.faces.push( faceCopy );
}
for ( var i = 0, il = geometry2.uvs.length; i < il; i ++ ) {
var uv = geometry2.uvs[ i ];
var uvCopy = [];
for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
uvCopy.push( new THREE.UV( uv[ j ].u, uv[ j ].v ) );
}
geometry1.uvs.push( uvCopy );
}
}
}
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Sphere.as * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Sphere.as
*/ */
var Sphere = function ( radius, segments_width, segments_height, smooth ) { var Sphere = function ( radius, segments_width, segments_height ) {
THREE.Geometry.call( this ); THREE.Geometry.call( this );
...@@ -78,15 +78,7 @@ var Sphere = function ( radius, segments_width, segments_height, smooth ) { ...@@ -78,15 +78,7 @@ var Sphere = function ( radius, segments_width, segments_height, smooth ) {
n2.normalize(); n2.normalize();
n3.normalize(); n3.normalize();
if ( smooth ) { this.faces.push( new THREE.Face3( aP1, aP2, aP3, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
this.faces.push( new THREE.Face3( aP1, aP2, aP3, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
} else {
this.faces.push( new THREE.Face3( aP1, aP2, aP3 ) );
}
this.uvs.push( [ aP1uv, aP2uv, aP3uv ] ); this.uvs.push( [ aP1uv, aP2uv, aP3uv ] );
...@@ -101,15 +93,7 @@ var Sphere = function ( radius, segments_width, segments_height, smooth ) { ...@@ -101,15 +93,7 @@ var Sphere = function ( radius, segments_width, segments_height, smooth ) {
n2.normalize(); n2.normalize();
n3.normalize(); n3.normalize();
if ( smooth ) { this.faces.push( new THREE.Face3( aP1, aP3, aP4, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
this.faces.push( new THREE.Face3( aP1, aP3, aP4, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
} else {
this.faces.push( new THREE.Face3( aP1, aP3, aP4 ) );
}
this.uvs.push( [ aP1uv, aP3uv, aP4uv ] ); this.uvs.push( [ aP1uv, aP3uv, aP4uv ] );
......
...@@ -16,7 +16,7 @@ THREE.Mesh = function ( geometry, material, normUVs ) { ...@@ -16,7 +16,7 @@ THREE.Mesh = function ( geometry, material, normUVs ) {
this.materialFaceGroup = {}; this.materialFaceGroup = {};
this.sortFacesByMaterial(); this.sortFacesByMaterial();
if( normUVs ) this.normalizeUVs(); if ( normUVs ) this.normalizeUVs();
this.geometry.computeBoundingBox(); this.geometry.computeBoundingBox();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册