sdf3d_render and sdf3d_sphere nodes.

This commit is contained in:
Relintai 2021-10-23 00:18:44 +02:00
parent 577e9d705a
commit e42a25a53b
6 changed files with 292 additions and 63 deletions

View File

@ -1,4 +1,4 @@
[gd_resource type="Resource" load_steps=107 format=2]
[gd_resource type="Resource" load_steps=116 format=2]
[ext_resource path="res://addons/mat_maker_gd/nodes/mm_material.gd" type="Script" id=1]
[ext_resource path="res://addons/mat_maker_gd/nodes/noise/noise.gd" type="Script" id=2]
@ -28,6 +28,8 @@
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf2d/sd_op_circle_repeat.gd" type="Script" id=26]
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf2d/sd_op_morph.gd" type="Script" id=27]
[ext_resource path="res://addons/mat_maker_gd/nodes/simple/curve.gd" type="Script" id=28]
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf3d/sdf3d_render.gd" type="Script" id=29]
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf3d/sdf3d_shape_sphere.gd" type="Script" id=30]
[sub_resource type="Resource" id=3]
script = ExtResource( 5 )
@ -65,7 +67,7 @@ sides = 11
radius = SubResource( 4 )
edge = SubResource( 3 )
[sub_resource type="Resource" id=101]
[sub_resource type="Resource" id=103]
script = ExtResource( 5 )
default_type = 5
default_int = 0
@ -77,7 +79,7 @@ default_color = Color( 0, 0, 0, 1 )
[sub_resource type="Resource" id=6]
script = ExtResource( 6 )
graph_position = Vector2( 300, -400 )
image = SubResource( 101 )
image = SubResource( 103 )
postfix = "-test"
[sub_resource type="Resource" id=9]
@ -708,7 +710,67 @@ c = Vector2( 0.35, -0.2 )
width = 0.24
repeat = 5
[sub_resource type="Resource" id=109]
script = ExtResource( 5 )
default_type = 2
default_int = 0
default_float = 0.0
default_vector2 = Vector2( 0, 0 )
default_vector3 = Vector3( 0, 0, 0 )
default_color = Color( 0, 0, 0, 1 )
[sub_resource type="Resource" id=104]
script = ExtResource( 5 )
default_type = 2
default_int = 0
default_float = 0.0
default_vector2 = Vector2( 0, 0 )
default_vector3 = Vector3( 0, 0, 0 )
default_color = Color( 0, 0, 0, 1 )
input_property = SubResource( 109 )
[sub_resource type="Resource" id=105]
script = ExtResource( 5 )
default_type = 5
default_int = 0
default_float = 0.0
default_vector2 = Vector2( 0, 0 )
default_vector3 = Vector3( 0, 0, 0 )
default_color = Color( 0, 0, 0, 1 )
[sub_resource type="Resource" id=106]
script = ExtResource( 5 )
default_type = 5
default_int = 0
default_float = 0.0
default_vector2 = Vector2( 0, 0 )
default_vector3 = Vector3( 0, 0, 0 )
default_color = Color( 0, 0, 0, 1 )
[sub_resource type="Resource" id=107]
script = ExtResource( 5 )
default_type = 5
default_int = 0
default_float = 0.0
default_vector2 = Vector2( 0, 0 )
default_vector3 = Vector3( 0, 0, 0 )
default_color = Color( 0, 0, 0, 1 )
[sub_resource type="Resource" id=108]
script = ExtResource( 29 )
graph_position = Vector2( -660, 420 )
input = SubResource( 104 )
out_height_map = SubResource( 106 )
out_normal_map = SubResource( 107 )
out_color_map = SubResource( 105 )
[sub_resource type="Resource" id=110]
script = ExtResource( 30 )
graph_position = Vector2( -1040, 500 )
output = SubResource( 109 )
radius = 0.5
[resource]
script = ExtResource( 1 )
image_size = Vector2( 128, 128 )
nodes = [ SubResource( 5 ), SubResource( 6 ), SubResource( 12 ), SubResource( 15 ), SubResource( 18 ), SubResource( 21 ), SubResource( 24 ), SubResource( 27 ), SubResource( 39 ), SubResource( 42 ), SubResource( 45 ), SubResource( 49 ), SubResource( 54 ), SubResource( 58 ), SubResource( 60 ), SubResource( 63 ), SubResource( 66 ), SubResource( 69 ), SubResource( 71 ), SubResource( 73 ), SubResource( 78 ), SubResource( 82 ), SubResource( 85 ), SubResource( 88 ), SubResource( 90 ), SubResource( 92 ), SubResource( 96 ), SubResource( 100 ) ]
nodes = [ SubResource( 5 ), SubResource( 6 ), SubResource( 12 ), SubResource( 15 ), SubResource( 18 ), SubResource( 21 ), SubResource( 24 ), SubResource( 27 ), SubResource( 39 ), SubResource( 42 ), SubResource( 45 ), SubResource( 49 ), SubResource( 54 ), SubResource( 58 ), SubResource( 60 ), SubResource( 63 ), SubResource( 66 ), SubResource( 69 ), SubResource( 71 ), SubResource( 73 ), SubResource( 78 ), SubResource( 82 ), SubResource( 85 ), SubResource( 88 ), SubResource( 90 ), SubResource( 92 ), SubResource( 96 ), SubResource( 100 ), SubResource( 108 ), SubResource( 110 ) ]

View File

@ -519,7 +519,8 @@ static func sdf3d_torus_z(p : Vector3, R : float, r : float) -> Vector2:
#vec2 raymarch_$name(vec2 uv) {
# vec3 ro = vec3(uv-vec2(0.5), 1.0);
# vec3 rd = vec3(0.0, 0.0, -1.0);
# float dO = 0.0;float c = 0.0;
# float dO = 0.0;
# float c = 0.0;
#
# for (int i=0; i < 100; i++) {
# vec3 p = ro + rd*dO;

View File

@ -91,6 +91,14 @@ func get_owner_value(uv : Vector2):
elif default_type == MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE:
return to_color(owner.get_property_value(uv))
func get_value_sdf3d(uv3 : Vector3, skip_owner_val : bool = false) -> Vector2:
if get_value_from_owner && !skip_owner_val:
return owner.get_property_value_sdf3d(uv3)
if !input_property:
return default_vector2
return input_property.get_value_sdf3d(uv3)
func to_int(val) -> int:
if val is int:

View File

@ -1,58 +0,0 @@
tool
extends MMNode
var SDF3D = preload("res://addons/mat_maker_gd/nodes/common/sdf3d.gd")
var image : Image
var tex : ImageTexture
export(Vector2) var bmin : Vector2 = Vector2(0.1, 0.1)
export(Vector2) var bmax : Vector2 = Vector2(1, 1)
export(bool) var refresh setget reff,reffg
func _ready():
if !Engine.editor_hint:
gen()
func gen() -> void:
if !image:
image = Image.new()
image.create(300, 300, false, Image.FORMAT_RGBA8)
if !tex:
tex = ImageTexture.new()
# var bmin : Vector2 = Vector2(0.1, 0.1)
# var bmax : Vector2 = Vector2(1, 1)
image.lock()
var w : float = image.get_width()
var h : float = image.get_width()
var pseed : float = randf() + randi()
for x in range(image.get_width()):
for y in range(image.get_height()):
var v : Vector2 = Vector2(x / w, y / h)
var col : Color = SDF3D.raymarch(v)
# var col : Color = SDF3D.raymarch2(v)
# var col : Color = SDF3D.raymarch3(v)
image.set_pixel(x, y, col)
image.unlock()
tex.create_from_image(image)
# texture = tex
func reffg():
return false
func reff(bb):
if bb:
gen()

View File

@ -0,0 +1,179 @@
tool
extends MMNode
const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
var SDF2D = preload("res://addons/mat_maker_gd/nodes/common/sdf3d.gd")
export(Resource) var input : Resource
export(Resource) var out_height_map : Resource
export(Resource) var out_normal_map : Resource
export(Resource) var out_color_map : Resource
func _init_properties():
if !input:
input = MMNodeUniversalProperty.new()
input.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_VECTOR2
#for some reason this doesn't work, todo check
# input.input_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_FLOAT
input.input_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_UNIVERSAL
input.slot_name = "Input"
if !out_height_map:
out_height_map = MMNodeUniversalProperty.new()
out_height_map.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
out_height_map.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
if !out_normal_map:
out_normal_map = MMNodeUniversalProperty.new()
out_normal_map.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
out_normal_map.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
if !out_color_map:
out_color_map = MMNodeUniversalProperty.new()
out_color_map.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE
out_color_map.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE
register_output_property(out_height_map)
register_output_property(out_normal_map)
register_output_property(out_color_map)
register_input_property(input)
func _register_methods(mm_graph_node) -> void:
mm_graph_node.add_slot_label_universal(input)
mm_graph_node.add_slot_texture_universal(out_height_map)
mm_graph_node.add_slot_texture_universal(out_normal_map)
mm_graph_node.add_slot_texture_universal(out_color_map)
func _render(material) -> void:
var height_map : Image = Image.new()
var normal_map : Image = Image.new()
var color_map : Image = Image.new()
height_map.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8)
normal_map.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8)
color_map.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8)
height_map.lock()
normal_map.lock()
color_map.lock()
var w : float = material.image_size.x
var h : float = material.image_size.y
var pseed : float = randf() + randi()
for x in range(material.image_size.x):
for y in range(material.image_size.y):
var uv : Vector2 = Vector2(x / w, y / h)
var raymarch : Vector2 = sdf3d_raymarch(uv)
#HeightMap - float - The generated height map
#1.0-$(name_uv)_d.x
var hmf : float = 1.0 - raymarch.x
var height_map_col : Color = Color(hmf, hmf, hmf, 1)
#NormalMap - rgb - The generated normal map
#vec3(0.5) + 0.5* normal_$name(vec3($uv-vec2(0.5), 1.0-$(name_uv)_d.x))
var nuv : Vector2 = uv - Vector2(0.5, 0.5)
var n : Vector3 = sdf3d_normal(Vector3(nuv.x, nuv.y, 1.0 - raymarch.x))
var nn : Vector3 = Vector3(0.5, 0.5, 0.5) + 0.5 * n
var normal_map_col : Color = Color(nn.x, nn.y, nn.z, 1)
#ColorMap - float - The generated color index map
#$(name_uv)_d.y
var color_map_col : Color = Color(raymarch.y, raymarch.y, raymarch.y, 1)
height_map.set_pixel(x, y, height_map_col)
normal_map.set_pixel(x, y, normal_map_col)
color_map.set_pixel(x, y, color_map_col)
height_map.unlock()
normal_map.unlock()
color_map.unlock()
out_height_map.set_value(height_map)
out_normal_map.set_value(normal_map)
out_color_map.set_value(color_map)
func get_value_for(uv : Vector2, pseed : int) -> Color:
return Color()
#vec2 raymarch_$name(vec2 uv) {
# vec3 ro = vec3(uv-vec2(0.5), 1.0);
# vec3 rd = vec3(0.0, 0.0, -1.0);
# float dO = 0.0;
# float c = 0.0;
#
# for (int i=0; i < 100; i++) {
# vec3 p = ro + rd*dO;
# vec2 dS = $sdf(p);
# dO += dS.x;
#
# if (dO >= 1.0) {
# break;
# } else if (dS.x < 0.0001) {
# c = dS.y;
# break;
# }
# }
#
# return vec2(dO, c);
#}
func sdf3d_raymarch(uv : Vector2) -> Vector2:
var ro : Vector3 = Vector3(uv.x - 0.5, uv.y - 0.5, 1.0);
var rd : Vector3 = Vector3(0.0, 0.0, -1.0);
var dO : float = 0.0;
var c : float = 0.0;
for i in range(100):
var p : Vector3 = ro + rd * dO;
var dS : Vector2 = input.get_value_sdf3d(p)
dO += dS.x;
if (dO >= 1.0):
break;
elif (dS.x < 0.0001):
c = dS.y;
break;
return Vector2(dO, c);
#vec3 normal_$name(vec3 p) {
# if (p.z <= 0.0) {
# return vec3(0.0, 0.0, 1.0);
# }
#
# float d = $sdf(p).x;
# float e = .001;
# vec3 n = d - vec3(
# $sdf(p-vec3(e, 0.0, 0.0)).x,
# $sdf(p-vec3(0.0, e, 0.0)).x,
# $sdf(p-vec3(0.0, 0.0, e)).x);
#
# return vec3(-1.0, -1.0, -1.0)*normalize(n);
#}
func sdf3d_normal(p : Vector3) -> Vector3:
if (p.z <= 0.0):
return Vector3(0.0, 0.0, 1.0);
var d : float = input.get_value_sdf3d(p).x
var e : float = .001;
var n : Vector3 = Vector3(
d - input.get_value_sdf3d(p - Vector3(e, 0.0, 0.0)).x,
d - input.get_value_sdf3d(p - Vector3(0.0, e, 0.0)).x,
d - input.get_value_sdf3d(p - Vector3(0.0, 0.0, e)).x)
return Vector3(-1.0, -1.0, -1.0) * n.normalized()

View File

@ -0,0 +1,37 @@
tool
extends MMNode
const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
var SDF3D = preload("res://addons/mat_maker_gd/nodes/common/sdf3d.gd")
export(Resource) var output : Resource
export(float) var radius : float = 0.5
func _init_properties():
if !output:
output = MMNodeUniversalProperty.new()
output.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_VECTOR2
output.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_FLOAT
output.slot_name = ">>> Output >>>"
output.get_value_from_owner = true
register_output_property(output)
func _register_methods(mm_graph_node) -> void:
mm_graph_node.add_slot_label_universal(output)
mm_graph_node.add_slot_float("get_radius", "set_radius", "Radius", 0.01)
func get_property_value_sdf3d(uv3 : Vector3) -> Vector2:
return SDF3D.sdf3d_sphere(uv3, radius)
#radius
func get_radius() -> float:
return radius
func set_radius(val : float) -> void:
radius = val
emit_changed()
output.emit_changed()