forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraymarching_direct_compute.compute
51 lines (44 loc) · 1.24 KB
/
raymarching_direct_compute.compute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma kernel CSMain
RWTexture2D<float4> render_texture;
float sphere (float3 p,float3 c,float r)
{
return length (p-c)-r;
}
float map (float3 p)
{
return sphere (p,float3(0.0,0.0,0.0),1.0);
}
float3 set_normal (float3 p)
{
float3 x = float3 (0.01,0.00,0.00);
float3 y = float3 (0.00,0.01,0.00);
float3 z = float3 (0.00,0.00,0.01);
return normalize(float3(map(p+x)-map(p-x), map(p+y)-map(p-y), map(p+z)-map(p-z)));
}
float3 lighting ( float3 p)
{
float3 AmbientLight = float3 (0.1,0.1,0.1);
float3 LightDirection = normalize(float3(4.0,10.0,-10.0));
float3 LightColor = float3 (1.0,1.0,1.0);
float3 NormalDirection = set_normal(p);
return max ( dot(LightDirection, NormalDirection),0.0) * LightColor + AmbientLight;
}
float4 raymarch (float3 ro,float3 rd)
{
for (int i=0;i<128;i++)
{
float t = map(ro);
if (t<0.01) return float4(lighting(ro),1.0); else ro+=t*rd;
}
return float4(0.0,0.0,0.0,1.0);
}
[numthreads(8,8,1)]
void CSMain (uint2 id : SV_DispatchThreadID)
{
float2 resolution = float2 (1024,1024);
float2 coordinates = float2 (id.x,id.y);
float2 p = (2.0*coordinates.xy-resolution.xy)/resolution.y;
float3 ro = float3 (0.0,0.0,-10.0);
float3 rd = normalize( float3(p.xy,2.0));
render_texture[id] = raymarch( ro, rd );
}