首先需要根据医院提供的数据生成Texture3D,然后在shader中使用内置函数tex3D进行3D纹理采样,最后通过lightCT封装的算法过滤颜色通道,只取单个通道。shader代码如下,效果如图。
Shader "Custom/CT"
{
Properties
{
_Volume ("Texture3D", 3D) = "" {}
_WindowWidth("WindowWidth",float) = 2000
_WindowPos("NarrowPos",float) = 1000
maxDataValue("DataMax",float) = 2000
minDataValue("DataMin",float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue" = "Geometry" }
LOD 100
//ZWrite Off
//ZTest Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile _ WHOLE
sampler3D _Volume;
float minDataValue;
float maxDataValue;
float _WindowWidth;
float _WindowPos;
float lightCT(float a){
float minV = saturate((_WindowPos - (_WindowWidth / 2) - minDataValue ) / (maxDataValue - minDataValue));
float maxV = saturate((_WindowPos + (_WindowWidth / 2) - minDataValue ) / (maxDataValue - minDataValue));
return saturate((a - minV)/(maxV - minV));
}
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD0;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPos = v.vertex;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 col = float4(0, 0, 0, 0);
float3 lpos =i.worldPos;
float3 pos =float3(lpos.x, lpos.y , lpos.z+0.5);//做成屏幕特效的坐标调整
col = tex3D(_Volume, float4(pos, 0));//采样3D纹理
col.r = lightCT(col.r); //CT图像生成算法过滤,此处取任意一个通道即可
return float4(col.rrr,1);
}
ENDCG
}
}
}