- Home /
Find TextureCoordinates from plane with out raycast only using mouse position on plan
Hello Guys. Wel I'm stuck and don't know what to do now,I will be very glad if any one of you help me. So I am working on a drawing feature of my app,It has a plan generator that generate 3Dplanes like grid .these tile contain Images on them .What I do is get the texture coordiantes by hitting Ray on the collider of plane and get the UV of that mesh and multiplying that UV by tex width and height to find pixel at that point of hit and assigning it to my Paint function. if (Input.GetMouseButton(0)) { if (Physics.Raycast(ray, out hit, 100f)) { Debug.DrawLine(ray.origin, hit.point, Color.blue, 2f); Renderer rend = hit.transform.GetComponent<Renderer>(); MeshCollider meshCollider = hit.collider as MeshCollider; if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null) return; Texture2D tex = rend.material.mainTexture as Texture2D; Vector2 pixelUV = hit.textureCoord; pixelUV.x *= tex.width; pixelUV.y *= tex.height; Debug.Log("UV of hit point" + hit.textureCoord); Debug.Log("Hit Point is "+ hit.point); Debug.Log("Vector3 Point is " + CursorOnTransform); DrawableObj.GetComponent<DrawViewController>().Paint(pixelUV); // } } }
but I have problem it works fine on one tile but when shifting tile .The problem is the ray is coming from camera on mouse position but when we move mouse faster then it skip alot of position e.g if on first frame the mouse is on left tile at some position then on next frame it is on next tile at some position...So to remove this constrant I ha convert the mouse position to a position on a plane Now I have a vector3 which is on the same position where my mouse is over the plan.I got this vector3 by this method......... public Vector3 cursorWorldPosOnNCP { get{ return Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Camera.main.nearClipPlane)); } } public Vector3 CameraToCursor { get{ return cursorWorldPosOnNCP - Camera.main.transform.position; } } public Vector3 CursorOnTransform { get { /* ParentPlan is and empty gameObject which have all the generated plan as child */ Vector3 camToTransform = ParentPlan.transform.position - Camera.main.transform.position; return Camera.main.transform.position + CameraToCursor * (Vector3.Dot(Camera.main.transform.forward, camToTransform) / Vector3.Dot(Camera.main.transform.forward, CameraToCursor)); } }
there is one thing
is same Now I want to get the texture coordinates of the plan that is under this globalPoint How I can Find it??? ![alt text][1] [1]: /storage/temp/152139-hitonplan.jpghit.point and Vector3 globalPoint = CursorOnTransform;
Your answer
Follow this Question
Related Questions
Alternative to ScreenPointToRay needed 1 Answer
rotate towards a raycast hit point 2 Answers
Directional Shot (!) 1 Answer
RayCast Help 1 Answer
Controlling object movement with mouse 0 Answers