This path merges the Musgrave and Noise Texture nodes into a single combined Noise Texture node. The reasoning is that both nodes intrinsically do the same thing, which is the layering of Perlin noise derivatives to produce fractal noise. So the patch de-duplicates code and unifies the use of fractal noise for the end use. Since the Noise node had a Distortion input and a Color output, while the Musgrave node did not, those are now available to the Musgrave types as new functionalities. The Dimension input of the Musgrave node is analogous to the Roughness input of the Noise node, so both inputs were unified to follow the same behavior of the Roughness input, which is arguable more intuitive to control. Similarly, the Detail input was slightly different across both nodes, since the Noise node evaluated one extra layer of noise. This was also unified to follow the behavior of the Noise node. The patch, coincidentally fixes an unreported bug causing repeated output for certain noise types and another floating precision bug #112180. The versioning code implemented with this patch ensures backward compatibility for both the Musgrave and Noise Texture nodes. When opening older Blender files in Blender 4.1 the output of both nodes are guaranteed to always be exactly identical to that of Blender files created before the nodes were merged in all cases. Forward compatibility with Blender 4.0 is implemented by #114236. Forward compatibility with Blender 3.6 LTS is implemented by #115015. Pull Request: #111187
221 lines
7.9 KiB
C++
221 lines
7.9 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#pragma once
|
|
|
|
#include "BLI_math_vector_types.hh"
|
|
|
|
namespace blender::noise {
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Hash Functions
|
|
*
|
|
* Create a randomized hash from the given inputs. Contrary to hash functions in `BLI_hash.hh`
|
|
* these functions produce better randomness but are more expensive to compute.
|
|
* \{ */
|
|
|
|
/* Hash integers to `uint32_t`. */
|
|
|
|
uint32_t hash(uint32_t kx);
|
|
uint32_t hash(uint32_t kx, uint32_t ky);
|
|
uint32_t hash(uint32_t kx, uint32_t ky, uint32_t kz);
|
|
uint32_t hash(uint32_t kx, uint32_t ky, uint32_t kz, uint32_t kw);
|
|
|
|
/* Hash floats to `uint32_t`. */
|
|
|
|
uint32_t hash_float(float kx);
|
|
uint32_t hash_float(float2 k);
|
|
uint32_t hash_float(float3 k);
|
|
uint32_t hash_float(float4 k);
|
|
|
|
/* Hash integers to `float` between 0 and 1. */
|
|
|
|
float hash_to_float(uint32_t kx);
|
|
float hash_to_float(uint32_t kx, uint32_t ky);
|
|
float hash_to_float(uint32_t kx, uint32_t ky, uint32_t kz);
|
|
float hash_to_float(uint32_t kx, uint32_t ky, uint32_t kz, uint32_t kw);
|
|
|
|
/* Hash floats to `float` between 0 and 1. */
|
|
|
|
float hash_float_to_float(float k);
|
|
float hash_float_to_float(float2 k);
|
|
float hash_float_to_float(float3 k);
|
|
float hash_float_to_float(float4 k);
|
|
|
|
float2 hash_float_to_float2(float2 k);
|
|
|
|
float3 hash_float_to_float3(float k);
|
|
float3 hash_float_to_float3(float2 k);
|
|
float3 hash_float_to_float3(float3 k);
|
|
float3 hash_float_to_float3(float4 k);
|
|
|
|
float4 hash_float_to_float4(float4 k);
|
|
|
|
/** \} */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Perlin Noise
|
|
* \{ */
|
|
|
|
/* Perlin noise in the range [-1, 1]. */
|
|
|
|
float perlin_signed(float position);
|
|
float perlin_signed(float2 position);
|
|
float perlin_signed(float3 position);
|
|
float perlin_signed(float4 position);
|
|
|
|
/* Perlin noise in the range [0, 1]. */
|
|
|
|
float perlin(float position);
|
|
float perlin(float2 position);
|
|
float perlin(float3 position);
|
|
float perlin(float4 position);
|
|
|
|
/* Perlin fractal Brownian motion. */
|
|
|
|
template<typename T>
|
|
float perlin_fbm(T p, float detail, float roughness, float lacunarity, bool normalize);
|
|
|
|
/* Distorted fractal perlin noise. */
|
|
|
|
template<typename T>
|
|
float perlin_fractal_distorted(T position,
|
|
float detail,
|
|
float roughness,
|
|
float lacunarity,
|
|
float offset,
|
|
float gain,
|
|
float distortion,
|
|
int type,
|
|
bool normalize);
|
|
|
|
/* Distorted fractal perlin noise that outputs a float3. */
|
|
|
|
float3 perlin_float3_fractal_distorted(float position,
|
|
float detail,
|
|
float roughness,
|
|
float lacunarity,
|
|
float offset,
|
|
float gain,
|
|
float distortion,
|
|
int type,
|
|
bool normalize);
|
|
float3 perlin_float3_fractal_distorted(float2 position,
|
|
float detail,
|
|
float roughness,
|
|
float lacunarity,
|
|
float offset,
|
|
float gain,
|
|
float distortion,
|
|
int type,
|
|
bool normalize);
|
|
float3 perlin_float3_fractal_distorted(float3 position,
|
|
float detail,
|
|
float roughness,
|
|
float lacunarity,
|
|
float offset,
|
|
float gain,
|
|
float distortion,
|
|
int type,
|
|
bool normalize);
|
|
float3 perlin_float3_fractal_distorted(float4 position,
|
|
float detail,
|
|
float roughness,
|
|
float lacunarity,
|
|
float offset,
|
|
float gain,
|
|
float distortion,
|
|
int type,
|
|
bool normalize);
|
|
|
|
/** \} */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Voronoi Noise
|
|
* \{ */
|
|
|
|
struct VoronoiParams {
|
|
float scale;
|
|
float detail;
|
|
float roughness;
|
|
float lacunarity;
|
|
float smoothness;
|
|
float exponent;
|
|
float randomness;
|
|
float max_distance;
|
|
bool normalize;
|
|
int feature;
|
|
int metric;
|
|
};
|
|
|
|
struct VoronoiOutput {
|
|
float distance = 0.0f;
|
|
float3 color{0.0f, 0.0f, 0.0f};
|
|
float4 position{0.0f, 0.0f, 0.0f, 0.0f};
|
|
};
|
|
|
|
/* ***** Distances ***** */
|
|
|
|
float voronoi_distance(const float a, const float b);
|
|
float voronoi_distance(const float2 a, const float2 b, const VoronoiParams ¶ms);
|
|
float voronoi_distance(const float3 a, const float3 b, const VoronoiParams ¶ms);
|
|
float voronoi_distance(const float4 a, const float4 b, const VoronoiParams ¶ms);
|
|
|
|
/* **** 1D Voronoi **** */
|
|
|
|
float4 voronoi_position(const float coord);
|
|
VoronoiOutput voronoi_f1(const VoronoiParams ¶ms, const float coord);
|
|
VoronoiOutput voronoi_smooth_f1(const VoronoiParams ¶ms,
|
|
const float coord,
|
|
const bool calc_color);
|
|
VoronoiOutput voronoi_f2(const VoronoiParams ¶ms, const float coord);
|
|
float voronoi_distance_to_edge(const VoronoiParams ¶ms, const float coord);
|
|
float voronoi_n_sphere_radius(const VoronoiParams ¶ms, const float coord);
|
|
|
|
/* **** 2D Voronoi **** */
|
|
|
|
float4 voronoi_position(const float2 coord);
|
|
VoronoiOutput voronoi_f1(const VoronoiParams ¶ms, const float2 coord);
|
|
VoronoiOutput voronoi_smooth_f1(const VoronoiParams ¶ms,
|
|
const float2 coord,
|
|
const bool calc_color);
|
|
VoronoiOutput voronoi_f2(const VoronoiParams ¶ms, const float2 coord);
|
|
float voronoi_distance_to_edge(const VoronoiParams ¶ms, const float2 coord);
|
|
float voronoi_n_sphere_radius(const VoronoiParams ¶ms, const float2 coord);
|
|
|
|
/* **** 3D Voronoi **** */
|
|
|
|
float4 voronoi_position(const float3 coord);
|
|
VoronoiOutput voronoi_f1(const VoronoiParams ¶ms, const float3 coord);
|
|
VoronoiOutput voronoi_smooth_f1(const VoronoiParams ¶ms,
|
|
const float3 coord,
|
|
const bool calc_color);
|
|
VoronoiOutput voronoi_f2(const VoronoiParams ¶ms, const float3 coord);
|
|
float voronoi_distance_to_edge(const VoronoiParams ¶ms, const float3 coord);
|
|
float voronoi_n_sphere_radius(const VoronoiParams ¶ms, const float3 coord);
|
|
|
|
/* **** 4D Voronoi **** */
|
|
|
|
float4 voronoi_position(const float4 coord);
|
|
VoronoiOutput voronoi_f1(const VoronoiParams ¶ms, const float4 coord);
|
|
VoronoiOutput voronoi_smooth_f1(const VoronoiParams ¶ms,
|
|
const float4 coord,
|
|
const bool calc_color);
|
|
VoronoiOutput voronoi_f2(const VoronoiParams ¶ms, const float4 coord);
|
|
float voronoi_distance_to_edge(const VoronoiParams ¶ms, const float4 coord);
|
|
float voronoi_n_sphere_radius(const VoronoiParams ¶ms, const float4 coord);
|
|
|
|
/* Fractal Voronoi Noise */
|
|
|
|
template<typename T>
|
|
VoronoiOutput fractal_voronoi_x_fx(const VoronoiParams ¶ms,
|
|
const T coord,
|
|
const bool calc_color);
|
|
template<typename T>
|
|
float fractal_voronoi_distance_to_edge(const VoronoiParams ¶ms, const T coord);
|
|
|
|
/** \} */
|
|
|
|
} // namespace blender::noise
|