From 72d295e266bb6e81178d085c1219c68c168ed454 Mon Sep 17 00:00:00 2001 From: kayomn Date: Tue, 10 Jan 2023 01:45:50 +0000 Subject: [PATCH] Adjust normal map calculations for terrain shading --- editor.scn | 4 ++-- terrain_instance_3d.gd | 2 +- terrain_shader.gdshader | 15 ++++++++------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/editor.scn b/editor.scn index cbf147f..c405301 100644 --- a/editor.scn +++ b/editor.scn @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:332a300c24da15e6ece7e0fe4ee07e394939d953e8ba482613ebc1fd70cc579e -size 5103 +oid sha256:009f7c0a98085bc2a1824ed2ce45223036b7b3f24df477178e1967a935b87971 +size 5105 diff --git a/terrain_instance_3d.gd b/terrain_instance_3d.gd index ee409da..c53cbc4 100644 --- a/terrain_instance_3d.gd +++ b/terrain_instance_3d.gd @@ -58,7 +58,7 @@ var size: Vector2i = Vector2i.ZERO: RenderingServer.instance_set_base(self.get_instance(), 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) size = value diff --git a/terrain_shader.gdshader b/terrain_shader.gdshader index 6073c56..2fca663 100644 --- a/terrain_shader.gdshader +++ b/terrain_shader.gdshader @@ -1,7 +1,5 @@ shader_type spatial; -uniform vec2 AREA; - uniform sampler2D LAYER_1_ALBEDO : 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 vec2 SIZE; + uniform sampler2D TERRAIN_MAP : hint_default_transparent, repeat_disable; void fragment() { - vec2 uv = UV * AREA; + vec2 uv = UV * SIZE; vec2 uv_alt = uv * -0.25; 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); @@ -46,10 +46,11 @@ float height(vec2 uv) { void vertex() { VERTEX.y = height(UV); - float left = height(vec2(UV.x - 1.0, UV.y)); - float right = height(vec2(UV.x + 1.0, UV.y)); - float forward = height(vec2(UV.x, UV.y + 1.0)); - float backward = height(vec2(UV.x, UV.y - 1.0)); + vec2 texel = vec2(1.0, 1.0) / SIZE; + float left = height(vec2(UV.x - texel.x, UV.y)); + float right = height(vec2(UV.x + texel.x, UV.y)); + 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)); }