Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
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 within Unity it renders an empty texture in app.

These points obviously make me think 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 this 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 this 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 relationship between the Unity Texture2D pixel data and that which 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

could you share some source? when I try this all I get from readpixels is black color.

avatar image Ricna · Jun 11, 2013 at 01:54 AM 0
Share

please some code :(

avatar image reefwirrax · Oct 29, 2013 at 07:29 AM 0
Share

cg FBO plugin please

avatar image slippdouglas · Jul 21, 2014 at 08:08 PM 0
Share
  • for no source code snippets.  Please improve your answer for all who come across it.  (The idea behind an answers site like this is that each question only needs to be asked once; as such, answers need to be useful to all, not just the OP.)

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.

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

Alternate to UnitySendMessage in iOS native 1 Answer

What is Beast? 1 Answer

Extremely slow texture upload in plugin when a model is being drawn 1 Answer

Plugin iOS to send Mail with MFMailComposeViewController 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