Geometry Nodes: Add Wave texture node

Port shader wave texture node

Reviewed By: JacquesLucke

Differential Revision: https://developer.blender.org/D12733
This commit is contained in:
Charlie Jolly 2021-10-19 17:28:55 +01:00 committed by Charlie Jolly
parent 56bf34aa17
commit 67dbb42236
3 changed files with 147 additions and 2 deletions

View File

@ -727,6 +727,7 @@ geometry_node_categories = [
NodeItem("ShaderNodeTexMusgrave"),
NodeItem("ShaderNodeTexNoise"),
NodeItem("ShaderNodeTexVoronoi"),
NodeItem("ShaderNodeTexWave"),
NodeItem("ShaderNodeTexWhiteNoise"),
]),
GeometryNodeCategory("GEO_UTILITIES", "Utilities", items=[

View File

@ -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;

View File

@ -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);
}