2023-08-24 11:59:05 +02:00
|
|
|
// PC3D rendering shader
|
|
|
|
|
2023-08-14 19:22:04 +02:00
|
|
|
varying vec2 v_vTexcoord;
|
|
|
|
varying vec4 v_vColour;
|
|
|
|
varying vec3 v_vNormal;
|
|
|
|
|
2023-08-23 20:01:09 +02:00
|
|
|
varying vec4 v_worldPosition;
|
2023-08-24 11:59:05 +02:00
|
|
|
varying vec3 v_viewPosition;
|
2023-08-23 20:01:09 +02:00
|
|
|
varying float v_cameraDistance;
|
2023-08-14 19:22:04 +02:00
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
#define PI 3.14159265359
|
|
|
|
#define TAU 6.28318530718
|
|
|
|
|
2023-08-16 20:16:31 +02:00
|
|
|
#region ---- light ----
|
|
|
|
uniform vec4 light_ambient;
|
2023-08-22 11:51:45 +02:00
|
|
|
uniform float shadowBias;
|
|
|
|
|
2023-08-16 20:16:31 +02:00
|
|
|
#define LIGHT_DIR_LIMIT 16
|
|
|
|
uniform int light_dir_count;
|
|
|
|
uniform vec3 light_dir_direction[LIGHT_DIR_LIMIT];
|
|
|
|
uniform vec4 light_dir_color[LIGHT_DIR_LIMIT];
|
|
|
|
uniform float light_dir_intensity[LIGHT_DIR_LIMIT];
|
2023-08-22 11:51:45 +02:00
|
|
|
|
|
|
|
uniform mat4 light_dir_view[LIGHT_DIR_LIMIT];
|
|
|
|
uniform mat4 light_dir_proj[LIGHT_DIR_LIMIT];
|
|
|
|
uniform int light_dir_shadow_active[LIGHT_DIR_LIMIT];
|
2023-08-23 20:01:09 +02:00
|
|
|
uniform float light_dir_shadow_bias[LIGHT_DIR_LIMIT];
|
2023-08-22 11:51:45 +02:00
|
|
|
uniform sampler2D light_dir_shadowmap_0;
|
|
|
|
uniform sampler2D light_dir_shadowmap_1;
|
|
|
|
uniform sampler2D light_dir_shadowmap_2;
|
|
|
|
uniform sampler2D light_dir_shadowmap_3;
|
|
|
|
|
2023-08-16 20:16:31 +02:00
|
|
|
#define LIGHT_PNT_LIMIT 16
|
|
|
|
uniform int light_pnt_count;
|
|
|
|
uniform vec3 light_pnt_position[LIGHT_PNT_LIMIT];
|
|
|
|
uniform vec4 light_pnt_color[LIGHT_PNT_LIMIT];
|
|
|
|
uniform float light_pnt_intensity[LIGHT_PNT_LIMIT];
|
|
|
|
uniform float light_pnt_radius[LIGHT_PNT_LIMIT];
|
2023-08-22 11:51:45 +02:00
|
|
|
|
|
|
|
uniform mat4 light_pnt_view[96];
|
|
|
|
uniform mat4 light_pnt_proj[LIGHT_PNT_LIMIT];
|
|
|
|
uniform int light_pnt_shadow_active[LIGHT_PNT_LIMIT];
|
2023-08-23 20:01:09 +02:00
|
|
|
uniform float light_pnt_shadow_bias[LIGHT_DIR_LIMIT];
|
2023-08-22 11:51:45 +02:00
|
|
|
uniform sampler2D light_pnt_shadowmap_0;
|
|
|
|
uniform sampler2D light_pnt_shadowmap_1;
|
|
|
|
uniform sampler2D light_pnt_shadowmap_2;
|
|
|
|
uniform sampler2D light_pnt_shadowmap_3;
|
|
|
|
#endregion
|
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
#region ---- material ----
|
|
|
|
vec4 mat_baseColor;
|
|
|
|
|
|
|
|
uniform float mat_diffuse;
|
|
|
|
uniform float mat_specular;
|
|
|
|
uniform float mat_shine;
|
|
|
|
uniform int mat_metalic;
|
2023-08-24 19:44:12 +02:00
|
|
|
uniform float mat_reflective;
|
|
|
|
|
|
|
|
uniform int mat_use_normal;
|
|
|
|
uniform float mat_normal_strength;
|
|
|
|
uniform sampler2D mat_normal_map;
|
2023-08-24 11:59:05 +02:00
|
|
|
#endregion
|
|
|
|
|
2023-08-22 11:51:45 +02:00
|
|
|
#region ---- rendering ----
|
2023-08-24 11:59:05 +02:00
|
|
|
uniform vec3 cameraPosition;
|
|
|
|
uniform int gammaCorrection;
|
2023-08-24 19:44:12 +02:00
|
|
|
|
|
|
|
uniform int env_use_mapping;
|
|
|
|
uniform sampler2D env_map;
|
|
|
|
uniform vec2 env_map_dimension;
|
2023-08-22 11:51:45 +02:00
|
|
|
#endregion
|
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
#region ++++ matrix ++++
|
2023-08-22 11:51:45 +02:00
|
|
|
float matrixGet(mat4 matrix, int index) {
|
|
|
|
if(index < 0 || index > 15) return 0.;
|
|
|
|
|
|
|
|
int _x = int(floor(float(index) / 4.));
|
|
|
|
int _y = int(mod(float(index), 4.));
|
|
|
|
return matrix[_x][_y];
|
|
|
|
}
|
|
|
|
|
|
|
|
mat4 matrixSet(mat4 matrix, int index, float value) {
|
|
|
|
if(index < 0 || index > 15) return matrix;
|
|
|
|
|
|
|
|
int _x = int(floor(float(index) / 4.));
|
|
|
|
int _y = int(mod(float(index), 4.));
|
|
|
|
matrix[_x][_y] = value;
|
|
|
|
return matrix;
|
|
|
|
}
|
2023-08-16 20:16:31 +02:00
|
|
|
#endregion
|
2023-08-14 19:22:04 +02:00
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
#region ++++ shadow sampler ++++
|
|
|
|
float sampleDirShadowMap(int index, vec2 position) {
|
|
|
|
if(index == 0) return texture2D(light_dir_shadowmap_0, position).r;
|
|
|
|
if(index == 1) return texture2D(light_dir_shadowmap_1, position).r;
|
|
|
|
if(index == 2) return texture2D(light_dir_shadowmap_2, position).r;
|
|
|
|
if(index == 3) return texture2D(light_dir_shadowmap_3, position).r;
|
|
|
|
return 0.;
|
2023-08-22 11:51:45 +02:00
|
|
|
}
|
2023-08-24 11:59:05 +02:00
|
|
|
|
|
|
|
float samplePntShadowMap(int index, vec2 position, int side) {
|
|
|
|
position.x /= 2.;
|
|
|
|
if(side >= 3) {
|
|
|
|
position.x += 0.5;
|
|
|
|
side -= 3;
|
|
|
|
}
|
2023-08-22 11:51:45 +02:00
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
if(index == 0) return texture2D(light_pnt_shadowmap_0, position)[side];
|
|
|
|
if(index == 1) return texture2D(light_pnt_shadowmap_1, position)[side];
|
|
|
|
if(index == 2) return texture2D(light_pnt_shadowmap_2, position)[side];
|
|
|
|
if(index == 3) return texture2D(light_pnt_shadowmap_3, position)[side];
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ++++ Phong shading ++++
|
|
|
|
vec3 phongLight(vec3 normal, vec3 lightVec, vec3 viewVec, vec3 light) {
|
|
|
|
vec3 lightDir = normalize(lightVec);
|
|
|
|
vec3 viewDir = normalize(viewVec);
|
|
|
|
vec3 refcDir = reflect(-lightDir, normal);
|
|
|
|
|
2023-08-24 12:43:03 +02:00
|
|
|
float kD = 1., kS = 0.;
|
2023-08-24 11:59:05 +02:00
|
|
|
|
2023-08-24 12:43:03 +02:00
|
|
|
if(mat_diffuse + mat_specular != 0.) {
|
2023-08-24 11:59:05 +02:00
|
|
|
kD = mat_diffuse / (mat_diffuse + mat_specular);
|
|
|
|
kS = mat_specular / (mat_diffuse + mat_specular);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 lLambert = max(0., dot(normal, lightDir)) * light;
|
|
|
|
|
|
|
|
float specular = pow(max(dot(viewDir, refcDir), 0.), max(0.001, mat_shine));
|
|
|
|
vec3 lSpecular = specular * light;
|
|
|
|
if(mat_metalic == 1) lSpecular *= mat_baseColor.rgb;
|
|
|
|
|
|
|
|
return kD * lLambert + kS * lSpecular;
|
|
|
|
}
|
|
|
|
#endregion
|
2023-08-22 11:51:45 +02:00
|
|
|
|
2023-08-24 19:44:12 +02:00
|
|
|
#region ++++ mapping ++++
|
|
|
|
vec2 equirectangularUv(vec3 dir) {
|
|
|
|
vec3 n = normalize(dir);
|
|
|
|
return vec2((atan(n.x, n.y) / TAU) + 0.5, 1. - acos(n.z) / PI);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-08-14 19:22:04 +02:00
|
|
|
void main() {
|
2023-08-24 11:59:05 +02:00
|
|
|
mat_baseColor = texture2D( gm_BaseTexture, v_vTexcoord );
|
|
|
|
mat_baseColor *= v_vColour;
|
|
|
|
|
|
|
|
vec4 final_color = mat_baseColor;
|
|
|
|
vec3 viewDirection = normalize(cameraPosition - v_worldPosition.xyz);
|
2023-08-23 20:01:09 +02:00
|
|
|
|
2023-08-24 19:44:12 +02:00
|
|
|
#region ++++ normal ++++
|
|
|
|
vec3 _norm = v_vNormal;
|
|
|
|
if(mat_use_normal == 1) {
|
|
|
|
vec3 _sampled_normal = texture2D(mat_normal_map, v_vTexcoord).rgb;
|
|
|
|
_norm += (_sampled_normal - 0.5) * 2. * mat_normal_strength;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec3 normal = normalize(_norm);
|
|
|
|
#endregion
|
2023-08-14 19:22:04 +02:00
|
|
|
|
2023-08-16 20:16:31 +02:00
|
|
|
#region ++++ light ++++
|
2023-08-22 11:51:45 +02:00
|
|
|
int shadow_map_index = 0;
|
2023-08-16 20:16:31 +02:00
|
|
|
vec3 light_effect = light_ambient.rgb;
|
2023-08-22 11:51:45 +02:00
|
|
|
float val = 0.;
|
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
#region ++++ directional ++++
|
2023-08-22 11:51:45 +02:00
|
|
|
float light_map_depth;
|
|
|
|
float lightDistance;
|
|
|
|
float shadow_culled;
|
2023-08-16 20:16:31 +02:00
|
|
|
|
2023-08-22 11:51:45 +02:00
|
|
|
shadow_map_index = 0;
|
|
|
|
for(int i = 0; i < light_dir_count; i++) {
|
2023-08-23 20:01:09 +02:00
|
|
|
vec3 lightVector = normalize(light_dir_direction[i]);
|
2023-08-24 19:44:12 +02:00
|
|
|
|
2023-08-22 11:51:45 +02:00
|
|
|
if(light_dir_shadow_active[i] == 1) {
|
|
|
|
vec4 cameraSpace = light_dir_view[i] * v_worldPosition;
|
|
|
|
vec4 screenSpace = light_dir_proj[i] * cameraSpace;
|
|
|
|
|
|
|
|
float v_lightDistance = screenSpace.z / screenSpace.w;
|
|
|
|
vec2 lightMapPosition = (screenSpace.xy / screenSpace.w * 0.5) + 0.5;
|
|
|
|
|
|
|
|
light_map_depth = sampleDirShadowMap(shadow_map_index, lightMapPosition);
|
|
|
|
shadow_map_index++;
|
|
|
|
lightDistance = v_lightDistance;
|
2023-08-23 20:01:09 +02:00
|
|
|
float shadowFactor = dot(normal, lightVector);
|
|
|
|
float bias = mix(light_dir_shadow_bias[i], 0., shadowFactor);
|
|
|
|
|
|
|
|
if(lightDistance > light_map_depth + bias)
|
2023-08-22 11:51:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-23 20:01:09 +02:00
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
vec3 light_phong = phongLight(normal, lightVector, viewDirection, light_dir_color[i].rgb);
|
|
|
|
|
|
|
|
light_effect += light_phong;
|
2023-08-22 11:51:45 +02:00
|
|
|
}
|
|
|
|
#endregion
|
2023-08-24 11:59:05 +02:00
|
|
|
|
|
|
|
#region ++++ point ++++
|
2023-08-22 11:51:45 +02:00
|
|
|
float light_distance;
|
|
|
|
float light_attenuation;
|
2023-08-16 20:16:31 +02:00
|
|
|
|
2023-08-22 11:51:45 +02:00
|
|
|
shadow_map_index = 0;
|
|
|
|
for(int i = 0; i < light_pnt_count; i++) {
|
2023-08-23 20:01:09 +02:00
|
|
|
vec3 lightVector = normalize(light_pnt_position[i] - v_worldPosition.xyz);
|
2023-08-24 19:44:12 +02:00
|
|
|
|
2023-08-23 20:01:09 +02:00
|
|
|
light_distance = length(lightVector);
|
2023-08-22 11:51:45 +02:00
|
|
|
if(light_distance > light_pnt_radius[i])
|
|
|
|
continue;
|
2023-08-16 20:16:31 +02:00
|
|
|
|
2023-08-22 11:51:45 +02:00
|
|
|
if(light_pnt_shadow_active[i] == 1) {
|
2023-08-23 20:01:09 +02:00
|
|
|
vec3 dirAbs = abs(lightVector);
|
2023-08-22 11:51:45 +02:00
|
|
|
int side = dirAbs.x > dirAbs.y ?
|
|
|
|
(dirAbs.x > dirAbs.z ? 0 : 2) :
|
|
|
|
(dirAbs.y > dirAbs.z ? 1 : 2);
|
|
|
|
side *= 2;
|
2023-08-24 19:44:12 +02:00
|
|
|
if(side == 0 && lightVector.x > 0.) side += 1;
|
|
|
|
else if(side == 2 && lightVector.y > 0.) side += 1;
|
|
|
|
else if(side == 4 && lightVector.z > 0.) side += 1;
|
2023-08-22 11:51:45 +02:00
|
|
|
|
2023-08-24 19:44:12 +02:00
|
|
|
vec4 cameraSpace = light_pnt_view[i * 6 + side] * v_worldPosition;
|
|
|
|
vec4 screenSpace = light_pnt_proj[i] * cameraSpace;
|
2023-08-22 11:51:45 +02:00
|
|
|
float v_lightDistance = screenSpace.z / screenSpace.w;
|
|
|
|
vec2 lightMapPosition = (screenSpace.xy / screenSpace.w * 0.5) + 0.5;
|
2023-08-24 19:44:12 +02:00
|
|
|
|
2023-08-23 20:01:09 +02:00
|
|
|
float shadowFactor = dot(normal, lightVector);
|
|
|
|
float bias = mix(light_pnt_shadow_bias[i], 0., shadowFactor);
|
2023-08-22 11:51:45 +02:00
|
|
|
|
|
|
|
light_map_depth = samplePntShadowMap(shadow_map_index, lightMapPosition, side);
|
|
|
|
shadow_map_index++;
|
|
|
|
|
2023-08-23 20:01:09 +02:00
|
|
|
if(v_lightDistance > light_map_depth + bias)
|
2023-08-22 11:51:45 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-24 11:59:05 +02:00
|
|
|
|
2023-08-22 11:51:45 +02:00
|
|
|
light_attenuation = 1. - pow(light_distance / light_pnt_radius[i], 2.);
|
2023-08-24 19:44:12 +02:00
|
|
|
|
2023-08-24 11:59:05 +02:00
|
|
|
vec3 light_phong = phongLight(normal, lightVector, viewDirection, light_pnt_color[i].rgb * light_attenuation);
|
|
|
|
|
|
|
|
light_effect += light_phong;
|
2023-08-22 11:51:45 +02:00
|
|
|
}
|
|
|
|
#endregion
|
2023-08-14 19:22:04 +02:00
|
|
|
|
2023-08-16 20:16:31 +02:00
|
|
|
light_effect = max(light_effect, 0.);
|
2023-08-24 11:59:05 +02:00
|
|
|
|
|
|
|
if(gammaCorrection == 1) {
|
|
|
|
light_effect.r = pow(light_effect.r, 1. / 2.2);
|
|
|
|
light_effect.g = pow(light_effect.g, 1. / 2.2);
|
|
|
|
light_effect.b = pow(light_effect.b, 1. / 2.2);
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:16:31 +02:00
|
|
|
final_color.rgb *= light_effect;
|
|
|
|
#endregion
|
2023-08-14 19:22:04 +02:00
|
|
|
|
2023-08-24 19:44:12 +02:00
|
|
|
#region ++++ environment ++++
|
|
|
|
if(env_use_mapping == 1) {
|
|
|
|
vec3 reflectDir = reflect(viewDirection, normal);
|
|
|
|
|
|
|
|
float refRad = mix(16., 0., mat_reflective);
|
|
|
|
vec2 tx = 1. / env_map_dimension;
|
|
|
|
vec2 reflect_sample_pos = equirectangularUv(reflectDir);
|
|
|
|
vec4 env_sampled = vec4(0.);
|
|
|
|
float weight = 0.;
|
|
|
|
|
|
|
|
for(float i = -refRad; i <= refRad; i++)
|
|
|
|
for(float j = -refRad; j <= refRad; j++) {
|
|
|
|
vec2 _map_pos = reflect_sample_pos + vec2(i, j) * tx;
|
|
|
|
vec4 _samp = texture2D(env_map, _map_pos);
|
|
|
|
env_sampled += _samp;
|
|
|
|
weight += _samp.a;
|
|
|
|
}
|
|
|
|
env_sampled /= weight;
|
|
|
|
env_sampled.a = 1.;
|
|
|
|
|
|
|
|
vec4 env_effect = mat_metalic == 1? env_sampled * final_color : env_sampled;
|
|
|
|
env_effect = 1. - ( mat_reflective * ( 1. - env_effect ));
|
|
|
|
|
|
|
|
final_color *= env_effect;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2023-08-23 20:01:09 +02:00
|
|
|
gl_FragData[0] = final_color;
|
|
|
|
gl_FragData[1] = vec4(0.5 + normal * 0.5, 1.);
|
2023-08-24 19:44:12 +02:00
|
|
|
gl_FragData[2] = vec4(vec3(v_cameraDistance), 1.);
|
2023-08-14 19:22:04 +02:00
|
|
|
}
|