Dynamic Textures & Unity

I’m very interested in using dynamic texture (run-time managed/updated) but it doesn’t seem like Unity expose an API targeted for that usage. Am I correct with this assumption ?

My goal is to update rectangular regions of a texture on a frame basis in C#.

The API seem limited to float4 pixel write, or entire texture update (also in float4)

Would it be possible to extended SetPixels() API to support sub-rect, and more formats?
At least “float” and “int16”…

Actually a “casting” mode would be very useful, where the data is simply copied as is.

SetPixels does support a rect, that’s what the x/y/blockWidth/blockHeight parameters do (also, mip level). SetPixels32 is byte4.

–Eric

What an oversight on my part! Thank you.

For the formats, is there a way to bypass going from float[ ] to Color[ ] (four float) to update a float32 texture ?

And ultimately, I also wonder how to manage float16 (half) format in C# buffers and Unity textures at run-time.
(Updating portions of texture each frame)
Because I would like to store all my volumetric data in float16 buffers on HW that support it.

“This function works only on ARGB32, RGB24 and Alpha8 texture formats”. For SetPixels, that is. SetPixels32 says ARGB32 only. SetPixels32 is faster and uses 4X less RAM, so I’d suggest that.

–Eric

“This function works only on ARGB32 texture formats. For other formats SetPixels32 is ignored”

My textures are currently “TextureFormat.RFloat” (and optimally “TextureFormat.RHalf”)
So it seem that there is no optimal path to work with those texture dynamically.

Would this fall into the “Feature request” category ?

It’s definitely not something that currently exists. :wink:

–Eric

wink, wink, nudge nudge ? say no more… :slight_smile:

Well, if the Unity team decide to refine dynamic texture (data streaming) believe that I have a good case for it.

Not as easy to use, but you can directly write data for half or float textures this way. Technically using SetPixels still requires the entire texture to be uploaded to the GPU again, its just changing less data on the CPU side that’s more efficient. The alternative is you could have your textures exist as render textures on the GPU and update them using blits or compute shaders and pass the data as smaller textures or structured buffers.

Whoa! I was planning on using sets of 4kx4k float textures and update block of 16x16 area at 60+ FPS,
so thats not going to happen.

The method you proposed is workable, but is indeed a bit convoluted and not as clean/efficient as streaming data directly to dynamic texture.

I’m relatively new to Unity, and its interesting how so many things are so easy to get done.
But then, you are also faced with writing extra code to work around artificial limitations.

I faced the same situation many years ago trying to use Directx for non gaming applications…
It only got better when MS internally decided to use DirectX for windows/apps, then all the feature requests got done :slight_smile:

@bgolus
I’d really love it if you could elaborate further, or even provide an example of this in usage. I have messed around a little with render textures and trying to figure out Graphics.Blit and such… but for the most part I can’t get the same effect as doing SetPixel() then Apply() to change a single pixel on a render texture.

Any idea where to get started writing an implementation of this kind of thing? I’d be happy to see a small example where a line is drawn or even just a single dot is changed to a texture, then drawn to the screen…

I suppose this asset is worth a try: https://www.assetstore.unity3d.com/en/#!/content/73577