Question

I am working on a Unity3D project which relies on a 3D texture momentarily.

The problem is, Unity only allows Pro users to make use of Texture3D. Hence I'm looking for an alternative to Texture3D, perhaps a one dimensional texture (although not natively available in Unity) that is interpreted as 3 dimensional in the shader (which uses the 3D texture).

Is there a way to do this whilst (preferably) keeping subpixel information?

(GLSL and Cg tags added because here lies the core of the problem)

Edit: The problem is addressed here as well: webgl glsl emulate texture3d However this is not yet finished and working properly.

Edit: For the time being I disregard proper subpixel information. So any help on converting a 2D texture to contain 3D information is appreciated!

Edit: I retracted my own answer as it isn't sufficient as of yet:

    float2 uvFromUvw( float3 uvw ) {
        float2 uv = float2(uvw.x, uvw.y / _VolumeTextureSize.z);
        uv.y += float(round(uvw.z * (_VolumeTextureSize.z - 1))) / _VolumeTextureSize.z;
        return uv;
    }

With initialization as Texture2D(volumeWidth, volumeHeight * volumeDepth).

Most of the time it works, but sometimes it shows wrong pixels, probably because of subpixel information it is picking up on. How can I fix this? Clamping the input doesn't work.

Was it helpful?

Solution

I'm using this for my 3D clouds if that helps:

float   SampleNoiseTexture( float3 _UVW, float _MipLevel )
{
    float2  WrappedUW = fmod( 16.0 * (1000.0 + _UVW.xz), 16.0 );    // UW wrapped in [0,16[

    float   IntW = floor( WrappedUW.y );                // Integer slice number
    float   dw = WrappedUW.y - IntW;                    // Remainder for intepolating between slices

    _UVW.x = (17.0 * IntW + WrappedUW.x + 0.25) * 0.00367647058823529411764705882353;   // divided by 17*16 = 272

    float4  Value = tex2D( _TexNoise3D, float4( _UVW.xy, 0.0, 0.0 ) );

    return lerp( Value.x, Value.y, dw );
}

The "3D texture" is packed as 16 slices of 17 pixels wide in a 272x16 texture, with the 17th column of each slice being a copy of the 1st column (wrap address mode)... Of course, no mip-mapping allowed with this technique.

OTHER TIPS

Here's the code I'm using to create the 3D texture if that's what bothering you:

static const    NOISE3D_TEXTURE_POT = 4;
static const    NOISE3D_TEXTURE_SIZE = 1 << NOISE3D_TEXTURE_POT;

// <summary>
// Create the "3D noise" texture
// To simulate 3D textures that are not available in Unity, I create a single long 2D slice of (17*16) x 16
// The width is 17*16 so that all 3D slices are packed into a single line, and I use 17 as a single slice width
//  because I pad the last pixel with the first column of the same slice so bilinear interpolation is correct.
// The texture contains 2 significant values in Red and Green :
//      Red is the noise value in the current W slice
//      Green is the noise value in the next W slice
//  Then, the actual 3D noise value is an interpolation of red and green based on the W remainder
// </summary>
protected NuajTexture2D Build3DNoise()
{
    // Build first noise mip level
    float[,,]   NoiseValues = new float[NOISE3D_TEXTURE_SIZE,NOISE3D_TEXTURE_SIZE,NOISE3D_TEXTURE_SIZE];
    for ( int W=0; W < NOISE3D_TEXTURE_SIZE; W++ )
        for ( int V=0; V < NOISE3D_TEXTURE_SIZE; V++ )
            for ( int U=0; U < NOISE3D_TEXTURE_SIZE; U++ )
                NoiseValues[U,V,W] = (float) SimpleRNG.GetUniform();

    // Build actual texture
    int MipLevel = 0;  // In my original code, I build several textures for several mips...
    int MipSize = NOISE3D_TEXTURE_SIZE >> MipLevel;
    int Width = MipSize*(MipSize+1);    // Pad with an additional column
    Color[] Content = new Color[MipSize*Width];

    // Build content
    for ( int W=0; W < MipSize; W++ )
    {
        int Offset = W * (MipSize+1);   // W Slice offset
        for ( int V=0; V < MipSize; V++ )
        {
            for ( int U=0; U <= MipSize; U++ )
            {
                Content[Offset+Width*V+U].r = NoiseValues[U & (MipSize-1),V,W];
                Content[Offset+Width*V+U].g = NoiseValues[U & (MipSize-1),V,(W+1) & (MipSize-1)];
            }
        }
    }

    // Create texture
    NuajTexture2D   Result = Help.CreateTexture( "Noise3D", Width, MipSize, TextureFormat.ARGB32, false, FilterMode.Bilinear, TextureWrapMode.Repeat );
    Result.SetPixels( Content, 0 );
    Result.Apply( false, true );

    return Result;
}

I followed Patapoms response and came to the following. However it's still off as it should be.

float getAlpha(float3 position)
{
    float2  WrappedUW = fmod( _Volume.xz * (1000.0 + position.xz), _Volume.xz );    // UW wrapped in [0,16[

    float   IntW = floor( WrappedUW.y );                // Integer slice number
    float   dw = WrappedUW.y - IntW;                    // Remainder for intepolating between slices

    position.x = ((_Volume.z + 1.0) * IntW + WrappedUW.x + 0.25) / ((_Volume.z + 1.0) * _Volume.x);   // divided by 17*16 = 272

    float4  Value = tex2Dlod( _VolumeTex, float4( position.xy, 0.0, 0.0 ) );

    return lerp( Value.x, Value.y, dw );
}


public int GetPixelId(int x, int y, int z) {
    return y * (volumeWidth + 1) * volumeDepth + z * (volumeWidth + 1) + x;
}

             // Code to set the pixelbuffer one pixel at a time starting from a clean slate
            pixelBuffer[GetPixelId(x, y, z)].r = color.r;

            if (z > 0)
                pixelBuffer[GetPixelId(x, y, z - 1)].g = color.r;

            if (z == volumeDepth - 1 || z == 0)
                pixelBuffer[GetPixelId(x, y, z)].g = color.r;

            if (x == 0) {
                pixelBuffer[GetPixelId(volumeWidth, y, z)].r = color.r;
                if (z > 0)
                    pixelBuffer[GetPixelId(volumeWidth, y, z - 1)].g = color.r;

                if (z == volumeDepth - 1 || z == 0)
                    pixelBuffer[GetPixelId(volumeWidth, y, z)].g = color.r;
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top