2 * @author zz85 / https://github.com/zz85
3 * @author Mugen87 / https://github.com/Mugen87
5 * Parametric Surfaces Geometry
6 * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
9 import { Geometry } from '../core/Geometry';
10 import { BufferGeometry } from '../core/BufferGeometry';
11 import { Float32BufferAttribute } from '../core/BufferAttribute';
12 import { Vector3 } from '../math/Vector3';
16 function ParametricGeometry( func, slices, stacks ) {
18 Geometry.call( this );
20 this.type = 'ParametricGeometry';
28 this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) );
33 ParametricGeometry.prototype = Object.create( Geometry.prototype );
34 ParametricGeometry.prototype.constructor = ParametricGeometry;
36 // ParametricBufferGeometry
38 function ParametricBufferGeometry( func, slices, stacks ) {
40 BufferGeometry.call( this );
42 this.type = 'ParametricBufferGeometry';
59 var normal = new Vector3();
61 var p0 = new Vector3(), p1 = new Vector3();
62 var pu = new Vector3(), pv = new Vector3();
66 // generate vertices, normals and uvs
68 var sliceCount = slices + 1;
70 for ( i = 0; i <= stacks; i ++ ) {
74 for ( j = 0; j <= slices; j ++ ) {
80 p0 = func( u, v, p0 );
81 vertices.push( p0.x, p0.y, p0.z );
85 // approximate tangent vectors via finite differences
89 p1 = func( u - EPS, v, p1 );
90 pu.subVectors( p0, p1 );
94 p1 = func( u + EPS, v, p1 );
95 pu.subVectors( p1, p0 );
101 p1 = func( u, v - EPS, p1 );
102 pv.subVectors( p0, p1 );
106 p1 = func( u, v + EPS, p1 );
107 pv.subVectors( p1, p0 );
111 // cross product of tangent vectors returns surface normal
113 normal.crossVectors( pu, pv ).normalize();
114 normals.push( normal.x, normal.y, normal.z );
126 for ( i = 0; i < stacks; i ++ ) {
128 for ( j = 0; j < slices; j ++ ) {
130 var a = i * sliceCount + j;
131 var b = i * sliceCount + j + 1;
132 var c = ( i + 1 ) * sliceCount + j + 1;
133 var d = ( i + 1 ) * sliceCount + j;
137 indices.push( a, b, d );
138 indices.push( b, c, d );
146 this.setIndex( indices );
147 this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
148 this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
149 this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
153 ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
154 ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
157 export { ParametricGeometry, ParametricBufferGeometry };