• 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
1
Question by Bork Awesome · Jan 16, 2012 at 02:27 AM · iostexturegraphicsplugin

Saving Unity Texture2D updated with OpenGL FBO via custom plugin

I'm currently rendering custom geometry into a Unity Texture2D on an iOS device, via a hand coded plugin using an OpenGL frame buffer attached to the Texture2D native texture ID. The updated texture is rendering as intended when running in the Unity app.

What I now want to do is save the updated Texture2D to file via EncodeToPNG() . What happens is EncodeToPNG returns a teeny weeny byte array (~5kb) that I assume only holds basic PNG format, width and height data, as when I view the PNG pulled from the device it displays in Photoshop with the correct width and height but with no pixel data i.e is empty!

Another point of interest is if I call Apply() on the texture wit$$anonymous$$n Unity it renders an empty texture in app.

These points obviously make me t$$anonymous$$nk that simply rendering to the texture in my plugin is not actually updating any pixel data that the Unity Texture2D may own, but here I come to a loss as to how to update the Texture2D.

My first thought was to call Texture2D.ReadPixels(..) but as t$$anonymous$$s is an iOS app the call I use to communicate the texture update to the Unity app from native code - UnitySendMessage - has a one frame delay and so the context is lost.

I've done a bit of investigation pulling the frame buffer data into a byte array using glReadPixels and updating the texture with t$$anonymous$$s and I have a feeling I'm just duplicating the frame buffers work in a roundabout way as that fails too..

I would be very grateful for any suggestions; especially an understanding of the relations$$anonymous$$p between the Unity Texture2D pixel data and that w$$anonymous$$ch is manipulated via OpenGL commands via the native texture ID.

Thank you!

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
-2
Best Answer

Answer by Bork Awesome · Jan 16, 2012 at 06:56 PM

Solved!

Seems I already had the answers but couldn't see the wood for the trees; to read the frame buffer back into a Texture2D you do the following:

In plugin/native code:

 - Update frame buffer attached to Texture2D's native texture ID with
    custom goodies 
 - Stop rendering into the frame buffer
 - Call UnitySendMessage with 'script.method' to handle the update


In Unity 'script.method' called by UnitySendMessage (one frame later):

 - Call plugin method to bind the frame buffer
 - Call Texture2D.ReadPixels(new Rect(0, 0, frame buffer width, frame buffer height), 0, 0)
 - Call Texture2D.Apply()
 - Call plugin method to un-bind the frame buffer/restore previous frame buffer


Et voila! Unity Texture2D is full of the lovely new pixels and can be saved out via Texture2D.EncodeToPNG :)

Comment
Add comment · Show 4 · 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 peterept2 · Jan 26, 2013 at 11:40 PM 0
Share
avatar image Ricna · Jun 11, 2013 at 01:54 AM 0
Share
avatar image reefwirrax · Oct 29, 2013 at 07:29 AM 0
Share
avatar image slippdouglas · Jul 21, 2014 at 08:08 PM 0
Share
avatar image
0

Answer by hzhamad · Oct 27, 2014 at 05:16 PM

It says in the documentation here, http://docs.unity3d.com/Manual/NativePluginInterface.html , that:

Note: The rendering callbacks to plugins are not currently supported on mobile platforms.

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.

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

8 People are following this question.

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

Related Questions

Issue with glDrawArrays in plugin 2 Answers

GetNativeTexturePtr is not working in iOS plugin 1 Answer

Admob Plugin Unity IOS 4 Answers

What is Beast? 1 Answer

What does Texture.GetNativeTexturePtr() return when using Metal? 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