diff --git a/modules/material_maker/SCsub b/modules/material_maker/SCsub index 93dfd873b..ea1a891a6 100644 --- a/modules/material_maker/SCsub +++ b/modules/material_maker/SCsub @@ -54,6 +54,7 @@ sources = [ "nodes/uniform/greyscale_uniform.cpp", "nodes/transform/translate.cpp", + "nodes/transform/transform.cpp", ] if env["tools"]: diff --git a/modules/material_maker/config.py b/modules/material_maker/config.py index 4dec6fd44..b4a2be433 100644 --- a/modules/material_maker/config.py +++ b/modules/material_maker/config.py @@ -24,6 +24,7 @@ def get_doc_classes(): "MMGreyscaleUniform", "MMTranslate", + "MMTransform", ] def get_doc_path(): diff --git a/modules/material_maker/nodes/transform/transform.cpp b/modules/material_maker/nodes/transform/transform.cpp index fbde35920..7fab7fdde 100644 --- a/modules/material_maker/nodes/transform/transform.cpp +++ b/modules/material_maker/nodes/transform/transform.cpp @@ -1,296 +1,233 @@ #include "transform.h" +#include "../../algos/mm_algos.h" +#include "../../editor/mm_graph_node.h" -Ref Transform::get_image() { - return image; +Ref MMTransform::get_image() { + return image; } -void Transform::set_image(const Ref &val) { -image = val; +void MMTransform::set_image(const Ref &val) { + image = val; } - -Ref Transform::get_input() { - return input; +Ref MMTransform::get_input() { + return input; } -void Transform::set_input(const Ref &val) { -input = val; +void MMTransform::set_input(const Ref &val) { + input = val; } - -Ref Transform::get_translate_x() { - return translate_x; +Ref MMTransform::get_translate_x() { + return translate_x; } -void Transform::set_translate_x(const Ref &val) { -translate_x = val; +void MMTransform::set_translate_x(const Ref &val) { + translate_x = val; } - -Ref Transform::get_translate_y() { - return translate_y; +Ref MMTransform::get_translate_y() { + return translate_y; } -void Transform::set_translate_y(const Ref &val) { -translate_y = val; +void MMTransform::set_translate_y(const Ref &val) { + translate_y = val; } - -Ref Transform::get_rotate() { - return rotate; +Ref MMTransform::get_rotate() { + return rotate; } -void Transform::set_rotate(const Ref &val) { -rotate = val; +void MMTransform::set_rotate(const Ref &val) { + rotate = val; } - -Ref Transform::get_scale_x() { - return scale_x; +Ref MMTransform::get_scale_x() { + return scale_x; } -void Transform::set_scale_x(const Ref &val) { -scale_x = val; +void MMTransform::set_scale_x(const Ref &val) { + scale_x = val; } - -Ref Transform::get_scale_y() { - return scale_y; +Ref MMTransform::get_scale_y() { + return scale_y; } -void Transform::set_scale_y(const Ref &val) { -scale_y = val; +void MMTransform::set_scale_y(const Ref &val) { + scale_y = val; } - -int Transform::get_mode() const { - return mode; +int MMTransform::get_mode() const { + return mode; } -void Transform::set_mode(const int val) { -mode = val; +void MMTransform::set_mode(const int val) { + mode = val; + set_dirty(true); } +void MMTransform::_init_properties() { + if (!input.is_valid()) { + input.instance(); + input->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_COLOR); + input->set_default_value(Color(0, 0, 0, 1)); + } + input->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); + input->set_slot_name(">>> Input "); - //tool; - //export(Resource) ; - Ref image; - //export(Resource) ; - Ref input; - //export(Resource) ; - Ref translate_x; - //export(Resource) ; - Ref translate_y; - //export(Resource) ; - Ref rotate; - //export(Resource) ; - Ref scale_x; - //export(Resource) ; - Ref scale_y; - //export(int, "Clamp,Repeat,Extend") ; - int mode = 0; + if (!image.is_valid()) { + image.instance(); + image->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_IMAGE); + } - void Transform::_init_properties() { + //image.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_FLOAT; + image->set_output_slot_type(MMNodeUniversalProperty::SLOT_TYPE_IMAGE); + //image.force_override = true; - if (!input) { - input = MMNodeUniversalProperty.new(); - input.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_COLOR; - input.set_default_value(Color(0, 0, 0, 1)); + if (!translate_x.is_valid()) { + translate_x.instance(); + translate_x->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT); + translate_x->set_default_value(0); + translate_x->set_value_step(0.01); + } + + translate_x->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); + translate_x->set_slot_name("Translate X"); + + if (!translate_y.is_valid()) { + translate_y.instance(); + translate_y->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT); + translate_y->set_default_value(0); + translate_y->set_value_step(0.01); + } + + translate_y->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); + translate_y->set_slot_name("Translate Y"); + + if (!rotate.is_valid()) { + rotate.instance(); + rotate->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT); + rotate->set_default_value(0); + rotate->set_value_step(0.01); + } + + rotate->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); + rotate->set_slot_name("Rotate"); + + if (!scale_x.is_valid()) { + scale_x.instance(); + scale_x->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT); + scale_x->set_default_value(1); + scale_x->set_value_step(0.01); + } + + scale_x->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); + scale_x->set_slot_name("Scale X"); + + if (!scale_y.is_valid()) { + scale_y.instance(); + scale_y->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT); + scale_y->set_default_value(1); + scale_y->set_value_step(0.01); + } + + scale_y->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL); + scale_y->set_slot_name("Scale Y"); + + register_input_property(input); + register_output_property(image); + register_input_property(translate_x); + register_input_property(translate_y); + register_input_property(rotate); + register_input_property(scale_x); + register_input_property(scale_y); } - input.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL; - input.slot_name = ">>> Input "; +void MMTransform::_register_methods(MMGraphNode *mm_graph_node) { + mm_graph_node->add_slot_label_universal(input); + mm_graph_node->add_slot_texture_universal(image); + mm_graph_node->add_slot_float_universal(translate_x); + mm_graph_node->add_slot_float_universal(translate_y); + mm_graph_node->add_slot_float_universal(rotate); + mm_graph_node->add_slot_float_universal(scale_x); + mm_graph_node->add_slot_float_universal(scale_y); - if (!image) { - image = MMNodeUniversalProperty.new(); - image.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_IMAGE; + Array arr; + arr.push_back("Clamp"); + arr.push_back("Repeat"); + arr.push_back("Extend"); + + mm_graph_node->add_slot_enum("get_mode", "set_mode", "Mode", arr); } - //image.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_FLOAT; - image.output_slot_type = MMNodeUniversalProperty.SLOT_TYPE_IMAGE; - //image.force_override = true; - - if (!translate_x) { - translate_x = MMNodeUniversalProperty.new(); - translate_x.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_FLOAT; - translate_x.set_default_value(0); - translate_x.value_step = 0.01; +void MMTransform::_render(const Ref &material) { + Ref img = render_image(material); + image->set_value(img); } - translate_x.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL; - translate_x.slot_name = "Translate X"; +Color MMTransform::_get_value_for(const Vector2 &uv, const int pseed) { + //$i($mode(transform2($uv, vec2($translate_x*(2.0*$tx($uv)-1.0), $translate_y*(2.0*$ty($uv)-1.0)), $rotate*0.01745329251*(2.0*$r($uv)-1.0), vec2($scale_x*(2.0*$sx($uv)-1.0), $scale_y*(2.0*$sy($uv)-1.0)))))",; + //Mode:; + //Clamp -> transform2_clamp; + //Repeat -> fract; + //Extend -> ""; + Vector2 tr = Vector2(float(translate_x->get_default_value()) * (2.0 * float(translate_x->get_value_or_zero(uv)) - 1.0), float(translate_y->get_default_value()) * (2.0 * float(translate_y->get_value_or_zero(uv)) - 1.0)); + float rot = float(rotate->get_default_value()) * 0.01745329251 * (2.0 * float(rotate->get_value_or_zero(uv)) - 1.0); + Vector2 sc = Vector2(float(scale_x->get_default_value()) * (2.0 * float(scale_x->get_value_or_zero(uv)) - 1.0), float(scale_y->get_default_value()) * (2.0 * float(scale_y->get_value_or_zero(uv)) - 1.0)); + Vector2 nuv = MMAlgos::transform2(uv, tr, rot, sc); - if (!translate_y) { - translate_y = MMNodeUniversalProperty.new(); - translate_y.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_FLOAT; - translate_y.set_default_value(0); - translate_y.value_step = 0.01; + if (mode == 0) { + nuv = MMAlgos::transform2_clamp(nuv); + } + + else if (mode == 1) { + nuv = MMAlgos::fractv2(nuv); + } + + return input->get_value(nuv); } - translate_y.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL; - translate_y.slot_name = "Translate Y"; - - if (!rotate) { - rotate = MMNodeUniversalProperty.new(); - rotate.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_FLOAT; - rotate.set_default_value(0); - rotate.value_step = 0.01; +MMTransform::MMTransform() { + mode = 0; } - rotate.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL; - rotate.slot_name = "Rotate"; - - if (!scale_x) { - scale_x = MMNodeUniversalProperty.new(); - scale_x.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_FLOAT; - scale_x.set_default_value(1); - scale_x.value_step = 0.01; +MMTransform::~MMTransform() { } - scale_x.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL; - scale_x.slot_name = "Scale X"; +void MMTransform::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_image"), &MMTransform::get_image); + ClassDB::bind_method(D_METHOD("set_image", "value"), &MMTransform::set_image); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_image", "get_image"); - if (!scale_y) { - scale_y = MMNodeUniversalProperty.new(); - scale_y.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_FLOAT; - scale_y.set_default_value(1); - scale_y.value_step = 0.01; + ClassDB::bind_method(D_METHOD("get_input"), &MMTransform::get_input); + ClassDB::bind_method(D_METHOD("set_input", "value"), &MMTransform::set_input); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_input", "get_input"); + + ClassDB::bind_method(D_METHOD("get_translate_x"), &MMTransform::get_translate_x); + ClassDB::bind_method(D_METHOD("set_translate_x", "value"), &MMTransform::set_translate_x); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "translate_x", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_translate_x", "get_translate_x"); + + ClassDB::bind_method(D_METHOD("get_translate_y"), &MMTransform::get_translate_y); + ClassDB::bind_method(D_METHOD("set_translate_y", "value"), &MMTransform::set_translate_y); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "translate_y", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_translate_y", "get_translate_y"); + + ClassDB::bind_method(D_METHOD("get_rotate"), &MMTransform::get_rotate); + ClassDB::bind_method(D_METHOD("set_rotate", "value"), &MMTransform::set_rotate); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "rotate", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_rotate", "get_rotate"); + + ClassDB::bind_method(D_METHOD("get_scale_x"), &MMTransform::get_scale_x); + ClassDB::bind_method(D_METHOD("set_scale_x", "value"), &MMTransform::set_scale_x); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_x", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_scale_x", "get_scale_x"); + + ClassDB::bind_method(D_METHOD("get_scale_y"), &MMTransform::get_scale_y); + ClassDB::bind_method(D_METHOD("set_scale_y", "value"), &MMTransform::set_scale_y); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_y", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_scale_y", "get_scale_y"); + + ClassDB::bind_method(D_METHOD("get_mode"), &MMTransform::get_mode); + ClassDB::bind_method(D_METHOD("set_mode", "value"), &MMTransform::set_mode); + ADD_PROPERTY(PropertyInfo(Variant::INT, "mode"), "set_mode", "get_mode"); } - - scale_y.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL; - scale_y.slot_name = "Scale Y"; - register_input_property(input); - register_output_property(image); - register_input_property(translate_x); - register_input_property(translate_y); - register_input_property(rotate); - register_input_property(scale_x); - register_input_property(scale_y); -} - - - void Transform::_register_methods(const Variant &mm_graph_node) { - mm_graph_node.add_slot_label_universal(input); - mm_graph_node.add_slot_texture_universal(image); - mm_graph_node.add_slot_float_universal(translate_x); - mm_graph_node.add_slot_float_universal(translate_y); - mm_graph_node.add_slot_float_universal(rotate); - mm_graph_node.add_slot_float_universal(scale_x); - mm_graph_node.add_slot_float_universal(scale_y); - mm_graph_node.add_slot_enum("get_mode", "set_mode", "Mode", [ "Clamp", "Repeat", "Extend" ]); -} - - - void Transform::_render(const Variant &material) { - Ref img = render_image(material); - image.set_value(img); -} - - - Color Transform::_get_value_for(const Vector2 &uv, const int pseed) { - //$i($mode(transform2($uv, vec2($translate_x*(2.0*$tx($uv)-1.0), $translate_y*(2.0*$ty($uv)-1.0)), $rotate*0.01745329251*(2.0*$r($uv)-1.0), vec2($scale_x*(2.0*$sx($uv)-1.0), $scale_y*(2.0*$sy($uv)-1.0)))))",; - //Mode:; - //Clamp -> transform2_clamp; - //Repeat -> fract; - //Extend -> ""; - Vector2 tr = Vector2(translate_x.get_default_value() * (2.0 * translate_x.get_value_or_zero(uv) - 1.0), translate_y.get_default_value() *(2.0 * translate_y.get_value_or_zero(uv) - 1.0)); - float rot = rotate.get_default_value() * 0.01745329251*(2.0 * rotate.get_value_or_zero(uv) - 1.0); - Vector2 sc = Vector2(scale_x.get_default_value() *(2.0 * scale_x.get_value_or_zero(uv) - 1.0), scale_y.get_default_value() *(2.0 * scale_y.get_value_or_zero(uv) - 1.0)); - Vector2 nuv = MMAlgos.transform2(uv, tr, rot, sc); - - if (mode == 0) { - nuv = MMAlgos.transform2_clamp(nuv); -} - - - else if (mode == 1) { - nuv = MMAlgos.fractv2(nuv); -} - - return input.get_value(nuv); -} - - //mode; - - int Transform::get_mode() { - return mode; -} - - - void Transform::set_mode(const int val) { - mode = val; - set_dirty(true); -} - -} - - Transform::Transform() { - image; - input; - translate_x; - translate_y; - rotate; - scale_x; - scale_y; - mode = 0; - } - - Transform::~Transform() { - } - - - static void Transform::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_image"), &Transform::get_image); - ClassDB::bind_method(D_METHOD("set_image", "value"), &Transform::set_image); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_image", "get_image"); - - - ClassDB::bind_method(D_METHOD("get_input"), &Transform::get_input); - ClassDB::bind_method(D_METHOD("set_input", "value"), &Transform::set_input); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_input", "get_input"); - - - ClassDB::bind_method(D_METHOD("get_translate_x"), &Transform::get_translate_x); - ClassDB::bind_method(D_METHOD("set_translate_x", "value"), &Transform::set_translate_x); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "translate_x", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_translate_x", "get_translate_x"); - - - ClassDB::bind_method(D_METHOD("get_translate_y"), &Transform::get_translate_y); - ClassDB::bind_method(D_METHOD("set_translate_y", "value"), &Transform::set_translate_y); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "translate_y", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_translate_y", "get_translate_y"); - - - ClassDB::bind_method(D_METHOD("get_rotate"), &Transform::get_rotate); - ClassDB::bind_method(D_METHOD("set_rotate", "value"), &Transform::set_rotate); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "rotate", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_rotate", "get_rotate"); - - - ClassDB::bind_method(D_METHOD("get_scale_x"), &Transform::get_scale_x); - ClassDB::bind_method(D_METHOD("set_scale_x", "value"), &Transform::set_scale_x); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_x", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_scale_x", "get_scale_x"); - - - ClassDB::bind_method(D_METHOD("get_scale_y"), &Transform::get_scale_y); - ClassDB::bind_method(D_METHOD("set_scale_y", "value"), &Transform::set_scale_y); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_y", PROPERTY_HINT_RESOURCE_TYPE, "Ref"), "set_scale_y", "get_scale_y"); - - - ClassDB::bind_method(D_METHOD("get_mode"), &Transform::get_mode); - ClassDB::bind_method(D_METHOD("set_mode", "value"), &Transform::set_mode); - ADD_PROPERTY(PropertyInfo(Variant::INT, "mode"), "set_mode", "get_mode"); - - - ClassDB::bind_method(D_METHOD("_init_properties"), &Transform::_init_properties); - ClassDB::bind_method(D_METHOD("_register_methods", "mm_graph_node"), &Transform::_register_methods); - ClassDB::bind_method(D_METHOD("_render", "material"), &Transform::_render); - ClassDB::bind_method(D_METHOD("_get_value_for", "uv", "pseed"), &Transform::_get_value_for); - ClassDB::bind_method(D_METHOD("get_mode"), &Transform::get_mode); - ClassDB::bind_method(D_METHOD("set_mode", "val"), &Transform::set_mode); - - } - - - diff --git a/modules/material_maker/nodes/transform/transform.h b/modules/material_maker/nodes/transform/transform.h index 1bf9e605b..580f28ae9 100644 --- a/modules/material_maker/nodes/transform/transform.h +++ b/modules/material_maker/nodes/transform/transform.h @@ -1,68 +1,57 @@ -#ifndef TRANSFORM_H -#define TRANSFORM_H +#ifndef MM_TRANSFORM_H +#define MM_TRANSFORM_H +#include "../mm_node.h" +#include "../mm_node_universal_property.h" -class Transform : public MMNode { - GDCLASS(Transform, MMNode); +class MMTransform : public MMNode { + GDCLASS(MMTransform, MMNode); - public: +public: + Ref get_image(); + void set_image(const Ref &val); - Ref get_image(); - void set_image(const Ref &val); + Ref get_input(); + void set_input(const Ref &val); - Ref get_input(); - void set_input(const Ref &val); + Ref get_translate_x(); + void set_translate_x(const Ref &val); - Ref get_translate_x(); - void set_translate_x(const Ref &val); + Ref get_translate_y(); + void set_translate_y(const Ref &val); - Ref get_translate_y(); - void set_translate_y(const Ref &val); + Ref get_rotate(); + void set_rotate(const Ref &val); - Ref get_rotate(); - void set_rotate(const Ref &val); + Ref get_scale_x(); + void set_scale_x(const Ref &val); - Ref get_scale_x(); - void set_scale_x(const Ref &val); + Ref get_scale_y(); + void set_scale_y(const Ref &val); - Ref get_scale_y(); - void set_scale_y(const Ref &val); + int get_mode() const; + void set_mode(const int val); - int get_mode() const; - void set_mode(const int val); + void _init_properties(); + void _register_methods(MMGraphNode *mm_graph_node); + void _render(const Ref &material); + Color _get_value_for(const Vector2 &uv, const int pseed); - void _init_properties(); - void _register_methods(const Variant &mm_graph_node); - void _render(const Variant &material); - Color _get_value_for(const Vector2 &uv, const int pseed); - int get_mode(); - void set_mode(const int val); + MMTransform(); + ~MMTransform(); - Transform(); - ~Transform(); +protected: + static void _bind_methods(); - protected: - static void _bind_methods(); - - //tool - //export(Resource) - Ref image; - //export(Resource) - Ref input; - //export(Resource) - Ref translate_x; - //export(Resource) - Ref translate_y; - //export(Resource) - Ref rotate; - //export(Resource) - Ref scale_x; - //export(Resource) - Ref scale_y; - //export(int, "Clamp,Repeat,Extend") - int mode = 0; - //mode + Ref image; + Ref input; + Ref translate_x; + Ref translate_y; + Ref rotate; + Ref scale_x; + Ref scale_y; + //export(int, "Clamp,Repeat,Extend") + int mode; }; - #endif diff --git a/modules/material_maker/register_types.cpp b/modules/material_maker/register_types.cpp index ac5f40c7d..0495d67d1 100644 --- a/modules/material_maker/register_types.cpp +++ b/modules/material_maker/register_types.cpp @@ -45,6 +45,7 @@ SOFTWARE. #include "nodes/uniform/greyscale_uniform.h" #include "nodes/uniform/uniform.h" +#include "nodes/transform/transform.h" #include "nodes/transform/translate.h" static _MMAlgos *_mm_algos_singleton = nullptr; @@ -71,6 +72,9 @@ void register_material_maker_types() { ClassDB::register_class(); MMAlgos::register_node_class("Transform", "MMTranslate"); + ClassDB::register_class(); + MMAlgos::register_node_class("Transform", "MMTransform"); + _mm_algos_singleton = memnew(_MMAlgos); Engine::get_singleton()->add_singleton(Engine::Singleton("MMAlgos", _MMAlgos::get_singleton()));