mirror of
https://github.com/Relintai/material-maker.git
synced 2024-12-23 21:16:54 +01:00
93c84650cd
This also simplifies some functions. This partially addresses #30.
82 lines
3.1 KiB
GDScript
82 lines
3.1 KiB
GDScript
tool
|
|
extends "res://addons/material_maker/node_base.gd"
|
|
|
|
var input_shader = ""
|
|
var saved_texture
|
|
|
|
const DIRECTION_H = 1
|
|
const DIRECTION_V = 2
|
|
const DIRECTIONS = [
|
|
{ name="Both", mask=DIRECTION_H|DIRECTION_V },
|
|
{ name="X", mask=DIRECTION_H },
|
|
{ name="Y", mask=DIRECTION_V }
|
|
]
|
|
|
|
func _ready() -> void:
|
|
# init size widget
|
|
$HBoxContainer1/size.clear()
|
|
for i in range(7):
|
|
$HBoxContainer1/size.add_item(str(int(pow(2, 5+i))), i)
|
|
$HBoxContainer1/size.selected = 5
|
|
# init direction widget
|
|
$HBoxContainer2/direction.clear()
|
|
for d in DIRECTIONS:
|
|
$HBoxContainer2/direction.add_item(d.name)
|
|
$HBoxContainer2/direction.selected = 0
|
|
initialize_properties([ $HBoxContainer1/size, $HBoxContainer2/direction, $HBoxContainer3/sigma ])
|
|
saved_texture = ImageTexture.new()
|
|
|
|
func get_gaussian_blur_shader(horizontal) -> String:
|
|
var convolution = { x=0, y=0, kernel=[], epsilon=1.0/pow(2, 5+parameters.size) }
|
|
var kernel_size = 50
|
|
if horizontal:
|
|
convolution.x = kernel_size
|
|
else:
|
|
convolution.y = kernel_size
|
|
convolution.kernel.resize(2*kernel_size+1)
|
|
var sum = 0
|
|
for x in range(-kernel_size, kernel_size+1):
|
|
var coef = exp(-0.5*(pow((x)/parameters.sigma, 2.0))) / (2.0*PI*parameters.sigma*parameters.sigma)
|
|
convolution.kernel[x+kernel_size] = coef
|
|
sum += coef
|
|
for x in range(-kernel_size, kernel_size+1):
|
|
convolution.kernel[x+kernel_size] /= sum
|
|
return get_convolution_shader(convolution)
|
|
|
|
func _rerender() -> void:
|
|
if DIRECTIONS[parameters.direction].mask & DIRECTION_H != 0:
|
|
get_parent().renderer.precalculate_shader(input_shader, get_source().get_textures(), int(pow(2, 5+parameters.size)), saved_texture, self, "pass_1", [])
|
|
else:
|
|
get_parent().renderer.precalculate_shader(input_shader, get_source().get_textures(), int(pow(2, 5+parameters.size)), saved_texture, self, "pass_2", [])
|
|
|
|
func pass_1() -> void:
|
|
if DIRECTIONS[parameters.direction].mask & DIRECTION_V != 0:
|
|
get_parent().renderer.precalculate_shader(get_gaussian_blur_shader(true), { input=saved_texture }, int(pow(2, 5+parameters.size)), saved_texture, self, "pass_2", [])
|
|
else:
|
|
get_parent().renderer.precalculate_shader(get_gaussian_blur_shader(true), { input=saved_texture }, int(pow(2, 5+parameters.size)), saved_texture, self, "rerender_targets", [])
|
|
|
|
func pass_2() -> void:
|
|
get_parent().renderer.precalculate_shader(get_gaussian_blur_shader(false), { input=saved_texture }, int(pow(2, 5+parameters.size)), saved_texture, self, "rerender_targets", [])
|
|
|
|
func get_textures() -> Dictionary:
|
|
var list = {}
|
|
list[name] = saved_texture
|
|
return list
|
|
|
|
func _get_shader_code(uv, slot = 0) -> Dictionary:
|
|
var rv = { defs="", code="" }
|
|
var src = get_source()
|
|
if src == null:
|
|
return rv
|
|
input_shader = get_parent().renderer.generate_shader(src.get_shader_code_with_globals("UV"))
|
|
_rerender()
|
|
if generated_variants.empty():
|
|
rv.defs = "uniform sampler2D "+name+"_tex;\n"
|
|
var variant_index = generated_variants.find(uv)
|
|
if variant_index == -1:
|
|
variant_index = generated_variants.size()
|
|
generated_variants.append(uv)
|
|
rv.code = "vec3 "+name+"_"+str(variant_index)+"_rgb = texture("+name+"_tex, "+uv+").rgb;\n"
|
|
rv.rgb = name+"_"+str(variant_index)+"_rgb"
|
|
return rv
|