How to make a getPerspectiveTransform and warpPerspective Like Opencv by a shader in Unity

100631-39t0llx4udnckan.png

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);
}

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.