2018-08-14 15:09:46 +02:00
|
|
|
tool
|
2018-09-03 21:35:44 +02:00
|
|
|
extends "res://addons/material_maker/node_base.gd"
|
2018-08-14 15:09:46 +02:00
|
|
|
|
|
|
|
var input_shader = ""
|
|
|
|
var input_texture
|
|
|
|
var final_texture
|
|
|
|
|
|
|
|
const CONVOLUTION = {
|
|
|
|
x=1,
|
|
|
|
y=1,
|
|
|
|
kernel=[
|
|
|
|
1, 2, 1,
|
|
|
|
0, 0, 0,
|
|
|
|
-1, -2, -1
|
|
|
|
],
|
|
|
|
epsilon=1.0/1024,
|
|
|
|
scale=0.5,
|
|
|
|
translate=Vector3(0.5, 0.5, 0.5)
|
|
|
|
}
|
|
|
|
|
|
|
|
const INDICES = [ 0, 1, 2, 5, 8, 7, 6, 3 ]
|
|
|
|
const COEFS = [ 1, 2, 1, 0, -1, -2, -1, 0 ]
|
|
|
|
|
|
|
|
func _ready():
|
2018-08-27 19:31:00 +02:00
|
|
|
$HBoxContainer1/size.clear()
|
|
|
|
for i in range(7):
|
|
|
|
$HBoxContainer1/size.add_item(str(int(pow(2, 5+i))), i)
|
2018-10-02 07:46:20 +02:00
|
|
|
$HBoxContainer1/size.selected = 5
|
2018-08-14 15:09:46 +02:00
|
|
|
input_texture = ImageTexture.new()
|
|
|
|
final_texture = ImageTexture.new()
|
2018-08-27 19:31:00 +02:00
|
|
|
initialize_properties([ $HBoxContainer1/size, $HBoxContainer2/direction ])
|
2018-08-14 15:09:46 +02:00
|
|
|
|
|
|
|
func _rerender():
|
2018-10-02 07:46:20 +02:00
|
|
|
get_parent().renderer.precalculate_shader(input_shader, get_source().get_textures(), int(pow(2, 5+parameters.size)), input_texture, self, "pass_1", [])
|
2018-08-14 15:09:46 +02:00
|
|
|
|
|
|
|
func pass_1():
|
|
|
|
var convolution = CONVOLUTION
|
2018-10-02 07:46:20 +02:00
|
|
|
convolution.epsilon=1.0/pow(2, 5+parameters.size)
|
2018-08-14 15:09:46 +02:00
|
|
|
for i in range(8):
|
2018-10-02 07:46:20 +02:00
|
|
|
convolution.kernel[INDICES[i]] = COEFS[(i+8-int(parameters.direction))%8]
|
|
|
|
get_parent().renderer.precalculate_shader(get_convolution_shader(convolution), {input=input_texture}, int(pow(2, 5+parameters.size)), final_texture, self, "rerender_targets", [])
|
2018-08-14 15:09:46 +02:00
|
|
|
|
|
|
|
func get_textures():
|
|
|
|
var list = {}
|
|
|
|
list[name] = final_texture
|
|
|
|
return list
|
|
|
|
|
2019-01-27 09:00:30 +01:00
|
|
|
func _get_shader_code(uv, slot = 0):
|
2018-08-14 15:09:46 +02:00
|
|
|
var rv = { defs="", code="" }
|
|
|
|
var src = get_source()
|
|
|
|
if src == null:
|
|
|
|
return rv
|
2019-01-27 09:00:30 +01:00
|
|
|
input_shader = get_parent().renderer.generate_shader(src.get_shader_code_with_globals("UV"))
|
2018-08-14 15:09:46 +02:00
|
|
|
_rerender()
|
|
|
|
if generated_variants.empty():
|
2018-08-20 15:43:03 +02:00
|
|
|
rv.defs = "uniform sampler2D %s_tex;\n" % [ name ]
|
2018-08-14 15:09:46 +02:00
|
|
|
var variant_index = generated_variants.find(uv)
|
|
|
|
if variant_index == -1:
|
|
|
|
variant_index = generated_variants.size()
|
|
|
|
generated_variants.append(uv)
|
2018-08-20 15:43:03 +02:00
|
|
|
rv.code = "vec3 %s_%d_rgb = texture(%s_tex, %s).rgb;\n" % [ name, variant_index, name, uv ]
|
|
|
|
rv.rgb = "%s_%d_rgb" % [ name, variant_index ]
|
2018-08-14 15:09:46 +02:00
|
|
|
return rv
|