GridHelper.js 2.0 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

B
bentok 已提交
5 6 7 8 9 10
import { LineSegments } from '../objects/LineSegments.js';
import { VertexColors } from '../constants.js';
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { Color } from '../math/Color.js';
R
Rich Harris 已提交
11

M
Mr.doob 已提交
12
function GridHelper( size, divisions, color1, color2 ) {
M
Mr.doob 已提交
13

M
Mugen87 已提交
14 15 16 17 18 19 20
	this.parameters = {
		size: size,
		divisions: divisions,
		color1: color1,
		color2: color2
	};

21 22
	size = size || 10;
	divisions = divisions || 10;
R
Rich Harris 已提交
23 24
	color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
	color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
M
Mr.doob 已提交
25

26
	var center = divisions / 2;
27
	var step = size / divisions;
M
Mr.doob 已提交
28
	var halfSize = size / 2;
29

30
	var vertices = [], colors = [];
31

32
	for ( var i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
M
Mr.doob 已提交
33

34 35
		vertices.push( - halfSize, 0, k, halfSize, 0, k );
		vertices.push( k, 0, - halfSize, k, 0, halfSize );
M
Mr.doob 已提交
36

37
		var color = i === center ? color1 : color2;
M
Mr.doob 已提交
38

39 40 41 42
		color.toArray( colors, j ); j += 3;
		color.toArray( colors, j ); j += 3;
		color.toArray( colors, j ); j += 3;
		color.toArray( colors, j ); j += 3;
M
Mr.doob 已提交
43 44 45

	}

R
Rich Harris 已提交
46
	var geometry = new BufferGeometry();
47 48
	geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
	geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
49

R
Rich Harris 已提交
50
	var material = new LineBasicMaterial( { vertexColors: VertexColors } );
51

R
Rich Harris 已提交
52
	LineSegments.call( this, geometry, material );
M
Mr.doob 已提交
53

M
Mr.doob 已提交
54
}
M
Mr.doob 已提交
55

M
Mugen87 已提交
56 57 58 59
GridHelper.prototype = Object.assign( Object.create( LineSegments.prototype ), {

	constructor: GridHelper,

M
Mugen87 已提交
60
	copy: function ( source ) {
M
Mugen87 已提交
61 62 63 64 65 66 67 68 69

		LineSegments.prototype.copy.call( this, source );

		Object.assign( this.parameters, source.parameters );

		return this;

	},

M
Mugen87 已提交
70
	clone: function () {
M
Mugen87 已提交
71

M
Mr.doob 已提交
72
		var parameters = this.parameters;
M
Mugen87 已提交
73

M
Mr.doob 已提交
74
		return new this.constructor( parameters.size, parameters.divisions, parameters.color1, parameters.color2 );
M
Mugen87 已提交
75 76 77 78

	}

} );
79

80
export { GridHelper };