Created
February 3, 2020 17:34
-
-
Save matyklug18/5534d665a7d8a529352800076c7973da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 460 core | |
float INF = 1.0 / 0.0; | |
float sdSphere(vec3 p, float s, vec3 off) | |
{ | |
return length(p+off)-s; | |
} | |
struct RayHit { | |
vec3 point; | |
vec4 color; | |
bool didHit; | |
}; | |
struct Camera { | |
vec3 origin; | |
vec3 forward; | |
vec3 up; | |
vec2 fov; | |
float near; | |
}; | |
uniform sampler3D voxs; | |
uniform vec2 iRes; | |
uniform float iTime; | |
out vec4 outColor; | |
bool isBigger(vec3 a, vec3 b) { | |
return a.x > b.x && a.y > b.y && a.z > b.z; | |
} | |
RayHit trace(vec3 point, vec3 dir, float step, int STEPS, vec3 offset, float scale, float len) { | |
vec3 rayPos = point; | |
vec4 col = vec4(0); | |
RayHit hit; | |
hit.didHit = false; | |
for(int i = 0; i < STEPS*1.5; i++) { | |
vec4 color = texture(voxs, (rayPos-offset)/scale); | |
if(length(rayPos-point) > len) | |
break; | |
if(color.a > 0.0) { | |
col = color.rgba; | |
hit.didHit = true; | |
break; | |
} | |
rayPos += dir * step; | |
} | |
hit.color = col; | |
hit.point = rayPos; | |
return hit; | |
} | |
void main() | |
{ | |
Camera camera; | |
camera.origin = vec3(0,0,-11); | |
camera.forward = vec3(0,0,1); | |
camera.up = vec3(0,1,0); | |
camera.fov = vec2(100, 2.0f*atan(tan(100.*0.5f)/(iRes.x/iRes.y))); | |
camera.near = 10.; | |
camera.forward = normalize(vec3(camera.forward.x*cos((sin(iTime)/180)) + camera.forward.z*sin((sin(iTime)/180)), camera.forward.y, camera.forward.y*sin((sin(iTime)/180)) + camera.forward.z*cos((sin(iTime)/180)))); | |
vec3 lamp = vec3(camera.origin); | |
float screen_world_height = tan(camera.fov.y / 2.) * camera.near * 2.; | |
float screen_world_width = tan(camera.fov.x / 2.) * camera.near * 2.; | |
vec3 camera_right = normalize(cross(camera.forward, camera.up)); | |
vec3 screen_center_world_pos = camera.origin + camera.forward * camera.near; | |
vec3 screen_left_world_pos = screen_center_world_pos + -camera_right * (screen_world_width / 2.f); | |
vec3 screen_right_world_pos = screen_center_world_pos + camera_right * (screen_world_width / 2.f); | |
vec3 screen_down_world_pos = screen_center_world_pos + -camera.up * (screen_world_height / 2.f); | |
vec3 screen_up_world_pos = screen_center_world_pos + camera.up * (screen_world_height / 2.f); | |
vec2 uv = vec2(gl_FragCoord.xy) / vec2(iRes); | |
vec3 projected_point_world_pos = | |
screen_center_world_pos | |
+ mix(screen_left_world_pos, screen_right_world_pos, uv.x) | |
+ mix(screen_down_world_pos, screen_up_world_pos, uv.y); | |
vec3 ray_world_direction = normalize(projected_point_world_pos | |
- camera.origin); | |
vec3 rayWorldPos = projected_point_world_pos; | |
vec4 col = vec4(0); | |
int STEPS = 500; | |
int SHADOW_STEPS = 500; | |
float scale = 10; | |
vec3 offset = vec3(-scale/2, -scale/2, 25); | |
float STEP_SIZE = 0.1; | |
bool flag = false; | |
RayHit surfHit; | |
surfHit = trace(rayWorldPos, ray_world_direction, STEP_SIZE, STEPS, offset, scale, INF); | |
if(surfHit.didHit) { | |
RayHit shadHit; | |
vec3 tPoint = surfHit.point-normalize(rayWorldPos - surfHit.point)*(STEP_SIZE); | |
shadHit = trace(tPoint, normalize(lamp - tPoint), STEP_SIZE, SHADOW_STEPS, offset, scale, length(lamp - tPoint)); | |
if(shadHit.didHit) | |
col = vec4(0); | |
else | |
col = surfHit.color; | |
} | |
outColor = col; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment