From 67dbb42236da7aa32a420707213b3fa69374a930 Mon Sep 17 00:00:00 2001 From: Charlie Jolly Date: Tue, 19 Oct 2021 17:28:55 +0100 Subject: Geometry Nodes: Add Wave texture node Port shader wave texture node Reviewed By: JacquesLucke Differential Revision: https://developer.blender.org/D12733 --- release/scripts/startup/nodeitems_builtins.py | 1 + source/blender/blenlib/BLI_float3.hh | 5 + .../nodes/shader/nodes/node_shader_tex_wave.cc | 143 ++++++++++++++++++++- 3 files changed, 147 insertions(+), 2 deletions(-) diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py index cfe685ef403..215bd65cd1a 100644 --- a/release/scripts/startup/nodeitems_builtins.py +++ b/release/scripts/startup/nodeitems_builtins.py @@ -727,6 +727,7 @@ geometry_node_categories = [ NodeItem("ShaderNodeTexMusgrave"), NodeItem("ShaderNodeTexNoise"), NodeItem("ShaderNodeTexVoronoi"), + NodeItem("ShaderNodeTexWave"), NodeItem("ShaderNodeTexWhiteNoise"), ]), GeometryNodeCategory("GEO_UTILITIES", "Utilities", items=[ diff --git a/source/blender/blenlib/BLI_float3.hh b/source/blender/blenlib/BLI_float3.hh index 8263ef72584..6ee0c4b973b 100644 --- a/source/blender/blenlib/BLI_float3.hh +++ b/source/blender/blenlib/BLI_float3.hh @@ -62,6 +62,11 @@ struct float3 { return {a.x + b.x, a.y + b.y, a.z + b.z}; } + friend float3 operator+(const float3 &a, const float &b) + { + return {a.x + b, a.y + b, a.z + b}; + } + float3 &operator+=(const float3 &b) { this->x += b.x; diff --git a/source/blender/nodes/shader/nodes/node_shader_tex_wave.cc b/source/blender/nodes/shader/nodes/node_shader_tex_wave.cc index 79fec5f6e0e..25e65e3d3f0 100644 --- a/source/blender/nodes/shader/nodes/node_shader_tex_wave.cc +++ b/source/blender/nodes/shader/nodes/node_shader_tex_wave.cc @@ -19,6 +19,8 @@ #include "../node_shader_util.h" +#include "BLI_noise.hh" + namespace blender::nodes { static void sh_node_tex_wave_declare(NodeDeclarationBuilder &b) @@ -79,17 +81,154 @@ static int node_shader_gpu_tex_wave(GPUMaterial *mat, GPU_constant(&wave_profile)); } -/* node type definition */ +namespace blender::nodes { + +class WaveFunction : public fn::MultiFunction { + private: + int wave_type_; + int bands_direction_; + int rings_direction_; + int wave_profile_; + + public: + WaveFunction(int wave_type, int bands_direction, int rings_direction, int wave_profile) + : wave_type_(wave_type), + bands_direction_(bands_direction), + rings_direction_(rings_direction), + wave_profile_(wave_profile) + { + static fn::MFSignature signature = create_signature(); + this->set_signature(&signature); + } + + static fn::MFSignature create_signature() + { + fn::MFSignatureBuilder signature{"MagicFunction"}; + signature.single_input("Vector"); + signature.single_input("Scale"); + signature.single_input("Distortion"); + signature.single_input("Detail"); + signature.single_input("Detail Scale"); + signature.single_input("Detail Roughness"); + signature.single_input("Phase Offset"); + signature.single_output("Color"); + signature.single_output("Fac"); + return signature.build(); + } + + void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override + { + const VArray &vector = params.readonly_single_input(0, "Vector"); + const VArray &scale = params.readonly_single_input(1, "Scale"); + const VArray &distortion = params.readonly_single_input(2, "Distortion"); + const VArray &detail = params.readonly_single_input(3, "Detail"); + const VArray &dscale = params.readonly_single_input(4, "Detail Scale"); + const VArray &droughness = params.readonly_single_input(5, "Detail Roughness"); + const VArray &phase = params.readonly_single_input(6, "Phase Offset"); + + MutableSpan r_color = + params.uninitialized_single_output_if_required(7, "Color"); + MutableSpan r_fac = params.uninitialized_single_output(8, "Fac"); + + for (int64_t i : mask) { + + float3 p = vector[i] * scale[i]; + /* Prevent precision issues on unit coordinates. */ + p = (p + 0.000001f) * 0.999999f; + + float n = 0.0f; + float val = 0.0f; + + switch (wave_type_) { + case SHD_WAVE_BANDS: + switch (bands_direction_) { + case SHD_WAVE_BANDS_DIRECTION_X: + n = p.x * 20.0f; + break; + case SHD_WAVE_BANDS_DIRECTION_Y: + n = p.y * 20.0f; + break; + case SHD_WAVE_BANDS_DIRECTION_Z: + n = p.z * 20.0f; + break; + case SHD_WAVE_BANDS_DIRECTION_DIAGONAL: + n = (p.x + p.y + p.z) * 10.0f; + break; + } + break; + case SHD_WAVE_RINGS: + float3 rp = p; + switch (rings_direction_) { + case SHD_WAVE_RINGS_DIRECTION_X: + rp *= float3(0.0f, 1.0f, 1.0f); + break; + case SHD_WAVE_RINGS_DIRECTION_Y: + rp *= float3(1.0f, 0.0f, 1.0f); + break; + case SHD_WAVE_RINGS_DIRECTION_Z: + rp *= float3(1.0f, 1.0f, 0.0f); + break; + case SHD_WAVE_RINGS_DIRECTION_SPHERICAL: + /* Ignore. */ + break; + } + n = len_v3(rp) * 20.0f; + break; + } + + n += phase[i]; + + if (distortion[i] != 0.0f) { + n += distortion[i] * + (noise::perlin_fractal(p * dscale[i], detail[i], droughness[i]) * 2.0f - 1.0f); + } + + switch (wave_profile_) { + case SHD_WAVE_PROFILE_SIN: + val = 0.5f + 0.5f * sinf(n - M_PI_2); + break; + case SHD_WAVE_PROFILE_SAW: + n /= M_PI * 2.0f; + val = n - floorf(n); + break; + case SHD_WAVE_PROFILE_TRI: + n /= M_PI * 2.0f; + val = fabsf(n - floorf(n + 0.5f)) * 2.0f; + break; + } + + r_fac[i] = val; + } + if (!r_color.is_empty()) { + for (int64_t i : mask) { + r_color[i] = ColorGeometry4f(r_fac[i], r_fac[i], r_fac[i], 1.0f); + } + } + } +}; + +static void sh_node_wave_tex_build_multi_function( + blender::nodes::NodeMultiFunctionBuilder &builder) +{ + bNode &node = builder.node(); + NodeTexWave *tex = (NodeTexWave *)node.storage; + builder.construct_and_set_matching_fn( + tex->wave_type, tex->bands_direction, tex->rings_direction, tex->wave_profile); +} + +} // namespace blender::nodes + void register_node_type_sh_tex_wave(void) { static bNodeType ntype; - sh_node_type_base(&ntype, SH_NODE_TEX_WAVE, "Wave Texture", NODE_CLASS_TEXTURE, 0); + sh_fn_node_type_base(&ntype, SH_NODE_TEX_WAVE, "Wave Texture", NODE_CLASS_TEXTURE, 0); ntype.declare = blender::nodes::sh_node_tex_wave_declare; node_type_size_preset(&ntype, NODE_SIZE_MIDDLE); node_type_init(&ntype, node_shader_init_tex_wave); node_type_storage(&ntype, "NodeTexWave", node_free_standard_storage, node_copy_standard_storage); node_type_gpu(&ntype, node_shader_gpu_tex_wave); + ntype.build_multi_function = blender::nodes::sh_node_wave_tex_build_multi_function; nodeRegisterType(&ntype); } -- cgit v1.2.3