2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* 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"
|
|
|
|
*/
|
|
|
|
|
2011-08-29 17:17:40 +00:00
|
|
|
__device_noinline float noise_musgrave_fBm(float3 p, NodeNoiseBasis basis, float H, float lacunarity, float octaves)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float rmd;
|
|
|
|
float value = 0.0f;
|
|
|
|
float pwr = 1.0f;
|
2013-06-07 16:06:17 +00:00
|
|
|
float pwHL = powf(lacunarity, -H);
|
2011-04-27 11:58:34 +00:00
|
|
|
int i;
|
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
for(i = 0; i < float_to_int(octaves); i++) {
|
2011-11-06 21:05:58 +00:00
|
|
|
value += snoise(p) * pwr;
|
2011-04-27 11:58:34 +00:00
|
|
|
pwr *= pwHL;
|
|
|
|
p *= lacunarity;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:45:22 +00:00
|
|
|
rmd = octaves - floorf(octaves);
|
2011-04-27 11:58:34 +00:00
|
|
|
if(rmd != 0.0f)
|
2011-11-06 21:05:58 +00:00
|
|
|
value += rmd * snoise(p) * pwr;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Musgrave Multifractal
|
|
|
|
*
|
|
|
|
* H: highest fractal dimension
|
|
|
|
* lacunarity: gap between successive frequencies
|
|
|
|
* octaves: number of frequencies in the fBm
|
|
|
|
*/
|
|
|
|
|
2011-08-29 17:17:40 +00:00
|
|
|
__device_noinline float noise_musgrave_multi_fractal(float3 p, NodeNoiseBasis basis, float H, float lacunarity, float octaves)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float rmd;
|
|
|
|
float value = 1.0f;
|
|
|
|
float pwr = 1.0f;
|
2013-06-07 16:06:17 +00:00
|
|
|
float pwHL = powf(lacunarity, -H);
|
2011-04-27 11:58:34 +00:00
|
|
|
int i;
|
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
for(i = 0; i < float_to_int(octaves); i++) {
|
2011-11-06 21:05:58 +00:00
|
|
|
value *= (pwr * snoise(p) + 1.0f);
|
2011-04-27 11:58:34 +00:00
|
|
|
pwr *= pwHL;
|
|
|
|
p *= lacunarity;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:45:22 +00:00
|
|
|
rmd = octaves - floorf(octaves);
|
2011-04-27 11:58:34 +00:00
|
|
|
if(rmd != 0.0f)
|
2011-11-06 21:05:58 +00:00
|
|
|
value *= (rmd * pwr * snoise(p) + 1.0f); /* correct? */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
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'
|
|
|
|
*/
|
|
|
|
|
2011-08-29 17:17:40 +00:00
|
|
|
__device_noinline float noise_musgrave_hetero_terrain(float3 p, NodeNoiseBasis basis, float H, float lacunarity, float octaves, float offset)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float value, increment, rmd;
|
2013-06-07 16:06:17 +00:00
|
|
|
float pwHL = powf(lacunarity, -H);
|
2011-04-27 11:58:34 +00:00
|
|
|
float pwr = pwHL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* first unscaled octave of function; later octaves are scaled */
|
2011-11-06 21:05:58 +00:00
|
|
|
value = offset + snoise(p);
|
2011-04-27 11:58:34 +00:00
|
|
|
p *= lacunarity;
|
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
for(i = 1; i < float_to_int(octaves); i++) {
|
2011-11-06 21:05:58 +00:00
|
|
|
increment = (snoise(p) + offset) * pwr * value;
|
2011-04-27 11:58:34 +00:00
|
|
|
value += increment;
|
|
|
|
pwr *= pwHL;
|
|
|
|
p *= lacunarity;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:45:22 +00:00
|
|
|
rmd = octaves - floorf(octaves);
|
2011-04-27 11:58:34 +00:00
|
|
|
if(rmd != 0.0f) {
|
2011-11-06 21:05:58 +00:00
|
|
|
increment = (snoise(p) + offset) * pwr * value;
|
2011-04-27 11:58:34 +00:00
|
|
|
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'
|
|
|
|
*/
|
|
|
|
|
2011-08-29 17:17:40 +00:00
|
|
|
__device_noinline float noise_musgrave_hybrid_multi_fractal(float3 p, NodeNoiseBasis basis, float H, float lacunarity, float octaves, float offset, float gain)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float result, signal, weight, rmd;
|
2013-06-07 16:06:17 +00:00
|
|
|
float pwHL = powf(lacunarity, -H);
|
2011-04-27 11:58:34 +00:00
|
|
|
float pwr = pwHL;
|
|
|
|
int i;
|
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
result = snoise(p) + offset;
|
2011-04-27 11:58:34 +00:00
|
|
|
weight = gain * result;
|
|
|
|
p *= lacunarity;
|
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
for(i = 1; (weight > 0.001f) && (i < float_to_int(octaves)); i++) {
|
2011-04-27 11:58:34 +00:00
|
|
|
if(weight > 1.0f)
|
|
|
|
weight = 1.0f;
|
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
signal = (snoise(p) + offset) * pwr;
|
2011-04-27 11:58:34 +00:00
|
|
|
pwr *= pwHL;
|
|
|
|
result += weight * signal;
|
|
|
|
weight *= gain * signal;
|
|
|
|
p *= lacunarity;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:45:22 +00:00
|
|
|
rmd = octaves - floorf(octaves);
|
2011-04-27 11:58:34 +00:00
|
|
|
if(rmd != 0.0f)
|
2011-11-06 21:05:58 +00:00
|
|
|
result += rmd * ((snoise(p) + offset) * pwr);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
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'
|
|
|
|
*/
|
|
|
|
|
2011-08-29 17:17:40 +00:00
|
|
|
__device_noinline float noise_musgrave_ridged_multi_fractal(float3 p, NodeNoiseBasis basis, float H, float lacunarity, float octaves, float offset, float gain)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
float result, signal, weight;
|
2013-06-07 16:06:17 +00:00
|
|
|
float pwHL = powf(lacunarity, -H);
|
2011-04-27 11:58:34 +00:00
|
|
|
float pwr = pwHL;
|
|
|
|
int i;
|
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
signal = offset - fabsf(snoise(p));
|
2011-04-27 11:58:34 +00:00
|
|
|
signal *= signal;
|
|
|
|
result = signal;
|
|
|
|
weight = 1.0f;
|
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
for(i = 1; i < float_to_int(octaves); i++) {
|
2011-04-27 11:58:34 +00:00
|
|
|
p *= lacunarity;
|
|
|
|
weight = clamp(signal * gain, 0.0f, 1.0f);
|
2011-11-06 21:05:58 +00:00
|
|
|
signal = offset - fabsf(snoise(p));
|
2011-04-27 11:58:34 +00:00
|
|
|
signal *= signal;
|
|
|
|
signal *= weight;
|
|
|
|
result += signal * pwr;
|
|
|
|
pwr *= pwHL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Shader */
|
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
__device float svm_musgrave(NodeMusgraveType type, float dimension, float lacunarity, float octaves, float offset, float intensity, float gain, float scale, float3 p)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-11-06 21:05:58 +00:00
|
|
|
NodeNoiseBasis basis = NODE_NOISE_PERLIN;
|
|
|
|
p *= scale;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
if(type == NODE_MUSGRAVE_MULTIFRACTAL)
|
|
|
|
return intensity*noise_musgrave_multi_fractal(p, basis, dimension, lacunarity, octaves);
|
|
|
|
else if(type == NODE_MUSGRAVE_FBM)
|
|
|
|
return intensity*noise_musgrave_fBm(p, basis, dimension, lacunarity, octaves);
|
|
|
|
else if(type == NODE_MUSGRAVE_HYBRID_MULTIFRACTAL)
|
|
|
|
return intensity*noise_musgrave_hybrid_multi_fractal(p, basis, dimension, lacunarity, octaves, offset, gain);
|
|
|
|
else if(type == NODE_MUSGRAVE_RIDGED_MULTIFRACTAL)
|
|
|
|
return intensity*noise_musgrave_ridged_multi_fractal(p, basis, dimension, lacunarity, octaves, offset, gain);
|
|
|
|
else if(type == NODE_MUSGRAVE_HETERO_TERRAIN)
|
|
|
|
return intensity*noise_musgrave_hetero_terrain(p, basis, dimension, lacunarity, octaves, offset);
|
|
|
|
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
__device void svm_node_tex_musgrave(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
|
|
|
|
{
|
|
|
|
uint4 node2 = read_node(kg, offset);
|
|
|
|
uint4 node3 = read_node(kg, offset);
|
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
uint type, co_offset, color_offset, fac_offset;
|
|
|
|
uint dimension_offset, lacunarity_offset, detail_offset, offset_offset;
|
|
|
|
uint gain_offset, scale_offset;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
decode_node_uchar4(node.y, &type, &co_offset, &color_offset, &fac_offset);
|
|
|
|
decode_node_uchar4(node.z, &dimension_offset, &lacunarity_offset, &detail_offset, &offset_offset);
|
2012-01-12 17:13:56 +00:00
|
|
|
decode_node_uchar4(node.w, &gain_offset, &scale_offset, NULL, NULL);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
float3 co = stack_load_float3(stack, co_offset);
|
|
|
|
float dimension = stack_load_float_default(stack, dimension_offset, node2.x);
|
|
|
|
float lacunarity = stack_load_float_default(stack, lacunarity_offset, node2.y);
|
2011-11-06 21:05:58 +00:00
|
|
|
float detail = stack_load_float_default(stack, detail_offset, node2.z);
|
2011-04-27 11:58:34 +00:00
|
|
|
float foffset = stack_load_float_default(stack, offset_offset, node2.w);
|
|
|
|
float gain = stack_load_float_default(stack, gain_offset, node3.x);
|
2011-11-06 21:05:58 +00:00
|
|
|
float scale = stack_load_float_default(stack, scale_offset, node3.y);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
dimension = fmaxf(dimension, 1e-5f);
|
|
|
|
detail = clamp(detail, 0.0f, 16.0f);
|
2011-04-27 11:58:34 +00:00
|
|
|
lacunarity = fmaxf(lacunarity, 1e-5f);
|
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
float f = svm_musgrave((NodeMusgraveType)type,
|
|
|
|
dimension, lacunarity, detail, foffset, 1.0f, gain, scale, co);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-11-06 21:05:58 +00:00
|
|
|
if(stack_valid(fac_offset))
|
|
|
|
stack_store_float(stack, fac_offset, f);
|
|
|
|
if(stack_valid(color_offset))
|
|
|
|
stack_store_float3(stack, color_offset, make_float3(f, f, f));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|