# Is it possible to get UV coordinates of an object without using mesh colliders ?

Hello,

I’d like to create a painting mechanic where you can paint on any surface. I already have all the painting system working but I’d like the paint to paint on adjacent surfaces rather than just the one model at a time (the one I have my mouse on).

Here is a preview of what I’m talking about :

The ground has been painted but the walls didn’t because my mouse wasn’t exactly on them

Here is the code I use to make it happen :

``````        // Find mesh to paint with the mouse
if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out _hit, 10f))
{
//Paint hit object
PaintableSurface paintableSurface = _hit.collider.GetComponent<PaintableSurface>();
if (paintableSurface != null)
{
paintableSurface.Paint(_hit.textureCoord);
}

Collider[] cols = Physics.OverlapSphere(_hit.point, 0.75f);
foreach (Collider col in cols)
{
{
// Get closest point and paint on
Vector3 closestPointOnCollider = col.ClosestPoint(_hit.point);
if (Physics.Raycast(new Ray(_hit.point, (closestPointOnCollider -_hit.point).normalized), out, _hit2, 5f))
{
Debug.Log(_hit.textureCoord + " " + _hit2.textureCoord);
}
}
}
}
``````

I use raycasts to get the texcoord of the surface I hit. This method requires to use only non convex mesh colliders if I want right UV coordinates.
To make the adjacent objects painted at the same time, I get the closest point between the collider and the hit point.
Unfortunately, the ClosestPoint method force me to use all colliders BUT non convex mesh colliders…

Since it will be on mobile and I want to be able to paint on large surfaces and many objects, is there another way to achieve the same effect efficiently without shooting random rays in all direction from the first hit point ?

Thank you for your lights !

Instead of Raycast, you may want to try SphereCastAll https://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html

Then, you may not need to do the OverlapSphere part.

1 Like

It’s about replicating Splatoon-like painting, so there might be something useful for you there?

1 Like

Thank you for the tip ! Tested it and it works ! But It does not reproduce exactly the effect intended.
Note at the end of the gif that the paint still does not cover the asjacent walls and the paint start upper from expected.
It cannot be solved by increasing the sphere value because it is a sphere and the paint will go even further away from the expected border we want the paint to be visible.
But it’s definitely better !

naturalrashfiddlercrab

Here is the modifed code using the method :

``````         _hits = Physics.SphereCastAll(_camera.ScreenPointToRay(Input.mousePosition), 1f, 10f, LayerMask.GetMask("Paintable"));
foreach (RaycastHit hit in _hits)
{
PaintableSurface paintableSurface = hit.collider.GetComponent<PaintableSurface>();
if (paintableSurface != null)
{
paintableSurface .Paint(hit.textureCoord);
}
}
``````

Anyway, I guess I could counter that by creating big models with only one texture rather than 1 mesh per wall (in this example).

I could also try to calculate the closest point along the mesh to the center hit, but I don’t know where to start.

@Olmi Thank you for the link, definitely will dive into, It seams exactly the kind of problematic they are tackling !
They seams to use vertex painting, but I’m not sure if it’s a good idea for mobile devices.

@antoined73 how do you manage to paint into that single point? im talking about the `adjacentPaintableSurface.Paint(_hit2.textureCoord);`