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