Help with cropping a part of an Image using rect.
Hello everyone, I am trying to crop an Image set on a quad using another quad's vertices as Rect. The problem I'm facing is that rect gets offset somehow and not able to capture the exact cropped image.
Here is waht it looks like first image.!
But after it get cropped it looks like second image!
Here is What I have done so far
public Transform imageQuadTrans;
public Renderer imageQuad;
public Renderer LipsMapper;
Texture2D face, lips;
public Bounds bounds;
void Start ()
{
face = imageQuad.material.mainTexture as Texture2D;
StartCoroutine (CaptureFrame ());
}
IEnumerator CaptureFrame ()
{
yield return new WaitForEndOfFrame ();
bounds = LipsMapper.bounds;
Vector3 topRight = bounds.max;
Vector3 bottomLeft = bounds.min;
Vector3 topLeft = new Vector3 (bottomLeft.x, topRight.y, topRight.z);
Vector3 bottomRight = new Vector3 (topRight.x, bottomLeft.y, bottomLeft.z);
Vector3 screenTopR = Camera.main.WorldToScreenPoint (topRight);
Vector3 screenTopL = Camera.main.WorldToScreenPoint (topLeft);
Vector3 screenBotR = Camera.main.WorldToScreenPoint (bottomRight);
Vector3 screenBotL = Camera.main.WorldToScreenPoint (bottomLeft);
Texture2D newTex = new Texture2D (128, 128, TextureFormat.ARGB32, false);
newTex.ReadPixels (new Rect (screenBotL.x, screenBotR.y, Screen.width - screenBotR.x, 0 + screenTopL.y), 0, 0, false);
newTex.Apply ();
LipsMapper.material.mainTexture = newTex;
}
}
Any help regarding this is highly appriciated.. Thank you.
Are you probably using WorldToScreenPoint in the wrong way? In WorldToScreenPoint "The z position is in world units from the camera." For example if the camera is at z=-10 and your object at z=2 you have to insert z=12 (and not just the z-value of the object).
Thanks for your quick reply.
I tried appliying your solution but still get same issue. And I forgot to mention that I'm using an orthographic camera so z value can be anything.
Update
I have found a way to crop image but it adds extra area in quad. Can anyone tell me how to fix this.
Thanks..!
Answer by 786dheeraj · Mar 14, 2017 at 09:20 AM
Did you find any solution? I am also stuck in a similar problem.
Your answer
Follow this Question
Related Questions
How to cut a rectangular area from ui image 1 Answer
Unity generate a sprite with main camera dimentions 0 Answers
Hello.I use the Region Capture to create the coloring of augmented reality. 0 Answers
AR Camera Projection 0 Answers
Convert Texture to Texture2d takes a lot of time in android device. 0 Answers