PlaneGeometry.js 1.7 KB
Newer Older
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
3 4 5
 * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
 */

M
Mr.doob 已提交
6
THREE.PlaneGeometry = function ( width, height, widthSegments, heightSegments ) {
7 8 9

	THREE.Geometry.call( this );

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	this.width = width;
	this.height = height;

	this.widthSegments = widthSegments || 1;
	this.heightSegments = heightSegments || 1;

	var ix, iz;
	var width_half = width / 2;
	var height_half = height / 2;

	var gridX = this.widthSegments;
	var gridZ = this.heightSegments;

	var gridX1 = gridX + 1;
	var gridZ1 = gridZ + 1;

	var segment_width = this.width / gridX;
	var segment_height = this.height / gridZ;

	var normal = new THREE.Vector3( 0, 0, 1 );
30

31
	for ( iz = 0; iz < gridZ1; iz ++ ) {
32

33
		for ( ix = 0; ix < gridX1; ix ++ ) {
34 35

			var x = ix * segment_width - width_half;
36
			var y = iz * segment_height - height_half;
37

38
			this.vertices.push( new THREE.Vector3( x, - y, 0 ) );
39 40 41 42

		}

	}
43

44
	for ( iz = 0; iz < gridZ; iz ++ ) {
45

46
		for ( ix = 0; ix < gridX; ix ++ ) {
47

48 49 50 51
			var a = ix + gridX1 * iz;
			var b = ix + gridX1 * ( iz + 1 );
			var c = ( ix + 1 ) + gridX1 * ( iz + 1 );
			var d = ( ix + 1 ) + gridX1 * iz;
52

M
Mr.doob 已提交
53 54 55 56 57
			var face = new THREE.Face4( a, b, c, d );
			face.normal.copy( normal );
			face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone(), normal.clone() );

			this.faces.push( face );
A
alteredq 已提交
58
			this.faceVertexUvs[ 0 ].push( [
59 60 61 62
				new THREE.UV( ix / gridX, 1 - iz / gridZ ),
				new THREE.UV( ix / gridX, 1 - ( iz + 1 ) / gridZ ),
				new THREE.UV( ( ix + 1 ) / gridX, 1 - ( iz + 1 ) / gridZ ),
				new THREE.UV( ( ix + 1 ) / gridX, 1 - iz / gridZ )
63
			] );
64 65 66 67 68 69 70

		}

	}

	this.computeCentroids();

71
};
72

73
THREE.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );