rk_island/game/shaders/common_opengles.frag

56 lines
2.1 KiB
GLSL

// Copyright (C) 2022 RozK
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#version 320 es
precision highp float;
#include "include/environment.glsl"
in vec4 v_position; // view space
in vec4 v_normal; // view space
in vec4 v_terrain_normal; // view space (x, y, z, weight)
in vec4 v_texcoord; // texture space (s, t, pixel_level, material_level)
#define v_weight v_terrain_normal.w
layout(binding=0) uniform highp sampler2DArray u_texture_sampler;
layout(location = 0) out vec4 o_color;
void main(void) {
vec4 pixel = texture(u_texture_sampler, v_texcoord.stp);
vec4 material = texture(u_texture_sampler, v_texcoord.stq);
#define m_metallic material.x
#define m_specular material.y
#define m_roughness material.z
vec3 normal = normalize(v_normal.xyz);
vec3 eye_dir = -normalize(v_position.xyz);
float d = dot(normal, u_light_direction);
float halfd = 0.5 + d * 0.5;
float td = dot(normalize(v_terrain_normal.xyz), u_light_direction);
float diffuse = halfd * mix(halfd, 0.5 + td * 0.5, v_weight) * (1.0 - m_metallic);
float s = max(0.0, dot(normal, normalize(u_light_direction + eye_dir)));
float shininess = 1.0 + pow(1.0 - m_roughness, 2.0) * 999.0;
float stepd = step(0.0, d);
float specular = pow(s, shininess) * mix(stepd, stepd * max(0.0, td), v_weight);
vec3 color = pixel.rgb * u_light_color;
o_color = vec4(
vec3(
color * diffuse +
color * (specular * m_metallic) +
u_light_color * (specular * m_specular)),
pixel.a);
}