Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GridHelper to support different size and divisions #29601

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/helpers/GridHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,43 @@ class GridHelper extends LineSegments {
color1 = new Color( color1 );
color2 = new Color( color2 );

const center = divisions / 2;
const step = size / divisions;
const halfSize = size / 2;
const [ width, depth ] = Array.isArray( size ) ? size : [ size, size ];
const [ divisionsX, divisionsZ ] = Array.isArray( divisions ) ? divisions : [ divisions, divisions ];

const stepX = width / divisionsX;
const stepZ = depth / divisionsZ;
const halfWidth = width / 2;
const halfDepth = depth / 2;

const vertices = [], colors = [];

for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
for ( let i = 0; i <= divisionsZ; i ++ ) {

const k = - halfDepth + i * stepZ;
vertices.push( - halfWidth, 0, k, halfWidth, 0, k );

vertices.push( - halfSize, 0, k, halfSize, 0, k );
vertices.push( k, 0, - halfSize, k, 0, halfSize );
const color = ( i === divisionsZ / 2 ) ? color1 : color2;
color.toArray( colors, colors.length );
color.toArray( colors, colors.length );

}

const color = i === center ? color1 : color2;
for ( let i = 0; i <= divisionsX; i ++ ) {

color.toArray( colors, j ); j += 3;
color.toArray( colors, j ); j += 3;
color.toArray( colors, j ); j += 3;
color.toArray( colors, j ); j += 3;
const k = - halfWidth + i * stepX;
vertices.push( k, 0, - halfDepth, k, 0, halfDepth );

const color = ( i === divisionsX / 2 ) ? color1 : color2;
color.toArray( colors, colors.length );
color.toArray( colors, colors.length );

}

const geometry = new BufferGeometry();
geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );

const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );

super( geometry, material );
super( geometry, new LineBasicMaterial( { vertexColors: true, toneMapped: false } ) );

this.type = 'GridHelper';

Expand All @@ -52,5 +62,4 @@ class GridHelper extends LineSegments {

}


export { GridHelper };