pandemonium_engine/modules/material_maker/nodes/sdf3d/sdf3d_render.cpp

274 lines
7.9 KiB
C++
Raw Normal View History

2022-06-16 15:29:08 +02:00
#include "sdf3d_render.h"
Ref<Resource> Sdf3dRender::get_input() {
return input;
}
void Sdf3dRender::set_input(const Ref<Resource> &val) {
input = val;
}
Ref<Resource> Sdf3dRender::get_out_height_map() {
return out_height_map;
}
void Sdf3dRender::set_out_height_map(const Ref<Resource> &val) {
out_height_map = val;
}
Ref<Resource> Sdf3dRender::get_out_normal_map() {
return out_normal_map;
}
void Sdf3dRender::set_out_normal_map(const Ref<Resource> &val) {
out_normal_map = val;
}
Ref<Resource> Sdf3dRender::get_out_color_map() {
return out_color_map;
}
void Sdf3dRender::set_out_color_map(const Ref<Resource> &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;
}
//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";
if (!out_height_map) {
out_height_map = MMNodeUniversalProperty.new();
out_height_map.default_type = MMNodeUniversalProperty.DEFAULT_TYPE_IMAGE;
}
out_height_map.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;
}
out_normal_map.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;
}
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 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();
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;
//1.0-$(name_uv)_d.x;
float hmf = 1.0 - raymarch.x;
Color height_map_col = 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));
Vector2 nuv = uv - Vector2(0.5, 0.5);
Vector3 n = sdf3d_normal(Vector3(nuv.x, nuv.y, 1.0 - raymarch.x));
Vector3 nn = Vector3(0.5, 0.5, 0.5) + 0.5 * n;
Color normal_map_col = Color(nn.x, nn.y, nn.z, 1);
//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.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) {
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);
//};
Vector2 Sdf3dRender::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;
float c = 0.0;
for (int i = 0; i < 100; ++i) { //i in range(100)
Vector3 p = ro + rd * dO;
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) {
if ((p.z <= 0.0)) {
return Vector3(0.0, 0.0, 1.0);
}
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);
return Vector3(-1.0, -1.0, -1.0) * n.normalized();
}
}
Sdf3dRender::Sdf3dRender() {
input;
out_height_map;
out_normal_map;
out_color_map;
}
Sdf3dRender::~Sdf3dRender() {
}
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);
}