ParametricGeometry.js 2.4 KB
Newer Older
M
Mr.doob 已提交
1
/**
Z
zz85 已提交
2
 * @author zz85 / https://github.com/zz85
3
 *
Z
zz85 已提交
4 5 6 7
 * Parametric Surfaces Geometry
 * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
 */

M
Mr.doob 已提交
8 9
import { Geometry } from '../core/Geometry';

M
Mr.doob 已提交
10
function ParametricGeometry( func, slices, stacks ) {
Z
zz85 已提交
11

R
Rich Harris 已提交
12
	Geometry.call( this );
Z
zz85 已提交
13

14 15 16 17 18 19 20 21
	this.type = 'ParametricGeometry';

	this.parameters = {
		func: func,
		slices: slices,
		stacks: stacks
	};

22 23
	this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) );
	this.mergeVertices();
Z
zz85 已提交
24

M
Mr.doob 已提交
25
}
Z
zz85 已提交
26

R
Rich Harris 已提交
27 28 29
ParametricGeometry.prototype = Object.create( Geometry.prototype );
ParametricGeometry.prototype.constructor = ParametricGeometry;

M
Mr.doob 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/**
 * @author Mugen87 / https://github.com/Mugen87
 *
 * Parametric Surfaces Geometry
 * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
 */

import { BufferGeometry } from '../core/BufferGeometry';
import { Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from '../core/BufferAttribute';

function ParametricBufferGeometry( func, slices, stacks ) {

	BufferGeometry.call( this );

	this.type = 'ParametricBufferGeometry';

	this.parameters = {
		func: func,
		slices: slices,
		stacks: stacks
	};

M
Mugen87 已提交
52
	// buffers
M
Mr.doob 已提交
53

M
Mugen87 已提交
54
	var indices = [];
M
Mr.doob 已提交
55 56 57
	var vertices = [];
	var uvs = [];

M
Mugen87 已提交
58
	var i, j;
M
Mr.doob 已提交
59

M
Mugen87 已提交
60 61
	// generate vertices and uvs

M
Mr.doob 已提交
62 63 64 65
	var sliceCount = slices + 1;

	for ( i = 0; i <= stacks; i ++ ) {

M
Mugen87 已提交
66
		var v = i / stacks;
M
Mr.doob 已提交
67 68 69

		for ( j = 0; j <= slices; j ++ ) {

M
Mugen87 已提交
70
			var u = j / slices;
M
Mr.doob 已提交
71

M
Mugen87 已提交
72
			var p = func( u, v );
M
Mr.doob 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
			vertices.push( p.x, p.y, p.z );

			uvs.push( u, v );

		}

	}

	// generate indices

	for ( i = 0; i < stacks; i ++ ) {

		for ( j = 0; j < slices; j ++ ) {

M
Mugen87 已提交
87 88 89 90
			var a = i * sliceCount + j;
			var b = i * sliceCount + j + 1;
			var c = ( i + 1 ) * sliceCount + j + 1;
			var d = ( i + 1 ) * sliceCount + j;
M
Mr.doob 已提交
91 92 93 94 95 96 97 98 99 100 101 102

			// faces one and two

			indices.push( a, b, d );
			indices.push( b, c, d );

		}

	}

	// build geometry

103
	this.setIndex( new ( vertices.length > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ) );
M
Mr.doob 已提交
104 105 106 107 108 109 110 111 112 113 114
	this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
	this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );

	// generate normals

	this.computeVertexNormals();

}

ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
R
Rich Harris 已提交
115

M
Mr.doob 已提交
116
export { ParametricGeometry, ParametricBufferGeometry };