Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by AtGfx · Apr 04, 2017 at 08:00 PM · cameragraphicsrendertexturenative pluginbehaviour

Problem with camera Rendering

Hello,

I would like to get the output of a camera in a native plugin, but I have some trouble to make it works. Here is the behaviour I use for the camera :

 public class TestNativePlugin : MonoBehaviour 
 {
   private Camera m_Camera;
 
   IEnumerator Start()
   {
     m_Camera = gameObject.GetComponent<Camera>();
     if (m_Camera != null && m_Camera.pixelHeight > 0 && m_Camera.pixelWidth > 0)
     {
       CreateTextureAndPassToPlugin();
       yield return StartCoroutine("CallPluginAtEndOfFrames");
     }
   }
 
   private void CreateTextureAndPassToPlugin()
   {
     var myRenderTexture = new RenderTexture(m_Camera.pixelWidth, m_Camera.pixelHeight, 24, RenderTextureFormat.ARGB32);
 
     myRenderTexture.Create();
 
     /*Texture2D tex = new Texture2D(512, 512);
     Color[] colors = new Color[512 * 512];
 
     for (int i =0; i < 512*512; i++)
     {
       colors[i] = Color.black;
     }
 
     tex.SetPixels(colors);
     tex.Apply();*/
 
     m_Camera.targetTexture = myRenderTexture;
     InitializePlugin(myRenderTexture.GetNativeTexturePtr());

   }
 
   private IEnumerator CallPluginAtEndOfFrames()
   {
     while (true)
     {
       yield return new WaitForEndOfFrame();
 
        GL.IssuePluginEvent(GetRenderEventFunc(), 1);
     }
   }
 }

In my native plugin, I simply copy the texture on the cpu and print the content in a file. And with this behaviour my output is all 0.

If I initialize my native plugin with a Texture2d that I construct myself (code in comment) everything goes well : the value that I initialize in my texture is the value output by my native plugin.

I don't really understand what happens =/

Thank you for your help !

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AtGfx · Apr 25, 2017 at 05:32 PM

After a lot of tests and a lot of luck I figured out the problem which is the initialization of the RenderTexture. The following piece of code works :

 IEnumerator Start()
   {
     m_Camera = gameObject.GetComponent<Camera>();
 
     if (m_Camera != null && m_Camera.pixelHeight > 0 && m_Camera.pixelWidth > 0)
     {
       CreateTextureAndPassToPlugin();
       yield return StartCoroutine("CallPluginAtEndOfFrames");
     }
   }
 
   private void CreateTextureAndPassToPlugin()
   {
     m_RenderTexture = new RenderTexture(m_Camera.pixelWidth, m_Camera.pixelHeight, 24, RenderTextureFormat.ARGB32);
     m_RenderTexture.Create();
 
     m_Camera.targetTexture = m_RenderTexture;
     m_Camera.Render();
 
     InitializePlugin(m_RenderTexture.GetNativeTexturePtr());
   }
 
   private IEnumerator CallPluginAtEndOfFrames()
   {
     while (true)
     {
       yield return new WaitForEndOfFrame();
 
       GL.IssuePluginEvent(GetRenderEventFunc(), 1);
     }
   }

And the magic key is the call to Render after assigning the camera target texture. The RenderTexture is not created untill it is filled with something, event if you call the Create method that should create it on the hardware. According the documentation, the RenderTexture is effectively created when it is set to active...

Comment
Add comment · 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
0

Answer by spaul13 · Apr 02, 2018 at 07:06 PM

I am also trying to get the same thing. Can u please tell me how u have used this native plugin with any unity game code?

Just import this as .jar and put into plugin folder under assets?

What I know we have to import the plugin using DllImport and then we have to call the functions from game code.

Just want to know if we have the same code as a function of any CSharp code in my project, not as a plugin and calling it from that, there is any difference as getting the frame captured by camera normally slows down the game?

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

132 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 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

Camera.RenderToCubemap in VR has black edges 1 Answer

RawImage with RenderTexture texture not working on Android 1 Answer

Is Camera.render() exactly render one frame to RenderTexture completely? 0 Answers

Proper SetTargetBuffers usage with Deferred rendering in unity 5.5 (possible problem?) 1 Answer

Wrong Coloring (Sometimes) when capturing "Screen"-shot 1 Answer


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