UNPKG

6.52 kBJavaScriptView Raw
1console.warn( "THREE.ParametricGeometries: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
2/**
3 * Experimenting of primitive geometry creation using Surface Parametric equations
4 */
5
6THREE.ParametricGeometries = {
7
8 klein: function ( v, u, target ) {
9
10 u *= Math.PI;
11 v *= 2 * Math.PI;
12
13 u = u * 2;
14 var x, y, z;
15 if ( u < Math.PI ) {
16
17 x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( u ) * Math.cos( v );
18 z = - 8 * Math.sin( u ) - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( u ) * Math.cos( v );
19
20 } else {
21
22 x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( v + Math.PI );
23 z = - 8 * Math.sin( u );
24
25 }
26
27 y = - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( v );
28
29 target.set( x, y, z );
30
31 },
32
33 plane: function ( width, height ) {
34
35 return function ( u, v, target ) {
36
37 var x = u * width;
38 var y = 0;
39 var z = v * height;
40
41 target.set( x, y, z );
42
43 };
44
45 },
46
47 mobius: function ( u, t, target ) {
48
49 // flat mobius strip
50 // http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-
51 u = u - 0.5;
52 var v = 2 * Math.PI * t;
53
54 var x, y, z;
55
56 var a = 2;
57
58 x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
59 y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
60 z = u * Math.sin( v / 2 );
61
62 target.set( x, y, z );
63
64 },
65
66 mobius3d: function ( u, t, target ) {
67
68 // volumetric mobius strip
69
70 u *= Math.PI;
71 t *= 2 * Math.PI;
72
73 u = u * 2;
74 var phi = u / 2;
75 var major = 2.25, a = 0.125, b = 0.65;
76
77 var x, y, z;
78
79 x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
80 z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
81 y = ( major + x ) * Math.sin( u );
82 x = ( major + x ) * Math.cos( u );
83
84 target.set( x, y, z );
85
86 }
87
88};
89
90
91/*********************************************
92 *
93 * Parametric Replacement for TubeGeometry
94 *
95 *********************************************/
96
97THREE.ParametricGeometries.TubeGeometry = function ( path, segments, radius, segmentsRadius, closed ) {
98
99 this.path = path;
100 this.segments = segments || 64;
101 this.radius = radius || 1;
102 this.segmentsRadius = segmentsRadius || 8;
103 this.closed = closed || false;
104
105 var scope = this, numpoints = this.segments + 1;
106
107 var frames = path.computeFrenetFrames( segments, closed ),
108 tangents = frames.tangents,
109 normals = frames.normals,
110 binormals = frames.binormals;
111
112 // proxy internals
113
114 this.tangents = tangents;
115 this.normals = normals;
116 this.binormals = binormals;
117
118 var position = new THREE.Vector3();
119
120 var ParametricTube = function ( u, v, target ) {
121
122 v *= 2 * Math.PI;
123
124 var i = u * ( numpoints - 1 );
125 i = Math.floor( i );
126
127 path.getPointAt( u, position );
128
129 var normal = normals[ i ];
130 var binormal = binormals[ i ];
131
132 var cx = - scope.radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
133 var cy = scope.radius * Math.sin( v );
134
135 position.x += cx * normal.x + cy * binormal.x;
136 position.y += cx * normal.y + cy * binormal.y;
137 position.z += cx * normal.z + cy * binormal.z;
138
139 target.copy( position );
140
141 };
142
143 THREE.ParametricGeometry.call( this, ParametricTube, segments, segmentsRadius );
144
145};
146
147THREE.ParametricGeometries.TubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
148THREE.ParametricGeometries.TubeGeometry.prototype.constructor = THREE.ParametricGeometries.TubeGeometry;
149
150
151/*********************************************
152 *
153 * Parametric Replacement for TorusKnotGeometry
154 *
155 *********************************************/
156THREE.ParametricGeometries.TorusKnotGeometry = function ( radius, tube, segmentsT, segmentsR, p, q ) {
157
158 this.radius = radius || 200;
159 this.tube = tube || 40;
160 this.segmentsT = segmentsT || 64;
161 this.segmentsR = segmentsR || 8;
162 this.p = p || 2;
163 this.q = q || 3;
164
165 function TorusKnotCurve() {
166
167 THREE.Curve.call( this );
168
169 }
170
171 TorusKnotCurve.prototype = Object.create( THREE.Curve.prototype );
172 TorusKnotCurve.prototype.constructor = TorusKnotCurve;
173
174 TorusKnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
175
176 var point = optionalTarget || new THREE.Vector3();
177
178 t *= Math.PI * 2;
179
180 var r = 0.5;
181
182 var x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
183 var y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
184 var z = r * Math.sin( q * t );
185
186 return point.set( x, y, z ).multiplyScalar( radius );
187
188 };
189
190 var segments = segmentsT;
191 var radiusSegments = segmentsR;
192 var extrudePath = new TorusKnotCurve();
193
194 THREE.ParametricGeometries.TubeGeometry.call( this, extrudePath, segments, tube, radiusSegments, true, false );
195
196};
197
198THREE.ParametricGeometries.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype );
199THREE.ParametricGeometries.TorusKnotGeometry.prototype.constructor = THREE.ParametricGeometries.TorusKnotGeometry;
200
201
202/*********************************************
203 *
204 * Parametric Replacement for SphereGeometry
205 *
206 *********************************************/
207THREE.ParametricGeometries.SphereGeometry = function ( size, u, v ) {
208
209 function sphere( u, v, target ) {
210
211 u *= Math.PI;
212 v *= 2 * Math.PI;
213
214 var x = size * Math.sin( u ) * Math.cos( v );
215 var y = size * Math.sin( u ) * Math.sin( v );
216 var z = size * Math.cos( u );
217
218 target.set( x, y, z );
219
220 }
221
222 THREE.ParametricGeometry.call( this, sphere, u, v );
223
224};
225
226THREE.ParametricGeometries.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
227THREE.ParametricGeometries.SphereGeometry.prototype.constructor = THREE.ParametricGeometries.SphereGeometry;
228
229
230/*********************************************
231 *
232 * Parametric Replacement for PlaneGeometry
233 *
234 *********************************************/
235
236THREE.ParametricGeometries.PlaneGeometry = function ( width, depth, segmentsWidth, segmentsDepth ) {
237
238 function plane( u, v, target ) {
239
240 var x = u * width;
241 var y = 0;
242 var z = v * depth;
243
244 target.set( x, y, z );
245
246 }
247
248 THREE.ParametricGeometry.call( this, plane, segmentsWidth, segmentsDepth );
249
250};
251
252THREE.ParametricGeometries.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );
253THREE.ParametricGeometries.PlaneGeometry.prototype.constructor = THREE.ParametricGeometries.PlaneGeometry;