-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
/
ParametricGeometries.js
274 lines (178 loc) · 6.33 KB
/
ParametricGeometries.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
/**
* @author zz85
*
* Experimenting of primitive geometry creation using Surface Parametric equations
*
*/
import {
ArrowHelper,
Curve,
Geometry,
Object3D,
ParametricGeometry,
Vector3
} from "../../../build/three.module.js";
var ParametricGeometries = {
klein: function ( v, u, target ) {
u *= Math.PI;
v *= 2 * Math.PI;
u = u * 2;
var x, y, z;
if ( u < Math.PI ) {
x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( u ) * Math.cos( v );
z = - 8 * Math.sin( u ) - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( u ) * Math.cos( v );
} else {
x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( v + Math.PI );
z = - 8 * Math.sin( u );
}
y = - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( v );
target.set( x, y, z );
},
plane: function ( width, height ) {
return function ( u, v, target ) {
var x = u * width;
var y = 0;
var z = v * height;
target.set( x, y, z );
};
},
mobius: function ( u, t, target ) {
// flat mobius strip
// http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-
u = u - 0.5;
var v = 2 * Math.PI * t;
var x, y, z;
var a = 2;
x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
z = u * Math.sin( v / 2 );
target.set( x, y, z );
},
mobius3d: function ( u, t, target ) {
// volumetric mobius strip
u *= Math.PI;
t *= 2 * Math.PI;
u = u * 2;
var phi = u / 2;
var major = 2.25, a = 0.125, b = 0.65;
var x, y, z;
x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
y = ( major + x ) * Math.sin( u );
x = ( major + x ) * Math.cos( u );
target.set( x, y, z );
}
};
/*********************************************
*
* Parametric Replacement for TubeGeometry
*
*********************************************/
ParametricGeometries.TubeGeometry = function ( path, segments, radius, segmentsRadius, closed, debug ) {
this.path = path;
this.segments = segments || 64;
this.radius = radius || 1;
this.segmentsRadius = segmentsRadius || 8;
this.closed = closed || false;
if ( debug ) this.debug = new Object3D();
var scope = this, numpoints = this.segments + 1;
var frames = path.computeFrenetFrames( segments, closed ),
tangents = frames.tangents,
normals = frames.normals,
binormals = frames.binormals;
// proxy internals
this.tangents = tangents;
this.normals = normals;
this.binormals = binormals;
var ParametricTube = function ( u, v, target ) {
v *= 2 * Math.PI;
var i = u * ( numpoints - 1 );
i = Math.floor( i );
var pos = path.getPointAt( u );
var tangent = tangents[ i ];
var normal = normals[ i ];
var binormal = binormals[ i ];
if ( scope.debug ) {
scope.debug.add( new ArrowHelper( tangent, pos, radius, 0x0000ff ) );
scope.debug.add( new ArrowHelper( normal, pos, radius, 0xff0000 ) );
scope.debug.add( new ArrowHelper( binormal, pos, radius, 0x00ff00 ) );
}
var cx = - scope.radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
var cy = scope.radius * Math.sin( v );
pos.x += cx * normal.x + cy * binormal.x;
pos.y += cx * normal.y + cy * binormal.y;
pos.z += cx * normal.z + cy * binormal.z;
target.copy( pos );
};
ParametricGeometry.call( this, ParametricTube, segments, segmentsRadius );
};
ParametricGeometries.TubeGeometry.prototype = Object.create( Geometry.prototype );
ParametricGeometries.TubeGeometry.prototype.constructor = ParametricGeometries.TubeGeometry;
/*********************************************
*
* Parametric Replacement for TorusKnotGeometry
*
*********************************************/
ParametricGeometries.TorusKnotGeometry = function ( radius, tube, segmentsT, segmentsR, p, q ) {
this.radius = radius || 200;
this.tube = tube || 40;
this.segmentsT = segmentsT || 64;
this.segmentsR = segmentsR || 8;
this.p = p || 2;
this.q = q || 3;
function TorusKnotCurve() {
Curve.call( this );
}
TorusKnotCurve.prototype = Object.create( Curve.prototype );
TorusKnotCurve.prototype.constructor = TorusKnotCurve;
TorusKnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
var point = optionalTarget || new Vector3();
t *= Math.PI * 2;
var r = 0.5;
var x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
var y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
var z = r * Math.sin( q * t );
return point.set( x, y, z ).multiplyScalar( radius );
};
var segments = segmentsT;
var radiusSegments = segmentsR;
var extrudePath = new TorusKnotCurve();
ParametricGeometries.TubeGeometry.call( this, extrudePath, segments, tube, radiusSegments, true, false );
};
ParametricGeometries.TorusKnotGeometry.prototype = Object.create( Geometry.prototype );
ParametricGeometries.TorusKnotGeometry.prototype.constructor = ParametricGeometries.TorusKnotGeometry;
/*********************************************
*
* Parametric Replacement for SphereGeometry
*
*********************************************/
ParametricGeometries.SphereGeometry = function ( size, u, v ) {
function sphere( u, v, target ) {
u *= Math.PI;
v *= 2 * Math.PI;
var x = size * Math.sin( u ) * Math.cos( v );
var y = size * Math.sin( u ) * Math.sin( v );
var z = size * Math.cos( u );
target.set( x, y, z );
}
ParametricGeometry.call( this, sphere, u, v );
};
ParametricGeometries.SphereGeometry.prototype = Object.create( Geometry.prototype );
ParametricGeometries.SphereGeometry.prototype.constructor = ParametricGeometries.SphereGeometry;
/*********************************************
*
* Parametric Replacement for PlaneGeometry
*
*********************************************/
ParametricGeometries.PlaneGeometry = function ( width, depth, segmentsWidth, segmentsDepth ) {
function plane( u, v, target ) {
var x = u * width;
var y = 0;
var z = v * depth;
target.set( x, y, z );
}
ParametricGeometry.call( this, plane, segmentsWidth, segmentsDepth );
};
ParametricGeometries.PlaneGeometry.prototype = Object.create( Geometry.prototype );
ParametricGeometries.PlaneGeometry.prototype.constructor = ParametricGeometries.PlaneGeometry;
export { ParametricGeometries };