Reimplemented normal map generation as 3x3 sobel

This commit is contained in:
RodZill4 2018-08-02 08:45:46 +02:00
parent 0c062563c1
commit 0fda03ca42
2 changed files with 14 additions and 8 deletions

View File

@ -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

View File

@ -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