8000 Merge pull request #8334 from Mugen87/dev · LikeABossProgrammer/three.js@1eb434c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1eb434c

Browse files
committed
Merge pull request mrdoob#8334 from Mugen87/dev
New LatheBufferGeometry
2 parents f44267b + af67619 commit 1eb434c

File tree

7 files changed

+291
-81
lines changed

7 files changed

+291
-81
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<base href="../../../" />
6+
<script src="list.js"></script>
7+
<script src="page.js"></script>
8+
<link type="text/css" rel="stylesheet" href="page.css" />
9+
</head>
10+
<body>
11+
[page:BufferGeometry] &rarr;
12+
13+
<h1>[name]</h1>
14+
15+
<div class="desc">This is the [page:BufferGeometry] port of [page:LatheGeometry].</div>
16+
17+
18+
<h2>Example</h2>
19+
20+
<iframe src='scenes/geometry-browser.html#LatheBufferGeometry'></iframe>
21+
22+
<code>
23+
var points = [];
24+
for ( var i = 0; i < 10; i ++ ) {
25+
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 10 + 5, ( i - 5 ) * 2 ) );
26+
}
27+
var geometry = new THREE.LatheBufferGeometry( points );
28+
var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
29+
var lathe = new THREE.Mesh( geometry, material );
30+
scene.add( lathe );
31+
</code>
32+
33+
<h2>Constructor</h2>
34+
35+
36+
<h3>[name]([page:Array points], [page:Integer segments], [page:Float phiStart], [page:Float phiLength])</h3>
37+
<div>
38+
points — Array of Vector2s. The x-coordinate of each point must be greater than zero.<br />
39+
segments — the number of circumference segments to generate. Default is 12.<br />
40+
phiStart — the starting angle in radians. Default is 0.<br />
41+
phiLength — the radian (0 to 2PI) range of the lathed section 2PI is a closed lathe, less than 2PI is a portion. Default is 2PI.
42+
</div>
43+
<div>
44+
This creates a LatheBufferGeometry based on the parameters.
45+
</div>
46+
47+
48+
<h2>Source</h2>
49+
50+
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
51+
</body>
52+
</html>

docs/api/extras/geometries/LatheGeometry.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ <h1>[name]</h1>
1717

1818
<h2>Example</h2>
1919

20+
<iframe src='scenes/geometry-browser.html#LatheGeometry'></iframe>
21+
2022
<code>
2123
var points = [];
2224
for ( var i = 0; i < 10; i ++ ) {
23-
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 15 + 50, ( i - 5 ) * 2 ) );
24-
25+
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 10 + 5, ( i - 5 ) * 2 ) );
2526
}
2627
var geometry = new THREE.LatheGeometry( points );
2728
var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
@@ -34,13 +35,13 @@ <h2>Constructor</h2>
3435

3536
<h3>[name]([page:Array points], [page:Integer segments], [page:Float phiStart], [page:Float phiLength])</h3>
3637
<div>
37-
points — Array of Vector2s.<br />
38+
points — Array of Vector2s. The x-coordinate of each point must be greater than zero.<br />
3839
segments — the number of circumference segments to generate. Default is 12.<br />
3940
phiStart — the starting angle in radians. Default is 0.<br />
40-
phiLength — the radian (0 to 2*PI) range of the lathed section 2*PI is a closed lathe, less than 2PI is a portion. Default is 2*PI
41+
phiLength — the radian (0 to 2PI) range of the lathed section 2PI is a closed lathe, less than 2PI is a portion. Default is 2PI.
4142
</div>
4243
<div>
43-
This creates a LatheGeometry based on the parameters.
44+
This creates a LatheBufferGeometry based on the parameters.
4445
</div>
4546

4647

docs/list.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ var list = {
200200
[ "DodecahedronGeometry", "api/extras/geometries/DodecahedronGeometry" ],
201201
[ "ExtrudeGeometry", "api/extras/geometries/ExtrudeGeometry" ],
202202
[ "IcosahedronGeometry", "api/extras/geometries/IcosahedronGeometry" ],
203+
[ "LatheBufferGeometry", "api/extras/geometries/LatheBufferGeometry" ],
203204
[ "LatheGeometry", "api/extras/geometries/LatheGeometry" ],
204205
[ "OctahedronGeometry", "api/extras/geometries/OctahedronGeometry" ],
205206
[ "ParametricGeometry", "api/extras/geometries/ParametricGeometry" ],

docs/scenes/js/geometry.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,78 @@ var guis = {
365365

366366
},
367367

368+
LatheBufferGeometry : function() {
369+
370+
var points = [];
371+
372+
for ( var i = 0; i < 10; i ++ ) {
373+
374+
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 10 + 5, ( i - 5 ) * 2 ) );
375+
376+
}
377+
378+
var data = {
379+
segments : 12,
380+
phiStart : 0,
381+
phiLength : twoPi,
382+
};
383+
384+
function generateGeometry() {
385+
386+
var geometry = new THREE.LatheBufferGeometry(
387+
points, data.segments, data.phiStart, data.phiLength
388+
);
389+
390+
updateGroupGeometry( mesh, geometry );
391+
392+
}
393+
394+
var folder = gui.addFolder( 'THREE.LatheBufferGeometry' );
395+
396+
folder.add( data, 'segments', 1, 30 ).step( 1 ).onChange( generateGeometry );
397+
folder.add( data, 'phiStart', 0, twoPi ).onChange( generateGeometry );
398+
folder.add( data, 'phiLength', 0, twoPi ).onChange( generateGeometry );
399+
400+
generateGeometry();
401+
402+
},
403+
404+
LatheGeometry : function() {
405+
406+
var points = [];
407+
408+
for ( var i = 0; i < 10; i ++ ) {
409+
410+
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 10 + 5, ( i - 5 ) * 2 ) );
411+
412+
}
413+
414+
var data = {
415+
segments : 12,
416+
phiStart : 0,
417+
phiLength : twoPi,
418+
};
419+
420+
function generateGeometry() {
421+
422+
var geometry = new THREE.LatheGeometry(
423+
points, data.segments, data.phiStart, data.phiLength
424+
);
425+
426+
updateGroupGeometry( mesh, geometry );
427+
428+
}
429+
430+
var folder = gui.addFolder( 'THREE.LatheGeometry' );
431+
432+
folder.add( data, 'segments', 1, 30 ).step( 1 ).onChange( generateGeometry );
433+
folder.add( data, 'phiStart', 0, twoPi ).onChange( generateGeometry );
434+
folder.add( data, 'phiLength', 0, twoPi ).onChange( generateGeometry );
435+
436+
generateGeometry();
437+
438+
},
439+
368440
OctahedronGeometry : function() {
369441

370442
var data = {
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**
2+
* @author Mugen87 / https://github.com/Mugen87
3+
*/
4+
5+
// points - to create a closed torus, one must use a set of points
6+
// like so: [ a, b, c, d, a ], see first is the same as last.
7+
// segments - the number of circumference segments to create
8+
// phiStart - the starting radian
9+
// phiLength - the radian (0 to 2PI) range of the lathed section
10+
// 2PI is a closed lathe, less than 2PI is a portion.
11+
12+
THREE.LatheBufferGeometry = function ( points, segments, phiStart, phiLength ) {
13+
14+
THREE.BufferGeometry.call( this );
15+
16+
this.type = 'LatheBufferGeometry';
17+
18+
this.parameters = {
19+
points: points,
20+
segments: segments,
21+
phiStart: phiStart,
22+
phiLength: phiLength
23+
};
24+
25+
segments = Math.floor( segments ) || 12;
26+
phiStart = phiStart || 0;
27+
phiLength = phiLength || Math.PI * 2;
28+
29+
// clamp phiLength so it's in range of [ 0, 2PI ]
30+
phiLength = THREE.Math.clamp( phiLength, 0, Math.PI * 2 );
31+
32+
// these are used to calculate buffer length
33+
var vertexCount = ( segments + 1 ) * points.length;
34+
var indexCount = segments * points.length * 2 * 3;
35+
36+
// buffers
37+
var indices = new THREE.BufferAttribute( new ( indexCount > 65535 ? Uint32Array : Uint16Array )( indexCount ) , 1 );
38+
var vertices = new THREE.BufferAttribute( new Float32Array( vertexCount * 3 ), 3 );
39+
var uvs = new THREE.BufferAttribute( new Float32Array( vertexCount * 2 ), 2 );
40+
41+
// helper variables
42+
var index = 0, indexOffset = 0, base;
43+
var inversePointLength = 1.0 / ( points.length - 1 );
44+
var inverseSegments = 1.0 / segments;
45+
var vertex = new THREE.Vector3();
46+
var uv = new THREE.Vector2();
47+
var i, j;
48+
49+
// generate vertices and uvs
50+
51+
for ( i = 0; i <= segments; i ++ ) {
52+
53+
var phi = phiStart + i * inverseSegments * phiLength;
54+
55+
var sin = Math.sin( phi );
56+
var cos = Math.cos( phi );
57+
58+
for ( j = 0; j <= ( points.length - 1 ); j ++ ) {
59+
60+
// vertex
61+
vertex.x = points[ j ].x * sin;
62+
vertex.y = points[ j ].y;
63+
vertex.z = points[ j ].x * cos;
64+
vertices.setXYZ( index, vertex.x, vertex.y, vertex.z );
65+
66+
// uv
67+
uv.x = i / segments;
68+
uv.y = j / ( points.length - 1 );
69+
uvs.setXY( index, uv.x, uv.y );
70+
71+
// increase index
72+
index ++;
73+
74+
}
75+
76+
}
77+
78+
// generate indices
79+
80+
for ( i = 0; i < segments; i ++ ) {
81+
82+
for ( j = 0; j < ( points.length - 1 ); j ++ ) {
83+
84+
base = j + i * points.length;
85+
86+
// indices
87+
var a = base;
88+
var b = base + points.length;
89+
var c = base + points.length + 1;
90+
var d = base + 1;
91+
92+
// face one
93+
indices.setX( indexOffset, a ); indexOffset++;
94+
indices.setX( indexOffset, b ); indexOffset++;
95+
indices.setX( indexOffset, d ); indexOffset++;
96+
97+
// face two
98+
indices.setX( indexOffset, b ); indexOffset++;
99+
indices.setX( indexOffset, c ); indexOffset++;
100+
indices.setX( indexOffset, d ); indexOffset++;
101+
102+
}
103+
104+
}
105+
106+
// build geometry
107+
108+
this.setIndex( indices );
109+
this.addAttribute( 'position', vertices );
110+
this.addAttribute( 'uv', uvs );
111+
112+
// generate normals
113+
114+
this.computeVertexNormals();
115+
116+
// if the geometry is closed, we need to average the normals along the seam.
117+
// because the corresponding vertices are identical (but still have different UVs).
118+
119+
if( phiLength === Math.PI * 2 ) {
120+
121+
var normals = this.attributes.normal.array;
122+
var n1 = new THREE.Vector3();
123+
var n2 = new THREE.Vector3();
124+
var n = new THREE.Vector3();
125+
126+
// this is the buffer offset for the last line of vertices
127+
base = segments * points.length * 3;
128+
129+
for( i = 0, j = 0; i < points.length; i ++, j += 3 ) {
130+
131+
// select the normal of the vertex in the first line
132+
n1.x = normals[ j + 0 ];
133+
n1.y = normals[ j + 1 ];
134+
n1.z = normals[ j + 2 ];
135+
136+
// select the normal of the vertex in the last line
137+
n2.x = normals[ base + j + 0 ];
138+
n2.y = normals[ base + j + 1 ];
139+
n2.z = normals[ base + j + 2 ];
140+
141+
// average normals
142+
n.addVectors( n1, n2 ).normalize();
143+
144+
// assign the new values to both normals
145+
normals[ j + 0 ] = normals[ base + j + 0 ] = n.x;
146+
normals[ j + 1 ] = normals[ base + j + 1 ] = n.y;
147+
normals[ j + 2 ] = normals[ base + j + 2 ] = n.z;
148+
149+
} // next row
150+
151+
}
152+
153+
};
154+
155+
THREE.LatheBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
156+
THREE.LatheBufferGeometry.prototype.constructor = THREE.LatheBufferGeometry;

0 commit comments

Comments
 (0)
0