-
Notifications
You must be signed in to change notification settings - Fork 175
/
Raymarching.compute
executable file
·246 lines (195 loc) · 6.78 KB
/
Raymarching.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#pragma kernel CSMain
Texture2D<float4> Source;
RWTexture2D<float4> Destination;
float4x4 _CameraToWorld;
float4x4 _CameraInverseProjection;
float3 _Light;
bool positionLight;
static const float maxDst = 80;
static const float epsilon = 0.001f;
static const float shadowBias = epsilon * 50;
struct Shape {
float3 position;
float3 size;
float3 colour;
int shapeType;
int operation;
float blendStrength;
int numChildren;
};
StructuredBuffer<Shape> shapes;
int numShapes;
struct Ray {
float3 origin;
float3 direction;
};
float SphereDistance(float3 eye, float3 centre, float radius) {
return distance(eye, centre) - radius;
}
float CubeDistance(float3 eye, float3 centre, float3 size) {
float3 o = abs(eye-centre) -size;
float ud = length(max(o,0));
float n = max(max(min(o.x,0),min(o.y,0)), min(o.z,0));
return ud+n;
}
// Following distance functions from http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float TorusDistance(float3 eye, float3 centre, float r1, float r2)
{
float2 q = float2(length((eye-centre).xz)-r1,eye.y-centre.y);
return length(q)-r2;
}
float PrismDistance(float3 eye, float3 centre, float2 h) {
float3 q = abs(eye-centre);
return max(q.z-h.y,max(q.x*0.866025+eye.y*0.5,-eye.y)-h.x*0.5);
}
float CylinderDistance(float3 eye, float3 centre, float2 h) {
float2 d = abs(float2(length((eye).xz), eye.y)) - h;
return length(max(d,0.0)) + max(min(d.x,0),min(d. y,0));
}
Ray CreateRay(float3 origin, float3 direction) {
Ray ray;
ray.origin = origin;
ray.direction = direction;
return ray;
}
Ray CreateCameraRay(float2 uv) {
float3 origin = mul(_CameraToWorld, float4(0,0,0,1)).xyz;
float3 direction = mul(_CameraInverseProjection, float4(uv,0,1)).xyz;
direction = mul(_CameraToWorld, float4(direction,0)).xyz;
direction = normalize(direction);
return CreateRay(origin,direction);
}
// polynomial smooth min (k = 0.1);
// from https://www.iquilezles.org/www/articles/smin/smin.htm
float4 Blend( float a, float b, float3 colA, float3 colB, float k )
{
float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 );
float blendDst = lerp( b, a, h ) - k*h*(1.0-h);
float3 blendCol = lerp(colB,colA,h);
return float4(blendCol, blendDst);
}
float4 Combine(float dstA, float dstB, float3 colourA, float3 colourB, int operation, float blendStrength) {
float dst = dstA;
float3 colour = colourA;
if (operation == 0) {
if (dstB < dstA) {
dst = dstB;
colour = colourB;
}
}
// Blend
else if (operation == 1) {
float4 blend = Blend(dstA,dstB,colourA,colourB, blendStrength);
dst = blend.w;
colour = blend.xyz;
}
// Cut
else if (operation == 2) {
// max(a,-b)
if (-dstB > dst) {
dst = -dstB;
colour = colourB;
}
}
// Mask
else if (operation == 3) {
// max(a,b)
if (dstB > dst) {
dst = dstB;
colour = colourB;
}
}
return float4(colour,dst);
}
float GetShapeDistance(Shape shape, float3 eye) {
if (shape.shapeType == 0) {
return SphereDistance(eye, shape.position, shape.size.x);
}
else if (shape.shapeType == 1) {
return CubeDistance(eye, shape.position, shape.size);
}
else if (shape.shapeType == 2) {
return TorusDistance(eye, shape.position, shape.size.x, shape.size.y);
}
return maxDst;
}
float4 SceneInfo(float3 eye) {
float globalDst = maxDst;
float3 globalColour = 1;
for (int i = 0; i < numShapes; i ++) {
Shape shape = shapes[i];
int numChildren = shape.numChildren;
float localDst = GetShapeDistance(shape,eye);
float3 localColour = shape.colour;
for (int j = 0; j < numChildren; j ++) {
Shape childShape = shapes[i+j+1];
float childDst = GetShapeDistance(childShape,eye);
float4 combined = Combine(localDst, childDst, localColour, childShape.colour, childShape.operation, childShape.blendStrength);
localColour = combined.xyz;
localDst = combined.w;
}
i+=numChildren; // skip over children in outer loop
float4 globalCombined = Combine(globalDst, localDst, globalColour, localColour, shape.operation, shape.blendStrength);
globalColour = globalCombined.xyz;
globalDst = globalCombined.w;
}
return float4(globalColour, globalDst);
}
float3 EstimateNormal(float3 p) {
float x = SceneInfo(float3(p.x+epsilon,p.y,p.z)).w - SceneInfo(float3(p.x-epsilon,p.y,p.z)).w;
float y = SceneInfo(float3(p.x,p.y+epsilon,p.z)).w - SceneInfo(float3(p.x,p.y-epsilon,p.z)).w;
float z = SceneInfo(float3(p.x,p.y,p.z+epsilon)).w - SceneInfo(float3(p.x,p.y,p.z-epsilon)).w;
return normalize(float3(x,y,z));
}
float CalculateShadow(Ray ray, float dstToShadePoint) {
float rayDst = 0;
int marchSteps = 0;
float shadowIntensity = .2;
float brightness = 1;
while (rayDst < dstToShadePoint) {
marchSteps ++;
float4 sceneInfo = SceneInfo(ray.origin);
float dst = sceneInfo.w;
if (dst <= epsilon) {
return shadowIntensity;
}
brightness = min(brightness,dst*200);
ray.origin += ray.direction * dst;
rayDst += dst;
}
return shadowIntensity + (1-shadowIntensity) * brightness;
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
uint width,height;
Destination.GetDimensions(width, height);
Destination[id.xy] = Source[id.xy];
float2 uv = id.xy / float2(width,height) * 2 - 1;
float rayDst = 0;
Ray ray = CreateCameraRay(uv);
int marchSteps = 0;
while (rayDst < maxDst) {
marchSteps ++;
float4 sceneInfo = SceneInfo(ray.origin);
float dst = sceneInfo.w;
if (dst <= epsilon) {
float3 pointOnSurface = ray.origin + ray.direction * dst;
float3 normal = EstimateNormal(pointOnSurface - ray.direction * epsilon);
float3 lightDir = (positionLight)?normalize(_Light-ray.origin):-_Light;
float lighting = saturate(saturate(dot(normal,lightDir)));
float3 col = sceneInfo.xyz;
// Shadow
float3 offsetPos = pointOnSurface + normal * shadowBias;
float3 dirToLight = (positionLight)?normalize(_Light- offsetPos):-_Light;
ray.origin = offsetPos;
ray.direction = dirToLight;
float dstToLight = (positionLight)?distance(offsetPos,_Light):maxDst;
float shadow = CalculateShadow(ray, dstToLight);
Destination[id.xy] = float4(col * lighting * shadow ,1);
break;
}
ray.origin += ray.direction * dst;
rayDst += dst;
}
}