2

I'm having an issue creating a 3D texture and filling it with images. What I want to do is creating a Volumetric/3D texture with the RGBA values of my images, and put it in order in the texture. In order to do this, I create a Cube, and apply this shader and script:

Script

 using UnityEditor;
 using UnityEngine;

 public class Create3DTex : MonoBehaviour
 {

     private Texture3D tex;
     private Texture2D[] slices;
     private Texture2D tex2d;

     void Start()
     {
         int n = 91;
         slices = new Texture2D[n];
         for (int j = 0; j < n; j++)
         {
             tex2d = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Resources/Textures/texture" + j + ".png", typeof(Texture2D));
             SetTextureImporterFormat(tex2d, true);
             slices[j] = tex2d;
         }
         int size = 64;
         tex = new Texture3D(size, size, size, TextureFormat.ARGB32, false);
         var cols = new Color[size * size * size];
         float mul = 1.0f / (size - 1);
         int idx = 0;
         var c = new Color[size * size * size];

         var countOffset = (slices.Length - 1) / (float)size;
         var sliceCount = 0;
         var sliceCountFloat = 0f;
         for (int z = 0; z < size; ++z)
         {
             sliceCountFloat += countOffset;
             sliceCount = Mathf.FloorToInt(sliceCountFloat);
             for (int y = 0; y < size; ++y)
             {
                 for (int x = 0; x < size; ++x, ++idx)
                 {
                     cols[idx] = slices[sliceCount].GetPixelBilinear(x / (float)size, y / (float)size);
                 }
             }
         }
         tex.SetPixels(cols);
         tex.Apply();
         GetComponent<Renderer>().material.SetTexture("_Volume", tex);
     }

     public static void SetTextureImporterFormat(Texture2D texture, bool isReadable)
     {
         if (null == texture) return;

         string assetPath = AssetDatabase.GetAssetPath(texture);
         var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
         if (tImporter != null)
         {
             tImporter.textureType = TextureImporterType.Advanced;

             tImporter.isReadable = isReadable;

             AssetDatabase.ImportAsset(assetPath);
             AssetDatabase.Refresh();
         }
     }
 }

Shader

  Shader "DX11/Sample 3D Texture" {
     Properties {
         _Volume ("Texture", 3D) = "" {}
     }
     SubShader {
         Pass {

             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma exclude_renderers flash gles

             #include "UnityCG.cginc"

             struct vs_input {
                 float4 vertex : POSITION;
             };

             struct ps_input {
                 float4 pos : SV_POSITION;
                 float3 uv : TEXCOORD0;
             };


             ps_input vert (vs_input v)
             {
                 ps_input o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.vertex.xyz;
                 return o;
             }

             sampler3D _Volume;

             float4 frag (ps_input i) : COLOR
             {
                 return tex3D (_Volume, i.uv);
             }

             ENDCG

             }
         }

         Fallback "VertexLit"
     }

Its seems that it loads something, but I dont know how to make the cube transparent and the textures doesnt look correctly. It shows something like this. Any ideas? Thanks in advance. Result

Bitelchús
  • 65
  • 2
  • 6

1 Answers1

0

In order to visualize 3d data correctly, you have to create a 3d structure with your data in the shader, and then integrate the resulting "volume pixels" along the view line. To do this you have to perform a raycast. http://www.alanzucconi.com/2016/07/01/raymarching/