1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

Unity 4 - 3D Textures (Volumes)

Discussion in 'Developer Preview Archive' started by alexandre-fiset, Aug 23, 2012.

  1. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    225
    Does anybody know how to create a simple 3D Texture from a script? In the release notes, it is specified that we cannot import those, so we have to create them in some way...

    I want to try the new feature, but since there is documentation covering the subject, I don't know where to start...

    I found things like:
    var myTexture : Texture3D;*
    myTexture.depth
    myTexture.height
    myTexture.width
    (...)

    Anyone? :p
     
    Last edited: Aug 23, 2012
  2. U2

    U2

    Joined:
    Aug 12, 2008
    Posts:
    215
    I would also like to know this.. now that Unity is supporting DirectX 11 do we have access to 3D Textures? Thank you very much...
     
  3. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,522
    Here's a small example C# script:
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. public class Create3DTex : MonoBehaviour {
    6.    
    7.     public Texture3D tex;
    8.     public int size = 16;
    9.  
    10.     void Start ()
    11.     {
    12.         tex = new Texture3D (size, size, size, TextureFormat.ARGB32, true);
    13.         var cols = new Color[size*size*size];
    14.         float mul = 1.0f / (size-1);
    15.         int idx = 0;
    16.         Color c = Color.white;
    17.         for (int z = 0; z < size; ++z)
    18.         {
    19.             for (int y = 0; y < size; ++y)
    20.             {
    21.                 for (int x = 0; x < size; ++x, ++idx)
    22.                 {
    23.                     c.r = ((x1)!=0) ? x*mul : 1-x*mul;
    24.                     c.g = ((y1)!=0) ? y*mul : 1-y*mul;
    25.                     c.b = ((z1)!=0) ? z*mul : 1-z*mul;
    26.                     cols[idx] = c;
    27.                 }
    28.             }
    29.         }
    30.         tex.SetPixels (cols);
    31.         tex.Apply ();
    32.         renderer.material.SetTexture ("_Volume", tex);
    33.     }  
    34. }
    35.  
    ...and a simple shader that uses it (uses object space positions as 3D texture coordinates):
    Code (csharp):
    1.  
    2. Shader "DX11/Sample 3D Texture" {
    3. Properties {
    4.     _Volume ("Texture", 3D) = "" {}
    5. }
    6. SubShader {
    7. Pass {
    8.  
    9. CGPROGRAM
    10. #pragma vertex vert
    11. #pragma fragment frag
    12. #pragma exclude_renderers flash gles
    13.  
    14. #include "UnityCG.cginc"
    15.  
    16. struct vs_input {
    17.     float4 vertex : POSITION;
    18. };
    19.  
    20. struct ps_input {
    21.     float4 pos : SV_POSITION;
    22.     float3 uv : TEXCOORD0;
    23. };
    24.  
    25.  
    26. ps_input vert (vs_input v)
    27. {
    28.     ps_input o;
    29.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    30.     o.uv = v.vertex.xyz*0.5+0.5;
    31.     return o;
    32. }
    33.  
    34. sampler3D _Volume;
    35.  
    36. float4 frag (ps_input i) : COLOR
    37. {
    38.     return tex3D (_Volume, i.uv);
    39. }
    40.  
    41. ENDCG
    42.  
    43. }
    44. }
    45.  
    46. Fallback "VertexLit"
    47. }
    48.  
     
  4. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,522
    Yes, in 4.0. It's not DX11 specific however, 3D textures work on D3D9, D3D11, OpenGL, Xbox 360 etc. They definitely do not work on Flash or mobile right now, since those platforms currently just can't support them.
     
  5. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    225
    Thanks Aras!
     
  6. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    I assume that using DirectX 11 with 10/11 level hardware allows you to render into different slices of a 3D volume texture using a geometry shader? Or can we only use 11 with random write?

    While we are at it, can I specify render targets and viewports in a geometry shader? With the oculus rift on the way I would like to be able to draw to both the left and right eye viewports in a single draw call on supported hardware.
     
    Last edited: Aug 29, 2012
  7. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,522
    Into slices: not in 4.0 - but I guess I could look into adding that. But on DX11 you can arbitrarily write into a 3D render texture using "random write" (UAV in DX11 lingo); e.g. if you're computing some fluid simulation from a compute shader and writing the results into a 3D texture.

    Good question, I haven't tried that.

    I think if you setup multiple render targets (possible since Unity 3.5) and just have a geometry shader that outputs to one of them, then all should just work (tm). We don't have any way of setting up multiple viewports in Unity right now however.
     
  8. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    first of all, thanks Aras for this example !
    Is it working on mac ?

     
  9. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,522
    Yes.
     
  10. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    Thanks, multiple viewports would be ideal but using MRT for the left and right eye images should work almost as well.

    I would really like to see rendering into slices though because then doing realtime 3D grid simulations/voxelization becomes feasible on DX10 hardware, not just 11. And its much easier to do voxelization of meshes this way even in DX11 because you can use the raster pipeline stage and clipping planes to nicely scan in the model slice by slice.
     
    Last edited: Aug 29, 2012
  11. GradyLorenzo

    GradyLorenzo

    Joined:
    Jan 17, 2010
    Posts:
    155
    Volumetric Light and Fog, please, that's be fantastic for clouds. I think volumetric is semi-implemented, though, with the God-rays image effect.
     
  12. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    225
    I think that the God Rays in Unity aren't "real" god rays like in Cry Engine or Unreal. If they are real, they certainly don't look as good...