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."][1] 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). [1]: http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

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..!

It's just the standard shader. Don't worry though I sorted it out.

1 Answer

1

Did you find any solution?
I am also stuck in a similar problem.