Anisotropic noise node.

This commit is contained in:
Relintai 2021-10-19 13:08:50 +02:00
parent 7ef34acfbf
commit fb502505a3
2 changed files with 105 additions and 70 deletions

View File

@ -34,64 +34,14 @@ const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
#noise_anisotropic.mmg #noise_anisotropic.mmg
#Generates x-axis interpolated value noise #Generates x-axis interpolated value noise
# "outputs": [ #Output:
# { #Output (float) - Shows a greyscale value noise
# "f": "anisotropic($(uv), vec2($(scale_x), $(scale_y)), $(seed), $(smoothness), $(interpolation))", #anisotropic($(uv), vec2($(scale_x), $(scale_y)), $(seed), $(smoothness), $(interpolation))
# "longdesc": "Shows a greyscale value noise",
# "shortdesc": "Output", #Input:
# "type": "f" #scale, Vector2, min: 1, 1, max: 32, 1024, step: 1, 1, default 4, 256
# } #smoothness, float, min: 0, max: 1, step: 0,01, default: 1
# ], #Interpolation, float, min: 0, max: 1, step: 0,01, default: 1
# "parameters": [
# {
# "control": "None",
# "default": 4,
# "label": "Scale X",
# "longdesc": "The scale along the X axis",
# "max": 32,
# "min": 1,
# "name": "scale_x",
# "shortdesc": "Scale.x",
# "step": 1,
# "type": "float"
# },
# {
# "control": "None",
# "default": 256,
# "label": "Scale Y",
# "longdesc": "The scale along the Y axis",
# "max": 1024,
# "min": 1,
# "name": "scale_y",
# "shortdesc": "Scale.y",
# "step": 1,
# "type": "float"
# },
# {
# "control": "None",
# "default": 1,
# "label": "Smoothness",
# "longdesc": "Controls how much the noise is blurred along the x-axis",
# "max": 1,
# "min": 0,
# "name": "smoothness",
# "shortdesc": "Smoothness",
# "step": 0.01,
# "type": "float"
# },
# {
# "control": "None",
# "default": 1,
# "label": "Interpolation",
# "longdesc": "Controls the type of interpolation used for the smoothing. 0 is linear interpolation, 1 is smooth interpolation",
# "max": 1,
# "min": 0,
# "name": "interpolation",
# "shortdesc": "Interpolation",
# "step": 0.01,
# "type": "float"
# }
# ],
#float dots(vec2 uv, float size, float density, float seed) { #float dots(vec2 uv, float size, float density, float seed) {
# vec2 seed2 = rand2(vec2(seed, 1.0-seed)); # vec2 seed2 = rand2(vec2(seed, 1.0-seed));
@ -108,22 +58,47 @@ static func dots(uv : Vector2, size : float, density : float, pseed : float) ->
var color : float = Commons.step(Commons.rand2(seed2 + point_pos).x, density); var color : float = Commons.step(Commons.rand2(seed2 + point_pos).x, density);
return color return color
#float anisotropic(vec2 uv, vec2 size, float seed, float smoothness, float interpolation) {\n\ static func anisotropicc(uv : Vector2, size : Vector2, pseed : float, smoothness : float, interpolation : float) -> Color:
# tvec2 seed2 = rand2(vec2(seed, 1.0-seed));\n\t\n\t var v : float = anisotropic(uv, size, pseed, smoothness, interpolation)
# vec2 xy = floor(uv*size);\n\t
# vec2 offset = vec2(rand(seed2 + xy.y), 0.0);\n\t return Color(v, v, v, 1)
# vec2 xy_offset = floor(uv * size + offset );\n
#float anisotropic(vec2 uv, vec2 size, float seed, float smoothness, float interpolation) {
# vec2 seed2 = rand2(vec2(seed, 1.0-seed));
# vec2 xy = floor(uv*size);
# vec2 offset = vec2(rand(seed2 + xy.y), 0.0);
# vec2 xy_offset = floor(uv * size + offset );
# #
# float f0 = rand(seed2+mod(xy_offset, size));\n # float f0 = rand(seed2+mod(xy_offset, size));
# float f1 = rand(seed2+mod(xy_offset+vec2(1.0, 0.0), size));\n\t # float f1 = rand(seed2+mod(xy_offset+vec2(1.0, 0.0), size));
# float mixer = clamp( (fract(uv.x*size.x+offset.x) -.5) / smoothness + 0.5, 0.0, 1.0 );\n # float mixer = clamp( (fract(uv.x*size.x+offset.x) -.5) / smoothness + 0.5, 0.0, 1.0 );
# float smooth_mix = smoothstep(0.0, 1.0, mixer);\n\t # float smooth_mix = smoothstep(0.0, 1.0, mixer);
# float linear = mix(f0, f1, mixer);\n\t # float linear = mix(f0, f1, mixer);
# float smoothed = mix(f0, f1, smooth_mix);\n\t\n # float smoothed = mix(f0, f1, smooth_mix);
# #
# return mix(linear, smoothed, interpolation);\n # return mix(linear, smoothed, interpolation);
#} #}
static func anisotropic(uv : Vector2, size : Vector2, pseed : float, smoothness : float, interpolation : float) -> float:
var seed2 : Vector2 = Commons.rand2(Vector2(pseed, 1.0 - pseed))
var xy : Vector2 = Commons.floorv2(uv * size)
var s2xy : Vector2 = seed2
s2xy.x += xy.y
s2xy.y += xy.y
var offset : Vector2 = Vector2(Commons.rand(s2xy), 0.0)
var xy_offset : Vector2 = Commons.floorv2(uv * size + offset)
var f0 : float = Commons.rand(seed2 + Commons.modv2(xy_offset, size));
var f1 : float = Commons.rand(seed2 + Commons.modv2(xy_offset + Vector2(1.0, 0.0), size))
var mixer : float = clamp((Commons.fract(uv.x * size.x + offset.x) - 0.5) / smoothness + 0.5, 0.0, 1.0)
var smooth_mix : float = smoothstep(0.0, 1.0, mixer)
var linear : float = lerp(f0, f1, mixer)
var smoothed : float = lerp(f0, f1, smooth_mix)
return lerp(linear, smoothed, interpolation)
#vec3 color_dots(vec2 uv, float size, float seed) { #vec3 color_dots(vec2 uv, float size, float seed) {
# vec2 seed2 = rand2(vec2(seed, 1.0-seed)); # vec2 seed2 = rand2(vec2(seed, 1.0-seed));
# uv /= size; # uv /= size;

View File

@ -0,0 +1,60 @@
tool
extends MMNode
var Noises = preload("res://addons/mat_maker_gd/nodes/common/noises.gd")
export(Resource) var image : Resource
export(Vector2) var scale : Vector2 = Vector2(4, 256)
export(float) var smoothness : float = 1
export(float) var interpolation : float = 1
func _init_properties():
if !image:
image = MMNodeUniversalProperty.new()
image.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
image.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
register_output_property(image)
func _register_methods(mm_graph_node) -> void:
mm_graph_node.add_slot_texture_universal(image)
mm_graph_node.add_slot_vector2("get_scale", "set_scale", "Scale", 1)#, Vector2(1, 10))
mm_graph_node.add_slot_float("get_smoothness", "set_smoothness", "Smoothness", 0.01)#, Vector2(0, 1))
mm_graph_node.add_slot_float("get_interpolation", "set_interpolation", "Interpolation", 0.01)#, Vector2(0, 1))
func get_value_for(uv : Vector2, pseed : int) -> Color:
var ps : float = 1.0 / float(pseed)
#anisotropic($(uv), vec2($(scale_x), $(scale_y)), $(seed), $(smoothness), $(interpolation))
return Noises.anisotropicc(uv, scale, ps, smoothness, interpolation)
func _render(material) -> void:
var img : Image = render_image(material)
image.set_value(img)
func get_scale() -> Vector2:
return scale
func set_scale(val : Vector2) -> void:
scale = val
set_dirty(true)
func get_smoothness() -> float:
return smoothness
func set_smoothness(val : float) -> void:
smoothness = val
set_dirty(true)
func get_interpolation() -> float:
return interpolation
func set_interpolation(val : float) -> void:
interpolation = val
set_dirty(true)