-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
PolarGridHelper: Allow zero radials or zero circles #24509
PolarGridHelper: Allow zero radials or zero circles #24509
Conversation
Prevents from getting NaN values when 0 radials or 0 circles are used.
src/helpers/PolarGridHelper.js
Outdated
@@ -16,7 +16,7 @@ class PolarGridHelper extends LineSegments { | |||
|
|||
// create the radials | |||
|
|||
for ( let i = 0; i <= radials; i ++ ) { | |||
for ( let i = 0; i < radials; i ++ ) { | |||
|
|||
const v = ( i / radials ) * ( Math.PI * 2 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the maximum value of v would be less than 2*Pi, which will result in radials not making full circle. Is this what we want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need v to go completely around the circle because that will create two lines at 0 degrees instead of only one.
I've added the change to my local branch and experimented with it. With this change, adding 0 for both Right now, it seems not allowing 0 is the better approach. |
@Mugen87 In my use case, I want to be able to set 0 radials and have only circles because otherwise it ends up cluttering the ground plane too much. As for 0 circles, I don't have that use case but I could see someone potentially only needing the radials. The way I see it, it would give the user a bit more freedom without the need of writing their own helper just for a slight modification. |
The production code has the following shortcomings:
This PR currently fixes (1) and (2). Instead, I would rename class PolarGridHelper extends LineSegments {
constructor( radius = 10, sectors = 16, rings = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) {
color1 = new Color( color1 );
color2 = new Color( color2 );
const vertices = [];
const colors = [];
// create the sectors
if ( sectors > 1 ) {
for ( let i = 0; i < sectors; i ++ ) {
const v = ( i / sectors ) * ( Math.PI * 2 );
const x = Math.sin( v ) * radius;
const z = Math.cos( v ) * radius;
vertices.push( 0, 0, 0 );
vertices.push( x, 0, z );
const color = ( i & 1 ) ? color1 : color2;
colors.push( color.r, color.g, color.b );
colors.push( color.r, color.g, color.b );
}
}
// create the rings
for ( let i = 0; i < rings; i ++ ) {
const color = ( i & 1 ) ? color1 : color2;
const r = radius - ( radius / rings * i );
for ( let j = 0; j < divisions; j ++ ) {
// first vertex
let v = ( j / divisions ) * ( Math.PI * 2 );
let x = Math.sin( v ) * r;
let z = Math.cos( v ) * r;
vertices.push( x, 0, z );
colors.push( color.r, color.g, color.b );
// second vertex
v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
x = Math.sin( v ) * r;
z = Math.cos( v ) * r;
vertices.push( x, 0, z );
colors.push( color.r, color.g, color.b );
}
}
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 );
this.type = 'PolarGridHelper';
}
} |
@WestLangley I have done the modifications you suggested. |
Thanks! |
* Allow zero radials or zero circles Prevents from getting NaN values when 0 radials or 0 circles are used. * Changed radials and circles to sectors and rings * Fixed lint errors Co-authored-by: Jeremie Bourque <[email protected]>
* Allow zero radials or zero circles Prevents from getting NaN values when 0 radials or 0 circles are used. * Changed radials and circles to sectors and rings * Fixed lint errors Co-authored-by: Jeremie Bourque <[email protected]>
Prevents from getting NaN values when 0 radials or 0 circles are used.
Related issue: none
Description
Previously, if a
PolarGridHelper
is created with 0 radials or 0 circles, we get the following error:The NaN values come from the for-loops doing an iteration for 0 wich causes a 0/0 division resulting in a NaN.
By changing the looping condition to a "less than", no iterations are done so no NaN is created, allowing for a polar grid with no radials or no circles without getting an error.