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

Raycaster: Add barycoord to intersection result, make attribution interpolation more convenient. #29340

Merged
merged 7 commits into from
Sep 18, 2024

Conversation

gkjohnson
Copy link
Collaborator

@gkjohnson gkjohnson commented Sep 6, 2024

Related issue: --

Description

Currently raycasting only returns a few pre-interpolated results but not all, which can be useful in cases where other attributes need to be interpolated abitrarily, such as when sampling the recently-added Cesium metadata plugins for 3D Tiles Renderer. This PR outputs the barcoord of the hit point relative to the surface to mesh raycast intersections and adds a getAttributeInterpolationFromBarycoord function to Triangle to make interpolating attributes more convenient.

Currently this is the code that needs to be run to interpolate a uv3 attribute after raycast:

const triangle = new Triangle();
const vec20 = new Vector2();
const vec21 = new Vector2();
const vec22 = new Vector2();
const barycoord = new Vector3();

// ...

const { object, face, point } = hit;

// get barycoord value
triangle.setFromAttributeAndIndices( object.geometry.attributes.position, face.a, face.b, face.c );
triangle.a.applyMatrix4( object.matrixWorld );
triangle.b.applyMatrix4( object.matrixWorld );
triangle.c.applyMatrix4( object.matrixWorld );
triangle.getBarycoord( point, barycoord );

// get attribute values per vertex
const uv3 = object.geometry.attributes.uv3;
vec20.fromBufferAttribute( uv3, face.a );
vec21.fromBufferAttribute( uv3, face.b );
vec22.fromBufferAttribute( uv3, face.c );

// calculate the interpolated value
const result = new Vector2();
result
  .addScaledVector( vec20, barycoord.x )
  .addScaledVector( vec21, barycoord.y )
  .addScaledVector( vec22, barycoord.z );

And this is what it is after:

const { object, face, barycoord } = hit;
const uv3 = object.geometry.attributes.uv3;
const result = new Vector2();
Triangle.getAttributeInterpolationFromBarycoord( uv3, face.a, face.b, face.c, barycoord, result );

This contribution is funded by Cesium GIS Ecosystem Grant

@gkjohnson gkjohnson added this to the r169 milestone Sep 6, 2024
Copy link

github-actions bot commented Sep 6, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 685.94
169.78
686.03
169.81
+93 B
+30 B
WebGPU 834.9
223.91
835
223.93
+93 B
+25 B
WebGPU Nodes 834.41
223.78
834.5
223.81
+93 B
+25 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 462.29
111.52
462.34
111.55
+48 B
+35 B
WebGPU 531.72
143.33
531.77
143.36
+48 B
+26 B
WebGPU Nodes 488.38
133.2
488.43
133.22
+48 B
+20 B

@@ -106,6 +111,25 @@ class Triangle {

}

static getAttributeInterpolationFromBarycoord( attr, i1, i2, i3, barycoord, target ) {
Copy link
Collaborator

@Mugen87 Mugen87 Sep 18, 2024

Choose a reason for hiding this comment

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

The change is okay for me 👍 .

Would you still add a documentation for getAttributeInterpolationFromBarycoord()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

Copy link
Collaborator

Choose a reason for hiding this comment

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

How about simply getInterpolatedAttribute().

It is more than just barycoord that you are passing in.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just renamed it! I think the simpler name is better, as well.

@gkjohnson gkjohnson merged commit 9cfaaa8 into mrdoob:dev Sep 18, 2024
12 checks passed
@gkjohnson gkjohnson deleted the interpolation-update branch September 18, 2024 21:23
@Mugen87 Mugen87 changed the title Raycasting: Add barycoord to intersection result, make attribution interpolation more convenient Raycaster: Add barycoord to intersection result, make attribution interpolation more convenient. Sep 19, 2024
LD2Studio pushed a commit to LD2Studio/LD2Studio-Editor that referenced this pull request Sep 20, 2024
…terpolation more convenient (mrdoob#29340)

* Add a barycoord helper

* add fields to point, line

* Fix import

* Remove unused variables

* Update docs

* Rename method

* Rename function in Mesh class
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.

3 participants