2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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"
|
|
|
|
*/
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_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
|
|
|
|
*/
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_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'
|
|
|
|
*/
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_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'
|
|
|
|
*/
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_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'
|
|
|
|
*/
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_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 */
|
|
|
|
|
2014-01-13 19:40:13 +00:00
|
|
|
ccl_device float svm_musgrave(NodeMusgraveType type, float dimension, float lacunarity, float octaves, float offset, float intensity, float gain, float3 p)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-11-06 21:05:58 +00:00
|
|
|
NodeNoiseBasis basis = NODE_NOISE_PERLIN;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device void svm_node_tex_musgrave(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
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,
|
2014-01-13 19:40:13 +00:00
|
|
|
dimension, lacunarity, detail, foffset, 1.0f, gain, co*scale);
|
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
|
|
|
|
|