/usr/share/k3d/shaders/k3d_noises.h is in k3d-data 0.8.0.3-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | /************************************************************************
* noises.h - various noise-based patterns
*
* Author: Larry Gritz (gritzl@acm.org), though they're obvious to any
* experience shader writer.
*
* Reference:
* _Advanced RenderMan: Creating CGI for Motion Picture_,
* by Anthony A. Apodaca and Larry Gritz, Morgan Kaufmann, 1999.
*
* $Revision: 1.7 $ $Date: 2006/03/17 17:31:39 $
*
************************************************************************/
#ifndef NOISES_H
#define NOISES_H 1
#ifndef FILTERWIDTH_H
#include "k3d_filterwidth.h" /* Needed for filterwidth and friends */
#endif
#ifndef PATTERNS_H
#include "k3d_patterns.h" /* Needed for filteredabs */
#endif
#ifndef snoise
/*
* Signed noise -- the original Perlin kind with range (-1,1) We prefer
* signed noise to regular noise mostly because its average is zero.
* We define three simple macros:
* snoise(p) - Perlin noise on either a 1-D (float) or 3-D (point) domain.
* snoisexy(x,y) - Perlin noise on a 2-D domain.
* vsnoise(p) - vector-valued Perlin noise on either 1-D or 3-D domain.
*/
#define snoise(p) (2 * (float noise(p)) - 1)
#endif
#define snoise2(x) (2.5*(noise(x)-0.5)) /* need to check this one */
#define snoisexy(x,y) (2 * (float noise(x,y)) - 1)
#define vsnoise(p) (2 * (vector noise(p)) - 1)
#define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
#define fnoise(p,width) (noise(p) * (1-smoothstep (0.2,0.75,width)))
#define adjustNoise(x, y, minVal, maxVal) snoisexy (x,y) * ((maxVal)-(minVal)+(minVal))
/* uniformly distributed noise
*
*/
#define udn(x,lo,hi) (smoothstep(.25, .75, noise(x)) * ((hi) - (lo)) + (lo))
#define udn2(x,y,lo,hi) (smoothstep(.25, .75, noise(x,y)) * ((hi)-(lo))+(lo))
/* If we know the filter size, we can crudely antialias snoise by fading
* to its average value at approximately the Nyquist limit.
*/
#define filteredsnoise(p,width) (snoise(p) * (1-smoothstep (0.2,0.75,width)))
#define filteredvsnoise(p,width) (vsnoise(p) * (1-smoothstep (0.2,0.75,width)))
/* fractional Brownian motion
* Inputs:
* p, filtwidth position and approximate inter-pixel spacing
* octaves max # of octaves to calculate
* lacunarity frequency spacing between successive octaves
* gain scaling factor between successive octaves
*/
float fBm (point p; float filtwidth;
uniform float octaves, lacunarity, gain)
{
uniform float amp = 1;
varying point pp = p;
varying float sum = 0, fw = filtwidth;
uniform float i;
for (i = 0; i < octaves; i += 1) {
#pragma nolint
sum += amp * filteredsnoise (pp, fw);
amp *= gain; pp *= lacunarity; fw *= lacunarity;
}
return sum;
}
/* Typical use of fBm: */
#define fBm_default(p) fBm (p, filterwidthp(p), 4, 2, 0.5)
/* A vector-valued antialiased fBm. */
vector
vfBm (point p; float filtwidth;
uniform float octaves, lacunarity, gain)
{
uniform float amp = 1;
varying point pp = p;
varying vector sum = 0;
varying float fw = filtwidth;
uniform float i;
for (i = 0; i < octaves; i += 1) {
#pragma nolint
sum += amp * filteredvsnoise (pp, fw);
amp *= gain; pp *= lacunarity; fw *= lacunarity;
}
return sum;
}
/* Typical use of vfBm: */
#define vfBm_default(p) vfBm (p, filterwidthp(p), 4, 2, 0.5)
/* The stuff that Ken Musgrave calls "VLNoise" */
#define VLNoise(Pt,scale) (snoise(vsnoise(Pt)*scale+Pt))
#define filteredVLNoise(Pt,fwidth,scale) \
(filteredsnoise(filteredvsnoise(Pt,fwidth)*scale+Pt,fwidth))
float
VLfBm (point p; float filtwidth;
uniform float octaves, lacunarity, gain, scale)
{
uniform float amp = 1;
varying point pp = p;
varying float sum = 0;
varying float fw = filtwidth;
uniform float i;
for (i = 0; i < octaves; i += 1) {
#pragma nolint
sum += amp * filteredVLNoise (pp, fw, scale);
amp *= gain; pp *= lacunarity; fw *= lacunarity;
}
return sum;
}
/* Typical use of vfBm: */
#define VLfBm_default(p) VLfBm (p, filterwidthp(p), 4, 2, 0.5, 1.0)
/* Antialiased turbulence. Watch out -- the abs() call introduces infinite
* frequency content, which makes our antialiasing efforts much trickier!
*/
float turbulence (point p; float filtwidth;
uniform float octaves, lacunarity, gain)
{
extern float du, dv; /* Needed for filterwidth macro */
uniform float amp = 1;
varying point pp = p;
varying float sum = 0, fw = filtwidth;
uniform float i;
for (i = 0; i < octaves; i += 1) {
#pragma nolint
float n = filteredsnoise (pp, fw);
sum += amp * filteredabs (n, fw);
amp *= gain; pp *= lacunarity; fw *= lacunarity;
}
return sum;
}
/* Typical use of turbulence: */
#define turbulence_default(p) turbulence (p, filterwidthp(p), 4, 2, 0.5)
/***************************************************************************
* Voronoi cell noise (a.k.a. Worley noise) functions
*
* These functions assume that space is filled with "features" (points
* of interest). There are interestingpatterns we can make by
* figuring out which feature we are closest to, or to what extent
* we're on the boundary between two features. Several varieties of
* these computations are below, categorized by the dimension of their
* domains, and the number of close features they are interested in.
*
* All these functions have similar inputs:
* P - position to test (for 3-D varieties; 2-D varieties use ss,tt)
* jitter - how much to jitter the cell center positions (1 is typical,
* smaller values make a more regular pattern, larger values
* make a more jagged pattern; use jitter >1 at your risk!).
* And outputs:
* f_n - distance to the nth nearest feature (f1 is closest, f2 is
* the distance to the 2nd closest, etc.)
* pos_n - the position of the nth nearest feature. For 2-D varieties,
* these are instead spos_n and tpos_n.
***************************************************************************/
/* Voronoi cell noise (a.k.a. Worley noise) -- 3-D, 1-feature version. */
void
voronoi_f1_3d (point P;
float jitter;
output float f1;
output point pos1;
)
{
point thiscell = point (floor(xcomp(P))+0.5, floor(ycomp(P))+0.5,
floor(zcomp(P))+0.5);
f1 = 1000;
uniform float i, j, k;
for (i = -1; i <= 1; i += 1) {
for (j = -1; j <= 1; j += 1) {
for (k = -1; k <= 1; k += 1) {
point testcell = thiscell + vector(i,j,k);
point pos = testcell +
jitter * (vector cellnoise (testcell) - 0.5);
vector offset = pos - P;
float dist = offset . offset; /* actually dist^2 */
if (dist < f1) {
f1 = dist; pos1 = pos;
}
}
}
}
f1 = sqrt(f1);
}
/* Voronoi cell noise (a.k.a. Worley noise) -- 3-D, 2-feature version. */
void
voronoi_f1f2_3d (point P;
float jitter;
output float f1; output point pos1;
output float f2; output point pos2;
)
{
point thiscell = point (floor(xcomp(P))+0.5, floor(ycomp(P))+0.5,
floor(zcomp(P))+0.5);
f1 = f2 = 1000;
uniform float i, j, k;
for (i = -1; i <= 1; i += 1) {
for (j = -1; j <= 1; j += 1) {
for (k = -1; k <= 1; k += 1) {
point testcell = thiscell + vector(i,j,k);
point pos = testcell +
jitter * (vector cellnoise (testcell) - 0.5);
vector offset = pos - P;
float dist = offset . offset; /* actually dist^2 */
if (dist < f1) {
f2 = f1; pos2 = pos1;
f1 = dist; pos1 = pos;
} else if (dist < f2) {
f2 = dist; pos2 = pos;
}
}
}
}
f1 = sqrt(f1); f2 = sqrt(f2);
}
/* Voronoi cell noise (a.k.a. Worley noise) -- 2-D, 1-feature version. */
void
voronoi_f1_2d (float ss, tt;
float jitter;
output float f1;
output float spos1, tpos1;
)
{
float sthiscell = floor(ss)+0.5, tthiscell = floor(tt)+0.5;
f1 = 1000;
uniform float i, j;
for (i = -1; i <= 1; i += 1) {
float stestcell = sthiscell + i;
for (j = -1; j <= 1; j += 1) {
float ttestcell = tthiscell + j;
float spos = stestcell +
jitter * (float cellnoise(stestcell, ttestcell) - 0.5);
float tpos = ttestcell +
jitter * (float cellnoise(stestcell+23, ttestcell-87) - 0.5);
float soffset = spos - ss;
float toffset = tpos - tt;
float dist = soffset*soffset + toffset*toffset;
if (dist < f1) {
f1 = dist;
spos1 = spos; tpos1 = tpos;
}
}
}
f1 = sqrt(f1);
}
/* Voronoi cell noise (a.k.a. Worley noise) -- 2-D, 2-feature version. */
void
voronoi_f1f2_2d (float ss, tt;
float jitter;
output float f1;
output float spos1, tpos1;
output float f2;
output float spos2, tpos2;
)
{
float sthiscell = floor(ss)+0.5, tthiscell = floor(tt)+0.5;
f1 = f2 = 1000;
uniform float i, j;
for (i = -1; i <= 1; i += 1) {
float stestcell = sthiscell + i;
for (j = -1; j <= 1; j += 1) {
float ttestcell = tthiscell + j;
float spos = stestcell +
jitter * (cellnoise(stestcell, ttestcell) - 0.5);
float tpos = ttestcell +
jitter * (cellnoise(stestcell+23, ttestcell-87) - 0.5);
float soffset = spos - ss;
float toffset = tpos - tt;
float dist = soffset*soffset + toffset*toffset;
if (dist < f1) {
f2 = f1; spos2 = spos1; tpos2 = tpos1;
f1 = dist; spos1 = spos; tpos1 = tpos;
} else if (dist < f2) {
f2 = dist;
spos2 = spos; tpos2 = tpos;
}
}
}
f1 = sqrt(f1); f2 = sqrt(f2);
}
#endif /* NOISES_H */
|