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

* `examples/geometry/primitives` ⟶ `src/extras/primitives`

上级 a35b35fd
/**
* @author mr.doob / http://mrdoob.com/
*/
var Cube = function (width, height, depth) {
THREE.Geometry.call(this);
var scope = this,
width_half = width / 2,
height_half = height / 2,
depth_half = depth / 2;
v( width_half, height_half, -depth_half );
v( width_half, -height_half, -depth_half );
v( -width_half, -height_half, -depth_half );
v( -width_half, height_half, -depth_half );
v( width_half, height_half, depth_half );
v( width_half, -height_half, depth_half );
v( -width_half, -height_half, depth_half );
v( -width_half, height_half, depth_half );
f4( 0, 1, 2, 3 );
f4( 4, 7, 6, 5 );
f4( 0, 4, 5, 1 );
f4( 1, 5, 6, 2 );
f4( 2, 6, 7, 3 );
f4( 4, 0, 3, 7 );
function v(x, y, z) {
scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
}
function f4(a, b, c, d) {
scope.faces.push( new THREE.Face4( a, b, c, d ) );
}
this.computeCentroids();
this.computeNormals();
}
Cube.prototype = new THREE.Geometry();
Cube.prototype.constructor = Cube;
/**
* @author kile / http://kile.stravaganza.org/
*/
var Cylinder = function (numSegs, topRad, botRad, height, topOffset, botOffset) {
THREE.Geometry.call(this);
var scope = this, i;
// VERTICES
// Top circle vertices
for (i = 0; i < numSegs; i++) {
v(
Math.sin(2 * 3.1415 * i / numSegs)*topRad,
Math.cos(2 * 3.1415 * i / numSegs)*topRad,
0);
}
// Bottom circle vertices
for (i = 0; i < numSegs; i++) {
v(
Math.sin(2 * 3.1415 * i / numSegs)*botRad,
Math.cos(2 * 3.1415 * i / numSegs)*botRad,
height);
}
// FACES
// Body
for (i = 0; i < numSegs; i++) {
f4(i, i + numSegs, numSegs + (i + 1) % numSegs, (i + 1) % numSegs, '#ff0000');
}
// Bottom circle
if (botRad != 0) {
v(0, 0, -topOffset);
for (i = numSegs; i < numSegs + (numSegs / 2); i++) {
f4(2 * numSegs,
(2 * i - 2 * numSegs) % numSegs,
(2 * i - 2 * numSegs + 1) % numSegs,
(2 * i - 2 * numSegs + 2) % numSegs);
}
}
// Top circle
if (topRad != 0) {
v(0, 0, height + topOffset);
for (i = numSegs + (numSegs / 2); i < 2 * numSegs; i++) {
f4( (2 * i - 2 * numSegs + 2) % numSegs + numSegs,
(2 * i - 2 * numSegs + 1) % numSegs + numSegs,
(2 * i - 2 * numSegs) % numSegs+numSegs,
2 * numSegs + 1);
}
}
this.computeCentroids();
this.computeNormals();
function v(x, y, z) {
scope.vertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
}
function f4(a, b, c, d) {
scope.faces.push( new THREE.Face4( a, b, c, d ) );
}
}
Cylinder.prototype = new THREE.Geometry();
Cylinder.prototype.constructor = Cylinder;
/**
* @author mr.doob / http://mrdoob.com/
* based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
*/
var Plane = function ( width, height, segments_width, segments_height ) {
THREE.Geometry.call( this );
var ix, iy,
width_half = width / 2,
height_half = height / 2,
gridX = segments_width || 1,
gridY = segments_height || 1,
gridX1 = gridX + 1,
gridY1 = gridY + 1,
segment_width = width / gridX,
segment_height = height / gridY;
for( iy = 0; iy < gridY1; iy++ ) {
for( ix = 0; ix < gridX1; ix++ ) {
var x = ix * segment_width - width_half;
var y = iy * segment_height - height_half;
this.vertices.push( new THREE.Vertex( new THREE.Vector3( x, - y, 0 ) ) );
}
}
for( iy = 0; iy < gridY; iy++ ) {
for( ix = 0; ix < gridX; ix++ ) {
var a = ix + gridX1 * iy;
var b = ix + gridX1 * ( iy + 1 );
var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
var d = ( ix + 1 ) + gridX1 * iy;
this.faces.push( new THREE.Face4( a, b, c, d ) );
this.uvs.push( [
new THREE.UV( ix / gridX, iy / gridY ),
new THREE.UV( ix / gridX, ( iy + 1 ) / gridY ),
new THREE.UV( ( ix + 1 ) / gridX, ( iy + 1 ) / gridY ),
new THREE.UV( ( ix + 1 ) / gridX, iy / gridY )
] );
}
}
this.computeCentroids();
this.computeNormals();
}
Plane.prototype = new THREE.Geometry();
Plane.prototype.constructor = Plane;
/**
* @author mr.doob / http://mrdoob.com/
* 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 ) {
THREE.Geometry.call( this );
var gridX = segments_width || 8,
gridY = segments_height || 6;
var i, j;
var iHor = Math.max( 3, gridX );
var iVer = Math.max( 2, gridY );
var aVtc = [];
for ( j = 0; j < ( iVer + 1 ) ; j++ ) {
var fRad1 = j / iVer;
var fZ = radius * Math.cos( fRad1 * Math.PI );
var fRds = radius * Math.sin( fRad1 * Math.PI );
var aRow = [];
var oVtx = 0;
for ( i = 0; i < iHor; i++ ) {
var fRad2 = 2 * i / iHor;
var fX = fRds * Math.sin( fRad2 * Math.PI );
var fY = fRds * Math.cos( fRad2 * Math.PI );
if ( !( ( j == 0 || j == iVer ) && i > 0 ) ) {
oVtx = this.vertices.push( new THREE.Vertex( new THREE.Vector3( fY, fZ, fX ) ) ) - 1;
}
aRow.push( oVtx );
}
aVtc.push( aRow );
}
var n1, n2, n3, iVerNum = aVtc.length;
for ( j = 0; j < iVerNum; j++ ) {
var iHorNum = aVtc[ j ].length;
if ( j > 0 ) {
for ( i = 0; i < iHorNum; i++ ) {
var bEnd = i == ( iHorNum - 1 );
var aP1 = aVtc[ j ][ bEnd ? 0 : i + 1 ];
var aP2 = aVtc[ j ][ ( bEnd ? iHorNum - 1 : i ) ];
var aP3 = aVtc[ j - 1 ][ ( bEnd ? iHorNum - 1 : i ) ];
var aP4 = aVtc[ j - 1 ][ bEnd ? 0 : i + 1 ];
var fJ0 = j / ( iVerNum - 1 );
var fJ1 = ( j - 1 ) / ( iVerNum - 1 );
var fI0 = ( i + 1 ) / iHorNum;
var fI1 = i / iHorNum;
var aP1uv = new THREE.UV( 1 - fI0, fJ0 );
var aP2uv = new THREE.UV( 1 - fI1, fJ0 );
var aP3uv = new THREE.UV( 1 - fI1, fJ1 );
var aP4uv = new THREE.UV( 1 - fI0, fJ1 );
if ( j < ( aVtc.length - 1 ) ) {
n1 = this.vertices[ aP1 ].position.clone();
n2 = this.vertices[ aP2 ].position.clone();
n3 = this.vertices[ aP3 ].position.clone();
n1.normalize();
n2.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 ) ] ) );
} else {
this.faces.push( new THREE.Face3( aP1, aP2, aP3 ) );
}
this.uvs.push( [ aP1uv, aP2uv, aP3uv ] );
}
if ( j > 1 ) {
n1 = this.vertices[aP1].position.clone();
n2 = this.vertices[aP3].position.clone();
n3 = this.vertices[aP4].position.clone();
n1.normalize();
n2.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 ) ] ) );
} else {
this.faces.push( new THREE.Face3( aP1, aP3, aP4 ) );
}
this.uvs.push( [ aP1uv, aP3uv, aP4uv ] );
}
}
}
}
this.computeCentroids();
this.computeNormals();
}
Sphere.prototype = new THREE.Geometry();
Sphere.prototype.constructor = Sphere;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册