Need help world position to texture position

,

Hello I am trying to make a paintball game inside my game. I have a Dynamic Texture in addition to a Shader painting in pink color when I shot a ball. But the pink circle not appears in the correct place of the texture. I am also using physics so my point of collision is on the physiscs event: (Sorry I don’t have
a playground, is a large project)
bullet.physicsImpostor.registerOnPhysicsCollide(colliders, (collider, collided, point) => {
so I get the point that is the world point and I project to the mesh:

                    // Step 1: Project the world coordinates onto the mesh
                    var projectedPoint = BABYLON.Vector3.Project(
                        point,
                        meshCollided.getWorldMatrix(), //world matrix
                        scene.getTransformMatrix(), //transform matrix
                        scene.activeCamera.viewport,//viewport
                    );

with the projected point I get the UV coordinates:

 // Step 2: Get the UV coordinates from the mesh
                    var pickedInfo = scene.pick(projectedPoint.x, projectedPoint.y);
                    if (pickedInfo.hit) {
                        var uv = pickedInfo.getTextureCoordinates();
                        

and with that UV coordinates I paint in the texture a Pink circle:
paintContext.arc(uv.x * paintTexture.getSize().width, (1 - uv.y) * paintTexture.getSize().height, 10, 0, 2 * Math.PI);
Something is wrong because when the ball hits the ground just the center of the grass is painted and when I move the camera also changes the collision painted point that shouldn’t. (see the image)
What is wrong with this code? and about this approach? Why the hit point changes when the camera changes? I think somethings wrong with the projection coordinates but I am not very good with matrices andI don’t know how to solve it. And something is wrong with the scale of painted texture too, because I have edited in debug mode and I just can paint the center of the grass field, now is 512x512 maybe make it greater?. Any suggestion is welcome. Thank you.



1 Like

This seems a perfect fit for decal texture:

Thanks I had already applied decals and it works, just getting some textures glichets on these decals. Do you know why is that? Maybe is the grass material that is PBR? Thank you

I have no idea why you get that, as we can see in your screenshot that it worked for a number of other decals… Make sure it’s really coming from the decal texture and it’s not something else (look at the decal texture in the inspector to see if there is a white area there).

Thanks, I checked the decal and the material was null, I was applying a random material from an array and the array was out of bounds. Fixed!

1 Like