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

Geometry: Move computeLineDistance() to Line/LineSegments #13147

Merged
merged 9 commits into from Feb 9, 2018

Conversation

Mugen87
Copy link
Collaborator

@Mugen87 Mugen87 commented Jan 21, 2018

This PR moves .computeLineDistance() to Line and adds support for (non-indexed) BufferGeometry. The combination of LineDashedMaterial and BufferGeometry does work now. This feature was frequently asked by users, e.g.

https://discourse.threejs.org/t/computelinedistances-with-buffergeometry/644 or
#7013

The implementation is based on @WestLangley's suggestion, see #7013 (comment)


var geometryCube = cube( 50 );

geometryCube.computeLineDistances();
Copy link
Collaborator Author

@Mugen87 Mugen87 Jan 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: With CanvasRenderer it is not necessary to compute the line distances for dashed lines...

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Jan 21, 2018

LineSegments is also supported with this PR. Like mentioned in the docs .computeLineDistance() calculates for each point in the geometry the distance to the very beginning of the line. This also works for LineSegments objects.

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Jan 21, 2018

I wonder if we could "hide" the call of .computeLineDistance() inside the renderer. This would avoid issues like https://discourse.threejs.org/t/linedashedmaterial-does-not-work/1640

So if the renderer detects an instance of LineDashedMaterial in a line object, it checks the line distance data and generates them if necessary.


if ( geometry.isBufferGeometry ) {

// we assume non-indexed geometry
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think you should handle indexed geometry gracefully?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. It should be better with 0286198


}

lineDistances.push( distance );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we did not always create these JS arrays unnecessarily. It is a pattern we use over-and-over...

@WestLangley
Copy link
Collaborator

LineSegments is also supported with this PR.

To do so, the distances need to be calculated differently for line segments.

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Jan 21, 2018

What would be different? I thought we calculate for each point the distance to the beginning of the line...

@WestLangley
Copy link
Collaborator

I thought we calculate for each point the distance to the beginning of the line...

We should be calculating the cumulative length.

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Jan 21, 2018

Ah, now i understand. When using LineSegments we should only consider the distances between the start and end point of each segment. So let's overwrite .computeLineDistance() in LineSegments.


geometry.lineDistances[ i ] = distance;

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geometry.lineDistances[ 0 ] = 0;

for ( var i = 1, l = vertices.length; i < l; i ++ ) {

	geometry.lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
 
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That did not work for me since geometry.lineDistances[ i ] needs to be initialized with the previous value. Otherwise the summation does not work.

geometry.lineDistances[ 0 ] = 0;

for ( var i = 1, l = vertices.length; i < l; i ++ ) {

        geometry.lineDistances[ i ] = geometry.lineDistances[ i - 1 ];
	geometry.lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] );
 
}

@Mugen87 Mugen87 changed the title Geometry: Move computeLineDistance() to Line Geometry: Move computeLineDistance() to Line/LineSegments Jan 21, 2018
@mrdoob mrdoob added this to the r91 milestone Jan 21, 2018
@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 7, 2018

Related: #8494

When the PR is merged, there is one less reason for users to convert their lines based on BufferGeomery to Geometry if they want to use LineDashedMaterial. The conversion is not supported anyway.

@mrdoob mrdoob modified the milestones: r91, r90 Feb 9, 2018
@mrdoob mrdoob merged commit e3cea7e into mrdoob:dev Feb 9, 2018
@mrdoob
Copy link
Owner

mrdoob commented Feb 9, 2018

Thanks!

@WestLangley
Copy link
Collaborator

N.B. LineDashedMaterial does not work correctly with LineLoop because there is no distance computed for the last leg.

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 14, 2018

Even if we compute the distance from the last vertex to the first one, how would we put this value in the lineDistances array/attribute? Normally we have for each vertex a respective distance value. This seems not to be true for LineLoop, right?

@WestLangley
Copy link
Collaborator

Right. That's the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants