blender/intern/cycles/kernel/osl/nodes/node_musgrave_texture.osl
Ton Roosendaal da376e0237 Cycles render engine, initial commit. This is the engine itself, blender modifications and build instructions will follow later.
Cycles uses code from some great open source projects, many thanks them:

* BVH building and traversal code from NVidia's "Understanding the Efficiency of Ray Traversal on GPUs":
http://code.google.com/p/understanding-the-efficiency-of-ray-traversal-on-gpus/
* Open Shading Language for a large part of the shading system:
http://code.google.com/p/openshadinglanguage/
* Blender for procedural textures and a few other nodes.
* Approximate Catmull Clark subdivision from NVidia Mesh tools:
http://code.google.com/p/nvidia-mesh-tools/
* Sobol direction vectors from:
http://web.maths.unsw.edu.au/~fkuo/sobol/
* Film response functions from:
http://www.cs.columbia.edu/CAVE/software/softlib/dorf.php
2011-04-27 11:58:34 +00:00

219 lines
5.7 KiB
Plaintext

/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "stdosl.h"
#include "node_texture.h"
/* Musgrave fBm
*
* H: fractal increment parameter
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
*
* from "Texturing and Modelling: A procedural approach"
*/
float noise_musgrave_fBm(point p, string basis, float H, float lacunarity, float octaves)
{
float rmd;
float value = 0.0;
float pwr = 1.0;
float pwHL = pow(lacunarity, -H);
int i;
for(i = 0; i < (int)octaves; i++) {
value += noise_basis(p, basis) * pwr;
pwr *= pwHL;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0)
value += rmd * noise_basis(p, basis) * pwr;
return value;
}
/* Musgrave Multifractal
*
* H: highest fractal dimension
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
*/
float noise_musgrave_multi_fractal(point p, string basis, float H, float lacunarity, float octaves)
{
float rmd;
float value = 1.0;
float pwr = 1.0;
float pwHL = pow(lacunarity, -H);
int i;
for(i = 0; i < (int)octaves; i++) {
value *= (pwr * noise_basis(p, basis) + 1.0);
pwr *= pwHL;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0)
value *= (rmd * pwr * noise_basis(p, basis) + 1.0); /* correct? */
return value;
}
/* Musgrave Heterogeneous Terrain
*
* H: fractal dimension of the roughest area
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
* offset: raises the terrain from `sea level'
*/
float noise_musgrave_hetero_terrain(point p, string basis, float H, float lacunarity, float octaves, float offset)
{
float value, increment, rmd;
float pwHL = pow(lacunarity, -H);
float pwr = pwHL;
int i;
/* first unscaled octave of function; later octaves are scaled */
value = offset + noise_basis(p, basis);
p *= lacunarity;
for(i = 1; i < (int)octaves; i++) {
increment = (noise_basis(p, basis) + offset) * pwr * value;
value += increment;
pwr *= pwHL;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0) {
increment = (noise_basis(p, basis) + offset) * pwr * value;
value += rmd * increment;
}
return value;
}
/* Hybrid Additive/Multiplicative Multifractal Terrain
*
* H: fractal dimension of the roughest area
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
* offset: raises the terrain from `sea level'
*/
float noise_musgrave_hybrid_multi_fractal(point p, string basis, float H, float lacunarity, float octaves, float offset, float gain)
{
float result, signal, weight, rmd;
float pwHL = pow(lacunarity, -H);
float pwr = pwHL;
int i;
result = noise_basis(p, basis) + offset;
weight = gain * result;
p *= lacunarity;
for(i = 1; (weight > 0.001) && (i < (int)octaves); i++) {
if(weight > 1.0)
weight = 1.0;
signal = (noise_basis(p, basis) + offset) * pwr;
pwr *= pwHL;
result += weight * signal;
weight *= gain * signal;
p *= lacunarity;
}
rmd = octaves - floor(octaves);
if(rmd != 0.0)
result += rmd * ((noise_basis(p, basis) + offset) * pwr);
return result;
}
/* Ridged Multifractal Terrain
*
* H: fractal dimension of the roughest area
* lacunarity: gap between successive frequencies
* octaves: number of frequencies in the fBm
* offset: raises the terrain from `sea level'
*/
float noise_musgrave_ridged_multi_fractal(point p, string basis, float H, float lacunarity, float octaves, float offset, float gain)
{
float result, signal, weight;
float pwHL = pow(lacunarity, -H);
float pwr = pwHL;
int i;
signal = offset - fabs(noise_basis(p, basis));
signal *= signal;
result = signal;
weight = 1.0;
for(i = 1; i < (int)octaves; i++) {
p *= lacunarity;
weight = clamp(signal * gain, 0.0, 1.0);
signal = offset - fabs(noise_basis(p, basis));
signal *= signal;
signal *= weight;
result += signal * pwr;
pwr *= pwHL;
}
return result;
}
/* Shader */
shader node_musgrave_texture(
string Type = "fBM",
string Basis = "Perlin",
float Dimension = 2.0,
float Lacunarity = 1.0,
float Octaves = 2.0,
float Offset = 0.0,
float Intensity = 1.0,
float Gain = 1.0,
float Size = 0.25,
point Vector = P,
output float Fac = 0.0)
{
float dimension = max(Dimension, 0.0);
float octaves = max(Octaves, 0.0);
float lacunarity = max(Lacunarity, 1e-5);
float size = nonzero(Size, 1e-5);
point p = Vector/size;
if(Type == "Multifractal")
Fac = Intensity*noise_musgrave_multi_fractal(p, Basis, dimension, lacunarity, octaves);
else if(Type == "fBM")
Fac = Intensity*noise_musgrave_fBm(p, Basis, dimension, lacunarity, octaves);
else if(Type == "Hybrid Multifractal")
Fac = Intensity*noise_musgrave_hybrid_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain);
else if(Type == "Ridged Multifractal")
Fac = Intensity*noise_musgrave_ridged_multi_fractal(p, Basis, dimension, lacunarity, octaves, Offset, Gain);
else if(Type == "Hetero Terrain")
Fac = Intensity*noise_musgrave_hetero_terrain(p, Basis, dimension, lacunarity, octaves, Offset);
}