Skip to content

Commit 9838e04

Browse files
Environment lighting.
1 parent 1544866 commit 9838e04

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

raymarching_procedural_cubemap.shader

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//see also: www.shadertoy.com
2+
3+
Shader "Raymarching Procedural Cubemap"
4+
{
5+
Subshader
6+
{
7+
Pass
8+
{
9+
CGPROGRAM
10+
#pragma vertex vertex_shader
11+
#pragma fragment pixel_shader
12+
#pragma target 3.0
13+
14+
struct structure
15+
{
16+
float4 screen_vertex : SV_POSITION;
17+
float3 world_vertex : TEXCOORD1;
18+
};
19+
20+
float4 hexagon (float3 p, float3 c, float2 h)
21+
{
22+
float3 q = abs(p+c);
23+
float3 m = float3 (1.0,0.0,0.0);
24+
return float4 (m,max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x));
25+
}
26+
27+
float4 sphere (float3 p,float3 c,float r)
28+
{
29+
float3 m = float3 (0.0,0.0,1.0);
30+
return float4 (m,length (p-c)-r);
31+
}
32+
33+
float4 ring (float3 p, float3 c, float2 t)
34+
{
35+
float3 m = float3(1.0,0.64,0.0);
36+
float2 q = float2(sqrt((p.x-c.x)*(p.x-c.x)+(p.z-c.z)*(p.z-c.z))-t.x,p.y-c.y);
37+
q=q*q; q=q*q; q=q*q;
38+
return float4(m,pow( q.x + q.y, 0.125 )-t.y);
39+
}
40+
41+
float4 map (float3 p)
42+
{
43+
float4 a = hexagon(p,float3(3,0,0),float2(1,1));
44+
float4 b = sphere(p,float3(3,0,0),1.0);
45+
float4 c = ring(p,float3(0,3,0),float2(1,0.25));
46+
float4 solid = lerp (a,b,step(b.w,a.w));
47+
return lerp (c, solid, step(solid.w,c.w ));
48+
}
49+
50+
float3 set_normal (float3 p)
51+
{
52+
float3 x = float3 (0.01,0.00,0.00);
53+
float3 y = float3 (0.00,0.01,0.00);
54+
float3 z = float3 (0.00,0.00,0.01);
55+
return normalize(float3(map(p+x).w-map(p-x).w,map(p+y).w-map(p-y).w,map(p+z).w-map(p-z).w));
56+
}
57+
58+
float3 cubemap(float3 dir)
59+
{
60+
float3 value = cos(dir*float3(1, 9, 2)+float3(2, 3, 1))*0.5+0.5;
61+
value = (value * float3(0.8, 0.3, 0.7)) + float3(0.2,0.2,0.2);
62+
value *= dir.y*0.5+0.5;
63+
value += exp(6.0*dir.y-2.0)*0.05;
64+
value = pow(value, float3(1.0/2.2,1.0/2.2,1.0/2.2));
65+
return value;
66+
}
67+
68+
float3 lighting (float3 p, float3 rd)
69+
{
70+
float3 ReflectionDirection=reflect(rd,set_normal(p));
71+
float3 LightDirection = ReflectionDirection;
72+
float3 LightColor = cubemap(ReflectionDirection);
73+
float3 NormalDirection = set_normal(p);
74+
return (max(dot(LightDirection, NormalDirection),0.0) * LightColor);
75+
}
76+
77+
float4 raymarch (float3 ro, float3 rd)
78+
{
79+
for (int i=0; i<128; i++)
80+
{
81+
float t = map(ro).w;
82+
float3 material = map(ro).xyz;
83+
if (t < 0.001)
84+
return float4 (lighting(ro,rd)*material,1.0);
85+
else
86+
ro+=t*rd;
87+
}
88+
return float4(cubemap(rd),1.0);
89+
}
90+
91+
structure vertex_shader (float4 vertex : POSITION)
92+
{
93+
structure vs;
94+
vs.screen_vertex = UnityObjectToClipPos (vertex);
95+
vs.world_vertex = mul (unity_ObjectToWorld, vertex);
96+
return vs;
97+
}
98+
99+
float4 pixel_shader (structure ps ) : SV_TARGET
100+
{
101+
float3 ro = ps.world_vertex;
102+
float3 rd = normalize(ps.world_vertex - _WorldSpaceCameraPos.xyz);
103+
return raymarch (ro,rd);
104+
}
105+
106+
ENDCG
107+
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)