UNPKG

1.99 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 PolarGridHelper extends LineSegments {
8
9 constructor( radius = 10, radials = 16, circles = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) {
10
11 color1 = new Color( color1 );
12 color2 = new Color( color2 );
13
14 const vertices = [];
15 const colors = [];
16
17 // create the radials
18
19 for ( let i = 0; i <= radials; i ++ ) {
20
21 const v = ( i / radials ) * ( Math.PI * 2 );
22
23 const x = Math.sin( v ) * radius;
24 const z = Math.cos( v ) * radius;
25
26 vertices.push( 0, 0, 0 );
27 vertices.push( x, 0, z );
28
29 const color = ( i & 1 ) ? color1 : color2;
30
31 colors.push( color.r, color.g, color.b );
32 colors.push( color.r, color.g, color.b );
33
34 }
35
36 // create the circles
37
38 for ( let i = 0; i <= circles; i ++ ) {
39
40 const color = ( i & 1 ) ? color1 : color2;
41
42 const r = radius - ( radius / circles * i );
43
44 for ( let j = 0; j < divisions; j ++ ) {
45
46 // first vertex
47
48 let v = ( j / divisions ) * ( Math.PI * 2 );
49
50 let x = Math.sin( v ) * r;
51 let z = Math.cos( v ) * r;
52
53 vertices.push( x, 0, z );
54 colors.push( color.r, color.g, color.b );
55
56 // second vertex
57
58 v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
59
60 x = Math.sin( v ) * r;
61 z = Math.cos( v ) * r;
62
63 vertices.push( x, 0, z );
64 colors.push( color.r, color.g, color.b );
65
66 }
67
68 }
69
70 const geometry = new BufferGeometry();
71 geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
72 geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
73
74 const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
75
76 super( geometry, material );
77
78 this.type = 'PolarGridHelper';
79
80 }
81
82}
83
84
85export { PolarGridHelper };