290 lines
9.1 KiB
GLSL
290 lines
9.1 KiB
GLSL
// vert //
|
|
|
|
out vec3 view_dir, world_dir;
|
|
|
|
uniform vec4 view_corners[4], world_corners[4];
|
|
|
|
void main(void) {
|
|
qgl_FragTexture = qgl_Texture;
|
|
gl_Position = qgl_ftransform();
|
|
view_dir = view_corners [gl_VertexID].xyz;
|
|
world_dir = world_corners[gl_VertexID].xyz;
|
|
}
|
|
|
|
|
|
// frag //
|
|
|
|
in vec3 view_dir, world_dir;
|
|
|
|
uniform vec2 dt, shadow_size;
|
|
uniform float z_near;
|
|
uniform sampler2D tex_coeffs[2];
|
|
uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4, tex_sh;
|
|
//uniform sampler2DShadow tex_shadow[16];
|
|
uniform sampler2DArrayShadow tex_shadows_cone;
|
|
uniform samplerCubeArrayShadow tex_shadows_omni;
|
|
uniform samplerCube tex_env;
|
|
uniform int lights_start, lights_count, soft_shadows_samples = 16;
|
|
uniform bool soft_shadows_enabled = false;
|
|
|
|
uniform vec4 fog_color = vec4(0.5, 0.5, 0.5, 1);
|
|
uniform float fog_decay = 10, fog_density = 0;
|
|
uniform mat3 view_mat;
|
|
const float _pe = 2.4e-7;
|
|
|
|
const vec3 luma = vec3(0.299, 0.587, 0.114);
|
|
const float _min_rough = 1.e-8, max_lod = 8;
|
|
const float PI = 3.1416;
|
|
|
|
vec4 pos, lpos, shp;
|
|
vec3 li, si, ldir, halfV, bn, bn2, lwdir;
|
|
vec3 normal, vds, vds2;
|
|
float rough_diff, rough_spec, dist, NdotL, NdotH, spot, ldist, diff, spec, sdist, shadow, shadow_dz;
|
|
uint flags;
|
|
|
|
|
|
vec4 mapScreenToShadow(in int light_index, in vec3 offset) {
|
|
vec4 shp = qgl_light_position[light_index].shadow_matrix * (pos + vec4(offset, 0));
|
|
//shp.z += 0.5;
|
|
return shp;
|
|
}
|
|
|
|
|
|
#ifdef SPOT
|
|
|
|
float getShadowCone(in vec3 uvz, in int layer) {
|
|
/*vec2 uvpix = uvz.xy * shadow_size + vec2(0.5f);
|
|
vec2 uvp = fract(uvpix), iuvp = vec2(1.f) - uvp;
|
|
vec4 gt = textureGather(tex_shadows_cone, vec3(floor(uvpix.xy) / shadow_size, layer), 0);
|
|
vec4 uvv;
|
|
uvv[0] = iuvp.x * uvp.y;
|
|
uvv[1] = uvp.x * uvp.y;
|
|
uvv[2] = uvp.x * iuvp.y;
|
|
uvv[3] = iuvp.x * iuvp.y;
|
|
shadow_dz = max(max(uvz.z - gt[0], uvz.z - gt[1]), max(uvz.z - gt[2], uvz.z - gt[3]));
|
|
return clamp(dot(step(vec4(uvz.z), gt), uvv), 0, 1);*/
|
|
float z = 1 - 1 / (uvz.z - z_near + 1);
|
|
return texture(tex_shadows_cone, vec4(uvz.xy, layer, z));
|
|
}
|
|
|
|
#else
|
|
|
|
float getShadowOmni(in vec4 uvwz, in int layer) {
|
|
/*vec3 uvpix = uvwz.xyz * vec3(shadow_size.x) + vec3(0.5f);
|
|
vec3 uvp = fract(uvpix), iuvp = vec3(1.f) - uvp;
|
|
vec4 gt = textureGather(tex_shadows_omni, vec4(floor(uvpix.xyz) / vec3(shadow_size.x), layer), 0);
|
|
vec4 uvv;
|
|
uvv[0] = iuvp.y * uvp.z;
|
|
uvv[1] = uvp.y * uvp.z;
|
|
uvv[2] = uvp.y * iuvp.z;
|
|
uvv[3] = iuvp.y * iuvp.z;
|
|
return clamp(dot(step(vec4(uvwz.w), gt), uvv), 0, 1);*/
|
|
float d = 1 - 1 / (uvwz.w - z_near + 1);
|
|
return texture(tex_shadows_omni, vec4(uvwz.xyz, layer), d);
|
|
//return step(uvwz.w, gt[3]);
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
float rand(vec2 co) {
|
|
float a = 12.9898;
|
|
float b = 78.233;
|
|
float c = 43758.5453;
|
|
float dt= dot(co.xy ,vec2(a,b));
|
|
float sn= mod(dt,3.14);
|
|
return fract(sin(sn) * c) - 0.5;
|
|
}
|
|
|
|
vec4 qgl_lightTexture(int index, vec2 coord, vec4 tex_shift) {
|
|
coord *= qgl_light_parameter[index].map.scale;
|
|
vec4 t = texture(qgl_texture_array[qgl_light_parameter[index].map.array_index],
|
|
vec3(coord, qgl_light_parameter[index].map.map_index));
|
|
t += tex_shift;
|
|
t.rgb *= qgl_light_parameter[index].map.amount + qgl_light_parameter[index].map.offset;
|
|
return t;
|
|
}
|
|
|
|
void calcLight(in int index, in vec3 n, in vec3 v) {
|
|
lpos = qgl_light_position[index].position;
|
|
ldir = lpos.xyz - (pos.xyz * lpos.w);
|
|
ldist = length(ldir);
|
|
ldir = normalize(ldir);
|
|
//ldir = vec3(0,0,1);
|
|
halfV = normalize(ldir + v);
|
|
NdotL = max(dot(n, ldir), 1E-8);
|
|
NdotH = max(dot(n, halfV), 1E-8);
|
|
spot = step(1.001E-8, NdotL) * qgl_light_parameter[index].decay_intensity.w;
|
|
vec3 light_color = qgl_light_parameter[index].color.rgb;
|
|
#ifdef SPOT
|
|
float scos = max(dot(-ldir, qgl_light_position[index].direction.xyz), 0.);
|
|
spot *= scos * step(qgl_light_parameter[index].angles.w, scos);
|
|
spot *= smoothstep(qgl_light_parameter[index].angles.w, qgl_light_parameter[index].angles.y, scos);
|
|
vec4 shp = mapScreenToShadow(index, vec3(0));
|
|
vec4 light_map_pix = qgl_lightTexture(index, shp.xy / shp.w, vec4(0));
|
|
light_color *= light_map_pix.rgb;
|
|
spot *= light_map_pix.a;
|
|
#endif
|
|
|
|
if (int(round(qgl_light_parameter[index].flags)) == 1 && bitfieldExtract(flags, 3, 1) == 1) {
|
|
vec3 odir = -(view_mat * ldir);
|
|
int layer = index - lights_start;
|
|
float shadow = 0.;
|
|
//float bias = abs(tan(PI/2.*(1 - abs(dot(normal, ldir)))) + 1) * z_near * 1;
|
|
float bias = (1. + 1. / abs(dot(normal, ldir))) * z_near * 5;
|
|
//bias = bias * bias + z_near;
|
|
|
|
if (soft_shadows_enabled) {
|
|
|
|
float ds = (ldist / 2.) / qgl_light_parameter[index].size;
|
|
vds = ds * bn.xyz;
|
|
vds2 = ds * bn2.xyz;
|
|
vec2 so;
|
|
|
|
for (int i = 1; i <= soft_shadows_samples; ++i) {
|
|
#ifdef SPOT
|
|
so = vec2(rand(vec2(shp.x + i, shp.y)), rand(vec2(shp.x + i + 1, shp.y)));
|
|
vec4 shp = mapScreenToShadow(index, vds * so.x + vds2 * so.y);
|
|
shp.xy /= shp.w;
|
|
shp.z -= bias;
|
|
shadow += getShadowCone(shp.xyz, layer);
|
|
#else
|
|
so = vec2(rand(vec2(odir.x + i, odir.y)), rand(vec2(odir.x + i + 1, odir.y)));
|
|
vec3 old = lpos.xyz - ((pos.xyz + vec3(vds * so.x + vds2 * so.y)) * lpos.w);
|
|
odir = -(view_mat * old);
|
|
shadow += getShadowOmni(vec4(odir, length(old) - bias), layer);
|
|
#endif
|
|
}
|
|
|
|
spot *= shadow / (soft_shadows_samples + 0);
|
|
|
|
} else {
|
|
|
|
#ifdef SPOT
|
|
shp.xy /= shp.w;
|
|
shp.z -= bias;
|
|
spot *= getShadowCone(shp.xyz, layer);
|
|
#else
|
|
spot *= getShadowOmni(vec4(odir, ldist - bias), layer);
|
|
#endif
|
|
|
|
}
|
|
//shp.z -= bias;
|
|
//shadow += getShadowCone(shp.xyz, layer, vec2(0));
|
|
|
|
}
|
|
|
|
vec3 dist_decay = vec3(1, ldist, ldist*ldist);
|
|
spot /= dot(qgl_light_parameter[index].decay_intensity.xyz, dist_decay);
|
|
|
|
float NdotLs = NdotL*NdotL;
|
|
float NdotHs = NdotH*NdotH;
|
|
|
|
float ndlc = (1. - NdotLs) / NdotLs;
|
|
diff = 2. / (1. + sqrt(1. + (1. - rough_diff) * ndlc));
|
|
//diff = texture(tex_coeffs[0], vec2(roughness, (NdotLs))).r;
|
|
li += spot * diff * light_color;
|
|
|
|
ndlc = (1. - NdotHs) / NdotHs;
|
|
float der = NdotHs * (rough_spec + ndlc);
|
|
spec = rough_spec / (der*der) / PI;
|
|
//spec = texture(tex_coeffs[1], vec2(roughness, (NdotHs))).r;
|
|
si += spot * spec * light_color;
|
|
}
|
|
|
|
|
|
void main(void) {
|
|
ivec2 tc = ivec2(gl_FragCoord.xy);
|
|
vec4 v1 = texelFetch(tex_1, tc, 0);
|
|
float z = v1.w;
|
|
if (z == 1.) {
|
|
discard;
|
|
}
|
|
pos.w = 1;
|
|
pos.xyz = view_dir * z;
|
|
vec3 v = normalize(-pos.xyz);
|
|
|
|
vec4 v0 = texelFetch(tex_0, tc, 0),
|
|
v2 = texelFetch(tex_2, tc, 0),
|
|
v3 = texelFetch(tex_3, tc, 0),
|
|
v4 = texelFetch(tex_4, tc, 0);
|
|
|
|
vec3 diffuse = v0.rgb;
|
|
normal = v1.xyz;
|
|
vec3 emission = v3.rgb;
|
|
float alpha = v0.a;
|
|
float metalness = v2.r;
|
|
float roughness = v2.g;
|
|
float reflectivity = v2.b;
|
|
float NdotV = dot(normal, v);
|
|
float roughness3 = roughness*roughness*roughness;
|
|
bn = normalize(cross(normal, view_dir));
|
|
bn2 = normalize(cross(normal, bn));
|
|
rough_diff = max(roughness, _min_rough);
|
|
rough_spec = max(roughness3, _min_rough);
|
|
float shlick = clamp(metalness + (1 - metalness) * pow(1 - NdotV, 5), 0, 1);
|
|
flags = uint(round(v2.w));
|
|
|
|
li = vec3(0.);//qgl_AmbientLight.color.rgb * qgl_AmbientLight.intensity;
|
|
si = vec3(0.);
|
|
if (bitfieldExtract(flags, 0, 1) == 1) {
|
|
for (int i = 0; i < lights_count; ++i)
|
|
calcLight(lights_start + i, normal, v);
|
|
} else {
|
|
li = vec3(1.);
|
|
}
|
|
si *= shlick;
|
|
li *= (1 - shlick);
|
|
alpha = min(1, alpha * (1 + shlick));
|
|
|
|
vec2 brdf = texture(tex_coeffs[0], vec2(NdotV*0.99, roughness*0.995)).rg;
|
|
float env_spec = shlick * brdf.x + brdf.y;
|
|
vec3 spec_col = mix(vec3(1), diffuse, metalness);
|
|
vec3 env_dir = view_mat * reflect(-v, normal);
|
|
vec3 env_col = textureLod(tex_env, env_dir, sqrt(roughness) * max_lod).rgb * spec_col;
|
|
|
|
vec3 res_col = max(vec3(0), li * diffuse + si * spec_col + emission);
|
|
res_col = mix(res_col, env_col, env_spec * reflectivity);
|
|
|
|
if (bitfieldExtract(flags, 1, 1) == 1) {
|
|
float plen = length(pos.xyz);
|
|
float fog = 1 - exp(-plen / fog_decay);
|
|
fog = clamp(fog * fog_color.a * fog_density, 0, 1);
|
|
res_col = mix(res_col, fog_color.rgb, fog);
|
|
}
|
|
|
|
qgl_FragColor = vec4(res_col, alpha);
|
|
//qgl_FragColor.rgb = vec3(qgl_light_parameter[0].size);
|
|
|
|
#ifdef SPOT
|
|
vec4 shp = mapScreenToShadow(0, vec3(0));
|
|
//shp.xy /= shp.w;
|
|
//shp.z -= bias;
|
|
//for (int xi = -2; xi <= 2; ++xi) {
|
|
// for (int yi = -2; yi <= 2; ++yi) {
|
|
//qgl_FragColor.rgb = vec3(step(shp.z, texture(tex_shadows_cone, vec3(shp.xy, 0)).r));
|
|
//qgl_FragColor.r = texture(tex_shadows_cone, vec3(0)).r;
|
|
//qgl_FragColor.gb = vec2(1);
|
|
//qgl_FragColor.rgb = vec3(shp.z / shp.w);//vec4(res_col, alpha);
|
|
|
|
//shp.z += 0.095;
|
|
//qgl_FragColor.rgb = vec3(texture(tex_sh, shp.xy).rgb);
|
|
//qgl_FragColor.rgb = vec3(textureProj(tex_shadow[0], shp));
|
|
//vec4 rp = qgl_ViewProjMatrix*vec4(qgl_FragTexture.xy,z,1);
|
|
//qgl_FragColor.rgb = vec3(texture(tex_shadows_cone, vec3(gl_FragCoord.xy/1000, 0)).r);
|
|
#else
|
|
//vec3 odir = -(view_mat * ldir);
|
|
//float otex = texture(tex_shadows_omni, vec4(odir, 0), 0.8);
|
|
//qgl_FragColor.rgb = vec3(otex);
|
|
//qgl_FragColor.rgb = vec3(ldist/50);
|
|
//qgl_FragColor.rgb = vec3(abs(otex - ldist) * 1.);
|
|
//qgl_FragColor.rgb = vec3(tan((1-abs(dot(view_mat*normal, odir)))));
|
|
#endif
|
|
|
|
//vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
|
|
//qgl_FragColor.rgb = vec3(shlick * brdf.x + brdf.y);
|
|
//qgl_FragColor.rgb = vec3(alpha);
|
|
//qgl_FragColor.rgb = vec3(textureLod(tex_env, world_dir, 0).rgb);
|
|
//qgl_FragColor.a = 1.;
|
|
}
|