forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CapsuleGeometry.js
376 lines (272 loc) · 11.8 KB
/
CapsuleGeometry.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import * as THREE from 'three';
/**
* @author maximequiblier
*/
class CapsuleGeometry extends THREE.BufferGeometry {
constructor(radiusTop, radiusBottom, height, radialSegments, heightSegments, capsTopSegments, capsBottomSegments, thetaStart, thetaLength) {
super();
this.type = 'CapsuleBufferGeometry';
this.parameters = {
radiusTop: radiusTop,
radiusBottom: radiusBottom,
height: height,
radialSegments: radialSegments,
heightSegments: heightSegments,
thetaStart: thetaStart,
thetaLength: thetaLength
};
var scope = this;
radiusTop = radiusTop !== undefined ? radiusTop : 1;
radiusBottom = radiusBottom !== undefined ? radiusBottom : 1;
height = height !== undefined ? height : 2;
radialSegments = Math.floor( radialSegments ) || 8;
heightSegments = Math.floor( heightSegments ) || 1;
capsTopSegments = Math.floor( capsTopSegments ) || 2;
capsBottomSegments = Math.floor( capsBottomSegments ) || 2;
thetaStart = thetaStart !== undefined ? thetaStart : 0.0;
thetaLength = thetaLength !== undefined ? thetaLength : 2.0 * Math.PI;
// Alpha is the angle such that Math.PI/2 - alpha is the cone part angle.
var alpha = height !== 0 ? Math.acos((radiusBottom-radiusTop)/height) : Math.PI;
var eqRadii = (radiusTop-radiusBottom === 0);
var vertexCount = calculateVertexCount();
var indexCount = calculateIndexCount();
// buffers
var indices = new THREE.BufferAttribute( new ( indexCount > 65535 ? Uint32Array : Uint16Array )( indexCount ), 1 );
var vertices = new THREE.BufferAttribute( new Float32Array( vertexCount * 3 ), 3 );
var normals = new THREE.BufferAttribute( new Float32Array( vertexCount * 3 ), 3 );
var uvs = new THREE.BufferAttribute( new Float32Array( vertexCount * 2 ), 2 );
// helper variables
var index = 0,
indexOffset = 0,
indexArray = [],
halfHeight = height / 2;
// generate geometry
generateTorso();
// build geometry
this.setIndex( indices );
this.setAttribute( 'position', vertices );
this.setAttribute( 'normal', normals );
this.setAttribute( 'uv', uvs );
// helper functions
function calculateVertexCount(){
var count = ( radialSegments + 1 ) * ( heightSegments + 1 + capsBottomSegments + capsTopSegments);
return count;
}
function calculateIndexCount() {
var count = radialSegments * (heightSegments + capsBottomSegments + capsTopSegments) * 2 * 3;
return count;
}
function generateTorso() {
var x, y;
var normal = new THREE.Vector3();
var vertex = new THREE.Vector3();
var cosAlpha = Math.cos(alpha);
var sinAlpha = Math.sin(alpha);
var cone_length =
new THREE.Vector2(
radiusTop*sinAlpha,
halfHeight+radiusTop*cosAlpha
).sub(new THREE.Vector2(
radiusBottom*sinAlpha,
-halfHeight+radiusBottom*cosAlpha
)
).length();
// Total length for v texture coord
var vl = radiusTop*alpha
+ cone_length
+ radiusBottom*(Math.PI/2-alpha);
var groupCount = 0;
// generate vertices, normals and uvs
var v = 0;
for( y = 0; y <= capsTopSegments; y++ ) {
var indexRow = [];
var a = Math.PI/2 - alpha*(y / capsTopSegments);
v += radiusTop*alpha/capsTopSegments;
var cosA = Math.cos(a);
var sinA = Math.sin(a);
// calculate the radius of the current row
var radius = cosA*radiusTop;
for ( x = 0; x <= radialSegments; x ++ ) {
var u = x / radialSegments;
var theta = u * thetaLength + thetaStart;
var sinTheta = Math.sin( theta );
var cosTheta = Math.cos( theta );
// vertex
vertex.x = radius * sinTheta;
vertex.y = halfHeight + sinA*radiusTop;
vertex.z = radius * cosTheta;
vertices.setXYZ( index, vertex.x, vertex.y, vertex.z );
// normal
normal.set( cosA*sinTheta, sinA, cosA*cosTheta );
normals.setXYZ( index, normal.x, normal.y, normal.z );
// uv
uvs.setXY( index, u, 1 - v/vl );
// save index of vertex in respective row
indexRow.push( index );
// increase index
index ++;
}
// now save vertices of the row in our index array
indexArray.push( indexRow );
}
var cone_height = height + cosAlpha*radiusTop - cosAlpha*radiusBottom;
var slope = sinAlpha * ( radiusBottom - radiusTop ) / cone_height;
for ( y = 1; y <= heightSegments; y++ ) {
var indexRow = [];
v += cone_length/heightSegments;
// calculate the radius of the current row
var radius = sinAlpha * ( y * ( radiusBottom - radiusTop ) / heightSegments + radiusTop);
for ( x = 0; x <= radialSegments; x ++ ) {
var u = x / radialSegments;
var theta = u * thetaLength + thetaStart;
var sinTheta = Math.sin( theta );
var cosTheta = Math.cos( theta );
// vertex
vertex.x = radius * sinTheta;
vertex.y = halfHeight + cosAlpha*radiusTop - y * cone_height / heightSegments;
vertex.z = radius * cosTheta;
vertices.setXYZ( index, vertex.x, vertex.y, vertex.z );
// normal
normal.set( sinTheta, slope, cosTheta ).normalize();
normals.setXYZ( index, normal.x, normal.y, normal.z );
// uv
uvs.setXY( index, u, 1 - v/vl );
// save index of vertex in respective row
indexRow.push( index );
// increase index
index ++;
}
// now save vertices of the row in our index array
indexArray.push( indexRow );
}
for( y = 1; y <= capsBottomSegments; y++ ) {
var indexRow = [];
var a = (Math.PI/2 - alpha) - (Math.PI - alpha)*( y / capsBottomSegments);
v += radiusBottom*alpha/capsBottomSegments;
var cosA = Math.cos(a);
var sinA = Math.sin(a);
// calculate the radius of the current row
var radius = cosA*radiusBottom;
for ( x = 0; x <= radialSegments; x ++ ) {
var u = x / radialSegments;
var theta = u * thetaLength + thetaStart;
var sinTheta = Math.sin( theta );
var cosTheta = Math.cos( theta );
// vertex
vertex.x = radius * sinTheta;
vertex.y = -halfHeight + sinA*radiusBottom;;
vertex.z = radius * cosTheta;
vertices.setXYZ( index, vertex.x, vertex.y, vertex.z );
// normal
normal.set( cosA*sinTheta, sinA, cosA*cosTheta );
normals.setXYZ( index, normal.x, normal.y, normal.z );
// uv
uvs.setXY( index, u, 1 - v/vl );
// save index of vertex in respective row
indexRow.push( index );
// increase index
index ++;
}
// now save vertices of the row in our index array
indexArray.push( indexRow );
}
// generate indices
for ( x = 0; x < radialSegments; x ++ ) {
for ( y = 0; y < capsTopSegments + heightSegments + capsBottomSegments; y ++ ) {
// we use the index array to access the correct indices
var i1 = indexArray[ y ][ x ];
var i2 = indexArray[ y + 1 ][ x ];
var i3 = indexArray[ y + 1 ][ x + 1 ];
var i4 = indexArray[ y ][ x + 1 ];
// face one
indices.setX( indexOffset, i1 ); indexOffset ++;
indices.setX( indexOffset, i2 ); indexOffset ++;
indices.setX( indexOffset, i4 ); indexOffset ++;
// face two
indices.setX( indexOffset, i2 ); indexOffset ++;
indices.setX( indexOffset, i3 ); indexOffset ++;
indices.setX( indexOffset, i4 ); indexOffset ++;
}
}
}
this.applyMatrix4(new THREE.Matrix4().makeRotationFromQuaternion(
new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), Math.PI*0.5)
));
}
}
CapsuleGeometry.fromPoints = function(pointA, pointB, radiusA, radiusB, radialSegments, heightSegments, capsTopSegments, capsBottomSegments, thetaStart, thetaLength ) {
let cmin = null;
let cmax = null;
let rmin = null;
let rmax = null;
if(radiusA > radiusB){
cmax = pointA;
cmin = pointB;
rmax = radiusA;
rmin = radiusB;
}else{
cmax = pointA;
cmin = pointB;
rmax = radiusA;
rmin = radiusB;
}
const c0 = cmin;
const c1 = cmax;
const r0 = rmin;
const r1 = rmax;
const sphereCenterTop = new THREE.Vector3( c0.x, c0.y, c0.z );
const sphereCenterBottom = new THREE.Vector3( c1.x, c1.y, c1.z );
const radiusTop = r0;
const radiusBottom = r1;
let height = sphereCenterTop.distanceTo( sphereCenterBottom );
// If the big sphere contains the small one, return a SphereBufferGeometry
if(height < Math.abs( r0 - r1 )){
let g = new THREE.SphereBufferGeometry(r1, radialSegments, capsBottomSegments, thetaStart, thetaLength);
g.translate(r1.x, r1.y, r1.z);
return g;
}
// useful values
const alpha = Math.acos( ( radiusBottom - radiusTop ) / height );
const cosAlpha = Math.cos( alpha );
const sinAlpha = Math.sin( alpha );
// compute cylinder properties
const coneHeight = height + cosAlpha * radiusTop - cosAlpha * radiusBottom;
const cylTopRadius = sinAlpha * radiusTop;
const cylBottomRadius = sinAlpha * radiusBottom;
// compute rotation matrix
const rotationMatrix = new THREE.Matrix4();
const quaternion = new THREE.Quaternion();
const capsuleModelUnitVector = new THREE.Vector3( 0, 1, 0 );
const capsuleUnitVector = new THREE.Vector3();
capsuleUnitVector.subVectors( sphereCenterTop, sphereCenterBottom );
capsuleUnitVector.normalize();
quaternion.setFromUnitVectors( capsuleModelUnitVector, capsuleUnitVector );
rotationMatrix.makeRotationFromQuaternion( quaternion );
// compute translation matrix from center point
const translationMatrix = new THREE.Matrix4();
const cylVec = new THREE.Vector3();
cylVec.subVectors( sphereCenterTop, sphereCenterBottom );
cylVec.normalize();
let cylTopPoint = new THREE.Vector3();
cylTopPoint = sphereCenterTop;
cylTopPoint.addScaledVector( cylVec, cosAlpha * radiusTop );
let cylBottomPoint = new THREE.Vector3();
cylBottomPoint = sphereCenterBottom;
cylBottomPoint.addScaledVector( cylVec, cosAlpha * radiusBottom );
// computing lerp for color
const dir = new THREE.Vector3();
dir.subVectors( cylBottomPoint, cylTopPoint );
dir.normalize();
const middlePoint = new THREE.Vector3();
middlePoint.lerpVectors( cylBottomPoint, cylTopPoint, 0.5 );
translationMatrix.makeTranslation( middlePoint.x, middlePoint.y, middlePoint.z );
// Instanciate a CylinderBufferGeometry from three.js
let g = new CapsuleGeometry(radiusBottom, radiusTop, height, radialSegments, heightSegments, capsTopSegments, capsBottomSegments, thetaStart, thetaLength);
// applying transformations
g.applyMatrix( rotationMatrix );
g.applyMatrix( translationMatrix );
return g;
};
export {
CapsuleGeometry,
};