2016-04-04 128 views
1

我在創建3D紋理並填充圖像時出現問題。我想要做的是用我的圖像的RGBA值創建一個Volumetric/3D紋理,並將其按順序放置在紋理中。爲了做到這一點,我創建了一個立方體,並應用這種着色器和腳本:Unity 3D紋理 - 加載2D紋理陣列

腳本

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 "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" 
    } 

其看來,它加載的東西,但我不知道如何使立方體透明,紋理看起來不正確。它顯示了這樣的事情。有任何想法嗎?提前致謝。 Result

回答

0

爲了正確顯示三維數據,您必須在着色器中用您的數據創建一個3d結構,然後沿視圖線集成結果「體積像素」。 要做到這一點,你必須執行一個raycast。 http://www.alanzucconi.com/2016/07/01/raymarching/