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 /
avatar image
1
Question by jules_fpvr · Aug 24, 2017 at 11:38 AM · renderingrendertexturereadpixels

How do I get the raw pixels of a RFloat RenderTexture on the CPU?

To access the pixels of a RenderTexture you can create a texture with matching pixel format, make your RenderTexture the active render target, use ReadPixels to copy the data and then use GetRawTextureData to access the pixels.

However ReadPixels only works with a limited set of RenderTextureFormats.

This function works on RGBA32, ARGB32 and RGB24 texture formats, when render target is of a similar format too (e.g. usual 32 or 16 bit render texture). Reading from a HDR render target (ARGBFloat or ARGBHalf render texture formats) into HDR texture formats (RGBAFloat or RGBAHalf) is supported too.

If my RenderTexture isn't one of thes formats (ie a RenderTextureFormat::RFloat) then how can I access the raw pixel data on the CPU?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jules_fpvr · Nov 16, 2017 at 08:11 AM

@burtles I'm afraid not. I ended up doing something incredibly inefficient (using an ARGBFloat32 and copying value to all 4 channels) as my use case was more about it working than perf or mem.

-

If perf / memory are an issue then there are some options I thought about:

-

In some cases you can workaround the problem by having each shader process 4 pixels instead of 1 and then packing your RFloat values into an ARGBFloat texture with a quarter of width. Whether this turns out to be inefficient then depends on the complexity of your shader but there are scenarios where it would be almost as fast as doing it with RFloats.

-

Instead of using RFloat use an integer format like ARGB32. Then when writing it out encode your float into 4 colour components. Might be tricky to make it completely accurate but should be possible.

-

Perhaps the reason Unity doesn't support this is due to limitations on some platforms, if you need to solve it on a specific platform it may be possible to use a native plug-in.

-

In my case I needed to access the pixels to save them to disk, sometimes one can rework a design to avoid the need to bring the data back to the CPU, in many cases even if you can do it, that copy is pretty inefficient.

-

The slightly frustrating thing is that Unity clearly can do it internally as you can get a preview of an RFloat texture in the editor.

-

Good luck

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 burtles · Nov 16, 2017 at 02:39 AM

Did you ever figure this out? As far as I can tell it's impossible.

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 VisionEEG · Feb 08, 2019 at 06:50 PM

You can just add the extra copy using this:

 RenderTexture tempRT = RenderTexture.GetTemporary( rTex.width, rTex.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear );
 Graphics.Blit( rTex, tempRT );
 

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 aelus · Apr 20, 2020 at 08:51 AM

You can use a shader to write the texture to a ComputeBuffer and then GetData from that buffer. Double device memory consumption implied, of course. Notice that under DX11 you can do this trick on following uavs only: R32_FLOAT R32_UINT R32_SINT

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 zhiyingf · Jul 04, 2021 at 07:37 AM

You can get the raw pixels of a RFloat Texture on the CPU, next convert Texture to RenderTexture, for example:

 const int maxGridSideLength = 50;
 float[] data = new float[maxGridSideLength  * maxGridSideLength  * maxGridSideLength ];//this is your raw data
 
 //set texture3D
 Texture3D texture = new Texture3D(maxGridSideLength, maxGridSideLength, maxGridSideLength, TextureFormat.RFloat, false);
 texture.filterMode = FilterMode.Bilinear;
 texture.SetPixelData(data, 0);
 
 //convert texture3D to RenderTexture
 RenderTexture renderTex = new RenderTexture(maxGridSideLength, maxGridSideLength, 0, RenderTextureFormat.RFloat);
 renderTex.enableRandomWrite = true; 
 renderTex.dimension = UnityEngine.Rendering.TextureDimension.Tex3D; 
 renderTex.volumeDepth = maxGridSideLength; 
 renderTex.filterMode = FilterMode.Bilinear;
 renderTex.wrapMode = TextureWrapMode.Repeat;
 renderTex.useMipMap = false;
 renderTex.Create();
 Graphics.CopyTexture(tex3D, renderTex);
 
 

But ,there may be a small problem that when multiple rendertextures exist, only the last rendertexture conversion can succeed. But I don't know why this is.

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

75 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

Related Questions

Rendering an HD PNG off-screen? 0 Answers

Writing to RWTexture3D using a pixel shader without using Graphics.Blit 0 Answers

Irregular shape window to a different background world? 0 Answers

Low resolution RenderTexture is jitter 0 Answers

RenderTexture captured from camera with post effects only shows the emission pass. 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