pandemonium_engine/modules/material_maker/nodes/simple/shape.cpp

200 lines
6.8 KiB
C++
Raw Normal View History

2023-12-18 00:02:58 +01:00
/*************************************************************************/
/* shape.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2022-06-16 15:29:08 +02:00
#include "shape.h"
2022-06-17 01:38:18 +02:00
#include "../../algos/mm_algos.h"
#include "../../editor/mm_graph_node.h"
#include "../mm_material.h"
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
Ref<MMNodeUniversalProperty> MMShape::get_image() {
return image;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::set_image(const Ref<MMNodeUniversalProperty> &val) {
image = val;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
int MMShape::get_shape_type() const {
return shape_type;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::set_shape_type(const int val) {
shape_type = val;
set_dirty(true);
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
int MMShape::get_sides() const {
return sides;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::set_sides(const int val) {
sides = val;
set_dirty(true);
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
Ref<MMNodeUniversalProperty> MMShape::get_radius() {
return radius;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::set_radius(const Ref<MMNodeUniversalProperty> &val) {
radius = val;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
Ref<MMNodeUniversalProperty> MMShape::get_edge() {
return edge;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::set_edge(const Ref<MMNodeUniversalProperty> &val) {
edge = val;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::_init_properties() {
if (!image.is_valid()) {
image.instance();
image->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_IMAGE);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
image->set_output_slot_type(MMNodeUniversalProperty::SLOT_TYPE_IMAGE);
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
if (!radius.is_valid()) {
radius.instance();
radius->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT);
radius->set_default_value(0.34375);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
radius->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL);
radius->set_slot_name("radius");
radius->set_value_step(0.05);
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
if (!edge.is_valid()) {
edge.instance();
edge->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_FLOAT);
edge->set_default_value(0.2);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
edge->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL);
edge->set_slot_name("edge");
edge->set_value_step(0.05);
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
register_input_property(radius);
register_input_property(edge);
register_output_property(image);
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::_register_methods(MMGraphNode *mm_graph_node) {
mm_graph_node->add_slot_texture_universal(image);
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
Array arr;
arr.push_back("Circle");
arr.push_back("Polygon");
arr.push_back("Star");
arr.push_back("Curved Star");
arr.push_back("Rays");
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
mm_graph_node->add_slot_enum("get_shape_type", "set_shape_type", "shape_type", arr);
//, Vector2(1, 10));
mm_graph_node->add_slot_int("get_sides", "set_sides", "sides");
mm_graph_node->add_slot_float_universal(radius);
mm_graph_node->add_slot_float_universal(edge);
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::_render(const Ref<MMMaterial> &material) {
Ref<Image> img = render_image(material);
image->set_value(img);
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
Color MMShape::_get_value_for(const Vector2 &uv, const int pseed) {
float c = 0;
float rad = radius->get_value(uv);
float edg = edge->get_value(uv);
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
if (rad == 0) {
rad = 0.0000001;
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
if (edg == 0) {
edg = 0.0000001;
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
if (shape_type == SHAPE_TYPE_CIRCLE) {
c = MMAlgos::shape_circle(uv, sides, rad, edg);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
else if (shape_type == SHAPE_TYPE_POLYGON) {
c = MMAlgos::shape_polygon(uv, sides, rad, edg);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
else if (shape_type == SHAPE_TYPE_STAR) {
c = MMAlgos::shape_star(uv, sides, rad, edg);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
else if (shape_type == SHAPE_TYPE_CURVED_STAR) {
c = MMAlgos::shape_curved_star(uv, sides, rad, edg);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
else if (shape_type == SHAPE_TYPE_RAYS) {
c = MMAlgos::shape_rays(uv, sides, rad, edg);
}
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
return Color(c, c, c, 1);
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
MMShape::MMShape() {
shape_type = 0;
sides = 6;
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
MMShape::~MMShape() {
2022-06-16 15:29:08 +02:00
}
2022-06-17 01:38:18 +02:00
void MMShape::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_image"), &MMShape::get_image);
ClassDB::bind_method(D_METHOD("set_image", "value"), &MMShape::set_image);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "MMNodeUniversalProperty"), "set_image", "get_image");
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
ClassDB::bind_method(D_METHOD("get_shape_type"), &MMShape::get_shape_type);
ClassDB::bind_method(D_METHOD("set_shape_type", "value"), &MMShape::set_shape_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "shape_type"), "set_shape_type", "get_shape_type");
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
ClassDB::bind_method(D_METHOD("get_sides"), &MMShape::get_sides);
ClassDB::bind_method(D_METHOD("set_sides", "value"), &MMShape::set_sides);
ADD_PROPERTY(PropertyInfo(Variant::INT, "sides"), "set_sides", "get_sides");
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
ClassDB::bind_method(D_METHOD("get_radius"), &MMShape::get_radius);
ClassDB::bind_method(D_METHOD("set_radius", "value"), &MMShape::set_radius);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "radius", PROPERTY_HINT_RESOURCE_TYPE, "MMNodeUniversalProperty"), "set_radius", "get_radius");
2022-06-16 15:29:08 +02:00
2022-06-17 01:38:18 +02:00
ClassDB::bind_method(D_METHOD("get_edge"), &MMShape::get_edge);
ClassDB::bind_method(D_METHOD("set_edge", "value"), &MMShape::set_edge);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edge", PROPERTY_HINT_RESOURCE_TYPE, "MMNodeUniversalProperty"), "set_edge", "get_edge");
2022-06-16 15:29:08 +02:00
}