1
+ Shader "Volume Render Texture"
2
+ {
3
+ SubShader
4
+ {
5
+ Cull Back
6
+ Pass
7
+ {
8
+ CGPROGRAM
9
+ #pragma vertex VSMain
10
+ #pragma fragment PSMain
11
+ #pragma target 5.0
12
+
13
+ sampler3D _Volume;
14
+
15
+ void VSMain (inout float4 vertex:POSITION , out float3 world:WORLD)
16
+ {
17
+ world = mul (unity_ObjectToWorld , vertex).xyz;
18
+ vertex = UnityObjectToClipPos (vertex);
19
+ }
20
+
21
+ float4 PSMain (float4 vertex:POSITION , float3 world:WORLD) : SV_Target
22
+ {
23
+ float3 ro = mul (unity_WorldToObject , float4 (world, 1 )).xyz;
24
+ float3 rd = normalize (mul ((float3x3 ) unity_WorldToObject , normalize (world - _WorldSpaceCameraPos )));
25
+ float3 tbot = (1.0 / rd) * (-0.5 - ro);
26
+ float3 ttop = (1.0 / rd) * (0.5 - ro);
27
+ float3 tmin = min (ttop, tbot);
28
+ float3 tmax = max (ttop, tbot);
29
+ float2 a = max (tmin.xx, tmin.yz);
30
+ float tnear = max (0.0 ,max (a.x, a.y));
31
+ float2 b = min (tmax.xx, tmax.yz);
32
+ float tfar = min (b.x, b.y);
33
+ float3 d = normalize ((ro + rd * tfar) - ro) * (abs (tfar - tnear) / 128.0 );
34
+ float4 t = float4 (0 , 0 , 0 , 0 );
35
+ [unroll]
36
+ for (int i = 0 ; i < 128 ; i++)
37
+ {
38
+ float v = tex3D (_Volume, ro+0.5 ).r;
39
+ float4 s = float4 (v, v, v, v);
40
+ s.a *= 0.5 ;
41
+ s.rgb *= s.a;
42
+ t = (1.0 - t.a) * s + t;
43
+ ro += d;
44
+ if (t.a > 0.99 ) break ;
45
+ }
46
+ return saturate (t);
47
+ }
48
+
49
+ ENDCG
50
+ }
51
+ }
52
+ }
0 commit comments