added raymarching demo for testing

This commit is contained in:
Piotrek
2021-04-14 15:42:31 +02:00
parent 70bb7a3e6e
commit 8094d8168d
10 changed files with 174 additions and 30 deletions
Binary file not shown.
Binary file not shown.
+91
View File
@@ -0,0 +1,91 @@
// Returns nearest distance to and object id from given point
vec2 map(in vec3 pos)
{
vec2 d1 = vec2(sdPlane(pos), 1);
vec2 d2 = vec2(sdSphere(pos - vec3(0.0, 2.0, 0.0)), 2);
vec2 d3 = vec2(sdSphere(pos - vec3(2.0, 1.2, 2.0)), 3);
return sdUnion(sdUnion(d1, d2), d3);
}
// Returns distance to and object id that has been intersected by ray
vec2 intersect(in vec3 ro, in vec3 rd)
{
float depth = NEAR;
for(int i=0;i<MAX_STEPS;i++)
{
// Travel through map
vec2 dist = map(ro + rd * depth);
if(dist.x < EPSILON)
{
// Hit something
return vec2(depth, dist.y);
}
// Move further
depth += dist.x;
}
// Nothing found
return vec2(0);
}
vec3 estimateNormal(vec3 p)
{
return normalize(vec3(
map(vec3(p.x + EPSILON, p.y, p.z)).x - map(vec3(p.x - EPSILON, p.y, p.z)).x,
map(vec3(p.x, p.y + EPSILON, p.z)).x - map(vec3(p.x, p.y - EPSILON, p.z)).x,
map(vec3(p.x, p.y, p.z + EPSILON)).x - map(vec3(p.x, p.y, p.z - EPSILON)).x
));
}
vec3 raymarch(in vec3 ro, in vec3 rd)
{
// Light dir
vec3 ld = normalize(vec3(-0.5, 2.0, -0.2));
vec2 result = intersect(ro, rd);
if(result.y > 0.0)
{
// Calculate intersection point, normal vector and reflection vector
vec3 pos = ro + rd * result.x;
vec3 nor = estimateNormal(pos);
vec3 ref = reflect(rd, nor);
// Light
vec3 light = vec3(1);
float dif = clamp( dot( nor, ld ), 0.0, 1.0 );
light += 2.20*dif*vec3(1.30,1.00,0.70);
vec3 hal = normalize( ld-rd );
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0);
spe *= dif;
spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,ld),0.0,1.0),5.0);
light += 5.00*spe*vec3(1.30,1.00,0.70);
//float diffuse = max(dot(norm, -ld), 0);
//color *= diffuse;
//vec3 ldr = normalize(reflect(-ld, norm));
//float specular = max(dot(ldr, rd), 0.0);
//specular = pow(specular, 3);
//color += vec3(0.2) * specular;
//float scatter = pow(1.0-dot(rd, -norm), 2);
// Calculate color
vec3 color = vec3(0);
if(result.y == 1) color = vec3(0.0, 0.1, 0.01);
else if(result.y == 2) color = vec3(0.5, 0.7, 0.05);
else if(result.y == 3) color = vec3(0.0, 0.2, 0.7);
// Return
color *= light;
return color;
}
else
{
return vec3(0);
}
}
+18 -3
View File
@@ -1,13 +1,28 @@
// shader.frag
#version 450
// Variables
layout(location=0) in vec2 vert_uv;
layout(location=0) out vec4 frag_color;
layout(location=0) out vec4 out_color;
layout(set=0, binding=0) uniform texture2D tex;
layout(set=0, binding=1) uniform sampler tex_smp;
// Defines
#define PI 3.141592
#define EPSILON 0.01
#define NEAR 0.01
#define MAX_STEPS 200
// Include
#include "shapes.cginc"
#include "raymarching.cginc"
void main()
{
frag_color = texture(sampler2D(tex, tex_smp), vert_uv);
// Calculate ray
vec3 ro = vec3(0.0, 2, -6.0); // Ray origin
vec3 rd = normalize(vec3(vert_uv - vec2(0.5, 0.5), 1.001)); // Ray direction
// Render
out_color = vec4(raymarch(ro, rd), 1);
}
+5 -2
View File
@@ -6,11 +6,14 @@ layout(location=0) out vec2 vert_uv;
layout(set=1, binding=0) uniform Uniforms
{
mat4 view_projection;
mat4 view; // world to camera
mat4 proj; // camera to screen
mat4 proj_inv; // screen to camera
vec3 cam_pos; // camera position
};
void main()
{
vert_uv = in_uv;
gl_Position = view_projection * vec4(in_position, 1.0);
gl_Position = vec4(in_position, 1.0);
}
+14
View File
@@ -0,0 +1,14 @@
float sdSphere(in vec3 pos)
{
return length(pos) - 1.0;
}
float sdPlane(in vec3 pos)
{
return pos.y;
}
vec2 sdUnion(vec2 a, vec2 b)
{
return (a.x < b.x) ? a : b;
}