first commit

This commit is contained in:
Ugric
2026-03-02 02:17:04 +00:00
commit 5d56860f3a
813 changed files with 41799 additions and 0 deletions

View File

@@ -0,0 +1,170 @@
shader_type spatial;
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
// I had to remove source_color` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splat_index_map;
uniform sampler2D u_terrain_splat_weight_map;
uniform sampler2D u_terrain_globalmap : source_color;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
uniform sampler2DArray u_ground_albedo_bump_array : source_color;
uniform sampler2DArray u_ground_normal_roughness_array;
// TODO Have UV scales for each texture in an array?
uniform float u_ground_uv_scale;
uniform float u_globalmap_blend_start;
uniform float u_globalmap_blend_distance;
uniform bool u_depth_blending = true;
varying float v_hole;
varying vec3 v_tint;
varying vec2 v_ground_uv;
varying float v_distance_to_camera;
vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
vec3 get_depth_blended_weights(vec3 splat, vec3 bumps) {
float dh = 0.2;
vec3 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec3 d = h + dh;
d.r -= max(h.g, h.b);
d.g -= max(h.r, h.b);
d.b -= max(h.g, h.r);
vec3 w = clamp(d, 0, 1);
// Had to normalize, since this approach does not preserve components summing to 1
return w / (w.x + w.y + w.z);
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = (u_terrain_inverse_transform * wpos).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
wpos.y = h;
vec3 base_ground_uv = vec3(cell_coords.x, h * MODEL_MATRIX[1][1], cell_coords.y);
v_ground_uv = base_ground_uv.xz / u_ground_uv_scale;
// Putting this in vertex saves 2 fetches from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate tint and splat, but it's not bad overall)
vec4 tint = texture(u_terrain_colormap, UV);
v_hole = tint.a;
v_tint = tint.rgb;
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
v_distance_to_camera = distance(wpos.xyz, CAMERA_POSITION_WORLD);
}
void fragment() {
if (v_hole < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
terrain_normal_world = normalize(terrain_normal_world);
vec3 normal = terrain_normal_world;
float globalmap_factor =
clamp((v_distance_to_camera - u_globalmap_blend_start) * u_globalmap_blend_distance, 0.0, 1.0);
globalmap_factor *= globalmap_factor; // slower start, faster transition but far away
vec3 global_albedo = texture(u_terrain_globalmap, UV).rgb;
ALBEDO = global_albedo;
// Doing this branch allows to spare a bunch of texture fetches for distant pixels.
// Eventually, there could be a split between near and far shaders in the future,
// if relevant on high-end GPUs
if (globalmap_factor < 1.0) {
vec4 tex_splat_indexes = texture(u_terrain_splat_index_map, UV);
vec4 tex_splat_weights = texture(u_terrain_splat_weight_map, UV);
// TODO Can't use texelFetch!
// https://github.com/godotengine/godot/issues/31732
vec3 splat_indexes = tex_splat_indexes.rgb * 255.0;
vec3 splat_weights = vec3(
tex_splat_weights.r,
tex_splat_weights.g,
1.0 - tex_splat_weights.r - tex_splat_weights.g
);
vec4 ab0 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv, splat_indexes.x));
vec4 ab1 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv, splat_indexes.y));
vec4 ab2 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv, splat_indexes.z));
vec4 nr0 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv, splat_indexes.x));
vec4 nr1 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv, splat_indexes.y));
vec4 nr2 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv, splat_indexes.z));
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
splat_weights = get_depth_blended_weights(splat_weights, vec3(ab0.a, ab1.a, ab2.a));
}
ALBEDO = v_tint * (
ab0.rgb * splat_weights.x
+ ab1.rgb * splat_weights.y
+ ab2.rgb * splat_weights.z
);
ROUGHNESS =
nr0.a * splat_weights.x
+ nr1.a * splat_weights.y
+ nr2.a * splat_weights.z;
vec3 normal0 = unpack_normal(nr0);
vec3 normal1 = unpack_normal(nr1);
vec3 normal2 = unpack_normal(nr2);
vec3 ground_normal =
normal0 * splat_weights.x
+ normal1 * splat_weights.y
+ normal2 * splat_weights.z;
// Combine terrain normals with detail normals (not sure if correct but looks ok)
normal = normalize(vec3(
terrain_normal_world.x + ground_normal.x,
terrain_normal_world.y,
terrain_normal_world.z + ground_normal.z));
normal = mix(normal, terrain_normal_world, globalmap_factor);
ALBEDO = mix(ALBEDO, global_albedo, globalmap_factor);
//ALBEDO = vec3(splat_weight0, splat_weight1, splat_weight2);
ROUGHNESS = mix(ROUGHNESS, 1.0, globalmap_factor);
}
NORMAL = (VIEW_MATRIX * (vec4(normal, 0.0))).xyz;
}

View File

@@ -0,0 +1 @@
uid://ir0n8l1h3d08

View File

@@ -0,0 +1,87 @@
// This shader is used to bake the global albedo map.
// It exposes a subset of the main shader API, so uniform names were not modified.
shader_type spatial;
// I had to remove source_color` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splat_index_map;
uniform sampler2D u_terrain_splat_weight_map;
uniform sampler2DArray u_ground_albedo_bump_array : source_color;
// TODO Have UV scales for each texture in an array?
uniform float u_ground_uv_scale;
// Keep depth blending because it has a high effect on the final result
uniform bool u_depth_blending = true;
vec3 get_depth_blended_weights(vec3 splat, vec3 bumps) {
float dh = 0.2;
vec3 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec3 d = h + dh;
d.r -= max(h.g, h.b);
d.g -= max(h.r, h.b);
d.b -= max(h.g, h.r);
vec3 w = clamp(d, 0, 1);
// Had to normalize, since this approach does not preserve components summing to 1
return w / (w.x + w.y + w.z);
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = wpos.xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = (cell_coords / vec2(textureSize(u_terrain_splat_index_map, 0)));
}
void fragment() {
vec4 tint = texture(u_terrain_colormap, UV);
vec4 tex_splat_indexes = texture(u_terrain_splat_index_map, UV);
vec4 tex_splat_weights = texture(u_terrain_splat_weight_map, UV);
// TODO Can't use texelFetch!
// https://github.com/godotengine/godot/issues/31732
vec3 splat_indexes = tex_splat_indexes.rgb * 255.0;
// Get bump at normal resolution so depth blending is accurate
vec2 ground_uv = UV / u_ground_uv_scale;
float b0 = texture(u_ground_albedo_bump_array, vec3(ground_uv, splat_indexes.x)).a;
float b1 = texture(u_ground_albedo_bump_array, vec3(ground_uv, splat_indexes.y)).a;
float b2 = texture(u_ground_albedo_bump_array, vec3(ground_uv, splat_indexes.z)).a;
// Take the center of the highest mip as color, because we can't see details from far away.
vec2 ndc_center = vec2(0.5, 0.5);
vec3 a0 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, splat_indexes.x), 10.0).rgb;
vec3 a1 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, splat_indexes.y), 10.0).rgb;
vec3 a2 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, splat_indexes.z), 10.0).rgb;
vec3 splat_weights = vec3(
tex_splat_weights.r,
tex_splat_weights.g,
1.0 - tex_splat_weights.r - tex_splat_weights.g
);
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
splat_weights = get_depth_blended_weights(splat_weights, vec3(b0, b1, b2));
}
ALBEDO = tint.rgb * (
a0 * splat_weights.x
+ a1 * splat_weights.y
+ a2 * splat_weights.z
);
}

View File

@@ -0,0 +1 @@
uid://bgrtecan31p7t

View File

@@ -0,0 +1,107 @@
shader_type spatial;
render_mode cull_disabled;
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_detailmap;
uniform sampler2D u_terrain_normalmap;
uniform sampler2D u_terrain_globalmap : source_color;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
uniform sampler2D u_albedo_alpha : source_color;
uniform float u_view_distance = 100.0;
uniform float u_globalmap_tint_bottom : hint_range(0.0, 1.0);
uniform float u_globalmap_tint_top : hint_range(0.0, 1.0);
uniform float u_bottom_ao : hint_range(0.0, 1.0);
uniform vec2 u_ambient_wind; // x: amplitude, y: time
uniform vec3 u_instance_scale = vec3(1.0, 1.0, 1.0);
uniform float u_roughness = 0.9;
varying vec3 v_normal;
varying vec2 v_map_uv;
float get_hash(vec2 c) {
return fract(sin(dot(c.xy, vec2(12.9898,78.233))) * 43758.5453);
}
vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
n.z *= -1.0;
return n;
}
vec3 get_ambient_wind_displacement(vec2 uv, float hash) {
// TODO This is an initial basic implementation. It may be improved in the future, especially for strong wind.
float t = u_ambient_wind.y;
float amp = u_ambient_wind.x * (1.0 - uv.y);
// Main displacement
vec3 disp = amp * vec3(cos(t), 0, sin(t * 1.2));
// Fine displacement
float fine_disp_frequency = 2.0;
disp += 0.2 * amp * vec3(cos(t * (fine_disp_frequency + hash)), 0, sin(t * (fine_disp_frequency + hash) * 1.2));
return disp;
}
float get_height(sampler2D heightmap, vec2 uv) {
return sample_heightmap(heightmap, uv);
}
void vertex() {
vec4 obj_pos = MODEL_MATRIX * vec4(0, 1, 0, 1);
vec3 cell_coords = (u_terrain_inverse_transform * obj_pos).xyz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords.xz += vec2(0.5);
vec2 map_uv = cell_coords.xz / vec2(textureSize(u_terrain_heightmap, 0));
v_map_uv = map_uv;
//float density = 0.5 + 0.5 * sin(4.0*TIME); // test
float density = texture(u_terrain_detailmap, map_uv).r;
float hash = get_hash(obj_pos.xz);
if (density > hash) {
vec3 normal = normalize(
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, map_uv)));
// Snap model to the terrain
float height = get_height(u_terrain_heightmap, map_uv) / cell_coords.y;
VERTEX *= u_instance_scale;
VERTEX.y += height;
VERTEX += get_ambient_wind_displacement(UV, hash);
// Fade alpha with distance
vec3 wpos = (MODEL_MATRIX * vec4(VERTEX, 1)).xyz;
float dr = distance(wpos, CAMERA_POSITION_WORLD) / u_view_distance;
COLOR.a = clamp(1.0 - dr * dr * dr, 0.0, 1.0);
// When using billboards,
// the normal is the same as the terrain regardless of face orientation
v_normal = normal;
} else {
// Discard, output degenerate triangles
VERTEX = vec3(0, 0, 0);
}
}
void fragment() {
NORMAL = (VIEW_MATRIX * (MODEL_MATRIX * vec4(v_normal, 0.0))).xyz;
ALPHA_SCISSOR_THRESHOLD = 0.5;
ROUGHNESS = u_roughness;
vec4 col = texture(u_albedo_alpha, UV);
ALPHA = col.a * COLOR.a;// - clamp(1.4 - UV.y, 0.0, 1.0);//* 0.5 + 0.5*cos(2.0*TIME);
ALBEDO = COLOR.rgb * col.rgb;
// Blend with ground color
float nh = sqrt(max(1.0 - UV.y, 0.0));
ALBEDO = mix(ALBEDO, texture(u_terrain_globalmap, v_map_uv).rgb, mix(u_globalmap_tint_bottom, u_globalmap_tint_top, nh));
// Fake bottom AO
ALBEDO = ALBEDO * mix(1.0, 1.0 - u_bottom_ao, UV.y * UV.y);
}

View File

@@ -0,0 +1 @@
uid://d0yxj54ellbka

View File

@@ -0,0 +1,66 @@
// Use functions from this file everywhere a heightmap is used,
// so it is easy to track and change the format
float sample_heightmap(sampler2D spl, vec2 pos) {
// RF
return texture(spl, pos).r;
}
vec4 encode_height_to_viewport(float h) {
//return vec4(encode_height_to_rgb8_unorm(h), 1.0);
// Encode regular floats into an assumed RGBA8 output color.
// This is used because Godot 4.0 doesn't support RF viewports,
// and the irony is, even if float viewports get supported, it's likely it will end up RGBAF,
// which is wasting bandwidth because we are only interested in R...
uint u = floatBitsToUint(h);
return vec4(
float((u >> 0u) & 255u),
float((u >> 8u) & 255u),
float((u >> 16u) & 255u),
float((u >> 24u) & 255u)
) / vec4(255.0);
}
float decode_height_from_viewport(vec4 c) {
uint u = uint(c.r * 255.0)
| (uint(c.g * 255.0) << 8u)
| (uint(c.b * 255.0) << 16u)
| (uint(c.a * 255.0) << 24u);
return uintBitsToFloat(u);
}
float sample_height_from_viewport(sampler2D screen, vec2 uv) {
ivec2 ts = textureSize(screen, 0);
vec2 norm_to_px = vec2(ts);
// Convert to pixels and apply a small offset so we interpolate from pixel centers
vec2 uv_px_f = uv * norm_to_px - vec2(0.5);
ivec2 uv_px = ivec2(uv_px_f);
// Get interpolation pixel positions
ivec2 p00 = uv_px;
ivec2 p10 = uv_px + ivec2(1, 0);
ivec2 p01 = uv_px + ivec2(0, 1);
ivec2 p11 = uv_px + ivec2(1, 1);
// Get pixels
vec4 c00 = texelFetch(screen, p00, 0);
vec4 c10 = texelFetch(screen, p10, 0);
vec4 c01 = texelFetch(screen, p01, 0);
vec4 c11 = texelFetch(screen, p11, 0);
// Decode heights
float h00 = decode_height_from_viewport(c00);
float h10 = decode_height_from_viewport(c10);
float h01 = decode_height_from_viewport(c01);
float h11 = decode_height_from_viewport(c11);
// Linear filter
vec2 f = fract(uv_px_f);
float h = mix(mix(h00, h10, f.x), mix(h01, h11, f.x), f.y);
return h;
}

View File

@@ -0,0 +1 @@
uid://cejki3yhcmmd8

View File

@@ -0,0 +1,57 @@
const float V2_UNIT_STEPS = 1024.0;
const float V2_MIN = -8192.0;
const float V2_MAX = 8191.0;
const float V2_DF = 255.0 / V2_UNIT_STEPS;
float decode_height_from_rgb8_unorm_2(vec3 c) {
return (c.r * 0.25 + c.g * 64.0 + c.b * 16384.0) * (4.0 * V2_DF) + V2_MIN;
}
vec3 encode_height_to_rgb8_unorm_2(float h) {
// TODO Check if this has float precision issues
// TODO Modulo operator might be a performance/compatibility issue
h -= V2_MIN;
int i = int(h * V2_UNIT_STEPS);
int r = i % 256;
int g = (i / 256) % 256;
int b = i / 65536;
return vec3(float(r), float(g), float(b)) / 255.0;
}
float decode_height_from_rgb8_unorm(vec3 c) {
return decode_height_from_rgb8_unorm_2(c);
}
vec3 encode_height_to_rgb8_unorm(float h) {
return encode_height_to_rgb8_unorm_2(h);
}
// TODO Remove for now?
// Bilinear filtering appears to work well enough without doing this.
// There are some artifacts, but we could easily live with them,
// and I suspect they could be easy to patch somehow in the encoding/decoding.
//
// In case bilinear filtering is required.
// This is slower than if we had a native float format.
// Unfortunately, Godot 4 removed support for 2D HDR viewports. They were used
// to edit this format natively. Using compute shaders would force users to
// have Vulkan. So we had to downgrade performance a bit using a technique from the GLES2 era...
float sample_height_bilinear_rgb8_unorm(sampler2D heightmap, vec2 uv) {
vec2 ts = vec2(textureSize(heightmap, 0));
vec2 p00f = uv * ts;
ivec2 p00 = ivec2(p00f);
vec3 s00 = texelFetch(heightmap, p00, 0).rgb;
vec3 s10 = texelFetch(heightmap, p00 + ivec2(1, 0), 0).rgb;
vec3 s01 = texelFetch(heightmap, p00 + ivec2(0, 1), 0).rgb;
vec3 s11 = texelFetch(heightmap, p00 + ivec2(1, 1), 0).rgb;
float h00 = decode_height_from_rgb8_unorm(s00);
float h10 = decode_height_from_rgb8_unorm(s10);
float h01 = decode_height_from_rgb8_unorm(s01);
float h11 = decode_height_from_rgb8_unorm(s11);
vec2 f = p00f - vec2(p00);
return mix(mix(h00, h10, f.x), mix(h01, h11, f.x), f.y);
}

View File

@@ -0,0 +1 @@
uid://ctuxl7ko61ko0

View File

@@ -0,0 +1,71 @@
shader_type spatial;
// Development shader used to debug or help authoring.
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_map; // This map will control color
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
varying float v_hole;
vec3 unpack_normal(vec4 rgba) {
// If we consider texture space starts from top-left corner and Y goes down,
// then Y+ in pixel space corresponds to Z+ in terrain space,
// while X+ also corresponds to X+ in terrain space.
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = (u_terrain_inverse_transform * wpos).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
wpos.y = h;
// Putting this in vertex saves 2 fetches from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate tint and splat, but it's not bad overall)
vec4 tint = texture(u_terrain_colormap, UV);
v_hole = tint.a;
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
}
void fragment() {
if (v_hole < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
terrain_normal_world = normalize(terrain_normal_world);
vec3 normal = terrain_normal_world;
vec4 value = texture(u_map, UV);
// TODO Blend toward checker pattern to show the alpha channel
ALBEDO = value.rgb;
ROUGHNESS = 0.5;
NORMAL = (VIEW_MATRIX * (vec4(normal, 0.0))).xyz;
}

View File

@@ -0,0 +1 @@
uid://dquktnr0k3ora

View File

@@ -0,0 +1,63 @@
shader_type spatial;
// This is a very simple shader for a low-poly coloured visual, without textures
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
// I had to remove `hint_albedo` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;// : hint_albedo;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
varying flat vec4 v_tint;
vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
void vertex() {
vec2 cell_coords = (u_terrain_inverse_transform * MODEL_MATRIX * vec4(VERTEX, 1)).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
// Putting this in vertex saves 2 fetches from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate tint and splat, but it's not bad overall)
v_tint = texture(u_terrain_colormap, UV);
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
}
void fragment() {
if (v_tint.a < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
terrain_normal_world = normalize(terrain_normal_world);
ALBEDO = v_tint.rgb;
ROUGHNESS = 1.0;
NORMAL = normalize(cross(dFdy(VERTEX), dFdx(VERTEX)));
}

View File

@@ -0,0 +1 @@
uid://dhjiulqqbukc8

View File

@@ -0,0 +1,373 @@
shader_type spatial;
// WIP
// This shader uses a texture array with multiple splatmaps, allowing up to 16 textures.
// Only the 4 textures having highest blending weight are sampled.
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
// I had to remove source_color` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splatmap;
uniform sampler2D u_terrain_splatmap_1;
uniform sampler2D u_terrain_splatmap_2;
uniform sampler2D u_terrain_splatmap_3;
uniform sampler2D u_terrain_globalmap : source_color;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
uniform sampler2DArray u_ground_albedo_bump_array : source_color;
uniform sampler2DArray u_ground_normal_roughness_array;
uniform float u_ground_uv_scale = 20.0;
uniform bool u_depth_blending = true;
uniform float u_globalmap_blend_start;
uniform float u_globalmap_blend_distance;
uniform bool u_tile_reduction = false;
varying float v_hole;
varying vec3 v_tint;
varying vec2 v_terrain_uv;
varying vec3 v_ground_uv;
varying float v_distance_to_camera;
// TODO Can't put this in a constant: https://github.com/godotengine/godot/issues/44145
//const int TEXTURE_COUNT = 16;
vec3 unpack_normal(vec4 rgba) {
// If we consider texture space starts from top-left corner and Y goes down,
// then Y+ in pixel space corresponds to Z+ in terrain space,
// while X+ also corresponds to X+ in terrain space.
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
vec4 pack_normal(vec3 n, float a) {
n.z *= -1.0;
return vec4((n.xzy + vec3(1.0)) * 0.5, a);
}
// Blends weights according to the bump of detail textures,
// so for example it allows to have sand fill the gaps between pebbles
vec4 get_depth_blended_weights(vec4 splat, vec4 bumps) {
float dh = 0.2;
vec4 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec4 d = h + dh;
d.r -= max(h.g, max(h.b, h.a));
d.g -= max(h.r, max(h.b, h.a));
d.b -= max(h.g, max(h.r, h.a));
d.a -= max(h.g, max(h.b, h.r));
return clamp(d, 0, 1);
}
vec3 get_triplanar_blend(vec3 world_normal) {
vec3 blending = abs(world_normal);
blending = normalize(max(blending, vec3(0.00001))); // Force weights to sum to 1.0
float b = blending.x + blending.y + blending.z;
return blending / vec3(b, b, b);
}
vec4 texture_triplanar(sampler2D tex, vec3 world_pos, vec3 blend) {
vec4 xaxis = texture(tex, world_pos.yz);
vec4 yaxis = texture(tex, world_pos.xz);
vec4 zaxis = texture(tex, world_pos.xy);
// blend the results of the 3 planar projections.
return xaxis * blend.x + yaxis * blend.y + zaxis * blend.z;
}
void get_splat_weights(vec2 uv, out vec4 out_high_indices, out vec4 out_high_weights) {
vec4 ew0 = texture(u_terrain_splatmap, uv);
vec4 ew1 = texture(u_terrain_splatmap_1, uv);
vec4 ew2 = texture(u_terrain_splatmap_2, uv);
vec4 ew3 = texture(u_terrain_splatmap_3, uv);
float weights[16] = {
ew0.r, ew0.g, ew0.b, ew0.a,
ew1.r, ew1.g, ew1.b, ew1.a,
ew2.r, ew2.g, ew2.b, ew2.a,
ew3.r, ew3.g, ew3.b, ew3.a
};
// float weights_sum = 0.0;
// for (int i = 0; i < 16; ++i) {
// weights_sum += weights[i];
// }
// for (int i = 0; i < 16; ++i) {
// weights_sum /= weights_sum;
// }
// weights_sum=1.1;
// Now we have to pick the 4 highest weights and use them to blend textures.
// Using arrays because Godot's shader version doesn't support dynamic indexing of vectors
// TODO We should not need to initialize, but apparently we don't always find 4 weights
int high_indices_array[4] = {0, 0, 0, 0};
float high_weights_array[4] = {0.0, 0.0, 0.0, 0.0};
int count = 0;
// We know weights are supposed to be normalized.
// That means the highest value of the pivot above which we can find 4 results
// is 1.0 / 4.0. However that would mean exactly 4 textures have exactly that weight,
// which is very unlikely. If we consider 1.0 / 5.0, we are a bit more likely to find
// 4 results, and finding 5 results remains almost impossible.
float pivot = /*weights_sum*/1.0 / 5.0;
for (int i = 0; i < 16; ++i) {
if (weights[i] > pivot) {
high_weights_array[count] = weights[i];
high_indices_array[count] = i;
weights[i] = 0.0;
++count;
}
}
while (count < 4 && pivot > 0.0) {
float max_weight = 0.0;
int max_index = 0;
for (int i = 0; i < 16; ++i) {
if (/*weights[i] <= pivot && */weights[i] > max_weight) {
max_weight = weights[i];
max_index = i;
weights[i] = 0.0;
}
}
high_indices_array[count] = max_index;
high_weights_array[count] = max_weight;
++count;
pivot = max_weight;
}
out_high_weights = vec4(
high_weights_array[0], high_weights_array[1],
high_weights_array[2], high_weights_array[3]);
out_high_indices = vec4(
float(high_indices_array[0]), float(high_indices_array[1]),
float(high_indices_array[2]), float(high_indices_array[3]));
out_high_weights /=
out_high_weights.r + out_high_weights.g + out_high_weights.b + out_high_weights.a;
}
vec4 depth_blend2(vec4 a_value, float a_bump, vec4 b_value, float b_bump, float t) {
// https://www.gamasutra.com
// /blogs/AndreyMishkinis/20130716/196339/Advanced_Terrain_Texture_Splatting.php
float d = 0.1;
float ma = max(a_bump + (1.0 - t), b_bump + t) - d;
float ba = max(a_bump + (1.0 - t) - ma, 0.0);
float bb = max(b_bump + t - ma, 0.0);
return (a_value * ba + b_value * bb) / (ba + bb);
}
vec2 rotate(vec2 v, float cosa, float sina) {
return vec2(cosa * v.x - sina * v.y, sina * v.x + cosa * v.y);
}
vec4 texture_array_antitile(sampler2DArray albedo_tex, sampler2DArray normal_tex, vec3 uv,
out vec4 out_normal) {
float frequency = 2.0;
float scale = 1.3;
float sharpness = 0.7;
// Rotate and scale UV
float rot = 3.14 * 0.6;
float cosa = cos(rot);
float sina = sin(rot);
vec3 uv2 = vec3(rotate(uv.xy, cosa, sina) * scale, uv.z);
vec4 col0 = texture(albedo_tex, uv);
vec4 col1 = texture(albedo_tex, uv2);
vec4 nrm0 = texture(normal_tex, uv);
vec4 nrm1 = texture(normal_tex, uv2);
//col0 = vec4(0.0, 0.5, 0.5, 1.0); // Highlights variations
// Normals have to be rotated too since we are rotating the texture...
// TODO Probably not the most efficient but understandable for now
vec3 n = unpack_normal(nrm1);
// Had to negate the Y axis for some reason. I never remember the myriad of conventions around
n.xz = rotate(n.xz, cosa, -sina);
nrm1 = pack_normal(n, nrm1.a);
// Periodically alternate between the two versions using a warped checker pattern
float t = 1.1 + 0.5
* sin(uv2.x * frequency + sin(uv.x) * 2.0)
* cos(uv2.y * frequency + sin(uv.y) * 2.0); // Result in [0..2]
t = smoothstep(sharpness, 2.0 - sharpness, t);
// Using depth blend because classic alpha blending smoothes out details.
out_normal = depth_blend2(nrm0, col0.a, nrm1, col1.a, t);
return depth_blend2(col0, col0.a, col1, col1.a, t);
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = (u_terrain_inverse_transform * wpos).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
wpos.y = h;
vec3 base_ground_uv = vec3(cell_coords.x, h * MODEL_MATRIX[1][1], cell_coords.y);
v_ground_uv = base_ground_uv / u_ground_uv_scale;
// Putting this in vertex saves a fetch from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate it, but it's not bad overall)
vec4 tint = texture(u_terrain_colormap, UV);
v_hole = tint.a;
v_tint = tint.rgb;
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
v_distance_to_camera = distance(wpos.xyz, CAMERA_POSITION_WORLD);
}
void fragment() {
if (v_hole < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * (unpack_normal(texture(u_terrain_normalmap, UV)));
terrain_normal_world = normalize(terrain_normal_world);
vec3 normal = terrain_normal_world;
float globalmap_factor = clamp((v_distance_to_camera - u_globalmap_blend_start)
* u_globalmap_blend_distance, 0.0, 1.0);
globalmap_factor *= globalmap_factor; // slower start, faster transition but far away
vec3 global_albedo = texture(u_terrain_globalmap, UV).rgb;
ALBEDO = global_albedo;
// Doing this branch allows to spare a bunch of texture fetches for distant pixels.
// Eventually, there could be a split between near and far shaders in the future,
// if relevant on high-end GPUs
if (globalmap_factor < 1.0) {
vec4 high_indices;
vec4 high_weights;
get_splat_weights(UV, high_indices, high_weights);
vec4 ab0, ab1, ab2, ab3;
vec4 nr0, nr1, nr2, nr3;
if (u_tile_reduction) {
ab0 = texture_array_antitile(
u_ground_albedo_bump_array, u_ground_normal_roughness_array,
vec3(v_ground_uv.xz, high_indices.x), nr0);
ab1 = texture_array_antitile(
u_ground_albedo_bump_array, u_ground_normal_roughness_array,
vec3(v_ground_uv.xz, high_indices.y), nr1);
ab2 = texture_array_antitile(
u_ground_albedo_bump_array, u_ground_normal_roughness_array,
vec3(v_ground_uv.xz, high_indices.z), nr2);
ab3 = texture_array_antitile(
u_ground_albedo_bump_array, u_ground_normal_roughness_array,
vec3(v_ground_uv.xz, high_indices.w), nr3);
} else {
ab0 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.x));
ab1 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.y));
ab2 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.z));
ab3 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.w));
nr0 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv.xz, high_indices.x));
nr1 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv.xz, high_indices.y));
nr2 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv.xz, high_indices.z));
nr3 = texture(u_ground_normal_roughness_array, vec3(v_ground_uv.xz, high_indices.w));
}
vec3 col0 = ab0.rgb * v_tint;
vec3 col1 = ab1.rgb * v_tint;
vec3 col2 = ab2.rgb * v_tint;
vec3 col3 = ab3.rgb * v_tint;
vec4 rough = vec4(nr0.a, nr1.a, nr2.a, nr3.a);
vec3 normal0 = unpack_normal(nr0);
vec3 normal1 = unpack_normal(nr1);
vec3 normal2 = unpack_normal(nr2);
vec3 normal3 = unpack_normal(nr3);
vec4 w;
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
w = get_depth_blended_weights(high_weights, vec4(ab0.a, ab1.a, ab2.a, ab3.a));
} else {
w = high_weights;
}
float w_sum = (w.r + w.g + w.b + w.a);
ALBEDO = (
w.r * col0.rgb +
w.g * col1.rgb +
w.b * col2.rgb +
w.a * col3.rgb) / w_sum;
ROUGHNESS = (
w.r * rough.r +
w.g * rough.g +
w.b * rough.b +
w.a * rough.a) / w_sum;
vec3 ground_normal = /*u_terrain_normal_basis **/ (
w.r * normal0 +
w.g * normal1 +
w.b * normal2 +
w.a * normal3) / w_sum;
// If no splat textures are defined, normal vectors will default to (1,1,1),
// which is incorrect, and causes the terrain to be shaded wrongly in some directions.
// However, this should not be a problem to fix in the shader,
// because there MUST be at least one splat texture set.
//ground_normal = normalize(ground_normal);
// TODO Make the plugin insert a default normalmap if it's empty
// Combine terrain normals with detail normals (not sure if correct but looks ok)
normal = normalize(vec3(
terrain_normal_world.x + ground_normal.x,
terrain_normal_world.y,
terrain_normal_world.z + ground_normal.z));
normal = mix(normal, terrain_normal_world, globalmap_factor);
ALBEDO = mix(ALBEDO, global_albedo, globalmap_factor);
ROUGHNESS = mix(ROUGHNESS, 1.0, globalmap_factor);
// if(count < 3) {
// ALBEDO = vec3(1.0, 0.0, 0.0);
// }
// Show splatmap weights
//ALBEDO = w.rgb;
}
// Highlight all pixels undergoing no splatmap at all
// else {
// ALBEDO = vec3(1.0, 0.0, 0.0);
// }
NORMAL = (VIEW_MATRIX * (vec4(normal, 0.0))).xyz;
}

View File

@@ -0,0 +1 @@
uid://bk4itxtdgihwp

View File

@@ -0,0 +1,173 @@
shader_type spatial;
// This shader uses a texture array with multiple splatmaps, allowing up to 16 textures.
// Only the 4 textures having highest blending weight are sampled.
// I had to remove source_color` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splatmap;
uniform sampler2D u_terrain_splatmap_1;
uniform sampler2D u_terrain_splatmap_2;
uniform sampler2D u_terrain_splatmap_3;
uniform sampler2DArray u_ground_albedo_bump_array : source_color;
uniform float u_ground_uv_scale = 20.0;
uniform bool u_depth_blending = true;
// TODO Can't put this in a constant: https://github.com/godotengine/godot/issues/44145
//const int TEXTURE_COUNT = 16;
// Blends weights according to the bump of detail textures,
// so for example it allows to have sand fill the gaps between pebbles
vec4 get_depth_blended_weights(vec4 splat, vec4 bumps) {
float dh = 0.2;
vec4 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec4 d = h + dh;
d.r -= max(h.g, max(h.b, h.a));
d.g -= max(h.r, max(h.b, h.a));
d.b -= max(h.g, max(h.r, h.a));
d.a -= max(h.g, max(h.b, h.r));
return clamp(d, 0, 1);
}
void get_splat_weights(vec2 uv, out vec4 out_high_indices, out vec4 out_high_weights) {
vec4 ew0 = texture(u_terrain_splatmap, uv);
vec4 ew1 = texture(u_terrain_splatmap_1, uv);
vec4 ew2 = texture(u_terrain_splatmap_2, uv);
vec4 ew3 = texture(u_terrain_splatmap_3, uv);
float weights[16] = {
ew0.r, ew0.g, ew0.b, ew0.a,
ew1.r, ew1.g, ew1.b, ew1.a,
ew2.r, ew2.g, ew2.b, ew2.a,
ew3.r, ew3.g, ew3.b, ew3.a
};
// float weights_sum = 0.0;
// for (int i = 0; i < 16; ++i) {
// weights_sum += weights[i];
// }
// for (int i = 0; i < 16; ++i) {
// weights_sum /= weights_sum;
// }
// weights_sum=1.1;
// Now we have to pick the 4 highest weights and use them to blend textures.
// Using arrays because Godot's shader version doesn't support dynamic indexing of vectors
// TODO We should not need to initialize, but apparently we don't always find 4 weights
int high_indices_array[4] = {0, 0, 0, 0};
float high_weights_array[4] = {0.0, 0.0, 0.0, 0.0};
int count = 0;
// We know weights are supposed to be normalized.
// That means the highest value of the pivot above which we can find 4 results
// is 1.0 / 4.0. However that would mean exactly 4 textures have exactly that weight,
// which is very unlikely. If we consider 1.0 / 5.0, we are a bit more likely to find
// 4 results, and finding 5 results remains almost impossible.
float pivot = /*weights_sum*/1.0 / 5.0;
for (int i = 0; i < 16; ++i) {
if (weights[i] > pivot) {
high_weights_array[count] = weights[i];
high_indices_array[count] = i;
weights[i] = 0.0;
++count;
}
}
while (count < 4 && pivot > 0.0) {
float max_weight = 0.0;
int max_index = 0;
for (int i = 0; i < 16; ++i) {
if (/*weights[i] <= pivot && */weights[i] > max_weight) {
max_weight = weights[i];
max_index = i;
weights[i] = 0.0;
}
}
high_indices_array[count] = max_index;
high_weights_array[count] = max_weight;
++count;
pivot = max_weight;
}
out_high_weights = vec4(
high_weights_array[0], high_weights_array[1],
high_weights_array[2], high_weights_array[3]);
out_high_indices = vec4(
float(high_indices_array[0]), float(high_indices_array[1]),
float(high_indices_array[2]), float(high_indices_array[3]));
out_high_weights /=
out_high_weights.r + out_high_weights.g + out_high_weights.b + out_high_weights.a;
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = wpos.xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_splatmap, 0));
}
void fragment() {
// These were moved from vertex to fragment,
// so we can generate part of the global map with just one quad and we get full quality
vec3 tint = texture(u_terrain_colormap, UV).rgb;
vec4 splat = texture(u_terrain_splatmap, UV);
vec4 high_indices;
vec4 high_weights;
get_splat_weights(UV, high_indices, high_weights);
// Get bump at normal resolution so depth blending is accurate
vec2 ground_uv = UV / u_ground_uv_scale;
float b0 = texture(u_ground_albedo_bump_array, vec3(ground_uv, high_indices.x)).a;
float b1 = texture(u_ground_albedo_bump_array, vec3(ground_uv, high_indices.y)).a;
float b2 = texture(u_ground_albedo_bump_array, vec3(ground_uv, high_indices.z)).a;
float b3 = texture(u_ground_albedo_bump_array, vec3(ground_uv, high_indices.w)).a;
// Take the center of the highest mip as color, because we can't see details from far away.
vec2 ndc_center = vec2(0.5, 0.5);
vec3 a0 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, high_indices.x), 10.0).rgb;
vec3 a1 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, high_indices.y), 10.0).rgb;
vec3 a2 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, high_indices.z), 10.0).rgb;
vec3 a3 = textureLod(u_ground_albedo_bump_array, vec3(ndc_center, high_indices.w), 10.0).rgb;
vec3 col0 = a0 * tint;
vec3 col1 = a1 * tint;
vec3 col2 = a2 * tint;
vec3 col3 = a3 * tint;
vec4 w;
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
w = get_depth_blended_weights(high_weights, vec4(b0, b1, b2, b3));
} else {
w = high_weights;
}
float w_sum = (w.r + w.g + w.b + w.a);
ALBEDO = (
w.r * col0.rgb +
w.g * col1.rgb +
w.b * col2.rgb +
w.a * col3.rgb) / w_sum;
}

View File

@@ -0,0 +1 @@
uid://dnosi2jx6eapb

View File

@@ -0,0 +1,254 @@
shader_type spatial;
// WIP
// This shader uses a texture array with multiple splatmaps, allowing up to 16 textures.
// Only the 4 textures having highest blending weight are sampled.
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
// I had to remove source_color` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splatmap;
uniform sampler2D u_terrain_splatmap_1;
uniform sampler2D u_terrain_splatmap_2;
uniform sampler2D u_terrain_splatmap_3;
uniform sampler2D u_terrain_globalmap : source_color;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
uniform sampler2DArray u_ground_albedo_bump_array : source_color;
uniform float u_ground_uv_scale = 20.0;
uniform bool u_depth_blending = true;
uniform float u_globalmap_blend_start;
uniform float u_globalmap_blend_distance;
varying float v_hole;
varying vec3 v_tint;
varying vec2 v_terrain_uv;
varying vec3 v_ground_uv;
varying float v_distance_to_camera;
// TODO Can't put this in a constant: https://github.com/godotengine/godot/issues/44145
//const int TEXTURE_COUNT = 16;
vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
// Blends weights according to the bump of detail textures,
// so for example it allows to have sand fill the gaps between pebbles
vec4 get_depth_blended_weights(vec4 splat, vec4 bumps) {
float dh = 0.2;
vec4 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec4 d = h + dh;
d.r -= max(h.g, max(h.b, h.a));
d.g -= max(h.r, max(h.b, h.a));
d.b -= max(h.g, max(h.r, h.a));
d.a -= max(h.g, max(h.b, h.r));
return clamp(d, 0, 1);
}
vec3 get_triplanar_blend(vec3 world_normal) {
vec3 blending = abs(world_normal);
blending = normalize(max(blending, vec3(0.00001))); // Force weights to sum to 1.0
float b = blending.x + blending.y + blending.z;
return blending / vec3(b, b, b);
}
vec4 texture_triplanar(sampler2D tex, vec3 world_pos, vec3 blend) {
vec4 xaxis = texture(tex, world_pos.yz);
vec4 yaxis = texture(tex, world_pos.xz);
vec4 zaxis = texture(tex, world_pos.xy);
// blend the results of the 3 planar projections.
return xaxis * blend.x + yaxis * blend.y + zaxis * blend.z;
}
void get_splat_weights(vec2 uv, out vec4 out_high_indices, out vec4 out_high_weights) {
vec4 ew0 = texture(u_terrain_splatmap, uv);
vec4 ew1 = texture(u_terrain_splatmap_1, uv);
vec4 ew2 = texture(u_terrain_splatmap_2, uv);
vec4 ew3 = texture(u_terrain_splatmap_3, uv);
float weights[16] = {
ew0.r, ew0.g, ew0.b, ew0.a,
ew1.r, ew1.g, ew1.b, ew1.a,
ew2.r, ew2.g, ew2.b, ew2.a,
ew3.r, ew3.g, ew3.b, ew3.a
};
// float weights_sum = 0.0;
// for (int i = 0; i < 16; ++i) {
// weights_sum += weights[i];
// }
// for (int i = 0; i < 16; ++i) {
// weights_sum /= weights_sum;
// }
// weights_sum=1.1;
// Now we have to pick the 4 highest weights and use them to blend textures.
// Using arrays because Godot's shader version doesn't support dynamic indexing of vectors
// TODO We should not need to initialize, but apparently we don't always find 4 weights
int high_indices_array[4] = {0, 0, 0, 0};
float high_weights_array[4] = {0.0, 0.0, 0.0, 0.0};
int count = 0;
// We know weights are supposed to be normalized.
// That means the highest value of the pivot above which we can find 4 results
// is 1.0 / 4.0. However that would mean exactly 4 textures have exactly that weight,
// which is very unlikely. If we consider 1.0 / 5.0, we are a bit more likely to find
// 4 results, and finding 5 results remains almost impossible.
float pivot = /*weights_sum*/1.0 / 5.0;
for (int i = 0; i < 16; ++i) {
if (weights[i] > pivot) {
high_weights_array[count] = weights[i];
high_indices_array[count] = i;
weights[i] = 0.0;
++count;
}
}
while (count < 4 && pivot > 0.0) {
float max_weight = 0.0;
int max_index = 0;
for (int i = 0; i < 16; ++i) {
if (/*weights[i] <= pivot && */weights[i] > max_weight) {
max_weight = weights[i];
max_index = i;
weights[i] = 0.0;
}
}
high_indices_array[count] = max_index;
high_weights_array[count] = max_weight;
++count;
pivot = max_weight;
}
out_high_weights = vec4(
high_weights_array[0], high_weights_array[1],
high_weights_array[2], high_weights_array[3]);
out_high_indices = vec4(
float(high_indices_array[0]), float(high_indices_array[1]),
float(high_indices_array[2]), float(high_indices_array[3]));
out_high_weights /=
out_high_weights.r + out_high_weights.g + out_high_weights.b + out_high_weights.a;
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = (u_terrain_inverse_transform * wpos).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
wpos.y = h;
vec3 base_ground_uv = vec3(cell_coords.x, h * MODEL_MATRIX[1][1], cell_coords.y);
v_ground_uv = base_ground_uv / u_ground_uv_scale;
// Putting this in vertex saves a fetch from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate it, but it's not bad overall)
vec4 tint = texture(u_terrain_colormap, UV);
v_hole = tint.a;
v_tint = tint.rgb;
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
v_distance_to_camera = distance(wpos.xyz, CAMERA_POSITION_WORLD);
}
void fragment() {
if (v_hole < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
terrain_normal_world = normalize(terrain_normal_world);
float globalmap_factor = clamp((v_distance_to_camera - u_globalmap_blend_start)
* u_globalmap_blend_distance, 0.0, 1.0);
globalmap_factor *= globalmap_factor; // slower start, faster transition but far away
vec3 global_albedo = texture(u_terrain_globalmap, UV).rgb;
ALBEDO = global_albedo;
// Doing this branch allows to spare a bunch of texture fetches for distant pixels.
// Eventually, there could be a split between near and far shaders in the future,
// if relevant on high-end GPUs
if (globalmap_factor < 1.0) {
vec4 high_indices;
vec4 high_weights;
get_splat_weights(UV, high_indices, high_weights);
vec4 ab0 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.x));
vec4 ab1 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.y));
vec4 ab2 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.z));
vec4 ab3 = texture(u_ground_albedo_bump_array, vec3(v_ground_uv.xz, high_indices.w));
vec3 col0 = ab0.rgb * v_tint;
vec3 col1 = ab1.rgb * v_tint;
vec3 col2 = ab2.rgb * v_tint;
vec3 col3 = ab3.rgb * v_tint;
vec4 w;
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
w = get_depth_blended_weights(high_weights, vec4(ab0.a, ab1.a, ab2.a, ab3.a));
} else {
w = high_weights;
}
float w_sum = (w.r + w.g + w.b + w.a);
ALBEDO = (
w.r * col0.rgb +
w.g * col1.rgb +
w.b * col2.rgb +
w.a * col3.rgb) / w_sum;
ALBEDO = mix(ALBEDO, global_albedo, globalmap_factor);
ROUGHNESS = mix(ROUGHNESS, 1.0, globalmap_factor);
// if(count < 3) {
// ALBEDO = vec3(1.0, 0.0, 0.0);
// }
// Show splatmap weights
//ALBEDO = w.rgb;
}
// Highlight all pixels undergoing no splatmap at all
// else {
// ALBEDO = vec3(1.0, 0.0, 0.0);
// }
NORMAL = (VIEW_MATRIX * (vec4(terrain_normal_world, 0.0))).xyz;
}

View File

@@ -0,0 +1 @@
uid://dykqpsolnarhn

View File

@@ -0,0 +1,329 @@
shader_type spatial;
// This is the reference shader of the plugin, and has the most features.
// it should be preferred for high-end graphics cards.
// For less features but lower-end targets, see the lite version.
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
// I had to remove `hint_albedo` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;
uniform sampler2D u_terrain_splatmap;
uniform sampler2D u_terrain_globalmap : source_color;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
// the reason bump is preferred with albedo is, roughness looks better with normal maps.
// If we want no normal mapping, roughness would only give flat mirror surfaces,
// while bump still allows to do depth-blending for free.
uniform sampler2D u_ground_albedo_bump_0 : source_color;
uniform sampler2D u_ground_albedo_bump_1 : source_color;
uniform sampler2D u_ground_albedo_bump_2 : source_color;
uniform sampler2D u_ground_albedo_bump_3 : source_color;
uniform sampler2D u_ground_normal_roughness_0;
uniform sampler2D u_ground_normal_roughness_1;
uniform sampler2D u_ground_normal_roughness_2;
uniform sampler2D u_ground_normal_roughness_3;
// Had to give this uniform a suffix, because it's declared as a simple float
// in other shaders, and its type cannot be inferred by the plugin.
// See https://github.com/godotengine/godot/issues/24488
uniform vec4 u_ground_uv_scale_per_texture = vec4(20.0, 20.0, 20.0, 20.0);
uniform bool u_depth_blending = true;
uniform bool u_triplanar = false;
// Each component corresponds to a ground texture. Set greater than zero to enable.
uniform vec4 u_tile_reduction = vec4(0.0, 0.0, 0.0, 0.0);
uniform float u_globalmap_blend_start;
uniform float u_globalmap_blend_distance;
uniform vec4 u_colormap_opacity_per_texture = vec4(1.0, 1.0, 1.0, 1.0);
varying float v_hole;
varying vec3 v_tint0;
varying vec3 v_tint1;
varying vec3 v_tint2;
varying vec3 v_tint3;
varying vec4 v_splat;
varying vec2 v_ground_uv0;
varying vec2 v_ground_uv1;
varying vec2 v_ground_uv2;
varying vec3 v_ground_uv3;
varying float v_distance_to_camera;
vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
vec4 pack_normal(vec3 n, float a) {
n.z *= -1.0;
return vec4((n.xzy + vec3(1.0)) * 0.5, a);
}
// Blends weights according to the bump of detail textures,
// so for example it allows to have sand fill the gaps between pebbles
vec4 get_depth_blended_weights(vec4 splat, vec4 bumps) {
float dh = 0.2;
vec4 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec4 d = h + dh;
d.r -= max(h.g, max(h.b, h.a));
d.g -= max(h.r, max(h.b, h.a));
d.b -= max(h.g, max(h.r, h.a));
d.a -= max(h.g, max(h.b, h.r));
return clamp(d, 0, 1);
}
vec3 get_triplanar_blend(vec3 world_normal) {
vec3 blending = abs(world_normal);
blending = normalize(max(blending, vec3(0.00001))); // Force weights to sum to 1.0
float b = blending.x + blending.y + blending.z;
return blending / vec3(b, b, b);
}
vec4 texture_triplanar(sampler2D tex, vec3 world_pos, vec3 blend) {
vec4 xaxis = texture(tex, world_pos.yz);
vec4 yaxis = texture(tex, world_pos.xz);
vec4 zaxis = texture(tex, world_pos.xy);
// blend the results of the 3 planar projections.
return xaxis * blend.x + yaxis * blend.y + zaxis * blend.z;
}
vec4 depth_blend2(vec4 a_value, float a_bump, vec4 b_value, float b_bump, float t) {
// https://www.gamasutra.com
// /blogs/AndreyMishkinis/20130716/196339/Advanced_Terrain_Texture_Splatting.php
float d = 0.1;
float ma = max(a_bump + (1.0 - t), b_bump + t) - d;
float ba = max(a_bump + (1.0 - t) - ma, 0.0);
float bb = max(b_bump + t - ma, 0.0);
return (a_value * ba + b_value * bb) / (ba + bb);
}
vec2 rotate(vec2 v, float cosa, float sina) {
return vec2(cosa * v.x - sina * v.y, sina * v.x + cosa * v.y);
}
vec4 texture_antitile(sampler2D albedo_tex, sampler2D normal_tex, vec2 uv, out vec4 out_normal) {
float frequency = 2.0;
float scale = 1.3;
float sharpness = 0.7;
// Rotate and scale UV
float rot = 3.14 * 0.6;
float cosa = cos(rot);
float sina = sin(rot);
vec2 uv2 = rotate(uv, cosa, sina) * scale;
vec4 col0 = texture(albedo_tex, uv);
vec4 col1 = texture(albedo_tex, uv2);
vec4 nrm0 = texture(normal_tex, uv);
vec4 nrm1 = texture(normal_tex, uv2);
//col0 = vec4(0.0, 0.5, 0.5, 1.0); // Highlights variations
// Normals have to be rotated too since we are rotating the texture...
// TODO Probably not the most efficient but understandable for now
vec3 n = unpack_normal(nrm1);
// Had to negate the Y axis for some reason. I never remember the myriad of conventions around
n.xz = rotate(n.xz, cosa, -sina);
nrm1 = pack_normal(n, nrm1.a);
// Periodically alternate between the two versions using a warped checker pattern
float t = 1.2 +
sin(uv2.x * frequency + sin(uv.x) * 2.0)
* cos(uv2.y * frequency + sin(uv.y) * 2.0); // Result in [0..2]
t = smoothstep(sharpness, 2.0 - sharpness, t);
// Using depth blend because classic alpha blending smoothes out details.
out_normal = depth_blend2(nrm0, col0.a, nrm1, col1.a, t);
return depth_blend2(col0, col0.a, col1, col1.a, t);
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = (u_terrain_inverse_transform * wpos).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
wpos.y = h;
vec3 base_ground_uv = vec3(cell_coords.x, h * MODEL_MATRIX[1][1], cell_coords.y);
v_ground_uv0 = base_ground_uv.xz / u_ground_uv_scale_per_texture.x;
v_ground_uv1 = base_ground_uv.xz / u_ground_uv_scale_per_texture.y;
v_ground_uv2 = base_ground_uv.xz / u_ground_uv_scale_per_texture.z;
v_ground_uv3 = base_ground_uv / u_ground_uv_scale_per_texture.w;
// Putting this in vertex saves 2 fetches from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate tint and splat, but it's not bad overall)
vec4 tint = texture(u_terrain_colormap, UV);
v_hole = tint.a;
v_tint0 = mix(vec3(1.0), tint.rgb, u_colormap_opacity_per_texture.x);
v_tint1 = mix(vec3(1.0), tint.rgb, u_colormap_opacity_per_texture.y);
v_tint2 = mix(vec3(1.0), tint.rgb, u_colormap_opacity_per_texture.z);
v_tint3 = mix(vec3(1.0), tint.rgb, u_colormap_opacity_per_texture.w);
v_splat = texture(u_terrain_splatmap, UV);
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
v_distance_to_camera = distance(wpos.xyz, CAMERA_POSITION_WORLD);
}
void fragment() {
if (v_hole < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
terrain_normal_world = normalize(terrain_normal_world);
vec3 normal = terrain_normal_world;
float globalmap_factor = clamp((v_distance_to_camera - u_globalmap_blend_start)
* u_globalmap_blend_distance, 0.0, 1.0);
globalmap_factor *= globalmap_factor; // slower start, faster transition but far away
vec3 global_albedo = texture(u_terrain_globalmap, UV).rgb;
ALBEDO = global_albedo;
// Doing this branch allows to spare a bunch of texture fetches for distant pixels.
// Eventually, there could be a split between near and far shaders in the future,
// if relevant on high-end GPUs
if (globalmap_factor < 1.0) {
vec4 ab0, ab1, ab2, ab3;
vec4 nr0, nr1, nr2, nr3;
if (u_triplanar) {
// Only do triplanar on one texture slot,
// because otherwise it would be very expensive and cost many more ifs.
// I chose the last slot because first slot is the default on new splatmaps,
// and that's a feature used for cliffs, which are usually designed later.
vec3 blending = get_triplanar_blend(terrain_normal_world);
ab3 = texture_triplanar(u_ground_albedo_bump_3, v_ground_uv3, blending);
nr3 = texture_triplanar(u_ground_normal_roughness_3, v_ground_uv3, blending);
} else {
if (u_tile_reduction[3] > 0.0) {
ab3 = texture_antitile(
u_ground_albedo_bump_3, u_ground_normal_roughness_3, v_ground_uv3.xz, nr3);
} else {
ab3 = texture(u_ground_albedo_bump_3, v_ground_uv3.xz);
nr3 = texture(u_ground_normal_roughness_3, v_ground_uv3.xz);
}
}
if (u_tile_reduction[0] > 0.0) {
ab0 = texture_antitile(
u_ground_albedo_bump_0, u_ground_normal_roughness_0, v_ground_uv0, nr0);
} else {
ab0 = texture(u_ground_albedo_bump_0, v_ground_uv0);
nr0 = texture(u_ground_normal_roughness_0, v_ground_uv0);
}
if (u_tile_reduction[1] > 0.0) {
ab1 = texture_antitile(
u_ground_albedo_bump_1, u_ground_normal_roughness_1, v_ground_uv1, nr1);
} else {
ab1 = texture(u_ground_albedo_bump_1, v_ground_uv1);
nr1 = texture(u_ground_normal_roughness_1, v_ground_uv1);
}
if (u_tile_reduction[2] > 0.0) {
ab2 = texture_antitile(
u_ground_albedo_bump_2, u_ground_normal_roughness_2, v_ground_uv2, nr2);
} else {
ab2 = texture(u_ground_albedo_bump_2, v_ground_uv2);
nr2 = texture(u_ground_normal_roughness_2, v_ground_uv2);
}
vec3 col0 = ab0.rgb * v_tint0;
vec3 col1 = ab1.rgb * v_tint1;
vec3 col2 = ab2.rgb * v_tint2;
vec3 col3 = ab3.rgb * v_tint3;
vec4 rough = vec4(nr0.a, nr1.a, nr2.a, nr3.a);
vec3 normal0 = unpack_normal(nr0);
vec3 normal1 = unpack_normal(nr1);
vec3 normal2 = unpack_normal(nr2);
vec3 normal3 = unpack_normal(nr3);
vec4 w;
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
w = get_depth_blended_weights(v_splat, vec4(ab0.a, ab1.a, ab2.a, ab3.a));
} else {
w = v_splat.rgba;
}
float w_sum = (w.r + w.g + w.b + w.a);
ALBEDO = (
w.r * col0.rgb +
w.g * col1.rgb +
w.b * col2.rgb +
w.a * col3.rgb) / w_sum;
ROUGHNESS = (
w.r * rough.r +
w.g * rough.g +
w.b * rough.b +
w.a * rough.a) / w_sum;
vec3 ground_normal = /*u_terrain_normal_basis **/ (
w.r * normal0 +
w.g * normal1 +
w.b * normal2 +
w.a * normal3) / w_sum;
// If no splat textures are defined, normal vectors will default to (1,1,1),
// which is incorrect, and causes the terrain to be shaded wrongly in some directions.
// However, this should not be a problem to fix in the shader,
// because there MUST be at least one splat texture set.
//ground_normal = normalize(ground_normal);
// TODO Make the plugin insert a default normalmap if it's empty
// Combine terrain normals with detail normals (not sure if correct but looks ok)
normal = normalize(vec3(
terrain_normal_world.x + ground_normal.x,
terrain_normal_world.y,
terrain_normal_world.z + ground_normal.z));
normal = mix(normal, terrain_normal_world, globalmap_factor);
ALBEDO = mix(ALBEDO, global_albedo, globalmap_factor);
ROUGHNESS = mix(ROUGHNESS, 1.0, globalmap_factor);
// Show splatmap weights
//ALBEDO = w.rgb;
}
// Highlight all pixels undergoing no splatmap at all
// else {
// ALBEDO = vec3(1.0, 0.0, 0.0);
// }
NORMAL = (VIEW_MATRIX * (vec4(normal, 0.0))).xyz;
}

View File

@@ -0,0 +1 @@
uid://c2h0pfp2xcy4x

View File

@@ -0,0 +1,83 @@
shader_type spatial;
// This shader is used to bake the global albedo map.
// It exposes a subset of the main shader API, so uniform names were not modified.
// I had to remove `hint_albedo` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;// : hint_albedo;
uniform sampler2D u_terrain_splatmap;
uniform sampler2D u_ground_albedo_bump_0 : source_color;
uniform sampler2D u_ground_albedo_bump_1 : source_color;
uniform sampler2D u_ground_albedo_bump_2 : source_color;
uniform sampler2D u_ground_albedo_bump_3 : source_color;
// Keep depth blending because it has a high effect on the final result
uniform bool u_depth_blending = true;
uniform float u_ground_uv_scale = 20.0;
vec4 get_depth_blended_weights(vec4 splat, vec4 bumps) {
float dh = 0.2;
vec4 h = bumps + splat;
h *= smoothstep(0, 0.05, splat);
vec4 d = h + dh;
d.r -= max(h.g, max(h.b, h.a));
d.g -= max(h.r, max(h.b, h.a));
d.b -= max(h.g, max(h.r, h.a));
d.a -= max(h.g, max(h.b, h.r));
return clamp(d, 0, 1);
}
void vertex() {
vec4 wpos = MODEL_MATRIX * vec4(VERTEX, 1);
vec2 cell_coords = wpos.xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results (#183)
cell_coords += vec2(0.5);
// Normalized UV
UV = (cell_coords / vec2(textureSize(u_terrain_splatmap, 0)));
}
void fragment() {
// These were moved from vertex to fragment,
// so we can generate part of the global map with just one quad and we get full quality
vec4 tint = texture(u_terrain_colormap, UV);
vec4 splat = texture(u_terrain_splatmap, UV);
// Get bump at normal resolution so depth blending is accurate
vec2 ground_uv = UV / u_ground_uv_scale;
float b0 = texture(u_ground_albedo_bump_0, ground_uv).a;
float b1 = texture(u_ground_albedo_bump_1, ground_uv).a;
float b2 = texture(u_ground_albedo_bump_2, ground_uv).a;
float b3 = texture(u_ground_albedo_bump_3, ground_uv).a;
// Take the center of the highest mip as color, because we can't see details from far away.
vec2 ndc_center = vec2(0.5, 0.5);
vec3 col0 = textureLod(u_ground_albedo_bump_0, ndc_center, 10.0).rgb;
vec3 col1 = textureLod(u_ground_albedo_bump_1, ndc_center, 10.0).rgb;
vec3 col2 = textureLod(u_ground_albedo_bump_2, ndc_center, 10.0).rgb;
vec3 col3 = textureLod(u_ground_albedo_bump_3, ndc_center, 10.0).rgb;
vec4 w;
if (u_depth_blending) {
w = get_depth_blended_weights(splat, vec4(b0, b1, b2, b3));
} else {
w = splat.rgba;
}
float w_sum = (w.r + w.g + w.b + w.a);
ALBEDO = tint.rgb * (
w.r * col0 +
w.g * col1 +
w.b * col2 +
w.a * col3) / w_sum;
}

View File

@@ -0,0 +1 @@
uid://k5ehxem4asrx

View File

@@ -0,0 +1,211 @@
shader_type spatial;
// This is a shader with less textures, in case the main one doesn't run on your GPU.
// It's mostly a big copy/paste, because Godot doesn't support #include or #ifdef...
#include "include/heightmap.gdshaderinc"
uniform sampler2D u_terrain_heightmap;
uniform sampler2D u_terrain_normalmap;
// I had to remove `hint_albedo` from colormap in Godot 3 because it makes sRGB conversion kick in,
// which snowballs to black when doing GPU painting on that texture...
uniform sampler2D u_terrain_colormap;// : hint_albedo;
uniform sampler2D u_terrain_splatmap;
uniform mat4 u_terrain_inverse_transform;
uniform mat3 u_terrain_normal_basis;
uniform sampler2D u_ground_albedo_bump_0 : source_color;
uniform sampler2D u_ground_albedo_bump_1 : source_color;
uniform sampler2D u_ground_albedo_bump_2 : source_color;
uniform sampler2D u_ground_albedo_bump_3 : source_color;
uniform float u_ground_uv_scale = 20.0;
uniform bool u_depth_blending = true;
uniform bool u_triplanar = false;
// Each component corresponds to a ground texture. Set greater than zero to enable.
uniform vec4 u_tile_reduction = vec4(0.0, 0.0, 0.0, 0.0);
varying vec4 v_tint;
varying vec4 v_splat;
varying vec3 v_ground_uv;
vec3 unpack_normal(vec4 rgba) {
vec3 n = rgba.xzy * 2.0 - vec3(1.0);
// Had to negate Z because it comes from Y in the normal map,
// and OpenGL-style normal maps are Y-up.
n.z *= -1.0;
return n;
}
// Blends weights according to the bump of detail textures,
// so for example it allows to have sand fill the gaps between pebbles
vec4 get_depth_blended_weights(vec4 splat, vec4 bumps) {
float dh = 0.2;
vec4 h = bumps + splat;
// TODO Keep improving multilayer blending, there are still some edge cases...
// Mitigation: nullify layers with near-zero splat
h *= smoothstep(0, 0.05, splat);
vec4 d = h + dh;
d.r -= max(h.g, max(h.b, h.a));
d.g -= max(h.r, max(h.b, h.a));
d.b -= max(h.g, max(h.r, h.a));
d.a -= max(h.g, max(h.b, h.r));
return clamp(d, 0, 1);
}
vec3 get_triplanar_blend(vec3 world_normal) {
vec3 blending = abs(world_normal);
blending = normalize(max(blending, vec3(0.00001))); // Force weights to sum to 1.0
float b = blending.x + blending.y + blending.z;
return blending / vec3(b, b, b);
}
vec4 texture_triplanar(sampler2D tex, vec3 world_pos, vec3 blend) {
vec4 xaxis = texture(tex, world_pos.yz);
vec4 yaxis = texture(tex, world_pos.xz);
vec4 zaxis = texture(tex, world_pos.xy);
// blend the results of the 3 planar projections.
return xaxis * blend.x + yaxis * blend.y + zaxis * blend.z;
}
vec4 depth_blend2(vec4 a, vec4 b, float t) {
// https://www.gamasutra.com
// /blogs/AndreyMishkinis/20130716/196339/Advanced_Terrain_Texture_Splatting.php
float d = 0.1;
float ma = max(a.a + (1.0 - t), b.a + t) - d;
float ba = max(a.a + (1.0 - t) - ma, 0.0);
float bb = max(b.a + t - ma, 0.0);
return (a * ba + b * bb) / (ba + bb);
}
vec4 texture_antitile(sampler2D tex, vec2 uv) {
float frequency = 2.0;
float scale = 1.3;
float sharpness = 0.7;
// Rotate and scale UV
float rot = 3.14 * 0.6;
float cosa = cos(rot);
float sina = sin(rot);
vec2 uv2 = vec2(cosa * uv.x - sina * uv.y, sina * uv.x + cosa * uv.y) * scale;
vec4 col0 = texture(tex, uv);
vec4 col1 = texture(tex, uv2);
//col0 = vec4(0.0, 0.0, 1.0, 1.0);
// Periodically alternate between the two versions using a warped checker pattern
float t = 0.5 + 0.5
* sin(uv2.x * frequency + sin(uv.x) * 2.0)
* cos(uv2.y * frequency + sin(uv.y) * 2.0);
// Using depth blend because classic alpha blending smoothes out details
return depth_blend2(col0, col1, smoothstep(0.5 * sharpness, 1.0 - 0.5 * sharpness, t));
}
void vertex() {
vec2 cell_coords = (u_terrain_inverse_transform * MODEL_MATRIX * vec4(VERTEX, 1)).xz;
// Must add a half-offset so that we sample the center of pixels,
// otherwise bilinear filtering of the textures will give us mixed results.
cell_coords += vec2(0.5);
// Normalized UV
UV = cell_coords / vec2(textureSize(u_terrain_heightmap, 0));
// Height displacement
float h = sample_heightmap(u_terrain_heightmap, UV);
VERTEX.y = h;
v_ground_uv = vec3(cell_coords.x, h * MODEL_MATRIX[1][1], cell_coords.y) / u_ground_uv_scale;
// Putting this in vertex saves 2 fetches from the fragment shader,
// which is good for performance at a negligible quality cost,
// provided that geometry is a regular grid that decimates with LOD.
// (downside is LOD will also decimate tint and splat, but it's not bad overall)
v_tint = texture(u_terrain_colormap, UV);
v_splat = texture(u_terrain_splatmap, UV);
// Need to use u_terrain_normal_basis to handle scaling.
NORMAL = u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
}
void fragment() {
if (v_tint.a < 0.5) {
// TODO Add option to use vertex discarding instead, using NaNs
discard;
}
vec3 terrain_normal_world =
u_terrain_normal_basis * unpack_normal(texture(u_terrain_normalmap, UV));
terrain_normal_world = normalize(terrain_normal_world);
// TODO Detail should only be rasterized on nearby chunks (needs proximity management to switch shaders)
vec2 ground_uv = v_ground_uv.xz;
vec4 ab0, ab1, ab2, ab3;
if (u_triplanar) {
// Only do triplanar on one texture slot,
// because otherwise it would be very expensive and cost many more ifs.
// I chose the last slot because first slot is the default on new splatmaps,
// and that's a feature used for cliffs, which are usually designed later.
vec3 blending = get_triplanar_blend(terrain_normal_world);
ab3 = texture_triplanar(u_ground_albedo_bump_3, v_ground_uv, blending);
} else {
if (u_tile_reduction[3] > 0.0) {
ab3 = texture(u_ground_albedo_bump_3, ground_uv);
} else {
ab3 = texture_antitile(u_ground_albedo_bump_3, ground_uv);
}
}
if (u_tile_reduction[0] > 0.0) {
ab0 = texture_antitile(u_ground_albedo_bump_0, ground_uv);
} else {
ab0 = texture(u_ground_albedo_bump_0, ground_uv);
}
if (u_tile_reduction[1] > 0.0) {
ab1 = texture_antitile(u_ground_albedo_bump_1, ground_uv);
} else {
ab1 = texture(u_ground_albedo_bump_1, ground_uv);
}
if (u_tile_reduction[2] > 0.0) {
ab2 = texture_antitile(u_ground_albedo_bump_2, ground_uv);
} else {
ab2 = texture(u_ground_albedo_bump_2, ground_uv);
}
vec3 col0 = ab0.rgb;
vec3 col1 = ab1.rgb;
vec3 col2 = ab2.rgb;
vec3 col3 = ab3.rgb;
vec4 w;
// TODO An #ifdef macro would be nice! Or copy/paste everything in a different shader...
if (u_depth_blending) {
w = get_depth_blended_weights(v_splat, vec4(ab0.a, ab1.a, ab2.a, ab3.a));
} else {
w = v_splat.rgba;
}
float w_sum = (w.r + w.g + w.b + w.a);
ALBEDO = v_tint.rgb * (
w.r * col0.rgb +
w.g * col1.rgb +
w.b * col2.rgb +
w.a * col3.rgb) / w_sum;
ROUGHNESS = 1.0;
NORMAL = (VIEW_MATRIX * (vec4(terrain_normal_world, 0.0))).xyz;
//ALBEDO = w.rgb;
//ALBEDO = v_ground_uv.xyz;
}

View File

@@ -0,0 +1 @@
uid://bgp3wo0dmhn00