WIP sources and TODOs.
This commit is contained in:
90
game/shaders/sky_opengles.frag
Normal file
90
game/shaders/sky_opengles.frag
Normal file
@ -0,0 +1,90 @@
|
||||
// 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;
|
||||
|
||||
in vec3 v_position;
|
||||
|
||||
uniform mat4 u_view_km; // world space -> view space, unit = km
|
||||
uniform vec3 u_light_direction; // view space (-direction_x, -direction_y, -direction_z)
|
||||
uniform vec3 u_light_color;
|
||||
uniform vec3 u_horizon_color;
|
||||
uniform vec3 u_sky_color;
|
||||
uniform vec3 u_sun_color;
|
||||
uniform float u_sea_phase;
|
||||
|
||||
#define u_right u_view_km[0].xyz
|
||||
#define u_forward u_view_km[1].xyz
|
||||
#define u_up u_view_km[2].xyz
|
||||
#define u_origin u_view_km[3].xyz
|
||||
|
||||
uniform highp sampler2DArray u_sea_polar_sampler;
|
||||
uniform highp sampler2D u_sea_detail_sampler;
|
||||
|
||||
const float c_sea_radius = 637.1;
|
||||
const float c_sea_radius_sq = c_sea_radius * c_sea_radius;
|
||||
const float c_sky_radius = c_sea_radius + 10.0;
|
||||
const vec3 c_normal_scale = vec3(2.0, 2.0, 1.0);
|
||||
const vec3 c_normal_shift = vec3(-1.0, -1.0, 0.0);
|
||||
const float c_detail_scale = 2.0;
|
||||
|
||||
layout(location = 0) out vec4 o_color;
|
||||
|
||||
vec3 sky(in vec3 ray_direction) {
|
||||
float d = max(0.0, dot(ray_direction, u_light_direction));
|
||||
return mix(u_horizon_color, u_sky_color, max(0.0, dot(ray_direction, u_up))) + u_sun_color * pow(d, 1000.0);
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
vec3 direction = normalize(v_position);
|
||||
vec3 earth_center = u_origin - u_up * c_sea_radius;
|
||||
float p_dist = dot(direction, earth_center);
|
||||
vec3 pc = earth_center - direction * p_dist;
|
||||
if (p_dist <= 0.0 || dot(pc, pc) >= c_sea_radius_sq) {
|
||||
// sky
|
||||
o_color = vec4(sky(direction), 1.0);
|
||||
} else {
|
||||
// sea
|
||||
vec3 sea_position = direction * (p_dist - sqrt(c_sea_radius_sq - dot(pc, pc))) - u_origin;
|
||||
vec3 sea_direction = normalize(sea_position);
|
||||
//TODO: vec2
|
||||
float s = dot(u_forward, sea_direction);
|
||||
if (dot(u_right, sea_direction) > 0.0) {
|
||||
// [1.0 -1.0] -> [0.0 0.5]
|
||||
s = (1.0 - s) * 0.25;
|
||||
} else {
|
||||
// [-1.0 1.0] -> [0.5 1.0] -> [0.0 0.5] + 0.5
|
||||
s = (1.0 + s) * 0.25 + 0.5;
|
||||
}
|
||||
float t = sqrt(length(sea_position)); //TODO: more accurate
|
||||
vec3 sea_polar1 = normalize(
|
||||
c_normal_shift + texture(u_sea_polar_sampler, vec3(s, t + u_sea_phase, 0.0)).xyz * c_normal_scale);
|
||||
vec3 sea_polar2 = normalize(
|
||||
c_normal_shift + texture(u_sea_polar_sampler, vec3(s, t - u_sea_phase, 1.0)).xyz * c_normal_scale);
|
||||
//TODO: vec2
|
||||
s = (u_sea_phase + dot(sea_position, u_right)) * c_detail_scale;
|
||||
t = (u_sea_phase + dot(sea_position, u_forward)) * c_detail_scale;
|
||||
vec3 sea_detail = normalize(c_normal_shift + texture(u_sea_detail_sampler, vec2(s, t)).xyz * c_normal_scale);
|
||||
//TODO: better blending, with earth normal
|
||||
vec4 normal = u_view_km * vec4(normalize(sea_polar1 + sea_polar2 + sea_detail), 0.0);
|
||||
float d = max(0.0, dot(normal.xyz, u_light_direction));
|
||||
s = pow(max(0.0, dot(normal.xyz, normalize(u_light_direction - direction))), 500.0) * step(0.0, d);
|
||||
o_color = vec4(
|
||||
u_sky_color * d + //TODO: sea color
|
||||
u_light_color * s +
|
||||
sky(reflect(direction, normal.xyz)), 1.0);
|
||||
}
|
||||
}
|
28
game/shaders/sky_opengles.vert
Normal file
28
game/shaders/sky_opengles.vert
Normal file
@ -0,0 +1,28 @@
|
||||
// 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;
|
||||
|
||||
layout(location = 0) in vec3 a_position; // view space
|
||||
|
||||
uniform mat4 u_projection; // view space -> screen space
|
||||
|
||||
out vec3 v_position; // view space
|
||||
|
||||
void main(void) {
|
||||
v_position = a_position;
|
||||
gl_Position = u_projection * vec4(a_position, 1.0);
|
||||
}
|
56
game/shaders/terrain_opengles.frag
Normal file
56
game/shaders/terrain_opengles.frag
Normal file
@ -0,0 +1,56 @@
|
||||
// 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;
|
||||
|
||||
in vec3 v_position; // view space
|
||||
in vec3 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
|
||||
|
||||
uniform vec3 u_light_direction; // view space (-direction_x, -direction_y, -direction_z)
|
||||
uniform vec3 u_light_color; // (color.r * power, color.g * power, color.b * power)
|
||||
|
||||
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);
|
||||
vec3 eye_dir = -normalize(v_position);
|
||||
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);
|
||||
}
|
63
game/shaders/terrain_opengles.vert
Normal file
63
game/shaders/terrain_opengles.vert
Normal file
@ -0,0 +1,63 @@
|
||||
// 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;
|
||||
|
||||
layout(location = 0) in vec3 a_position; // model space
|
||||
layout(location = 1) in vec3 a_normal; // model space
|
||||
layout(location = 2) in vec2 a_texcoord; // texture space
|
||||
|
||||
layout(location = 3) in vec4 i_translation; // per mesh, model space -> world space (x, y, z, texlevel)
|
||||
layout(location = 4) in vec3 i_orientation; // per mesh, model space -> world space
|
||||
|
||||
#define i_texlevel i_translation.w
|
||||
|
||||
uniform mat4 u_view; // world space -> view space
|
||||
uniform mat4 u_projection; // view space -> screen space
|
||||
|
||||
uniform highp sampler2D u_height_sampler;
|
||||
uniform highp sampler2D u_normal_sampler;
|
||||
|
||||
const vec3 c_normal_scale = vec3(2.0, 2.0, 1.0);
|
||||
const vec3 c_normal_shift = vec3(-1.0, -1.0, 0.0);
|
||||
const vec2 c_terrain_scale = vec2(1.0 / 2048.0, -1.0 / 2048.0);
|
||||
const vec2 c_terrain_shift = vec2(0.5, 0.5);
|
||||
const float c_weight_scale = 1.0 / 64.f;
|
||||
const vec3 c_world_forward = vec3(0.0, 1.0, 0.0);
|
||||
const vec3 c_world_up = vec3(0.0, 0.0, 1.0);
|
||||
|
||||
out vec3 v_position; // view space
|
||||
out vec3 v_normal; // view space
|
||||
out vec4 v_terrain_normal; // view space (x, y, z, weigth)
|
||||
out vec4 v_texcoord; // texture space (s, t, pixel_level, material_level)
|
||||
|
||||
void main(void) {
|
||||
vec3 orientation = normalize(i_orientation);
|
||||
mat3 rotation = mat3(cross(orientation, c_world_up), orientation, c_world_up);
|
||||
vec4 world_position = vec4(i_translation.xyz + rotation * a_position, 1.0);
|
||||
float weight = max(0.0, 1.0 - world_position.z * c_weight_scale);
|
||||
vec3 world_normal = rotation * normalize(a_normal);
|
||||
vec2 terrain_coords = c_terrain_shift + world_position.xy * c_terrain_scale;
|
||||
world_position.z += texture(u_height_sampler, terrain_coords).r;
|
||||
vec4 view_position = u_view * world_position;
|
||||
vec3 terrain_normal = normalize(c_normal_shift + texture(u_normal_sampler, terrain_coords).rgb * c_normal_scale);
|
||||
world_normal = mat3(cross(c_world_forward, terrain_normal), c_world_forward, terrain_normal) * world_normal;
|
||||
v_position = view_position.xyz;
|
||||
v_normal = (u_view * vec4(world_normal, 0.0)).xyz;
|
||||
v_terrain_normal = vec4((u_view * vec4(terrain_normal, 0.0)).xyz, weight);
|
||||
v_texcoord = vec4(a_texcoord, i_texlevel, i_texlevel + 1.0);
|
||||
gl_Position = u_projection * view_position;
|
||||
}
|
Reference in New Issue
Block a user