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 54 55 56 57 58 59

	var vertices = [];
	var uvs = [];

	var i, j, p;
	var u, v;

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

M
Mr.doob 已提交
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
	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( new ( indices.length > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ) );
	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 已提交
118

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