- Home /
How to convert touch / mouse point to Texture coordinate ?
Hi guys,
I'm trying to work on a simple drawing game. You basically touch the screen on the area you want to paint (like the bucket tool in MS Paint), and the area will be filled with one color. I tried using some Flood Fill algorithms available out there, and while it is working fine I got a number of precisions problems : when I touch somewhere on the screen, I don't know how to translate that in the texture 2D space to get the pixel I just clicked. I'm working in Canvas so i can't use the data that comes with the Raycast (like hit.texcoord).
This is the implementation I'm working on : http://wiki.unity3d.com/index.php/TextureFloodFill
I don't know why but the parameters aX and aY are not valid and the algorithm does not always fill at the correct position I passed it. Any idea how to get a pixel from a touch / mouse Input ? Thanks !
Answer by UnitedCoders · Feb 07, 2018 at 10:29 AM
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UV_Cordinate : MonoBehaviour {
public GameObject brushCursor,brushContainer;
public Camera sceneCamera,RT_Cam;
void Update () {
if (Input.GetMouseButton(0)) {
// GenerateMask();
}
UpdateBrushCursor ();
}
void GenerateMask(){
Vector3 uvWorldPosition=Vector3.zero;
if(HitTestUVPosition(ref uvWorldPosition)){
// GameObject brushObj;
// brushObj=(GameObject)Instantiate(mask1);
// brushObj.transform.parent=brushContainer.transform;
// brushObj.transform.localPosition=new Vector3(uvWorldPosition.x,uvWorldPosition.y,mask1.transform.localPosition.z); //The position of the brush (in the UVMap)
}
}
void UpdateBrushCursor(){
Vector3 uvWorldPosition=Vector3.zero;
if (HitTestUVPosition (ref uvWorldPosition)) {
brushCursor.SetActive(true);
brushCursor.transform.position =uvWorldPosition+brushContainer.transform.position;
brushCursor.transform.position = new Vector3 (brushCursor.transform.position.x, brushCursor.transform.position.y, 5f);
} else {
// brushCursor.SetActive(true);
}
}
//Returns the position on the texuremap according to a hit in the mesh collider
bool HitTestUVPosition(ref Vector3 uvWorldPosition){
RaycastHit hit;
Vector3 cursorPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0.0f);
Ray cursorRay=sceneCamera.ScreenPointToRay (cursorPos);
if (Physics.Raycast(cursorRay,out hit,200)){
MeshCollider meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
return false;
Vector2 pixelUV = new Vector2(hit.textureCoord.x,hit.textureCoord.y);
uvWorldPosition.x=pixelUV.x-RT_Cam.orthographicSize;//To center the UV on X
uvWorldPosition.y=pixelUV.y-RT_Cam.orthographicSize;//To center the UV on Y
uvWorldPosition.z=0.0f;
return true;
}
else{
return false;
}
}
}
I hope this will help you, I made this script to get texture UV cordinate x,y .
Thank you for your help, I'll try to test your script right away. I'm concerned about your Raycast : I'm working with a UI Image so maybe it won't accept the Raycast ? Any thoughts on this matter ?
Yes i got that box checked on my Image, but the Raycast does not hit anything, unfortunately ...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to fill texture that would be avaliable to ither shaders? 1 Answer