Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. 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

Texture3D not working in Unity Plus

Discussion in 'Graphics Experimental Previews' started by natesb, Dec 4, 2017.

  1. natesb

    natesb

    Joined:
    Aug 26, 2017
    Posts:
    26
    Hi,

    Is Texture3D supported in Unity plus or just Unity pro? I couldn't find that specific info anywhere.

    I'm currently running Unity plus and trying to create a voxel based vector field texture for some DirectCompute fluid sim/ particle system work I'm doing.

    If it is supported, can someone tell me what I am doing wrong? The texture seems to be created fine (i.e. it has colors when I open it in the inspector.)

    Here's my code for creating the texture:
    Code (CSharp):
    1.  
    2. void CreateVectorFieldTexture()
    3.     {
    4.         int idx = 0;
    5.  
    6.         int res = (int)_vectorField._fieldInfo[0].Resolution.x;
    7.         var data = new Color[res * res * res];
    8.  
    9.         _vectorFieldTexture = new Texture3D(res, res, res, TextureFormat.RGBAFloat, true);
    10.         _vectorFieldTexture.name = "VectorField";
    11.         _vectorFieldTexture.filterMode = FilterMode.Trilinear;
    12.         _vectorFieldTexture.wrapMode = TextureWrapMode.Repeat;
    13.  
    14.         for (int x = 0; x < res; x++)
    15.             for (int y = 0; y < res; y++)
    16.                 for (int z = 0; z < res; z++)
    17.                 {
    18.                     Color c = new Color();
    19.                     var dir = _vectorField._vectorInfo[x + res * y + res * (x + y)].Direction;
    20.                     c.r = dir.x;
    21.                     c.g = dir.y;
    22.                     c.b = dir.z;
    23.  
    24.                     data[idx] = c;
    25.  
    26.                     idx++;
    27.                 }
    28.         _vectorFieldTexture.SetPixels(data);
    29.         _vectorFieldTexture.Apply();
    30.     }
    31.  
    Binding the texture...

    Code (CSharp):
    1.  
    2.         ComputeKernels.SetTexture(0, "_vectorField", _vectorFieldTexture);
    3.  
    And the shader code...

    Code (CSharp):
    1. Texture3D<float4> _vectorField : register(t0);
    2. SamplerState sampler_vectorField : register(s0)
    3. {
    4.     Filter = MIN_MAG_MIP_POINT;
    5.     AddressU = Clamp;
    6.     AddressV = Clamp;
    7. };
    8.  
    9.  
    10.  
    11. [numthreads(NumThreads, 1, 1)]
    12. void UpdateParticles(uint3 id : SV_DispatchThreadID)
    13. {
    14.    Particle p = Particles[id.x];
    15. // should sample the texture at mip level 0
    16.    p.velocity = _vectorField.SampleLevel(sampler_vectorField, float3(.2,.3, .1), 0);
    17.    Particles[id.x] = p;
    18. }
    The sampler is returning 0,0,0 no matter where I sample from. Not sure what I'm doing wrong...

    Any ideas greatly appreciated!
     
  2. natesb

    natesb

    Joined:
    Aug 26, 2017
    Posts:
    26
    Oops I meant to put this in the regular Graphics section. Mods plz delete this one...