Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Dynamic Textures & Unity

Discussion in 'General Graphics' started by sschaem, Nov 12, 2016.

  1. sschaem

    sschaem

    Joined:
    Feb 14, 2014
    Posts:
    148
    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.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    SetPixels does support a rect, that's what the x/y/blockWidth/blockHeight parameters do (also, mip level). SetPixels32 is byte4.

    --Eric
     
  3. sschaem

    sschaem

    Joined:
    Feb 14, 2014
    Posts:
    148
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    "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
     
  5. sschaem

    sschaem

    Joined:
    Feb 14, 2014
    Posts:
    148
    "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 ?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's definitely not something that currently exists. ;)

    --Eric
     
  7. sschaem

    sschaem

    Joined:
    Feb 14, 2014
    Posts:
    148
    wink, wink, nudge nudge ? say no more... :)

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

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,374
    https://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html

    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.
     
  9. sschaem

    sschaem

    Joined:
    Feb 14, 2014
    Posts:
    148
    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 :)
     
    Last edited: Nov 17, 2016
  10. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,665
    @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
     
    Last edited: Nov 16, 2016