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

M
Mr.doob 已提交
6 7
import { Geometry } from '../core/Geometry';

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

R
Rich Harris 已提交
10
	Geometry.call( this );
11

12
	this.type = 'PlaneGeometry';
M
Mr.doob 已提交
13

M
Mr.doob 已提交
14 15 16 17 18 19 20
	this.parameters = {
		width: width,
		height: height,
		widthSegments: widthSegments,
		heightSegments: heightSegments
	};

R
Rich Harris 已提交
21
	this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
M
Mr.doob 已提交
22

M
Mr.doob 已提交
23
}
M
Mr.doob 已提交
24

R
Rich Harris 已提交
25 26 27
PlaneGeometry.prototype = Object.create( Geometry.prototype );
PlaneGeometry.prototype.constructor = PlaneGeometry;

M
Mr.doob 已提交
28 29
/**
 * @author mrdoob / http://mrdoob.com/
M
Mugen87 已提交
30 31
 * @author Mugen87 / https://github.com/Mugen87
 *
M
Mr.doob 已提交
32 33 34
 * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
 */

M
Mugen87 已提交
35
import { Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from '../core/BufferAttribute';
M
Mr.doob 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
import { BufferGeometry } from '../core/BufferGeometry';

function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {

	BufferGeometry.call( this );

	this.type = 'PlaneBufferGeometry';

	this.parameters = {
		width: width,
		height: height,
		widthSegments: widthSegments,
		heightSegments: heightSegments
	};

	var width_half = width / 2;
	var height_half = height / 2;

	var gridX = Math.floor( widthSegments ) || 1;
	var gridY = Math.floor( heightSegments ) || 1;

	var gridX1 = gridX + 1;
	var gridY1 = gridY + 1;

	var segment_width = width / gridX;
	var segment_height = height / gridY;

M
Mugen87 已提交
63
	var ix, iy;
M
Mr.doob 已提交
64

M
Mugen87 已提交
65
	// buffers
M
Mr.doob 已提交
66

M
Mugen87 已提交
67 68 69 70 71 72 73 74
	var indices = [];
	var vertices = [];
	var normals = [];
	var uvs = [];

	// generate vertices, normals and uvs

	for ( iy = 0; iy < gridY1; iy ++ ) {
M
Mr.doob 已提交
75 76 77

		var y = iy * segment_height - height_half;

M
Mugen87 已提交
78
		for ( ix = 0; ix < gridX1; ix ++ ) {
M
Mr.doob 已提交
79 80 81

			var x = ix * segment_width - width_half;

M
Mugen87 已提交
82
			vertices.push( x, - y, 0 );
M
Mr.doob 已提交
83

M
Mugen87 已提交
84
			normals.push( 0, 0, 1 );
M
Mr.doob 已提交
85

M
Mugen87 已提交
86 87
			uvs.push( ix / gridX );
			uvs.push( 1 - ( iy / gridY ) );
M
Mr.doob 已提交
88 89 90 91 92

		}

	}

M
Mugen87 已提交
93
	// indices
M
Mr.doob 已提交
94

M
Mugen87 已提交
95
	for ( iy = 0; iy < gridY; iy ++ ) {
M
Mr.doob 已提交
96

M
Mugen87 已提交
97
		for ( ix = 0; ix < gridX; ix ++ ) {
M
Mr.doob 已提交
98 99 100 101 102 103

			var a = ix + gridX1 * iy;
			var b = ix + gridX1 * ( iy + 1 );
			var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
			var d = ( ix + 1 ) + gridX1 * iy;

M
Mugen87 已提交
104
			// faces
M
Mr.doob 已提交
105

M
Mugen87 已提交
106 107
			indices.push( a, b, d );
			indices.push( b, c, d );
M
Mr.doob 已提交
108 109 110 111 112

		}

	}

M
Mugen87 已提交
113 114
	// build geometry

115
	this.setIndex( new ( vertices.length > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ) );
M
Mugen87 已提交
116 117 118
	this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
	this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
	this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
M
Mr.doob 已提交
119 120 121 122 123

}

PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
R
Rich Harris 已提交
124

M
Mr.doob 已提交
125
export { PlaneGeometry, PlaneBufferGeometry };