# Texture2D to Texture3D

I would like to know how can I create a Texture3D from a Texture2D.

I’ve found some good examples : Unity 4 - 3D Textures (Volumes) or Unity - 3D Textures or Color Correction Lookup Texture

``````int     dim = tex2D.height;
Color[] c2D = tex2D.GetPixels();
Color[] c3D = new Color[c2D.Length];
for (int x = 0; x < dim; ++x)
{
for (int y = 0; y < dim; ++y)
{
for (int z = 0; z < dim; ++z)
{
int y_ = dim - y - 1;
c3D[x + (y * dim) + (z * dim * dim)] = c2D[z * dim + x + y_ * dim * dim];
}
}
}
``````

But this only works when you have

``````Texture2D.height= Mathf.FloorToInt(Mathf.Sqrt(Texture2D.width))
``````

or if

``````Depth = Width = Height
``````

How can I extract the values when the depth is not equal to the width or the height ?
It seems simple but I am missing something…

Thank you very much.

Are you asking how 2D and 3D arrays convert back and forth to linear arrays? Common programming topic, so should be able to find non-Unity references, maybe some pictures.

Imagine your texture is 5 wide, by 12 up and down. Number row one with 0-4, then number row two with 5-9. The last row will be 55-59. The next higher level (z=1) will be the same but start at 60. So each time y increases, add 5 (dim1.) Each time z increases, add 60 (dim1*dim2.)

So, to find the linear position in a “everything laid out in a line” array, use `z*60+y*5+y`. That’s all the code above is doing, I think.

The sample code uses dim for height and width, assuming they will be the same. Replace some dims with width, and others with height. If you get confused, go back to the sketch with numbers, and plug in some sample coords.

Or, maybe even easier way to think of it: suppose the base (width by height) is 90. Each level above that is +90, +180, +270 … . So, forget x,y,z. Just go through pixels 0,1,2… . To find the pixel below you, just use `index%90`.

BTW: Does that just copy the colors all the way up? I think if you just use the 2D array, it might automatically do that anyway.

Thanks to @NicoSchertler for his great answer.

You can split the texture as follows:

``````//Iterate the result
for(int z = 0; z < depth; ++z)
for(int y = 0; y < height; ++y)
for(int x = 0; x < width; ++x)
c3D[x + y * width + z * width * height]
= c2D[x + y * width * depth + z * width]
``````

You can get to this index formula as follows:

• Advancing by 1 in the x-direction
results in an increment by `1` (just
the next pixel).
• Advancing by 1 in
the y-direction results in an
increment by `depth * width` (skip 4
images with the according width).
• Advancing by 1 in the z-direction
results in an increment by `width`
(skip one image row).

Or if you prefer the other direction:

``````//Iterate the original image
for(int y = 0; y < height; ++y)
for(int x = 0; x < width * depth; ++x)
c3D[(x % width) + y * width + (x / width) * width * height] = c2D[x + y * width * depth];
``````