This commit is contained in:
Mikulas Florek 2020-10-10 21:20:21 +02:00
parent 3a924964ae
commit 26e22da8f1
3 changed files with 0 additions and 309 deletions

View file

@ -1,134 +0,0 @@
include "pipelines/common.glsl"
------------------
vertex_shader [[
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 i_rot;
layout(location = 2) in vec3 i_pos;
layout(location = 3) in float i_radius;
layout(location = 4) in float i_attn;
layout(location = 5) in vec3 i_color;
layout(location = 6) in vec3 i_dir;
layout(location = 7) in float i_fov;
layout(location = 0) out float v_radius;
layout(location = 1) out vec3 v_center;
layout(location = 2) out vec4 v_rot;
layout(location = 3) out float v_attn;
layout(location = 4) out vec3 v_color;
layout(location = 5) out vec3 v_dir;
layout(location = 6) out float v_fov;
void main()
{
v_center = i_pos;
v_rot = i_rot;
v_radius = i_radius;
v_attn = i_attn;
v_color = i_color;
v_dir = i_dir;
v_fov = i_fov;
vec3 pos = rotateByQuat(i_rot, a_position * i_radius);
pos += i_pos;
gl_Position = u_camera_projection * u_camera_view * vec4(pos, 1);
}
]]
---------------------
fragment_shader [[
layout(location = 0) out vec4 o_color;
layout(location = 0) in float v_radius;
layout(location = 1) in vec3 v_center;
layout(location = 2) in vec4 v_rot;
layout(location = 3) in float v_attn;
layout(location = 4) in vec3 v_color;
layout(location = 5) in vec3 v_dir;
layout(location = 6) in float v_fov;
layout (binding=0) uniform sampler2D u_gbuffer0;
layout (binding=1) uniform sampler2D u_gbuffer1;
layout (binding=2) uniform sampler2D u_gbuffer2;
layout (binding=3) uniform sampler2D u_gbuffer_depth;
void main()
{
/*float f0 = 0.04;
vec2 screen_uv = gl_FragCoord.xy / u_framebuffer_size;
vec3 wpos = getViewPosition(u_gbuffer_depth, u_camera_inv_view_projection, screen_uv);
vec4 gbuffer1_val = texture(u_gbuffer1, screen_uv * 0.5 + 0.5);
vec4 gbuffer2_val = texture(u_gbuffer2, screen_uv * 0.5 + 0.5);
vec3 normal = normalize(gbuffer1_val.xyz * 2 - 1);
vec4 albedo = texture(u_gbuffer0, screen_uv * 0.5 + 0.5);
float roughness = albedo.w;
float metallic = gbuffer1_val.w;
vec3 light_dir = normalize(v_center - wpos);
PBR_ComputeDirectLight(normal, light_dir, view, v_color_attn.rgb, f0, roughness, diff, spec);
vec4 r = v_rot;
r.w = -r.w;
vec3 lpos = rotateByQuat(r, wpos - v_center);
float dist = length(lpos);
float attn = pow(max(0, 1 - dist / v_radius), v_attn);
if (dist > v_radius) discard;
o_color = vec4(attn, attn, attn, 1);*/
float f0 = 0.04;
vec2 screen_uv = gl_FragCoord.xy / u_framebuffer_size;
vec4 gbuffer1_val = texture(u_gbuffer1, screen_uv);
vec4 gbuffer2_val = texture(u_gbuffer2, screen_uv);
vec3 N = normalize(gbuffer1_val.xyz * 2 - 1);
vec4 albedo = texture(u_gbuffer0, screen_uv);
float roughness = albedo.w;
float metallic = gbuffer1_val.w;
vec3 wpos = getViewPosition(u_gbuffer_depth, u_camera_inv_view_projection, screen_uv);
vec3 diff;
vec3 spec;
vec3 V = normalize(-wpos);
vec3 lpos = v_center - wpos.xyz;
vec3 L = normalize(lpos);
vec3 direct_light = PBR_ComputeDirectLight(albedo.rgb, N, L, V, v_color.rgb, roughness, metallic);
float dist = length(lpos);
float attn = pow(max(0, 1 - dist / v_radius), v_attn);
if(v_fov < 3.14159) {
float cosDir = dot(normalize(v_dir), normalize(L));
float cosCone = cos(v_fov * 0.5);
if(cosDir < cosCone) discard;
attn *= (cosDir - cosCone) / (1 - cosCone);
}/**/
/*vec3 lp = v_pos_radius.xyz - wpos;
float dist = length(lp);
float attn = pow(max(0, 1 - dist / v_pos_radius.w), v_color_attn.w);
if(v_dir_fov.w < 3.14159)
{
float cosDir = dot(normalize(v_dir_fov.xyz), normalize(light_dir));
float cosCone = cos(v_dir_fov.w * 0.5);
if(cosDir < cosCone)
discard;
attn *= (cosDir - cosCone) / (1 - cosCone);
}
vec4 specular_color = (f0 - f0 * metallic) + albedo * metallic;
vec4 diffuse_color = albedo - albedo * metallic;
o_color.xyz = attn * (diff * diffuse_color.rgb + spec * specular_color.rgb);
o_color.w = 1;*/
o_color.rgb = direct_light * attn;
//o_color.rgb = vec3(saturate(wpos));
o_color.a = 1;
}
]]

View file

@ -1,33 +0,0 @@
enabled = true
function postprocess(env, transparent_phase, hdr_buffer, gbuffer0, gbuffer1, gbuffer_depth, shadowmap)
if not enabled then return hdr_buffer end
if transparent_phase ~= "pre" then return hdr_buffer end
env.beginBlock("sky")
if env.procedural_sky_shader == nil then
env.procedural_sky_shader = env.preloadShader("pipelines/sky.shd")
end
env.setRenderTargets(hdr_buffer, gbuffer_depth)
local state = {
stencil_write_mask = 0,
stencil_func = env.STENCIL_NOT_EQUAL,
stencil_ref = 1,
stencil_mask = 0xff,
stencil_sfail = env.STENCIL_KEEP,
stencil_zfail = env.STENCIL_KEEP,
stencil_zpass = env.STENCIL_REPLACE,
depth_write = false,
depth_test = false
}
env.drawArray(0, 4, env.procedural_sky_shader, {}, {}, {}, state)
env.endBlock()
return hdr_buffer
end
function awake()
_G["postprocesses"] = _G["postprocesses"] or {}
_G["postprocesses"]["sky"] = postprocess
end
function onDestroy()
_G["postprocesses"]["sky"] = nil
end

View file

@ -1,142 +0,0 @@
include "pipelines/common.glsl"
vertex_shader [[
layout (location = 0) out vec2 v_uv;
void main()
{
gl_Position = fullscreenQuad(gl_VertexID, v_uv);
}
]]
fragment_shader [[
layout (location = 0) in vec2 v_uv;
layout (location = 0) out vec4 o_color;
vec3 air_color = vec3(0.18867780436772762, 0.4978442963618773, 0.6616065586417131);
float phase(float alpha, float g)
{
float a = 3.0*(1.0-g*g);
float b = 2.0*(2.0+g*g);
float c = 1.0+alpha*alpha;
float d = pow(1.0+g*g-2.0*g*alpha, 1.5);
return (a/b)*(c/d);
}
float atmospheric_depth(vec3 position, vec3 dir)
{
float a = dot(dir, dir);
float b = 2.0*dot(dir, position);
float c = dot(position, position)-1.0;
float det = b*b-4.0*a*c;
float detSqrt = sqrt(det);
float q = (-b - detSqrt)/2.0;
float t1 = c/q;
return t1;
}
float horizon_extinction(vec3 position, vec3 dir, float radius)
{
float u = dot(dir, -position);
if(u<0.0){
return 1.0;
}
vec3 near = position + u*dir;
if(length(near) < radius){
return 0.0;
}
else{
vec3 v2 = normalize(near)*radius - position;
float diff = acos(dot(normalize(v2), dir));
return smoothstep(0.0, 1.0, pow(diff*2.0, 3.0));
}
}
vec3 absorb(float dist, vec3 color, float factor)
{
return color-color*pow(air_color.rgb, vec3(factor/dist));
}
vec3 getWorldNormal(vec2 frag_coord)
{
float z = 1;
#ifdef _ORIGIN_BOTTOM_LEFT
vec4 posProj = vec4(frag_coord * 2 - 1, z, 1.0);
#else
vec4 posProj = vec4(vec2(frag_coord.x, 1-frag_coord.y) * 2 - 1, z, 1.0);
#endif
vec4 wpos = u_camera_inv_view_projection * posProj;
wpos /= wpos.w;
vec3 view = (u_camera_inv_view * vec4(0.0, 0.0, 0.0, 1.0)).xyz - wpos.xyz;
return -normalize(view);
}
float getFogFactorSky(float cam_height, vec3 eye_dir, float fog_density, float fog_bottom, float fog_height)
{
if(eye_dir.y == 0) return 1.0;
float to_top = max(0, (fog_bottom + fog_height) - cam_height);
float avg_y = (fog_bottom + fog_height + cam_height) * 0.5;
float avg_density = fog_density * clamp(1 - (avg_y - fog_bottom) / fog_height, 0, 1);
float res = exp(-pow(avg_density * to_top / eye_dir.y, 2));
res = 1 - clamp(res - (1-min(0.2, eye_dir.y)*5), 0, 1);
return res;
}
void main()
{
const vec3 strength = vec3(0.028, 0.139, 0.0264);
const vec3 brightness = vec3(5, 0.15, 200);
const float rayleigh_collection_power = 0.51f;
const float mie_collection_power = 0.39f;
const float mie_distribution = 0.13f;
const float sun_size = 150;
const float surface_height = 0.993;
const float intensity = 1.8;
const int step_count = 4;
vec3 lightdir = u_light_direction.xyz;
vec3 eyedir = getWorldNormal(v_uv);
float alpha = dot(eyedir, lightdir);
float rayleigh_factor = phase(alpha, -0.01) * brightness.x;
float mie_factor = phase(alpha, mie_distribution) * brightness.y;
float spot = smoothstep(0.0, sun_size, phase(alpha, 0.9995)) * brightness.z;
vec3 eye_position = vec3(0.0, surface_height, 0.0);
float eye_depth = atmospheric_depth(eye_position, eyedir);
float step_length = eye_depth / float(step_count);
float eye_extinction = horizon_extinction(eye_position, eyedir, surface_height - 0.15);
vec3 rayleigh_collected = vec3(0.0, 0.0, 0.0);
vec3 mie_collected = vec3(0.0, 0.0, 0.0);
for (int i = 0; i < step_count; ++i) {
float sample_distance = step_length * float(i);
vec3 position = eye_position + eyedir * sample_distance;
float extinction = horizon_extinction(position, lightdir, surface_height - 0.35);
float sample_depth = atmospheric_depth(position, lightdir);
vec3 influx = absorb(sample_depth, vec3(intensity), strength.x) * extinction;
rayleigh_collected += absorb(sample_distance, air_color.rgb * influx, strength.y);
mie_collected += absorb(sample_distance, influx, strength.z);
}
rayleigh_collected = (rayleigh_collected * eye_extinction * pow(eye_depth, rayleigh_collection_power))/float(step_count);
mie_collected = (mie_collected * eye_extinction * pow(eye_depth, mie_collection_power))/float(step_count);
vec3 color = vec3(spot * mie_collected + mie_factor * mie_collected + rayleigh_factor * rayleigh_collected);
float fog_factor = getFogFactorSky(u_camera_world_pos.y, eyedir, u_fog_params.x, u_fog_params.y, u_fog_params.z);
o_color.xyz = mix(color.rgb, u_fog_color.rgb, fog_factor);
o_color.w = 1;
}
]]