Skip to content

Commit 64e2259

Browse files
Another signed distance bound.
1 parent 64041f0 commit 64e2259

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

pillar.shader

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//https://github.com/przemyslawzaworski/Unity3D-CG-programming
2+
3+
Shader "Pillar"
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 vertex : SV_POSITION;
17+
float2 uv : TEXCOORD0;
18+
};
19+
20+
float3 mod(float3 x, float3 y)
21+
{
22+
return x - y * floor(x/y);
23+
}
24+
25+
void radial(inout float2 p, float repetitions)
26+
{
27+
float angle = 6.2831853071/repetitions;
28+
float a = atan2(p.y, p.x) + angle*0.5;
29+
float r = length(p);
30+
float c = floor(a/angle);
31+
a = mod(a,angle)-angle*0.5;
32+
p = float2(cos(a),sin(a))*r;
33+
}
34+
35+
float cylinder( float3 p, float3 c,float2 h )
36+
{
37+
p=p-c;
38+
float2 d = abs(float2(length(p.xz),p.y)) - h;
39+
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
40+
}
41+
42+
float map (float3 p)
43+
{
44+
float d = cylinder(p,float3(0.0,0.0,0.0),float2(0.5,1.0));
45+
radial(p.xz,16.0);
46+
return max(d,-cylinder(p,float3(0.87,0.0,0.0),float2(0.4,1.1)) );
47+
}
48+
49+
float4 lighting (float3 p, float e)
50+
{
51+
float4 a = float4 (0.1,0.1,0.1,1.0); //ambient light color
52+
float4 b = float4(0.2,0.5,0.7,1.0); //directional light color
53+
float3 l = normalize(float3(5,4,-12)); //directional light direction
54+
float c = (map(p+l*e)-map(p))/e; //directional derivative equation
55+
return saturate(c)*b+a; //return diffuse color
56+
}
57+
58+
float4 raymarch (float3 ro, float3 rd)
59+
{
60+
for (int i=0; i<128; i++)
61+
{
62+
float t = map(ro);
63+
if (t < 0.001) return lighting(ro,0.001);
64+
ro+=t*rd;
65+
}
66+
return float4(0.7,0.7,0,1)*length(rd.xy);
67+
}
68+
69+
structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
70+
{
71+
structure vs;
72+
vs.vertex = UnityObjectToClipPos (vertex);
73+
vs.uv = uv;
74+
return vs;
75+
}
76+
77+
float4 pixel_shader (structure ps) : COLOR
78+
{
79+
float2 iResolution = float2(1024,1024);
80+
float2 FragCoord = ps.uv*iResolution;
81+
float2 uv = (2.0*FragCoord-iResolution)/iResolution.y;
82+
float3 ro = float3(0,0,-3);
83+
float3 rd = normalize(float3(uv,2.0));
84+
return raymarch(ro,rd);
85+
}
86+
ENDCG
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)