Blender V4.3
node_fractal_voronoi.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "node_voronoi.h"
6#include "stdcycles.h"
7#include "vector2.h"
8#include "vector4.h"
9
10#define vector3 point
11
12/* The fractalization logic is the same as for fBM Noise, except that some additions are replaced
13 * by lerps. */
14#define FRACTAL_VORONOI_X_FX(T) \
15 VoronoiOutput fractal_voronoi_x_fx(VoronoiParams params, T coord) \
16 { \
17 float amplitude = 1.0; \
18 float max_amplitude = 0.0; \
19 float scale = 1.0; \
20\
21 VoronoiOutput Output; \
22 Output.Distance = 0.0; \
23 Output.Color = color(0.0, 0.0, 0.0); \
24 Output.Position = vector4(0.0, 0.0, 0.0, 0.0); \
25 int zero_input = params.detail == 0.0 || params.roughness == 0.0; \
26\
27 for (int i = 0; i <= ceil(params.detail); ++i) { \
28 VoronoiOutput octave; \
29 if (params.feature == "f2") { \
30 octave = voronoi_f2(params, coord * scale); \
31 } \
32 else if (params.feature == "smooth_f1" && params.smoothness != 0.0) { \
33 octave = voronoi_smooth_f1(params, coord * scale); \
34 } \
35 else { \
36 octave = voronoi_f1(params, coord * scale); \
37 } \
38\
39 if (zero_input) { \
40 max_amplitude = 1.0; \
41 Output = octave; \
42 break; \
43 } \
44 else if (i <= params.detail) { \
45 max_amplitude += amplitude; \
46 Output.Distance += octave.Distance * amplitude; \
47 Output.Color += octave.Color * amplitude; \
48 Output.Position = mix(Output.Position, octave.Position / scale, amplitude); \
49 scale *= params.lacunarity; \
50 amplitude *= params.roughness; \
51 } \
52 else { \
53 float remainder = params.detail - floor(params.detail); \
54 if (remainder != 0.0) { \
55 max_amplitude = mix(max_amplitude, max_amplitude + amplitude, remainder); \
56 Output.Distance = mix( \
57 Output.Distance, Output.Distance + octave.Distance * amplitude, remainder); \
58 Output.Color = mix(Output.Color, Output.Color + octave.Color * amplitude, remainder); \
59 Output.Position = mix(Output.Position, \
60 mix(Output.Position, octave.Position / scale, amplitude), \
61 remainder); \
62 } \
63 } \
64 } \
65\
66 if (params.normalize) { \
67 Output.Distance /= max_amplitude * params.max_distance; \
68 Output.Color /= max_amplitude; \
69 } \
70\
71 Output.Position = safe_divide(Output.Position, params.scale); \
72\
73 return Output; \
74 }
75
76/* The fractalization logic is the same as for fBM Noise, except that some additions are replaced
77 * by lerps. */
78#define FRACTAL_VORONOI_DISTANCE_TO_EDGE_FUNCTION(T) \
79 float fractal_voronoi_distance_to_edge(VoronoiParams params, T coord) \
80 { \
81 float amplitude = 1.0; \
82 float max_amplitude = params.max_distance; \
83 float scale = 1.0; \
84 float distance = 8.0; \
85\
86 int zero_input = params.detail == 0.0 || params.roughness == 0.0; \
87\
88 for (int i = 0; i <= ceil(params.detail); ++i) { \
89 float octave_distance = voronoi_distance_to_edge(params, coord * scale); \
90\
91 if (zero_input) { \
92 distance = octave_distance; \
93 break; \
94 } \
95 else if (i <= params.detail) { \
96 max_amplitude = mix(max_amplitude, params.max_distance / scale, amplitude); \
97 distance = mix(distance, min(distance, octave_distance / scale), amplitude); \
98 scale *= params.lacunarity; \
99 amplitude *= params.roughness; \
100 } \
101 else { \
102 float remainder = params.detail - floor(params.detail); \
103 if (remainder != 0.0) { \
104 float lerp_amplitude = mix(max_amplitude, params.max_distance / scale, amplitude); \
105 max_amplitude = mix(max_amplitude, lerp_amplitude, remainder); \
106 float lerp_distance = mix(distance, min(distance, octave_distance / scale), amplitude); \
107 distance = mix(distance, min(distance, lerp_distance), remainder); \
108 } \
109 } \
110 } \
111\
112 if (params.normalize) { \
113 distance /= max_amplitude; \
114 } \
115\
116 return distance; \
117 }
118
119/* **** 1D Fractal Voronoi **** */
120
122
124
125/* **** 2D Fractal Voronoi **** */
126
128
130
131/* **** 3D Fractal Voronoi **** */
132
134
136
137/* **** 4D Fractal Voronoi **** */
138
140
#define FRACTAL_VORONOI_X_FX(T)
#define vector3
#define FRACTAL_VORONOI_DISTANCE_TO_EDGE_FUNCTION(T)