From 0fda03ca42c04632fcf0ae855f3b354cc88f8daf Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Thu, 2 Aug 2018 08:45:46 +0200 Subject: [PATCH] Reimplemented normal map generation as 3x3 sobel --- .../procedural_material/nodes/normal_map.gd | 20 ++++++++++++------- .../procedural_material/nodes/normal_map.tscn | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/addons/procedural_material/nodes/normal_map.gd b/addons/procedural_material/nodes/normal_map.gd index 1b8f8e9..798c07b 100644 --- a/addons/procedural_material/nodes/normal_map.gd +++ b/addons/procedural_material/nodes/normal_map.gd @@ -15,14 +15,20 @@ func _get_shader_code(uv): return rv var variant_index = generated_variants.find(uv) if variant_index == -1: + var epsilon = 0.005 variant_index = generated_variants.size() generated_variants.append(uv) - var src_code0 = src.get_shader_code(uv+"+vec2(0.01, 0.0)") - var src_code1 = src.get_shader_code(uv+"+vec2(-0.01, 0.0)") - var src_code2 = src.get_shader_code(uv+"+vec2(0.0, 0.01)") - var src_code3 = src.get_shader_code(uv+"+vec2(0.0, -0.01)") - rv.defs = src_code0.defs - rv.code = src_code0.code+src_code1.code+src_code2.code+src_code3.code - rv.code += "vec3 "+name+"_"+str(variant_index)+"_rgb = vec3(0.5, 0.5, 0.5) + 0.5*normalize("+str(amount)+"*vec3("+get_source_f(src_code0)+"-"+get_source_f(src_code1)+", "+get_source_f(src_code2)+"-"+get_source_f(src_code3)+", 0.0) + vec3(0.0, 0.0, 1.0));\n" + var src_code_tl = src.get_shader_code(uv+"+vec2(-%.9f, -%.9f)" % [ epsilon, epsilon ]) + var src_code_l = src.get_shader_code(uv+"+vec2(-%.9f, 0.0)" % [ epsilon, ]) + var src_code_bl = src.get_shader_code(uv+"+vec2(-%.9f, %.9f)" % [ epsilon, epsilon ]) + var src_code_tr = src.get_shader_code(uv+"+vec2(%.9f, -%.9f)" % [ epsilon, epsilon ]) + var src_code_r = src.get_shader_code(uv+"+vec2(%.9f, 0.0)" % [ epsilon ]) + var src_code_br = src.get_shader_code(uv+"+vec2(%.9f, %.9f)" % [ epsilon, epsilon ]) + var src_code_t = src.get_shader_code(uv+"+vec2(0.0, -%.9f)" % [ epsilon ]) + var src_code_b = src.get_shader_code(uv+"+vec2(0.0, %.9f)" % [ epsilon ]) + rv.defs = src_code_tl.defs + rv.code = src_code_tl.code+src_code_l.code+src_code_bl.code+src_code_tr.code + rv.code += src_code_r.code+src_code_br.code+src_code_t.code+src_code_b.code + rv.code += "vec3 %s_%d_rgb = vec3(0.5, 0.5, 0.5) + 0.5*normalize(%.9f*vec3(%s+2.0*%s+%s-%s-2.0*%s-%s, %s+2.0*%s+%s-%s-2.0*%s-%s, 0.0) + vec3(0.0, 0.0, 1.0));\n" % [ name, variant_index, amount, src_code_tr.f, src_code_r.f, src_code_br.f, src_code_tl.f, src_code_l.f, src_code_bl.f, src_code_bl.f, src_code_b.f, src_code_br.f, src_code_tl.f, src_code_t.f, src_code_tr.f ] rv.rgb = name+"_"+str(variant_index)+"_rgb" return rv diff --git a/addons/procedural_material/nodes/normal_map.tscn b/addons/procedural_material/nodes/normal_map.tscn index 68fd50d..3610135 100644 --- a/addons/procedural_material/nodes/normal_map.tscn +++ b/addons/procedural_material/nodes/normal_map.tscn @@ -5,7 +5,7 @@ [sub_resource type="Theme" id=1] -[node name="NormalMap" type="GraphNode"] +[node name="NormalMap" type="GraphNode" index="0"] anchor_left = 0.0 anchor_top = 0.0