UNPKG

2.64 kBJavaScriptView Raw
1/**
2 * @author mrdoob / http://mrdoob.com/
3 * @author Mugen87 / https://github.com/Mugen87
4 */
5
6import { Geometry } from '../core/Geometry.js';
7import { BufferGeometry } from '../core/BufferGeometry.js';
8import { Float32BufferAttribute } from '../core/BufferAttribute.js';
9
10// PlaneGeometry
11
12function PlaneGeometry( width, height, widthSegments, heightSegments ) {
13
14 Geometry.call( this );
15
16 this.type = 'PlaneGeometry';
17
18 this.parameters = {
19 width: width,
20 height: height,
21 widthSegments: widthSegments,
22 heightSegments: heightSegments
23 };
24
25 this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
26 this.mergeVertices();
27
28}
29
30PlaneGeometry.prototype = Object.create( Geometry.prototype );
31PlaneGeometry.prototype.constructor = PlaneGeometry;
32
33// PlaneBufferGeometry
34
35function PlaneBufferGeometry( width, height, widthSegments, heightSegments ) {
36
37 BufferGeometry.call( this );
38
39 this.type = 'PlaneBufferGeometry';
40
41 this.parameters = {
42 width: width,
43 height: height,
44 widthSegments: widthSegments,
45 heightSegments: heightSegments
46 };
47
48 width = width || 1;
49 height = height || 1;
50
51 var width_half = width / 2;
52 var height_half = height / 2;
53
54 var gridX = Math.floor( widthSegments ) || 1;
55 var gridY = Math.floor( heightSegments ) || 1;
56
57 var gridX1 = gridX + 1;
58 var gridY1 = gridY + 1;
59
60 var segment_width = width / gridX;
61 var segment_height = height / gridY;
62
63 var ix, iy;
64
65 // buffers
66
67 var indices = [];
68 var vertices = [];
69 var normals = [];
70 var uvs = [];
71
72 // generate vertices, normals and uvs
73
74 for ( iy = 0; iy < gridY1; iy ++ ) {
75
76 var y = iy * segment_height - height_half;
77
78 for ( ix = 0; ix < gridX1; ix ++ ) {
79
80 var x = ix * segment_width - width_half;
81
82 vertices.push( x, - y, 0 );
83
84 normals.push( 0, 0, 1 );
85
86 uvs.push( ix / gridX );
87 uvs.push( 1 - ( iy / gridY ) );
88
89 }
90
91 }
92
93 // indices
94
95 for ( iy = 0; iy < gridY; iy ++ ) {
96
97 for ( ix = 0; ix < gridX; ix ++ ) {
98
99 var a = ix + gridX1 * iy;
100 var b = ix + gridX1 * ( iy + 1 );
101 var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
102 var d = ( ix + 1 ) + gridX1 * iy;
103
104 // faces
105
106 indices.push( a, b, d );
107 indices.push( b, c, d );
108
109 }
110
111 }
112
113 // build geometry
114
115 this.setIndex( indices );
116 this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
117 this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
118 this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
119
120}
121
122PlaneBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
123PlaneBufferGeometry.prototype.constructor = PlaneBufferGeometry;
124
125
126export { PlaneGeometry, PlaneBufferGeometry };