Cleaned up MMSdf3dRender.

This commit is contained in:
Relintai 2022-06-17 12:25:33 +02:00
parent 5c67416ad5
commit b1cc1cb693
5 changed files with 235 additions and 299 deletions

View File

@ -69,11 +69,14 @@ sources = [
"nodes/simple/image.cpp",
"nodes/simple/curve.cpp",
"nodes/sdf3d/sdf3d_render.cpp",
"nodes/sdf3d/sdf3d_tf_translate.cpp",
"nodes/sdf3d/sdf3d_tf_scale.cpp",
"nodes/sdf3d/sdf3d_tf_rotate.cpp",
"nodes/sdf3d/sdf3d_shape_torus.cpp",
]
if env["tools"]:

View File

@ -39,6 +39,8 @@ def get_doc_classes():
"MMImage",
"MMCurve",
"MMSdf3dRender",
"MMSdf3dTfTranslate",
"MMSdf3dTfScale",
"MMSdf3dTfRotate",

View File

@ -1,118 +1,110 @@
#include "sdf3d_render.h"
#include "../../algos/mm_algos.h"
#include "../../editor/mm_graph_node.h"
#include "../mm_material.h"
Ref<Resource> Sdf3dRender::get_input() {
Ref<MMNodeUniversalProperty> MMSdf3dRender::get_input() {
return input;
}
void Sdf3dRender::set_input(const Ref<Resource> &val) {
input = val;
void MMSdf3dRender::set_input(const Ref<MMNodeUniversalProperty> &val) {
input = val;
}
Ref<Resource> Sdf3dRender::get_out_height_map() {
Ref<MMNodeUniversalProperty> MMSdf3dRender::get_out_height_map() {
return out_height_map;
}
void Sdf3dRender::set_out_height_map(const Ref<Resource> &val) {
out_height_map = val;
void MMSdf3dRender::set_out_height_map(const Ref<MMNodeUniversalProperty> &val) {
out_height_map = val;
}
Ref<Resource> Sdf3dRender::get_out_normal_map() {
Ref<MMNodeUniversalProperty> MMSdf3dRender::get_out_normal_map() {
return out_normal_map;
}
void Sdf3dRender::set_out_normal_map(const Ref<Resource> &val) {
out_normal_map = val;
void MMSdf3dRender::set_out_normal_map(const Ref<MMNodeUniversalProperty> &val) {
out_normal_map = val;
}
Ref<Resource> Sdf3dRender::get_out_color_map() {
Ref<MMNodeUniversalProperty> MMSdf3dRender::get_out_color_map() {
return out_color_map;
}
void Sdf3dRender::set_out_color_map(const Ref<Resource> &val) {
out_color_map = val;
void MMSdf3dRender::set_out_color_map(const Ref<MMNodeUniversalProperty> &val) {
out_color_map = val;
}
//tool;
//export(Resource) ;
Ref<Resource> input;
//export(Resource) ;
Ref<Resource> out_height_map;
//export(Resource) ;
Ref<Resource> out_normal_map;
//export(Resource) ;
Ref<Resource> out_color_map;
void Sdf3dRender::_init_properties() {
if (!input) {
input = MMNodeUniversalProperty.new();
input.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_VECTOR2;
}
void MMSdf3dRender::_init_properties() {
if (!input.is_valid()) {
input.instance();
input->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_VECTOR2);
}
//for some reason this doesn't work, todo check;
// input.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_FLOAT;
input.input_slot_type = MMNodeUniversalProperty.SLOT_TYPE_UNIVERSAL;
input.slot_name = "Input";
input->set_input_slot_type(MMNodeUniversalProperty::SLOT_TYPE_UNIVERSAL);
input->set_slot_name("Input");
if (!out_height_map) {
out_height_map = MMNodeUniversalProperty.new();
out_height_map.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_IMAGE;
}
if (!out_height_map.is_valid()) {
out_height_map.instance();
out_height_map->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_IMAGE);
}
out_height_map.output_slot_type = MMNodeUniversalProperty.SLOT_TYPE_IMAGE;
out_height_map->set_output_slot_type(MMNodeUniversalProperty::SLOT_TYPE_IMAGE);
if (!out_normal_map) {
out_normal_map = MMNodeUniversalProperty.new();
out_normal_map.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_IMAGE;
}
if (!out_normal_map.is_valid()) {
out_normal_map.instance();
out_normal_map->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_IMAGE);
}
out_normal_map.output_slot_type = MMNodeUniversalProperty.SLOT_TYPE_IMAGE;
out_normal_map->set_output_slot_type(MMNodeUniversalProperty::SLOT_TYPE_IMAGE);
if (!out_color_map) {
out_color_map = MMNodeUniversalProperty.new();
out_color_map.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_IMAGE;
}
if (!out_color_map.is_valid()) {
out_color_map.instance();
out_color_map->set_default_type(MMNodeUniversalProperty::DEFAULT_TYPE_IMAGE);
}
out_color_map->set_output_slot_type(MMNodeUniversalProperty::SLOT_TYPE_IMAGE);
out_color_map.output_slot_type = MMNodeUniversalProperty.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);
}
void Sdf3dRender::_register_methods(const Variant &mm_graph_node) {
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);
void MMSdf3dRender::_register_methods(MMGraphNode *mm_graph_node) {
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);
}
void MMSdf3dRender::_render(const Ref<MMMaterial> &material) {
Ref<Image> height_map;
Ref<Image> normal_map;
Ref<Image> color_map;
void Sdf3dRender::_render(const Variant &material) {
Ref<Image> height_map = Image.new();
Ref<Image> normal_map = Image.new();
Ref<Image> color_map = 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();
float w = material.image_size.x;
float h = material.image_size.y;
float pseed = randf() + randi();
height_map.instance();
normal_map.instance();
color_map.instance();
for (int x = 0; x < material.image_size.x; ++x) { //x in range(material.image_size.x)
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);
for (int y = 0; y < material.image_size.y; ++y) { //y in range(material.image_size.y)
height_map->lock();
normal_map->lock();
color_map->lock();
float w = material->image_size.x;
float h = material->image_size.y;
//float pseed = Math::randf() + Math::rand();
for (int x = 0; x < material->image_size.x; ++x) { //x in range(material.image_size.x)
for (int y = 0; y < material->image_size.y; ++y) { //y in range(material.image_size.y)
Vector2 uv = Vector2(x / w, y / h);
Vector2 raymarch = sdf3d_raymarch(uv);
//HeightMap - float - The generated height map;
@ -128,49 +120,48 @@ out_color_map = val;
//ColorMap - float - The generated color index map;
//$(name_uv)_d.y;
Color color_map_col = 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->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);
}
}
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);
}
Color Sdf3dRender::_get_value_for(const Vector2 &uv, const int pseed) {
Color MMSdf3dRender::_get_value_for(const Vector2 &uv, const int pseed) {
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);
//};
//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);
//};
Vector2 Sdf3dRender::sdf3d_raymarch(const Vector2 &uv) {
Vector2 MMSdf3dRender::sdf3d_raymarch(const Vector2 &uv) {
Vector3 ro = Vector3(uv.x - 0.5, uv.y - 0.5, 1.0);
Vector3 rd = Vector3(0.0, 0.0, -1.0);
float dO = 0.0;
@ -178,96 +169,71 @@ out_color_map = val;
for (int i = 0; i < 100; ++i) { //i in range(100)
Vector3 p = ro + rd * dO;
Vector2 dS = input.get_value_sdf3d(p);
Vector2 dS = input->get_value_sdf3d(p);
dO += dS.x;
if ((dO >= 1.0)) {
break;
}
}
else if ((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);
//};
Vector3 Sdf3dRender::sdf3d_normal(const Vector3 &p) {
//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);
//};
Vector3 MMSdf3dRender::sdf3d_normal(const Vector3 &p) {
if ((p.z <= 0.0)) {
return Vector3(0.0, 0.0, 1.0);
}
}
float d = input.get_value_sdf3d(p).x;
float d = input->get_value_sdf3d(p).x;
float e = .001;
Vector3 n = 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);
Vector3 n = 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();
}
MMSdf3dRender::MMSdf3dRender() {
}
Sdf3dRender::Sdf3dRender() {
input;
out_height_map;
out_normal_map;
out_color_map;
}
MMSdf3dRender::~MMSdf3dRender() {
}
Sdf3dRender::~Sdf3dRender() {
}
void MMSdf3dRender::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_input"), &MMSdf3dRender::get_input);
ClassDB::bind_method(D_METHOD("set_input", "value"), &MMSdf3dRender::set_input);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input", PROPERTY_HINT_RESOURCE_TYPE, "Ref<MMNodeUniversalProperty>"), "set_input", "get_input");
ClassDB::bind_method(D_METHOD("get_out_height_map"), &MMSdf3dRender::get_out_height_map);
ClassDB::bind_method(D_METHOD("set_out_height_map", "value"), &MMSdf3dRender::set_out_height_map);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "out_height_map", PROPERTY_HINT_RESOURCE_TYPE, "Ref<MMNodeUniversalProperty>"), "set_out_height_map", "get_out_height_map");
static void Sdf3dRender::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_input"), &Sdf3dRender::get_input);
ClassDB::bind_method(D_METHOD("set_input", "value"), &Sdf3dRender::set_input);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input", PROPERTY_HINT_RESOURCE_TYPE, "Ref<Resource>"), "set_input", "get_input");
ClassDB::bind_method(D_METHOD("get_out_height_map"), &Sdf3dRender::get_out_height_map);
ClassDB::bind_method(D_METHOD("set_out_height_map", "value"), &Sdf3dRender::set_out_height_map);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "out_height_map", PROPERTY_HINT_RESOURCE_TYPE, "Ref<Resource>"), "set_out_height_map", "get_out_height_map");
ClassDB::bind_method(D_METHOD("get_out_normal_map"), &Sdf3dRender::get_out_normal_map);
ClassDB::bind_method(D_METHOD("set_out_normal_map", "value"), &Sdf3dRender::set_out_normal_map);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "out_normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Ref<Resource>"), "set_out_normal_map", "get_out_normal_map");
ClassDB::bind_method(D_METHOD("get_out_color_map"), &Sdf3dRender::get_out_color_map);
ClassDB::bind_method(D_METHOD("set_out_color_map", "value"), &Sdf3dRender::set_out_color_map);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "out_color_map", PROPERTY_HINT_RESOURCE_TYPE, "Ref<Resource>"), "set_out_color_map", "get_out_color_map");
ClassDB::bind_method(D_METHOD("_init_properties"), &Sdf3dRender::_init_properties);
ClassDB::bind_method(D_METHOD("_register_methods", "mm_graph_node"), &Sdf3dRender::_register_methods);
ClassDB::bind_method(D_METHOD("_render", "material"), &Sdf3dRender::_render);
ClassDB::bind_method(D_METHOD("_get_value_for", "uv", "pseed"), &Sdf3dRender::_get_value_for);
ClassDB::bind_method(D_METHOD("sdf3d_raymarch", "uv"), &Sdf3dRender::sdf3d_raymarch);
ClassDB::bind_method(D_METHOD("sdf3d_normal", "p"), &Sdf3dRender::sdf3d_normal);
}
ClassDB::bind_method(D_METHOD("get_out_normal_map"), &MMSdf3dRender::get_out_normal_map);
ClassDB::bind_method(D_METHOD("set_out_normal_map", "value"), &MMSdf3dRender::set_out_normal_map);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "out_normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Ref<MMNodeUniversalProperty>"), "set_out_normal_map", "get_out_normal_map");
ClassDB::bind_method(D_METHOD("get_out_color_map"), &MMSdf3dRender::get_out_color_map);
ClassDB::bind_method(D_METHOD("set_out_color_map", "value"), &MMSdf3dRender::set_out_color_map);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "out_color_map", PROPERTY_HINT_RESOURCE_TYPE, "Ref<MMNodeUniversalProperty>"), "set_out_color_map", "get_out_color_map");
ClassDB::bind_method(D_METHOD("sdf3d_raymarch", "uv"), &MMSdf3dRender::sdf3d_raymarch);
ClassDB::bind_method(D_METHOD("sdf3d_normal", "p"), &MMSdf3dRender::sdf3d_normal);
}

View File

@ -1,82 +1,42 @@
#ifndef SDF3D_RENDER_H
#define SDF3D_RENDER_H
#ifndef MM_SDF3D_RENDER_H
#define MM_SDF3D_RENDER_H
#include "../mm_node.h"
#include "../mm_node_universal_property.h"
class Sdf3dRender : public MMNode {
GDCLASS(Sdf3dRender, MMNode);
class MMSdf3dRender : public MMNode {
GDCLASS(MMSdf3dRender, MMNode);
public:
public:
Ref<MMNodeUniversalProperty> get_input();
void set_input(const Ref<MMNodeUniversalProperty> &val);
Ref<Resource> get_input();
void set_input(const Ref<Resource> &val);
Ref<MMNodeUniversalProperty> get_out_height_map();
void set_out_height_map(const Ref<MMNodeUniversalProperty> &val);
Ref<Resource> get_out_height_map();
void set_out_height_map(const Ref<Resource> &val);
Ref<MMNodeUniversalProperty> get_out_normal_map();
void set_out_normal_map(const Ref<MMNodeUniversalProperty> &val);
Ref<Resource> get_out_normal_map();
void set_out_normal_map(const Ref<Resource> &val);
Ref<Resource> get_out_color_map();
void set_out_color_map(const Ref<Resource> &val);
Ref<MMNodeUniversalProperty> get_out_color_map();
void set_out_color_map(const Ref<MMNodeUniversalProperty> &val);
void _init_properties();
void _register_methods(const Variant &mm_graph_node);
void _render(const Variant &material);
void _register_methods(MMGraphNode *mm_graph_node);
void _render(const Ref<MMMaterial> &material);
Color _get_value_for(const Vector2 &uv, const int pseed);
Vector2 sdf3d_raymarch(const Vector2 &uv);
Vector3 sdf3d_normal(const Vector3 &p);
Sdf3dRender();
~Sdf3dRender();
MMSdf3dRender();
~MMSdf3dRender();
protected:
protected:
static void _bind_methods();
//tool
//export(Resource)
Ref<Resource> input;
//export(Resource)
Ref<Resource> out_height_map;
//export(Resource)
Ref<Resource> out_normal_map;
//export(Resource)
Ref<Resource> out_color_map;
//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);
//}
//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);
//}
Ref<MMNodeUniversalProperty> input;
Ref<MMNodeUniversalProperty> out_height_map;
Ref<MMNodeUniversalProperty> out_normal_map;
Ref<MMNodeUniversalProperty> out_color_map;
};
#endif

View File

@ -61,6 +61,8 @@ SOFTWARE.
#include "nodes/simple/image.h"
#include "nodes/simple/shape.h"
#include "nodes/sdf3d/sdf3d_render.h"
#include "nodes/sdf3d/sdf3d_tf_rotate.h"
#include "nodes/sdf3d/sdf3d_tf_scale.h"
#include "nodes/sdf3d/sdf3d_tf_translate.h"
@ -118,6 +120,9 @@ void register_material_maker_types() {
ClassDB::register_class<MMCurve>();
MMAlgos::register_node_class("Simple", "MMCurve");
ClassDB::register_class<MMSdf3dRender>();
MMAlgos::register_node_class("SDF3D", "MMSdf3dRender");
ClassDB::register_class<MMSdf3dTfTranslate>();
MMAlgos::register_node_class("SDF3D - TF", "MMSdf3dTfTranslate");
ClassDB::register_class<MMSdf3dTfScale>();