Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Jolly <charlie>2021-10-19 19:28:55 +0300
committerCharlie Jolly <mistajolly@gmail.com>2021-10-19 19:30:24 +0300
commit67dbb42236da7aa32a420707213b3fa69374a930 (patch)
tree677e4d1270faab87938e695e10f8c3f3a9e4f926
parent56bf34aa174f99eb26a678a6d1bd53e96c94ed00 (diff)
Geometry Nodes: Add Wave texture node
Port shader wave texture node Reviewed By: JacquesLucke Differential Revision: https://developer.blender.org/D12733
-rw-r--r--release/scripts/startup/nodeitems_builtins.py1
-rw-r--r--source/blender/blenlib/BLI_float3.hh5
-rw-r--r--source/blender/nodes/shader/nodes/node_shader_tex_wave.cc143
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<float3>("Vector");
+ signature.single_input<float>("Scale");
+ signature.single_input<float>("Distortion");
+ signature.single_input<float>("Detail");
+ signature.single_input<float>("Detail Scale");
+ signature.single_input<float>("Detail Roughness");
+ signature.single_input<float>("Phase Offset");
+ signature.single_output<ColorGeometry4f>("Color");
+ signature.single_output<float>("Fac");
+ return signature.build();
+ }
+
+ void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
+ {
+ const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
+ const VArray<float> &scale = params.readonly_single_input<float>(1, "Scale");
+ const VArray<float> &distortion = params.readonly_single_input<float>(2, "Distortion");
+ const VArray<float> &detail = params.readonly_single_input<float>(3, "Detail");
+ const VArray<float> &dscale = params.readonly_single_input<float>(4, "Detail Scale");
+ const VArray<float> &droughness = params.readonly_single_input<float>(5, "Detail Roughness");
+ const VArray<float> &phase = params.readonly_single_input<float>(6, "Phase Offset");
+
+ MutableSpan<ColorGeometry4f> r_color =
+ params.uninitialized_single_output_if_required<ColorGeometry4f>(7, "Color");
+ MutableSpan<float> r_fac = params.uninitialized_single_output<float>(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<WaveFunction>(
+ 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);
}