diff options
author | Charlie Jolly <mistajolly@gmail.com> | 2021-05-24 14:14:06 +0300 |
---|---|---|
committer | Charlie Jolly <mistajolly@gmail.com> | 2021-05-25 17:43:51 +0300 |
commit | 8cd506639a789694bb229698adad6de36bc1a26d (patch) | |
tree | c4c8273b70634ead3346a4cd7247d84e4c4a7fa8 /source | |
parent | 54ae7baa4c3e0d4fa745f55b632148f505fad935 (diff) |
Geometry Nodes: Add Shader Curve Nodes
Convert curve vec and curve rgb shader nodes to geometry nodes, based on node_shader_valToRgb.cc implementation.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenkernel/intern/node.cc | 2 | ||||
-rw-r--r-- | source/blender/nodes/shader/nodes/node_shader_curves.cc | 100 |
2 files changed, 99 insertions, 3 deletions
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 643dc58af18..3377f5c69dc 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -513,7 +513,7 @@ void ntreeBlendWrite(BlendWriter *writer, bNodeTree *ntree) if (node->storage) { /* could be handlerized at some point, now only 1 exception still */ - if ((ntree->type == NTREE_SHADER) && + if ((ELEM(ntree->type, NTREE_SHADER, NTREE_GEOMETRY)) && ELEM(node->type, SH_NODE_CURVE_VEC, SH_NODE_CURVE_RGB)) { BKE_curvemapping_blend_write(writer, (const CurveMapping *)node->storage); } diff --git a/source/blender/nodes/shader/nodes/node_shader_curves.cc b/source/blender/nodes/shader/nodes/node_shader_curves.cc index 551c1e4e8c4..bc99e4b5dd8 100644 --- a/source/blender/nodes/shader/nodes/node_shader_curves.cc +++ b/source/blender/nodes/shader/nodes/node_shader_curves.cc @@ -104,17 +104,65 @@ static int gpu_shader_curve_vec(GPUMaterial *mat, GPU_uniform(ext_xyz[2])); } +class CurveVecFunction : public blender::fn::MultiFunction { + private: + const CurveMapping &cumap_; + + public: + CurveVecFunction(const CurveMapping &cumap) : cumap_(cumap) + { + static blender::fn::MFSignature signature = create_signature(); + this->set_signature(&signature); + } + + static blender::fn::MFSignature create_signature() + { + blender::fn::MFSignatureBuilder signature{"Curve Vec"}; + signature.single_input<float>("Fac"); + signature.single_input<blender::float3>("Vector"); + signature.single_output<blender::float3>("Vector"); + return signature.build(); + } + + void call(blender::IndexMask mask, + blender::fn::MFParams params, + blender::fn::MFContext UNUSED(context)) const override + { + const blender::VArray<float> &fac = params.readonly_single_input<float>(0, "Fac"); + const blender::VArray<blender::float3> &vec_in = params.readonly_single_input<blender::float3>( + 1, "Vector"); + blender::MutableSpan<blender::float3> vec_out = + params.uninitialized_single_output<blender::float3>(2, "Vector"); + + for (int64_t i : mask) { + BKE_curvemapping_evaluate3F(&cumap_, vec_out[i], vec_in[i]); + if (fac[i] != 1.0f) { + interp_v3_v3v3(vec_out[i], vec_in[i], vec_out[i], fac[i]); + } + } + } +}; + +static void sh_node_curve_vec_expand_in_mf_network(blender::nodes::NodeMFNetworkBuilder &builder) +{ + bNode &bnode = builder.bnode(); + CurveMapping *cumap = (CurveMapping *)bnode.storage; + BKE_curvemapping_init(cumap); + builder.construct_and_set_matching_fn<CurveVecFunction>(*cumap); +} + void register_node_type_sh_curve_vec(void) { static bNodeType ntype; - sh_node_type_base(&ntype, SH_NODE_CURVE_VEC, "Vector Curves", NODE_CLASS_OP_VECTOR, 0); + sh_fn_node_type_base(&ntype, SH_NODE_CURVE_VEC, "Vector Curves", NODE_CLASS_OP_VECTOR, 0); node_type_socket_templates(&ntype, sh_node_curve_vec_in, sh_node_curve_vec_out); node_type_init(&ntype, node_shader_init_curve_vec); node_type_size_preset(&ntype, NODE_SIZE_LARGE); node_type_storage(&ntype, "CurveMapping", node_free_curves, node_copy_curves); node_type_exec(&ntype, node_initexec_curves, nullptr, node_shader_exec_curve_vec); node_type_gpu(&ntype, gpu_shader_curve_vec); + ntype.expand_in_mf_network = sh_node_curve_vec_expand_in_mf_network; nodeRegisterType(&ntype); } @@ -230,17 +278,65 @@ static int gpu_shader_curve_rgb(GPUMaterial *mat, GPU_uniform(ext_rgba[3])); } +class CurveRGBFunction : public blender::fn::MultiFunction { + private: + const CurveMapping &cumap_; + + public: + CurveRGBFunction(const CurveMapping &cumap) : cumap_(cumap) + { + static blender::fn::MFSignature signature = create_signature(); + this->set_signature(&signature); + } + + static blender::fn::MFSignature create_signature() + { + blender::fn::MFSignatureBuilder signature{"Curve RGB"}; + signature.single_input<float>("Fac"); + signature.single_input<blender::Color4f>("Color"); + signature.single_output<blender::Color4f>("Color"); + return signature.build(); + } + + void call(blender::IndexMask mask, + blender::fn::MFParams params, + blender::fn::MFContext UNUSED(context)) const override + { + const blender::VArray<float> &fac = params.readonly_single_input<float>(0, "Fac"); + const blender::VArray<blender::Color4f> &col_in = + params.readonly_single_input<blender::Color4f>(1, "Color"); + blender::MutableSpan<blender::Color4f> col_out = + params.uninitialized_single_output<blender::Color4f>(2, "Color"); + + for (int64_t i : mask) { + BKE_curvemapping_evaluateRGBF(&cumap_, col_out[i], col_in[i]); + if (fac[i] != 1.0f) { + interp_v3_v3v3(col_out[i], col_in[i], col_out[i], fac[i]); + } + } + } +}; + +static void sh_node_curve_rgb_expand_in_mf_network(blender::nodes::NodeMFNetworkBuilder &builder) +{ + bNode &bnode = builder.bnode(); + CurveMapping *cumap = (CurveMapping *)bnode.storage; + BKE_curvemapping_init(cumap); + builder.construct_and_set_matching_fn<CurveRGBFunction>(*cumap); +} + void register_node_type_sh_curve_rgb(void) { static bNodeType ntype; - sh_node_type_base(&ntype, SH_NODE_CURVE_RGB, "RGB Curves", NODE_CLASS_OP_COLOR, 0); + sh_fn_node_type_base(&ntype, SH_NODE_CURVE_RGB, "RGB Curves", NODE_CLASS_OP_COLOR, 0); node_type_socket_templates(&ntype, sh_node_curve_rgb_in, sh_node_curve_rgb_out); node_type_init(&ntype, node_shader_init_curve_rgb); node_type_size_preset(&ntype, NODE_SIZE_LARGE); node_type_storage(&ntype, "CurveMapping", node_free_curves, node_copy_curves); node_type_exec(&ntype, node_initexec_curves, nullptr, node_shader_exec_curve_rgb); node_type_gpu(&ntype, gpu_shader_curve_rgb); + ntype.expand_in_mf_network = sh_node_curve_rgb_expand_in_mf_network; nodeRegisterType(&ntype); } |