diff options
author | Gone2Daly <71726742+Gone2Daly@users.noreply.github.com> | 2023-07-22 21:05:42 +0200 |
---|---|---|
committer | Gone2Daly <71726742+Gone2Daly@users.noreply.github.com> | 2023-07-22 21:05:42 +0200 |
commit | 71b3cd829f80de4c2cd3972d8bfd5ee470a5d180 (patch) | |
tree | b4280fde6eef2ae6987648bc7bf8e00e9011bb7f /game/addons/zylann.hterrain/shaders/low_poly.gdshader | |
parent | ce9022d0df74d6c33db3686622be2050d873ab0b (diff) |
init_testtest3d
Diffstat (limited to 'game/addons/zylann.hterrain/shaders/low_poly.gdshader')
-rw-r--r-- | game/addons/zylann.hterrain/shaders/low_poly.gdshader | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/game/addons/zylann.hterrain/shaders/low_poly.gdshader b/game/addons/zylann.hterrain/shaders/low_poly.gdshader new file mode 100644 index 0000000..6c98104 --- /dev/null +++ b/game/addons/zylann.hterrain/shaders/low_poly.gdshader @@ -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))); +} + |