UNPKG

1.4 kBJavaScriptView Raw
1import { LineSegments } from '../objects/LineSegments.js';
2import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
3import { Float32BufferAttribute } from '../core/BufferAttribute.js';
4import { BufferGeometry } from '../core/BufferGeometry.js';
5import { Color } from '../math/Color.js';
6
7class GridHelper extends LineSegments {
8
9 constructor( size = 10, divisions = 10, color1 = 0x444444, color2 = 0x888888 ) {
10
11 color1 = new Color( color1 );
12 color2 = new Color( color2 );
13
14 const center = divisions / 2;
15 const step = size / divisions;
16 const halfSize = size / 2;
17
18 const vertices = [], colors = [];
19
20 for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
21
22 vertices.push( - halfSize, 0, k, halfSize, 0, k );
23 vertices.push( k, 0, - halfSize, k, 0, halfSize );
24
25 const color = i === center ? color1 : color2;
26
27 color.toArray( colors, j ); j += 3;
28 color.toArray( colors, j ); j += 3;
29 color.toArray( colors, j ); j += 3;
30 color.toArray( colors, j ); j += 3;
31
32 }
33
34 const geometry = new BufferGeometry();
35 geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
36 geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
37
38 const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
39
40 super( geometry, material );
41
42 this.type = 'GridHelper';
43
44 }
45
46}
47
48
49export { GridHelper };