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

LineSegments2: Fix raytracing when the geometry has instanceCount set. #25032

Merged
Merged
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
18 changes: 10 additions & 8 deletions examples/jsm/lines/LineSegments2.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const _box = new Box3();
const _sphere = new Sphere();
const _clipToWorldVector = new Vector4();

let _ray, _instanceStart, _instanceEnd, _lineWidth;
let _ray, _lineWidth;

// Returns the margin required to expand by in world space given the distance from the camera,
// line width, resolution, and camera projection
Expand All @@ -52,11 +52,15 @@ function getWorldSpaceHalfWidth( camera, distance, resolution ) {
function raycastWorldUnits( lineSegments, intersects ) {

const matrixWorld = lineSegments.matrixWorld;
const geometry = lineSegments.geometry;
const instanceStart = geometry.attributes.instanceStart;
const instanceEnd = geometry.attributes.instanceEnd;
const segmentCount = Math.min( geometry.instanceCount, instanceStart.count );

for ( let i = 0, l = _instanceStart.count; i < l; i ++ ) {
for ( let i = 0, l = segmentCount; i < l; i ++ ) {

_line.start.fromBufferAttribute( _instanceStart, i );
_line.end.fromBufferAttribute( _instanceEnd, i );
_line.start.fromBufferAttribute( instanceStart, i );
_line.end.fromBufferAttribute( instanceEnd, i );

_line.applyMatrix4( matrixWorld );

Expand Down Expand Up @@ -95,6 +99,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {
const geometry = lineSegments.geometry;
const instanceStart = geometry.attributes.instanceStart;
const instanceEnd = geometry.attributes.instanceEnd;
const segmentCount = Math.min( geometry.instanceCount, instanceStart.count );

const near = - camera.near;

Expand All @@ -120,7 +125,7 @@ function raycastScreenSpace( lineSegments, camera, intersects ) {

_mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld );

for ( let i = 0, l = instanceStart.count; i < l; i ++ ) {
for ( let i = 0, l = segmentCount; i < l; i ++ ) {

_start4.fromBufferAttribute( instanceStart, i );
_end4.fromBufferAttribute( instanceEnd, i );
Expand Down Expand Up @@ -279,9 +284,6 @@ class LineSegments2 extends Mesh {

_lineWidth = material.linewidth + threshold;

_instanceStart = geometry.attributes.instanceStart;
_instanceEnd = geometry.attributes.instanceEnd;

// check if we intersect the sphere bounds
if ( geometry.boundingSphere === null ) {

Expand Down