forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParametricBufferGeometry.js
More file actions
89 lines (55 loc) · 1.66 KB
/
ParametricBufferGeometry.js
File metadata and controls
89 lines (55 loc) · 1.66 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
import { BufferGeometry } from '../core/BufferGeometry';
import { Float32Attribute, Uint16Attribute, Uint32Attribute } from '../core/BufferAttribute';
/**
* @author Mugen87 / https://github.com/Mugen87
*
* Parametric Surfaces Geometry
* based on the brilliant article by @prideout http://prideout.net/blog/?p=44
*/
function ParametricBufferGeometry( func, slices, stacks ) {
BufferGeometry.call( this );
this.type = 'ParametricBufferGeometry';
this.parameters = {
func: func,
slices: slices,
stacks: stacks
};
// generate vertices and uvs
var vertices = [];
var uvs = [];
var i, j, p;
var u, v;
var sliceCount = slices + 1;
for ( i = 0; i <= stacks; i ++ ) {
v = i / stacks;
for ( j = 0; j <= slices; j ++ ) {
u = j / slices;
p = func( u, v );
vertices.push( p.x, p.y, p.z );
uvs.push( u, v );
}
}
// generate indices
var indices = [];
var a, b, c, d;
for ( i = 0; i < stacks; i ++ ) {
for ( j = 0; j < slices; j ++ ) {
a = i * sliceCount + j;
b = i * sliceCount + j + 1;
c = ( i + 1 ) * sliceCount + j + 1;
d = ( i + 1 ) * sliceCount + j;
// faces one and two
indices.push( a, b, d );
indices.push( b, c, d );
}
}
// build geometry
this.setIndex( ( indices.length > 65535 ? Uint32Attribute : Uint16Attribute )( indices, 1 ) );
this.addAttribute( 'position', Float32Attribute( vertices, 3 ) );
this.addAttribute( 'uv', Float32Attribute( uvs, 2 ) );
// generate normals
this.computeVertexNormals();
}
ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
export { ParametricBufferGeometry };