Adjust normal map calculations for terrain shading

This commit is contained in:
kayomn 2023-01-10 01:45:50 +00:00
parent 9a144705f5
commit 72d295e266
3 changed files with 11 additions and 10 deletions

BIN
editor.scn (Stored with Git LFS)

Binary file not shown.

View File

@ -58,7 +58,7 @@ var size: Vector2i = Vector2i.ZERO:
RenderingServer.instance_set_base(self.get_instance(), RenderingServer.instance_set_base(self.get_instance(),
surface_tool.commit(self._mesh).get_rid()) surface_tool.commit(self._mesh).get_rid())
self._material.set_shader_parameter("AREA", Vector2(value)) self._material.set_shader_parameter("SIZE", Vector2(value))
self._mesh.surface_set_material(0, self._material) self._mesh.surface_set_material(0, self._material)
size = value size = value

View File

@ -1,7 +1,5 @@
shader_type spatial; shader_type spatial;
uniform vec2 AREA;
uniform sampler2D LAYER_1_ALBEDO : hint_default_black; uniform sampler2D LAYER_1_ALBEDO : hint_default_black;
uniform sampler2D LAYER_1_NORMAL_MAP : hint_default_black; uniform sampler2D LAYER_1_NORMAL_MAP : hint_default_black;
@ -20,10 +18,12 @@ uniform sampler2D LAYER_4_NORMAL_MAP : hint_default_black;
uniform float MAX_HEIGHT = 100.0; uniform float MAX_HEIGHT = 100.0;
uniform vec2 SIZE;
uniform sampler2D TERRAIN_MAP : hint_default_transparent, repeat_disable; uniform sampler2D TERRAIN_MAP : hint_default_transparent, repeat_disable;
void fragment() { void fragment() {
vec2 uv = UV * AREA; vec2 uv = UV * SIZE;
vec2 uv_alt = uv * -0.25; vec2 uv_alt = uv * -0.25;
vec3 splat_map = texture(TERRAIN_MAP, UV).rgb; vec3 splat_map = texture(TERRAIN_MAP, UV).rgb;
float blank = clamp(1.0 - splat_map.r - splat_map.g - splat_map.b, 0.0, 1.0); float blank = clamp(1.0 - splat_map.r - splat_map.g - splat_map.b, 0.0, 1.0);
@ -46,10 +46,11 @@ float height(vec2 uv) {
void vertex() { void vertex() {
VERTEX.y = height(UV); VERTEX.y = height(UV);
float left = height(vec2(UV.x - 1.0, UV.y)); vec2 texel = vec2(1.0, 1.0) / SIZE;
float right = height(vec2(UV.x + 1.0, UV.y)); float left = height(vec2(UV.x - texel.x, UV.y));
float forward = height(vec2(UV.x, UV.y + 1.0)); float right = height(vec2(UV.x + texel.x, UV.y));
float backward = height(vec2(UV.x, UV.y - 1.0)); float forward = height(vec2(UV.x, UV.y + texel.y));
float backward = height(vec2(UV.x, UV.y - texel.y));
NORMAL = normalize(vec3(left - right, 2.0, forward - forward)); NORMAL = normalize(vec3(left - right, 2.0, forward - forward));
} }