-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaymarchingShader.compute
More file actions
371 lines (299 loc) · 11.7 KB
/
RaymarchingShader.compute
File metadata and controls
371 lines (299 loc) · 11.7 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// unity specific setup as in https://github.com/SebLague/Ray-Marching
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it with cs.SetTexture
RWTexture2D<float4> Result;
// import scene parameters
float2 Resolution;
float4x4 CameraCoord_to_WorldCoord;
float4x4 Inverse_Camera_Projection_Matrix;
float3 backgroundColor;
float ambientLightIntensity;
// set fixed parameters
static const float maxDistance = 80;
static const float epsilon = 0.01;
static const int maxSteps = 150;
// import scene primitives
struct Prim{
int primType;
int combinationMode;
float smoothAmount;
float3 color;
float diffuse;
float specular;
int specularHardness;
float3 position;
float3 size;
float3 rotation;
};
StructuredBuffer<Prim> prims;
int primCount;
// import scene lights
struct Light{
float3 position;
float3 brightness;
};
StructuredBuffer<Light> lights;
int lightCount;
// create Ray object
struct Ray{
float3 origin;
float3 direction;
};
Ray CreateRay(float3 origin, float3 direction){
Ray ray;
ray.origin = origin;
ray.direction = direction;
return ray;
}
// create Ray from Camera based on uv coordinates
Ray CreateCameraRay(float2 uv){
// set ray origin by translating camera origin to world coordinates
float3 origin = mul(CameraCoord_to_WorldCoord, float4(0,0,0,1)).xyz;
// set ray direction from camera origin to uv coordinate
float3 direction = mul(Inverse_Camera_Projection_Matrix, float4(uv,0,1)).xyz;
// translate ray direction to world coordinates
direction = mul(CameraCoord_to_WorldCoord, float4(direction,0)).xyz;
// normalize direction vector
direction = normalize(direction);
return CreateRay(origin, direction);
}
float maxcomp(float3 v){
return max(max(v.x, v.y), v.z);
}
float3 rotateAndTranslate(float3 v, float3 rot, float3 trans){
rot = radians(rot);
float alpha = rot.x;
float beta = rot.y;
float gamma = rot.z;
float4x4 rotation = {cos(beta)*cos(gamma), cos(alpha)*sin(gamma)+sin(alpha)*sin(beta)*cos(gamma), sin(alpha)*sin(gamma)-cos(alpha)*sin(beta)*cos(gamma), 0,
-cos(beta)*sin(gamma), cos(alpha)*cos(gamma)-sin(alpha)*sin(beta)*sin(gamma), sin(alpha)*cos(gamma)+cos(alpha)*sin(beta)*sin(gamma), 0,
sin(beta), -sin(alpha)*cos(beta), cos(alpha)*cos(beta), 0,
0, 0, 0, 1 };
float4x4 translation = {1, 0, 0, -trans.x,
0, 1, 0, -trans.y,
0, 0, 1, -trans.z,
0, 0, 0, 1 };
float4 v4 = float4(v, 1);
float4 v_new = mul(rotation,mul(translation,v4));
return float3 (v_new.x/v_new.w, v_new.y/v_new.w, v_new.z/v_new.w);
}
// SDF for sphere
float GetDistanceSphere(Prim prim, float3 p){
return distance(p, prim.position)-prim.size.x;
}
// SDF for box from https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float GetDistanceBox(Prim box, float3 p){
float3 q = abs(p)-box.size;
float n = min(maxcomp(q),0);
return length(max(q,0))+n;
}
// SDF for torus from https://iquilezles.org/www/articles/distfunctions/distfunctions.htm
float GetDistanceTorus(Prim torus, float3 p){
float2 q = float2(length(p.xz)-torus.size.x,p.y);
return length(q)-torus.size.y;
}
// call matching SDF for Primitive
float GetDistance(Prim prim, float3 p){
if(prim.primType == 0){
return GetDistanceSphere(prim, p);
}
if(prim.primType == 1){
return p.y;
}
if(prim.primType == 2){
float3 p_t = rotateAndTranslate(p, prim.rotation, prim.position);
return GetDistanceBox(prim, p_t);
}
if(prim.primType == 3){
float3 p_t = rotateAndTranslate(p, prim.rotation, prim.position);
return GetDistanceTorus(prim, p_t);
}
// return maxDistance if no Primitive is found
return maxDistance;
}
// cubic smooth min function:
// from: https://iquilezles.org/www/articles/smin/smin.htm
float2 cubicSmoothMin(float dstA, float dstB, float k){
float h = max((k-abs(dstA-dstB)),0)/k;
float m = h*h*0.5;
return float2(min(dstA, dstB) - (k/6)*h*h*h, m);
}
// gets minimum distance from point to all primitives with respect to combination mode
// provides material information in second matrix row
float2x4 minDst(float3 p){
// Set Distance and Color if no Primitives can be found
float minDistance = maxDistance;
float3 pointColor = 1;
float pointDiffuse = 1;
float pointSpecular = 0;
float pointSpecH = 1;
// Evaluate for all Primitives in Scene
for(int i = 0; i<primCount; i++){
Prim prim = prims[i];
float primDistance = GetDistance(prim, p);
float3 primColor = prim.color;
float primDiffuse = prim.diffuse;
float primSpecular = prim.specular;
float primSpecH = prim.specularHardness;
if (prim.smoothAmount>0){
float2 smooth = cubicSmoothMin(minDistance, primDistance, prim.smoothAmount);
if(minDistance > primDistance){
pointColor = lerp(pointColor, primColor, 1-smooth.y);
pointDiffuse = lerp(pointDiffuse, primDiffuse, 1-smooth.y);
pointSpecular = lerp(pointSpecular, primSpecular, 1-smooth.y);
pointSpecH = lerp(pointSpecH, primSpecH, 1-smooth.y);
}else{
pointColor = lerp(pointColor, primColor, smooth.y);
pointDiffuse = lerp(pointDiffuse, primDiffuse, smooth.y);
pointSpecular = lerp(pointSpecular, primSpecular, smooth.y);
pointSpecH = lerp(pointSpecH, primSpecH, smooth.y);
}
minDistance = smooth.x;
}
// Subtract
else if(prim.combinationMode==1){
if(minDistance < -primDistance){
minDistance = -primDistance;
}
}
// Intersect
else if(prim.combinationMode==2){
if(minDistance < primDistance){
minDistance = primDistance;
}
}
// Union
else{
if(minDistance > primDistance){
minDistance = primDistance;
pointColor = primColor;
pointDiffuse = primDiffuse;
pointSpecular = primSpecular;
pointSpecH = primSpecH;
}
}
}
// return minimum distance and material info
return float2x4(pointColor, minDistance, pointDiffuse, pointSpecular, pointSpecH, 0);
}
float3 approximateNormal(float3 p){
return normalize(float3(
(minDst(float3(p.x + epsilon, p.y, p.z))[0][3] - minDst(float3(p.x - epsilon, p.y, p.z))[0][3]),
(minDst(float3(p.x, p.y + epsilon, p.z))[0][3] - minDst(float3(p.x, p.y - epsilon, p.z))[0][3]),
(minDst(float3(p.x, p.y, p.z + epsilon))[0][3] - minDst(float3(p.x, p.y, p.z - epsilon))[0][3])
));
}
float basicShadow(float3 p, float3 pNormal, float3 lightPos){
// create new Ray from Light to Point
float3 direction = p - lightPos;
direction = normalize(direction);
float distanceToLight = distance(p, lightPos);
Ray shadowRay = CreateRay(lightPos, direction);
float globalDistance = 0;
int maxSteps = 50;
int steps = 0;
while(globalDistance + epsilon*50 < distanceToLight && steps < maxSteps){
float4 evalSceneForPoint = minDst(shadowRay.origin)[0];
float dist = evalSceneForPoint.w;
steps ++;
// check if collision occured
if(dist < epsilon){
return 0;
}
// update new point position
shadowRay.origin += shadowRay.direction * dist;
// update current distance
globalDistance += dist;
}
return 1;
}
float softShadow(float3 p, float3 pNormal, float3 lightPos, float k){
// create new Ray from Light to Point
float3 direction = p - lightPos;
direction = normalize(direction);
float distanceToLight = distance(p, lightPos);
Ray shadowRay = CreateRay(lightPos, direction);
float globalDistance = 0;
float shadow = 1;
int maxSteps = 50;
int steps = 0;
while(globalDistance + epsilon*5 < distanceToLight && steps < maxSteps){
float4 evalSceneForPoint = minDst(shadowRay.origin)[0];
float dist = evalSceneForPoint.w;
steps++;
// get distance between Point to shade and current ray-end
float dstObjP = distanceToLight - globalDistance;
// get shadow intensity
shadow = min(shadow, k * dist/dstObjP);
// update new point position
shadowRay.origin += shadowRay.direction * dist;
// update current distance
globalDistance += dist;
}
return shadow;
}
float3 diffAndSpec(float3 p, float3 pNormal, float3 pColor, float3 lightPos, float distanceToLight, float3 brightness, float diffuse, float specular, float power){
float kd = diffuse;
float ks = specular;
// falloff light intensity
float3 lIn = brightness / pow(distanceToLight,2);
// vector from Point to Lighsource (normalized)
float3 l = lightPos - p;
l = normalize(l);
// vector from Point to Camera (normalized)
float3 v = mul(CameraCoord_to_WorldCoord, float4(0,0,0,1)).xyz - p;
v = normalize(v);
// halfway Vektor
float3 h =normalize(l+v);
// Blinn-Phong model for diff and spec
float3 lightDiffuse = lIn * kd * pColor * max(dot(l,pNormal),0);
float3 lightSpecular = lIn * ks * (pow(max(dot(h, pNormal),0), power));
return lightDiffuse + lightSpecular;
}
float3 phongLight(float3 p, float3 pNormal, float3 pColor, float diffuse, float specular, float power){
float3 pointLight = 0;
// add diff and spec and shadow of all lights
for(int i = 0; i<lightCount; i++){
Light light = lights[i];
float distanceToLight = distance(p, light.position);
float3 diffSpec = diffAndSpec(p, pNormal, pColor, light.position, distanceToLight, light.brightness, diffuse, specular, power);
float sShadow = softShadow(p, pNormal, light.position, 3);
// float sShadow = basicShadow(p, pNormal, light.position);
pointLight += diffSpec * sShadow;
}
pointLight += pColor * ambientLightIntensity;
return pointLight;
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID){
// track global distance and steps marched
float globalDistance = 0;
int steps = 0;
// create "uv-coordinates" with centered origin
float2 uv = id.xy / Resolution.xy *2 -1;
// create Ray from Canera to uv-coordinate
Ray ray = CreateCameraRay(uv);
Result[id.xy] = float4(backgroundColor, 1);
while(globalDistance <= maxDistance && steps < maxSteps){
steps++;
float4 evalSceneForPoint = minDst(ray.origin)[0];
float4 material = minDst(ray.origin)[1];
float dist = evalSceneForPoint.w;
// check if collision occured
if(dist < epsilon){
// set color at point
globalDistance += dist;
float3 hitPoint = ray.origin + ray.direction*dist;
float3 pointNormal = approximateNormal(hitPoint - ray.direction * epsilon);
float3 pColor = evalSceneForPoint.xyz;
float3 color = phongLight(hitPoint, pointNormal, pColor, material.x, material.y, material.z);
Result[id.xy] = float4(color, 1);
break;
}
// update new point position
ray.origin += ray.direction * dist;
// update current distance
globalDistance += dist;
}
}