FBplus
July 27, 2020, 3:56am
#1
I don’t know if this description is accurate,actually I want to implement a function like:RaycastHit.textureCoord in Unity or Raycaster.intersectObject in Three.js which returns the uv coordinate of the hitten mesh.
After some googling, I found a nice solution on github which implement a part of this function:
https://github.com/playcanvas/engine/pull/989
But that is not enough
Does anyone know how to accomplish this or any better solution?
1 Like
Hi @FBplus ,
I haven’t seen any similar method or project in Playcanvas. Agreed it would be a good feature to have.
Here is the Three.js implementation, if anybody would like to give it a try:
if ( morphTargets !== undefined && morphTargets.length > 0 ) {
console.error( 'THREE.Mesh.updateMorphTargets() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
}
}
},
raycast: function ( raycaster, intersects ) {
const geometry = this.geometry;
const material = this.material;
const matrixWorld = this.matrixWorld;
if ( material === undefined ) return;
// Checking boundingSphere distance to ray
if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
1 Like
FBplus
July 27, 2020, 9:33am
#3
@Leonidas Hi Leonidas,Thanks for your reply
I’ve read that code and found this function:
https://github.com/mrdoob/three.js/blob/0950e5b6e8bceb520c154f45b5c240af45f0ed11/src/objects/Mesh.js#L438
I think the only thing I have to figure out may be the uv coordinates of each vertex
If I know the index of a triangle based on IndexBuffer,Is there anyway to get the uv coordinates of three vertices of that triangle?
There isn’t a Playcanvas built in method for that, but study the Three.js Triangle class to see the math behind it:
},
containsPoint: function ( point, a, b, c ) {
Triangle.getBarycoord( point, a, b, c, _v3 );
return ( _v3.x >= 0 ) && ( _v3.y >= 0 ) && ( ( _v3.x + _v3.y ) <= 1 );
},
getUV: function ( point, p1, p2, p3, uv1, uv2, uv3, target ) {
this.getBarycoord( point, p1, p2, p3, _v3 );
target.set( 0, 0 );
target.addScaledVector( uv1, _v3.x );
target.addScaledVector( uv2, _v3.y );
target.addScaledVector( uv3, _v3.z );
return target;
1 Like
FBplus
July 28, 2020, 8:39am
#5
@Leonidas I made it
I’ve created a test project base on this function and it works fine
https://playcanvas.com/project/704805/overview/paint-3d-test
2 Likes
Wow @FBplus , that is working beautifully!! Many thanks for sharing.
I am going to definitely take a look at your implementation, this is quite useful for many things.
Nice work
We also have released a similar demo if both of you are interested: https://playcanvas.com/project/702588/overview/character-damage-demo
3 Likes
FBplus
July 28, 2020, 8:52am
#8
@Leonidas I am so glad if you found this project useful
To be honest, I just copy and paste the code from threeJS with minor adjustments without considering the performance and there is still a lot to improve.
1 Like
Hello @FBplus and @Leonidas - I want to paint the back of a woman: https://playcanvas.com/editor/scene/1031376
unfortunately, and no matter what I do, the brush only works on the top left area of my cutout back model?
As you can see; one of my workaround has been to alter this line in raycast.js:
this.cameraEntity.camera.screenToWorld(screenPosition.x*3.6, screenPosition.y, this.cameraEntity.camera.farClip, this.ray.direction);
(screenPosition.x multiplied by 3.6 in order to center the painted)
But otherwise, the problem of not being able to; paint blue on the whole area, still stands.
ok, got it working with a similar adjustment … don’t bother (nice example anyway @FBplus )
This example does not work anymore? Any updated on this subject?
Edit: Raycast does not return uv0 positions anymore I guess
There’s also this older and bit out of date PR that could be cleaned up and used I suspect
playcanvas:master
← scarletsky:ray-intersects-meshinstance
opened 02:20AM - 22 Aug 17 UTC
1 Like
Hello @yaustar . I forked your original Character damage demo but it isn’t working anymore in the latest version of playcanvas editor. Any ideas?
https://playcanvas.com/project/702588/overview/character-damage-demo
Thanks!
yaustar
November 3, 2022, 10:32am
#14
Unfortunately, we’ve removed that project from the tutorials as the developer who made it is on longer on our team and the project is no longer maintained.
Thanks for the update @yaustar
yaustar
November 10, 2022, 12:10pm
#16
1 Like
That’s awesome!! thanks yaustar!!