pandemonium_engine/modules/material_maker/nodes/mm_node.cpp

284 lines
9.0 KiB
C++
Raw Normal View History

#include "mm_node.h"
2022-06-07 10:11:45 +02:00
#include "core/object.h"
#include "mm_material.h"
#include "mm_node_universal_property.h"
2022-06-07 10:11:45 +02:00
#include "scene/main/node.h"
Vector2 MMNode::get_graph_position() {
return graph_position;
}
2022-06-07 01:56:45 +02:00
void MMNode::set_graph_position(const Vector2 &pos) {
graph_position = pos;
emit_changed();
}
2022-06-07 12:35:24 +02:00
Vector<Variant> MMNode::get_input_properties() {
Vector<Variant> r;
for (int i = 0; i < input_properties.size(); i++) {
r.push_back(input_properties[i].get_ref_ptr());
}
return r;
}
void MMNode::set_input_properties(const Vector<Variant> &val) {
input_properties.clear();
for (int i = 0; i < val.size(); i++) {
Ref<MMNode> e = Ref<MMNode>(val[i]);
input_properties.push_back(e);
}
}
Vector<Variant> MMNode::get_output_properties() {
Vector<Variant> r;
for (int i = 0; i < output_properties.size(); i++) {
r.push_back(output_properties[i].get_ref_ptr());
}
return r;
}
void MMNode::set_output_properties(const Vector<Variant> &val) {
output_properties.clear();
for (int i = 0; i < val.size(); i++) {
Ref<MMNode> e = Ref<MMNode>(val[i]);
output_properties.push_back(e);
}
}
bool MMNode::get_properties_initialized() const {
return properties_initialized;
}
void MMNode::set_properties_initialized(const bool val) {
properties_initialized = val;
}
bool MMNode::get_dirty() const {
return dirty;
}
void MMNode::set_dirty(const bool val) {
2022-06-07 01:56:45 +02:00
bool changed = val != dirty;
dirty = val;
2022-06-07 01:56:45 +02:00
if (changed) {
emit_changed();
}
}
2022-06-07 01:56:45 +02:00
bool MMNode::render(const Ref<MMMaterial> &material) {
if (!dirty) {
return false;
}
2022-06-07 01:56:45 +02:00
for (int i = 0; i < input_properties.size(); ++i) {
Ref<MMNodeUniversalProperty> p = input_properties[i];
2022-06-07 13:32:04 +02:00
if (!p.is_valid()) {
continue;
}
2022-06-07 01:56:45 +02:00
if (p->get_input_property().is_valid() && p->get_input_property()->get_owner()->get_dirty()) {
return false;
}
}
2022-06-07 10:11:45 +02:00
call("_render", material);
dirty = false;
return true;
}
2022-06-07 01:56:45 +02:00
void MMNode::_render(const Ref<MMMaterial> &material) {
}
2022-06-07 01:56:45 +02:00
Ref<Image> MMNode::render_image(const Ref<MMMaterial> &material) {
2022-06-07 12:35:24 +02:00
return call("_render_image", material);
}
Ref<Image> MMNode::_render_image(const Ref<MMMaterial> &material) {
2022-06-07 01:56:45 +02:00
Ref<Image> image;
image.instance();
image->create(material->image_size.x, material->image_size.y, false, Image::FORMAT_RGBA8);
image->lock();
2022-06-07 01:56:45 +02:00
float w = image->get_width();
float h = image->get_height();
float pseed = Math::randf() + Math::rand();
2022-06-07 01:56:45 +02:00
for (int x = 0; x < image->get_width(); ++x) { //x in range(image.get_width())
for (int y = 0; y < image->get_height(); ++y) { //y in range(image.get_height())
Vector2 v = Vector2(x / w, y / h);
Color col = get_value_for(v, pseed);
2022-06-07 01:56:45 +02:00
image->set_pixel(x, y, col);
}
}
2022-06-07 01:56:45 +02:00
image->unlock();
return image;
}
Color MMNode::get_value_for(const Vector2 &uv, const int pseed) {
2022-06-07 12:35:24 +02:00
return call("_get_value_for", uv, pseed);
}
Color MMNode::_get_value_for(const Vector2 &uv, const int pseed) {
return Color();
}
void MMNode::init_properties() {
if (!properties_initialized) {
properties_initialized = true;
2022-06-07 10:11:45 +02:00
call("_init_properties");
}
}
void MMNode::_init_properties() {
}
2022-06-07 10:11:45 +02:00
void MMNode::register_methods(Node *mm_graph_node) {
init_properties();
2022-06-07 10:11:45 +02:00
call("_register_methods", mm_graph_node);
}
void MMNode::_register_methods_bind(Node *mm_graph_node) {
2022-06-07 12:35:24 +02:00
_register_methods(mm_graph_node);
}
2022-06-07 10:11:45 +02:00
void MMNode::_register_methods(Node *mm_graph_node) {
}
2022-06-07 01:56:45 +02:00
void MMNode::register_input_property(const Ref<MMNodeUniversalProperty> &p_prop) {
Ref<MMNodeUniversalProperty> prop = p_prop;
2022-06-07 01:56:45 +02:00
prop->set_owner(this);
2022-06-07 01:56:45 +02:00
if (!prop->is_connected("changed", this, "on_input_property_changed")) {
prop->connect("changed", this, "on_input_property_changed");
}
2022-06-07 01:56:45 +02:00
input_properties.push_back(prop);
}
2022-06-07 01:56:45 +02:00
void MMNode::unregister_input_property(const Ref<MMNodeUniversalProperty> &p_prop) {
Ref<MMNodeUniversalProperty> prop = p_prop;
if (prop->get_owner() == this) {
prop->set_owner(nullptr);
}
2022-06-07 01:56:45 +02:00
if (prop->is_connected("changed", this, "on_input_property_changed")) {
prop->disconnect("changed", this, "on_input_property_changed");
}
input_properties.erase(prop);
}
2022-06-07 01:56:45 +02:00
void MMNode::register_output_property(const Ref<MMNodeUniversalProperty> &p_prop) {
Ref<MMNodeUniversalProperty> prop = p_prop;
2022-06-07 01:56:45 +02:00
prop->set_owner(this);
output_properties.push_back(prop);
}
2022-06-07 01:56:45 +02:00
void MMNode::unregister_output_property(const Ref<MMNodeUniversalProperty> &p_prop) {
Ref<MMNodeUniversalProperty> prop = p_prop;
2022-06-07 01:56:45 +02:00
if (prop->get_owner() == this) {
prop->set_owner(nullptr);
}
2022-06-07 01:56:45 +02:00
output_properties.erase(prop);
}
void MMNode::on_input_property_changed() {
set_dirty(true);
emit_changed();
}
Variant MMNode::get_property_value(const Vector2 &uv) {
return call("_get_property_value", uv);
}
Variant MMNode::_get_property_value(const Vector2 &uv) {
return Variant();
}
Vector2 MMNode::get_property_value_sdf3d(const Vector3 &uv3) {
return call("_get_property_value_sdf3d", uv3);
}
Vector2 MMNode::_get_property_value_sdf3d(const Vector3 &uv3) {
return Vector2();
}
MMNode::MMNode() {
properties_initialized = false;
dirty = true;
}
MMNode::~MMNode() {
}
2022-06-07 01:56:45 +02:00
void MMNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_graph_position"), &MMNode::get_graph_position);
ClassDB::bind_method(D_METHOD("set_graph_position", "value"), &MMNode::set_graph_position);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_position"), "set_graph_position", "get_graph_position");
2022-06-07 12:35:24 +02:00
ClassDB::bind_method(D_METHOD("get_input_properties"), &MMNode::get_input_properties);
ClassDB::bind_method(D_METHOD("set_input_properties", "value"), &MMNode::set_input_properties);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "input_properties", PROPERTY_HINT_NONE, "17/17:MMNodeUniversalProperty", PROPERTY_USAGE_DEFAULT, "MMNodeUniversalProperty"), "set_input_properties", "get_input_properties");
ClassDB::bind_method(D_METHOD("get_output_properties"), &MMNode::get_output_properties);
ClassDB::bind_method(D_METHOD("set_output_properties", "value"), &MMNode::set_output_properties);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "output_properties", PROPERTY_HINT_NONE, "17/17:MMNodeUniversalProperty", PROPERTY_USAGE_DEFAULT, "MMNodeUniversalProperty"), "set_output_properties", "get_output_properties");
ClassDB::bind_method(D_METHOD("get_properties_initialized"), &MMNode::get_properties_initialized);
ClassDB::bind_method(D_METHOD("set_properties_initialized", "value"), &MMNode::set_properties_initialized);
2022-06-07 10:11:45 +02:00
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "properties_initialized", PROPERTY_HINT_NONE, "", 0), "set_properties_initialized", "get_properties_initialized");
ClassDB::bind_method(D_METHOD("get_dirty"), &MMNode::get_dirty);
ClassDB::bind_method(D_METHOD("set_dirty", "value"), &MMNode::set_dirty);
2022-06-07 10:11:45 +02:00
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dirty", PROPERTY_HINT_NONE, "", 0), "set_dirty", "get_dirty");
2022-06-07 10:11:45 +02:00
BIND_VMETHOD(MethodInfo("_render", PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "MMMaterial")));
ClassDB::bind_method(D_METHOD("render", "material"), &MMNode::render);
ClassDB::bind_method(D_METHOD("_render", "material"), &MMNode::_render);
2022-06-07 12:35:24 +02:00
BIND_VMETHOD(MethodInfo("_render_image", PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "MMMaterial")));
ClassDB::bind_method(D_METHOD("render_image", "material"), &MMNode::render_image);
2022-06-07 12:35:24 +02:00
ClassDB::bind_method(D_METHOD("_render_image", "material"), &MMNode::_render_image);
2022-06-07 12:35:24 +02:00
BIND_VMETHOD(MethodInfo("_get_value_for", PropertyInfo(Variant::VECTOR2, "uv"), PropertyInfo(Variant::INT, "pseed")));
ClassDB::bind_method(D_METHOD("get_value_for", "uv", "pseed"), &MMNode::get_value_for);
2022-06-07 12:35:24 +02:00
ClassDB::bind_method(D_METHOD("_get_value_for", "uv", "pseed"), &MMNode::_get_value_for);
2022-06-07 10:11:45 +02:00
BIND_VMETHOD(MethodInfo("_init_properties"));
ClassDB::bind_method(D_METHOD("init_properties"), &MMNode::init_properties);
ClassDB::bind_method(D_METHOD("_init_properties"), &MMNode::_init_properties);
2022-06-07 12:35:24 +02:00
BIND_VMETHOD(MethodInfo("_register_methods", PropertyInfo(Variant::OBJECT, "mm_graph_node")));
ClassDB::bind_method(D_METHOD("register_methods", "mm_graph_node"), &MMNode::register_methods);
2022-06-07 10:11:45 +02:00
ClassDB::bind_method(D_METHOD("_register_methods", "mm_graph_node"), &MMNode::_register_methods_bind);
ClassDB::bind_method(D_METHOD("register_input_property", "prop"), &MMNode::register_input_property);
ClassDB::bind_method(D_METHOD("unregister_input_property", "prop"), &MMNode::unregister_input_property);
ClassDB::bind_method(D_METHOD("register_output_property", "prop"), &MMNode::register_output_property);
ClassDB::bind_method(D_METHOD("unregister_output_property", "prop"), &MMNode::unregister_output_property);
ClassDB::bind_method(D_METHOD("on_input_property_changed"), &MMNode::on_input_property_changed);
BIND_VMETHOD(MethodInfo("_get_property_value", PropertyInfo(Variant::VECTOR2, "uv")));
ClassDB::bind_method(D_METHOD("get_property_value", "uv"), &MMNode::get_property_value);
ClassDB::bind_method(D_METHOD("_get_property_value", "uv"), &MMNode::_get_property_value);
BIND_VMETHOD(MethodInfo("_get_property_value_sdf3d", PropertyInfo(Variant::VECTOR3, "uv3")));
ClassDB::bind_method(D_METHOD("get_property_value_sdf3d", "uv3"), &MMNode::get_property_value_sdf3d);
ClassDB::bind_method(D_METHOD("_get_property_value_sdf3d", "uv3"), &MMNode::_get_property_value_sdf3d);
}