- Home /
How to make a getPerspectiveTransform and warpPerspective Like Opencv by a shader in Unity
I used OpencvforUnity api getPerspectiveTransform and warpPerspective to Transform a Texture2d in unity, it take much time at RenderTexture Convert to Texture2d , Texture2d to Mat ,Mat to Texture2d,it very Influence performance,What can I do to deal with such a problem?
My Script:
/// <summary>
/// UpdateFunction2
/// </summary>
IEnumerator UpdateFunction2()
{
yield return new WaitForEndOfFrame();
while (true)
{
yield return new WaitForEndOfFrame();
if UNITY_EDITOR
sw.Reset();
sw.Start();
endif
RenderTexture.active = EyeRenderTexture;
EyeTexture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
EyeTexture2D.Apply();
RenderTexture.active = null;
if UNITY_EDITOR
sw.Stop();
Debug.Log(this.gameObject.name + " ->ReadPixels:" + sw.ElapsedMilliseconds);
endif
if (point0Target)
point0 = point0Target.position;
if (point1Target)
point1 = point1Target.position;
if (point2Target)
point2 = point2Target.position;
if (point3Target)
point3 = point3Target.position;
Imagepoint0 = ScreenConvertToImage(height, point0);
Imagepoint1 = ScreenConvertToImage(height, point1);
Imagepoint2 = ScreenConvertToImage(height, point2);
Imagepoint3 = ScreenConvertToImage(height, point3);
ScrPoints.fromArray(new OpenCVForUnity.Point[]
{
new OpenCVForUnity.Point(Imagepoint0.x,Imagepoint0.y),
new OpenCVForUnity.Point(Imagepoint1.x,Imagepoint1.y),
new OpenCVForUnity.Point(Imagepoint2.x,Imagepoint2.y),
new OpenCVForUnity.Point(Imagepoint3.x,Imagepoint3.y)
});
if UNITY_EDITOR
sw.Reset();
sw.Start();
endif
OpenCVForUnity.Utils.fastTexture2DToMat(EyeTexture2D, SrcMat);
using (var matrix = OpenCVForUnity.Imgproc.getPerspectiveTransform(ScrPoints, dstPoints))
{
OpenCVForUnity.Imgproc.warpPerspective(SrcMat, DstMat, matrix, new OpenCVForUnity.Size(width, height), OpenCVForUnity.Imgproc.INTER_LINEAR);
OpenCVForUnity.Utils.fastMatToTexture2D(DstMat, PerspectiveTexture2D);
}
if UNITY_EDITOR
sw.Stop();
Debug.Log(this.gameObject.name + " ->warpPerspective:" + sw.ElapsedMilliseconds);
endif
}
}
/// /// Screen coordinates are converted to image coordinates /// ///
Scree height /// point value /// Image position Vector2 ScreenConvertToImage(float height, Vector3 point) { Vector3 screenpos = thisCamera.WorldToScreenPoint(point); return new Vector2(screenpos.x, height - screenpos.y); }Did you manage to find a solution for this? A quad doesn't work as it have 2 triangles..
Answer by FortisVenaliter · Aug 25, 2017 at 03:48 PM
Why don't you just create a quad mesh with the vertices transformed the way you want? You could even generate it procedurally with much less effort than all this.
I wanted to put the specified rectangle on the screen where the camera was seen and spread it over the screen,I already know the world coordinates for 4 points, but I don't know how to put the 4 side shapes on the screen
Your answer
![](https://koobas.hobune.stream/wayback/20220612133510im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shading is visible through object using surface shader 0 Answers
Shader Working in Shader Graph But Not In Scene 1 Answer
Updating a shader from Unity 3.3 0 Answers
Shader Help 0 Answers