Skip to content

Commit bdd8109

Browse files
committed
Cleaned up SliceRenderingShader.shader. Also added manual clamping of data texture coordinates (since Unity doens't seem to support clamp to custom border value). This should fix artifacts when moving the slicing plane object.
1 parent b1a8461 commit bdd8109

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

Assets/Shaders/SliceRenderingShader.shader

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,40 @@ Shader "VolumeRendering/SliceRenderingShader"
3636

3737
sampler3D _DataTex;
3838
sampler2D _TFTex;
39+
// Parent's inverse transform (used to convert from world space to volume space)
3940
uniform float4x4 _parentInverseMat;
41+
// Plane transform
4042
uniform float4x4 _planeMat;
4143

4244
v2f vert (appdata v)
4345
{
4446
v2f o;
45-
float3 vert = float3(-v.uv.x, 0.0f, -v.uv.y) + float3(0.5f, 0.0f, 0.5f);
46-
vert = mul(_planeMat, float4(vert, 1.0f));
47-
//o.vertex = mul(UNITY_MATRIX_VP, float4(vert, 1.0f));
48-
o.vertex = UnityObjectToClipPos(v.vertex);;
47+
o.vertex = UnityObjectToClipPos(v.vertex);
48+
// Calculate plane vertex world position.
49+
float3 vert = mul(_planeMat, float4(0.5f -v.uv.x, 0.0f, 0.5f -v.uv.y, 1.0f));
50+
// Convert from world space to volume space.
4951
o.relVert = mul(_parentInverseMat, float4(vert, 1.0f));
5052
o.uv = v.uv;
5153
return o;
5254
}
5355

5456
fixed4 frag (v2f i) : SV_Target
5557
{
56-
float3 dataCoord = i.relVert +float3(0.5f, 0.5f, 0.5f);
57-
float dataVal = tex3D(_DataTex, dataCoord);
58-
float4 col = tex2D(_TFTex, float2(dataVal, 0.0f));
59-
col.a = 1.0f;
60-
61-
return col;
58+
float3 dataCoord = i.relVert + float3(0.5f, 0.5f, 0.5f);
59+
// If the current fragment is outside the volume, simply colour it black.
60+
// Note: Unity does not seem to have support for clamping texture coordinates to a border value, so we have to do this manually
61+
if (dataCoord.x > 1.0f || dataCoord.y > 1.0f || dataCoord.z > 1.0f || dataCoord.x < 0.0f || dataCoord.y < 0.0f || dataCoord.z < 0.0f)
62+
{
63+
return float4(0.0f, 0.0f, 0.0f, 1.0f);
64+
}
65+
else
66+
{
67+
// Sample the volume texture.
68+
float dataVal = tex3D(_DataTex, dataCoord);
69+
float4 col = tex2D(_TFTex, float2(dataVal, 0.0f));
70+
col.a = 1.0f;
71+
return col;
72+
}
6273
}
6374
ENDCG
6475
}

0 commit comments

Comments
 (0)