Skip to content

Commit bcd67db

Browse files
Solution for faster rendering.
1 parent 4c1776d commit bcd67db

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

directional_derivative.shader

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//Lighting is computed without normal vectors, for faster rendering.
2+
//Reference: http://iquilezles.org/www/articles/derivative/derivative.htm
3+
4+
Shader "Directional derivative"
5+
{
6+
Subshader
7+
{
8+
Pass
9+
{
10+
CGPROGRAM
11+
#pragma vertex vertex_shader
12+
#pragma fragment pixel_shader
13+
#pragma target 3.0
14+
15+
struct type
16+
{
17+
float4 vertex : SV_POSITION;
18+
float2 uv : TEXCOORD0;
19+
};
20+
21+
float sphere (float3 p, float3 c, float r)
22+
{
23+
return length(p-c)-r;
24+
}
25+
26+
float map (float3 p)
27+
{
28+
return sphere(p,float3(0,0,0),1.0);
29+
}
30+
31+
float4 lighting (float3 p, float e)
32+
{
33+
float4 a = float4 (0.1,0.1,0.1,1.0); //ambient light color
34+
float4 b = float4(1.0,1.0,0.0,1.0); //directional light color
35+
float3 l = normalize(float3(6,15,-7)); //directional light direction
36+
float c = (map(p+l*e)-map(p))/e; //directional derivative equation
37+
return saturate(c)*b+a; //return diffuse color
38+
}
39+
40+
float4 raymarch (float3 ro, float3 rd)
41+
{
42+
for (int i=0; i<128; i++)
43+
{
44+
float t = map(ro);
45+
if (t < 0.001) return lighting(ro,0.001);
46+
ro+=t*rd;
47+
}
48+
return 0;
49+
}
50+
51+
type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
52+
{
53+
type vs;
54+
vs.vertex = UnityObjectToClipPos (vertex);
55+
vs.uv = uv;
56+
return vs;
57+
}
58+
59+
float4 pixel_shader (type ps) : COLOR
60+
{
61+
float2 resolution = float2(1024,1024);
62+
float2 fragCoord = ps.uv*resolution;
63+
float2 uv = (2.0*fragCoord-resolution)/resolution.y;
64+
float3 worldPosition = float3(0,0,-10);
65+
float3 viewDirection = normalize(float3(uv,2.0));
66+
return raymarch(worldPosition,viewDirection);
67+
}
68+
ENDCG
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)