Skip to content

Commit d24c457

Browse files
VolumeRenderTexture example
1 parent 4f2a451 commit d24c457

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma kernel CSMain
2+
3+
Texture3D<float4> VolumeReader;
4+
RWTexture3D<float4> VolumeWriter;
5+
SamplerState _PointClamp;
6+
float3 Center;
7+
8+
float circle(float3 p, float3 c, float r)
9+
{
10+
return step(length(p-c)-r,0.0);
11+
}
12+
13+
[numthreads(8,8,8)]
14+
void CSMain (uint3 id : SV_DispatchThreadID)
15+
{
16+
float3 uv = float3((float)id.x/256.0,(float)id.y/256.0,(float)id.z/256.0);
17+
float k = circle(uv,Center,0.03) + VolumeReader.SampleLevel(_PointClamp,uv,0);
18+
VolumeWriter[id] = float4(k,k,k,k);
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)