• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by XinYueVR · Aug 25, 2017 at 05:02 AM · shader programmingshader writing

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

alt text

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); }
39t0llx4udnckan.png (77.3 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LuisCRSousa · Feb 17, 2018 at 06:37 PM 0
Share

Did you manage to find a solution for this? A quad doesn't work as it have 2 triangles..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image XinYueVR · Aug 26, 2017 at 09:01 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fog not working in my Shader~ 0 Answers

Obtaining relative speed of vertex in a shader 1 Answer

[SOLVED] Need help with Custom Toon Shader 1 Answer

Shader: Mask Without a Texture? 2 Answers

Surface shader changing z value 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges