Skip to content

Commit 598153f

Browse files
Basic volume texture sphere tracer.
1 parent ef57efd commit 598153f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Diff for: volume_texture.shader

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Add quad to Main Camera, then add volume_texture.cs to quad and material with shader (both to script and Mesh Renderer).
2+
//Set quad position to (0,0,0.4). Play.
3+
4+
Shader "Volume Texture"
5+
{
6+
Properties
7+
{
8+
[HideInInspector]
9+
volume ("Texture 3D", 3D) = "black" {}
10+
}
11+
Subshader
12+
{
13+
Pass
14+
{
15+
CGPROGRAM
16+
#pragma vertex vertex_shader
17+
#pragma fragment pixel_shader
18+
#pragma target 3.0
19+
20+
sampler3D volume;
21+
22+
struct structure
23+
{
24+
float4 screen_vertex : SV_POSITION;
25+
float3 world_vertex : TEXCOORD0;
26+
};
27+
28+
structure vertex_shader (float4 vertex:POSITION)
29+
{
30+
structure vs;
31+
vs.screen_vertex = UnityObjectToClipPos (vertex);
32+
vs.world_vertex = mul (unity_ObjectToWorld, vertex);
33+
return vs;
34+
}
35+
36+
float4 pixel_shader (structure ps) : SV_TARGET
37+
{
38+
float3 ro = ps.world_vertex;
39+
float3 rd = normalize(ps.world_vertex - _WorldSpaceCameraPos.xyz);
40+
float4 s = float4(0.0,0.0,0.0,0.0);
41+
for (float t = 1.0; t < 32.0; t++)
42+
{
43+
ro+=rd*t;
44+
s+=tex3Dlod(volume,float4(ro*.002,0))*.06;
45+
if (s.w >= 1.0) break;
46+
}
47+
return lerp(float4(0.0,0.0,0.0,0.0),s,s.w);
48+
}
49+
ENDCG
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)