-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTangentOgiveGeometry.js
More file actions
127 lines (81 loc) · 3.32 KB
/
TangentOgiveGeometry.js
File metadata and controls
127 lines (81 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
RocketClubs.TangentOgiveGeometry = function ( baseDiameter, height, radiusSegments, heightSegments, openEnded ) {
THREE.Geometry.call( this );
this.radiusTop = radiusTop = 0;
this.radiusBottom = radiusBottom = baseDiameter !== undefined ? baseDiameter / 2 : 20;
this.height = height = height !== undefined ? height : 100;
this.radiusSegments = radiusSegments = radiusSegments || 8;
this.heightSegments = heightSegments = heightSegments || 1;
this.openEnded = openEnded = openEnded !== undefined ? openEnded : false;
var heightHalf = height / 2;
var ogiveRadius = (Math.pow(radiusBottom, 2) + Math.pow(height, 2)) / (2 * radiusBottom);
var ogiveRadiusSquared = Math.pow(ogiveRadius, 2);
var backHalf = radiusBottom - ogiveRadius;
var x, y, vertices = [], uvs = [];
for ( y = 0; y <= heightSegments; y ++ ) {
var verticesRow = [];
var uvsRow = [];
var v = y / heightSegments;
var radius = Math.sqrt(ogiveRadiusSquared - Math.pow(height * (1 - v), 2)) + backHalf;
for ( x = 0; x <= radiusSegments; x ++ ) {
var u = x / radiusSegments;
var vertex = new THREE.Vector3();
vertex.x = radius * Math.sin( u * Math.PI * 2 );
vertex.y = - v * height + heightHalf;
vertex.z = radius * Math.cos( u * Math.PI * 2 );
this.vertices.push( vertex );
verticesRow.push( this.vertices.length - 1 );
uvsRow.push( new THREE.Vector2( u, 1 - v ) );
}
vertices.push( verticesRow );
uvs.push( uvsRow );
}
var tanTheta = ( radiusBottom - radiusTop ) / height;
var na, nb;
for ( x = 0; x < radiusSegments; x ++ ) {
if ( radiusTop !== 0 ) {
na = this.vertices[ vertices[ 0 ][ x ] ].clone();
nb = this.vertices[ vertices[ 0 ][ x + 1 ] ].clone();
} else {
na = this.vertices[ vertices[ 1 ][ x ] ].clone();
nb = this.vertices[ vertices[ 1 ][ x + 1 ] ].clone();
}
na.setY( Math.sqrt( na.x * na.x + na.z * na.z ) * tanTheta ).normalize();
nb.setY( Math.sqrt( nb.x * nb.x + nb.z * nb.z ) * tanTheta ).normalize();
for ( y = 0; y < heightSegments; y ++ ) {
var v1 = vertices[ y ][ x ];
var v2 = vertices[ y + 1 ][ x ];
var v3 = vertices[ y + 1 ][ x + 1 ];
var v4 = vertices[ y ][ x + 1 ];
var n1 = na.clone();
var n2 = na.clone();
var n3 = nb.clone();
var n4 = nb.clone();
var uv1 = uvs[ y ][ x ].clone();
var uv2 = uvs[ y + 1 ][ x ].clone();
var uv3 = uvs[ y + 1 ][ x + 1 ].clone();
var uv4 = uvs[ y ][ x + 1 ].clone();
this.faces.push( new THREE.Face4( v1, v2, v3, v4, [ n1, n2, n3, n4 ] ) );
this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3, uv4 ] );
}
}
// bottom cap
if ( openEnded === false && radiusBottom > 0 ) {
this.vertices.push( new THREE.Vector3( 0, - heightHalf, 0 ) );
for ( x = 0; x < radiusSegments; x ++ ) {
var v1 = vertices[ y ][ x + 1 ];
var v2 = vertices[ y ][ x ];
var v3 = this.vertices.length - 1;
var n1 = new THREE.Vector3( 0, - 1, 0 );
var n2 = new THREE.Vector3( 0, - 1, 0 );
var n3 = new THREE.Vector3( 0, - 1, 0 );
var uv1 = uvs[ y ][ x + 1 ].clone();
var uv2 = uvs[ y ][ x ].clone();
var uv3 = new THREE.Vector2( uv2.u, 1 );
this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
}
}
this.computeCentroids();
this.computeFaceNormals();
}
RocketClubs.TangentOgiveGeometry.prototype = Object.create( THREE.Geometry.prototype );