1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # Another Noise Tool - Noise and Effects
23 from mathutils
.noise
import (
42 # ------------------------------------------------------------
44 def Height_Scale(input, iscale
, offset
, invert
):
46 return (1.0 - input) * iscale
+ offset
48 return input * iscale
+ offset
51 # Functions for marble_noise and effects:
54 return sqrt((x
* x
) + (y
* y
))
58 return 0.5 + 0.5 * sin(a
)
62 return 0.5 + 0.5 * cos(a
)
67 a
= 1 - 2 * abs(floor((a
* (1 / b
)) + 0.5) - (a
* (1 / b
)))
89 return sharp(sharp(a
))
96 def shapes(x
, y
, z
, shape
=0):
102 s
= cos(x
**2 + y
**2) / (x
**2 + y
**2 + 0.5)
107 s
= ((x
* sin(x
* x
+ y
* y
) + y
* cos(x
* x
+ y
* y
)) / (x
**2 + y
**2 + 0.5))
113 s
= 1 - ((cos(x
* p
) + cos(y
* p
) + cos(z
* p
)) - 0.5)
130 s
= ((x
+ y
+ z
) * 5)
135 def marble_noise(x
, y
, z
, origin
, size
, shape
, bias
, sharpnes
, turb
, depth
, hard
, basis
, amp
, freq
):
137 s
= shapes(x
, y
, z
, shape
)
141 value
= s
+ turb
* turbulence_vector((x
, y
, z
), depth
, hard
, basis
)[1]
144 value
= cos_bias(value
)
146 value
= tri_bias(value
)
148 value
= saw_bias(value
)
150 value
= sin_bias(value
)
153 value
= 1.0 - sharp(value
)
155 value
= 1.0 - sharper(value
)
161 value
= sharper(value
)
163 value
= 1.0 - soft(value
)
168 # vl_noise_turbulence:
169 def vlnTurbMode(coords
, distort
, basis
, vlbasis
, hardnoise
):
172 return (abs(-variable_lacunarity(coords
, distort
, basis
, vlbasis
)))
175 return variable_lacunarity(coords
, distort
, basis
, vlbasis
)
178 def vl_noise_turbulence(coords
, distort
, depth
, basis
, vlbasis
, hardnoise
, amp
, freq
):
180 value
= vlnTurbMode(coords
, distort
, basis
, vlbasis
, hardnoise
)
182 for i
in range(depth
):
184 value
+= vlnTurbMode((x
* (freq
* i
), y
* (freq
* i
), z
* (freq
* i
)), distort
, basis
, vlbasis
, hardnoise
) * (amp
* 0.5 / i
)
189 def double_multiFractal(coords
, H
, lacunarity
, octaves
, offset
, gain
, basis
, vlbasis
):
191 n1
= multi_fractal((x
* 1.5 + 1, y
* 1.5 + 1, z
* 1.5 + 1), 1.0, 1.0, 1.0, basis
) * (offset
* 0.5)
192 n2
= multi_fractal((x
- 1, y
- 1, z
- 1), H
, lacunarity
, octaves
, vlbasis
) * (gain
* 0.5)
193 return (n1
* n1
+ n2
* n2
) * 0.5
196 ## distorted_heteroTerrain:
197 def distorted_heteroTerrain(coords
, H
, lacunarity
, octaves
, offset
, distort
, basis
, vlbasis
):
199 h1
= (hetero_terrain((x
, y
, z
), 1.0, 2.0, 1.0, 1.0, basis
) * 0.5)
201 h2
= (hetero_terrain((x
+ d
, y
+ d
, z
+ d
), H
, lacunarity
, octaves
, offset
, vlbasis
) * 0.25)
202 return (h1
* h1
+ h2
* h2
) * 0.5
206 def slick_rock(coords
, H
, lacunarity
, octaves
, offset
, gain
, distort
, basis
, vlbasis
):
208 n
= multi_fractal((x
,y
,z
), 1.0, 2.0, 2.0, basis
) * distort
* 0.25
209 r
= ridged_multi_fractal((x
+ n
, y
+ n
, z
+ n
), H
, lacunarity
, octaves
, offset
+ 0.1, gain
* 2, vlbasis
)
210 return (n
+ (n
* r
)) * 0.5
214 def vl_hTerrain(coords
, H
, lacunarity
, octaves
, offset
, basis
, vlbasis
, distort
):
216 ht
= hetero_terrain((x
, y
, z
), H
, lacunarity
, octaves
, offset
, basis
) * 0.25
217 vl
= ht
* variable_lacunarity((x
, y
, z
), distort
, basis
, vlbasis
) * 0.5 + 0.5
222 def ant_turbulence(coords
, depth
, hardnoise
, nbasis
, amp
, freq
, distortion
):
224 t
= turbulence_vector((x
/2, y
/2, z
/2), depth
, 0, nbasis
, amp
, freq
) * 0.5 * distortion
225 return turbulence((t
[0], t
[1], t
[2]), 2, hardnoise
, 3) * 0.5 + 0.5
229 def rocks_noise(coords
, depth
, hardnoise
, nbasis
, distortion
):
231 p
= turbulence((x
, y
, z
), 4, 0, 0) * 0.125 * distortion
233 a
= turbulence((xx
+ p
, yy
+ p
, zz
), 2, 0, 7)
234 pa
= a
* 0.1875 * distortion
235 b
= turbulence((x
, y
, z
+ pa
), depth
, hardnoise
, nbasis
)
236 return ((a
+ 0.5 * (b
- a
)) * 0.5 + 0.5)
239 # shattered_hterrain:
240 def shattered_hterrain(coords
, H
, lacunarity
, octaves
, offset
, distort
, basis
):
242 d
= (turbulence_vector(coords
, 6, 0, 0)[0] * 0.5 + 0.5) * distort
* 0.5
243 t1
= (turbulence_vector((x
+ d
, y
+ d
, z
+ d
), 0, 0, 7)[0] + 0.5)
244 t2
= (hetero_terrain((x
* 2, y
* 2, z
* 2), H
, lacunarity
, octaves
, offset
, basis
) * 0.5)
245 return ((t1
* t2
) + t2
* 0.5) * 0.5
249 def strata_hterrain(coords
, H
, lacunarity
, octaves
, offset
, distort
, basis
):
251 value
= hetero_terrain((x
, y
, z
), H
, lacunarity
, octaves
, offset
, basis
) * 0.5
252 steps
= (sin(value
* (distort
* 5) * pi
) * (0.1 / (distort
* 5) * pi
))
253 return (value
* (1.0 - 0.5) + steps
* 0.5)
256 # Planet Noise by: Farsthary
257 # https://farsthary.com/2010/11/24/new-planet-procedural-texture/
258 def planet_noise(coords
, oct=6, hard
=0, noisebasis
=1, nabla
=0.001):
261 offset
= nabla
* 1000
262 x
= turbulence((x
, y
, z
), oct, hard
, noisebasis
)
263 y
= turbulence((x
+ offset
, y
, z
), oct, hard
, noisebasis
)
264 z
= turbulence((x
, y
+ offset
, z
), oct, hard
, noisebasis
)
265 xdy
= x
- turbulence((x
, y
+ d
, z
), oct, hard
, noisebasis
)
266 xdz
= x
- turbulence((x
, y
, z
+ d
), oct, hard
, noisebasis
)
267 ydx
= y
- turbulence((x
+ d
, y
, z
), oct, hard
, noisebasis
)
268 ydz
= y
- turbulence((x
, y
, z
+ d
), oct, hard
, noisebasis
)
269 zdx
= z
- turbulence((x
+ d
, y
, z
), oct, hard
, noisebasis
)
270 zdy
= z
- turbulence((x
, y
+ d
, z
), oct, hard
, noisebasis
)
271 return (zdy
- ydz
), (zdx
- xdz
), (ydx
- xdy
)
274 ###----------------------------------------------------------------------
275 # v.1.04 Effect functions:
287 def Mix_Modes(a
, b
, mixfactor
, mode
):
289 a
= a
* (1.0 - mixfactor
)
290 b
= b
* (1.0 + mixfactor
)
293 return (a
* (1.0 - 0.5) + b
* 0.5)
308 return 1.0 - ((1.0 - a
) * (1.0 - b
) / 1.0)
322 Bias_Types
= [sin_bias
, cos_bias
, tri_bias
, saw_bias
, no_bias
]
323 Sharp_Types
= [soft
, sharp
, sharper
]
326 # Transpose effect coords:
327 def Trans_Effect(coords
, size
, loc
):
329 x
= (x
* 2.0 / size
+ loc
[0])
330 y
= (y
* 2.0 / size
+ loc
[1])
334 # Effect_Basis_Function:
335 def Effect_Basis_Function(coords
, type, bias
):
344 effect
= offset
+ iscale
* (Bias_Types
[bias
](x
+ y
))
347 effect
= offset
+ iscale
* 0.5 * (Bias_Types
[bias
](x
* pi
) + Bias_Types
[bias
](y
* pi
))
350 effect
= offset
+ iscale
* Bias_Types
[bias
](offset
+ iscale
* sin(x
* pi
+ sin(y
* pi
)))
353 effect
= offset
+ iscale
* (Bias_Types
[bias
](cos(x
) + sin(y
) + cos(x
* 2 + y
* 2) - sin(-x
* 4 + y
* 4)))
356 effect
= offset
+ iscale
* 1 - Bias_Types
[bias
]((sin(x
* pi
) + sin(y
* pi
)))
359 effect
= offset
+ iscale
* (Bias_Types
[bias
](x
* pi
* 2) * Bias_Types
[bias
](y
* pi
* 2)) - 0.5
362 effect
= offset
+ iscale
* (Bias_Types
[bias
](1.0 - (x
* x
+ y
* y
)))
365 effect
= offset
+ iscale
* Bias_Types
[bias
]( (x
* sin(x
* x
+ y
* y
) + y
* cos(x
* x
+ y
* y
)) / (x
**2 + y
**2 + 0.5)) * 2
366 ## square / piramide:
368 effect
= offset
+ iscale
* Bias_Types
[bias
](1.0 - sqrt((x
* x
)**10 + (y
* y
)**10)**0.1)
371 effect
= (0.5 - max(Bias_Types
[bias
](x
* pi
) , Bias_Types
[bias
](y
* pi
)))
374 effect
= offset
+ iscale
* effect
377 effect
= (0.025 - min(Bias_Types
[bias
](x
* pi
), Bias_Types
[bias
](y
* pi
)))
380 effect
= offset
+ iscale
* effect
383 a
= max(Bias_Types
[bias
](x
* pi
), Bias_Types
[bias
](y
* pi
))
384 b
= max(Bias_Types
[bias
](x
* pi
* 2 + 2), Bias_Types
[bias
](y
* pi
* 2 + 2))
385 effect
= min(Bias_Types
[bias
](a
), Bias_Types
[bias
](b
)) * 3.0 - 2.0
388 effect
= offset
+ iscale
* effect
391 t
= turbulence((x
, y
, 0), 6, 0, 0) * 0.25
392 effect
= variable_lacunarity((x
, y
, t
), 0.25, 0, 8)
395 effect
= offset
+ iscale
* effect
396 ## sparse cracks noise:
398 effect
= 2.5 * abs(noise((x
, y
, 0), 1)) - 0.1
401 effect
= offset
+ iscale
* (effect
* 2.5)
402 ## shattered rock noise:
404 effect
= 0.5 + noise((x
, y
, 0), 7)
407 effect
= offset
+ iscale
* effect
410 effect
= 0.25 + 1.5 * voronoi((x
, y
, 0), 1)[0][0]
413 effect
= offset
+ iscale
* effect
* 2
416 effect
= cos(5 * noise((x
, y
, 0), 0))
417 effect
= offset
+ iscale
* (effect
* 0.5)
420 n
= 0.5 + 0.5 * turbulence((x
* 5, y
* 5, 0), 8, 0, 0)
421 effect
= ((n
* n
)**5)
422 effect
= offset
+ iscale
* effect
425 effect
= offset
+ iscale
* (noise((x
* 2, y
* 2, 0), 0) * 1.5 - 0.75)
428 t
= turbulence((x
, y
, 0), 6, 0, 0)
432 effect
= offset
+ iscale
* effect
435 t
= 1 - voronoi((x
, y
, 0), 1)[0][0]
439 effect
= offset
+ iscale
* effect
449 # fractalize Effect_Basis_Function: ------------------------------
450 def Effect_Function(coords
, type, bias
, turb
, depth
, frequency
, amplitude
):
455 t
= turb
* ( 0.5 + 0.5 * turbulence(coords
, 6, 0, 0))
460 result
= Effect_Basis_Function((x
, y
, z
), type, bias
) * amplitude
464 for i
in range(depth
):
468 result
+= Effect_Basis_Function((x
, y
, z
), type, bias
) * amplitude
/ i
473 # ------------------------------------------------------------
475 def noise_gen(coords
, props
):
477 terrain_name
= props
[0]
484 texture_name
= props
[7]
487 meshsize_x
= props
[10]
488 meshsize_y
= props
[11]
499 nbasis
= int(props
[22])
500 vlbasis
= int(props
[23])
501 distortion
= props
[24]
502 hardnoise
= int(props
[25])
506 dimension
= props
[29]
507 lacunarity
= props
[30]
510 marblebias
= int(props
[33])
511 marblesharpnes
= int(props
[34])
512 marbleshape
= int(props
[35])
514 height_invert
= props
[37]
515 height_offset
= props
[38]
518 falloff
= int(props
[41])
519 edge_level
= props
[42]
520 falloffsize_x
= props
[43]
521 falloffsize_y
= props
[44]
522 stratatype
= props
[45]
525 waterlevel
= props
[48]
526 vert_group
= props
[49]
527 remove_double
= props
[50]
528 fx_mixfactor
= props
[51]
529 fx_mix_mode
= props
[52]
534 fx_frequency
= props
[57]
535 fx_amplitude
= props
[58]
539 fx_height
= props
[62]
540 fx_offset
= props
[63]
541 fx_invert
= props
[64]
547 origin
= x_offset
, y_offset
, z_offset
556 origin
= random_unit_vector()
557 ox
= (origin
[0] * o_range
)
558 oy
= (origin
[1] * o_range
)
559 oz
= (origin
[2] * o_range
)
560 origin_x
= (ox
- (ox
/ 2)) + x_offset
561 origin_y
= (oy
- (oy
/ 2)) + y_offset
562 origin_z
= (oz
- (oz
/ 2)) + z_offset
564 ncoords
= (x
/ (nsize
* size_x
) + origin_x
, y
/ (nsize
* size_y
) + origin_y
, z
/ (nsize
* size_z
) + origin_z
)
568 nbasis
= 14 # Cellnoise
573 if ntype
in [0, 'multi_fractal']:
574 value
= multi_fractal(ncoords
, dimension
, lacunarity
, depth
, nbasis
) * 0.5
576 elif ntype
in [1, 'ridged_multi_fractal']:
577 value
= ridged_multi_fractal(ncoords
, dimension
, lacunarity
, depth
, offset
, gain
, nbasis
) * 0.5
579 elif ntype
in [2, 'hybrid_multi_fractal']:
580 value
= hybrid_multi_fractal(ncoords
, dimension
, lacunarity
, depth
, offset
, gain
, nbasis
) * 0.5
582 elif ntype
in [3, 'hetero_terrain']:
583 value
= hetero_terrain(ncoords
, dimension
, lacunarity
, depth
, offset
, nbasis
) * 0.25
585 elif ntype
in [4, 'fractal']:
586 value
= fractal(ncoords
, dimension
, lacunarity
, depth
, nbasis
)
588 elif ntype
in [5, 'turbulence_vector']:
589 value
= turbulence_vector(ncoords
, depth
, hardnoise
, nbasis
, amp
, freq
)[0]
591 elif ntype
in [6, 'variable_lacunarity']:
592 value
= variable_lacunarity(ncoords
, distortion
, nbasis
, vlbasis
)
594 elif ntype
in [7, 'marble_noise']:
595 value
= marble_noise(
596 (ncoords
[0] - origin_x
+ x_offset
),
597 (ncoords
[1] - origin_y
+ y_offset
),
598 (ncoords
[2] - origin_z
+ z_offset
),
599 (origin
[0] + x_offset
, origin
[1] + y_offset
, origin
[2] + z_offset
), nsize
,
600 marbleshape
, marblebias
, marblesharpnes
,
601 distortion
, depth
, hardnoise
, nbasis
, amp
, freq
603 elif ntype
in [8, 'shattered_hterrain']:
604 value
= shattered_hterrain(ncoords
, dimension
, lacunarity
, depth
, offset
, distortion
, nbasis
)
606 elif ntype
in [9, 'strata_hterrain']:
607 value
= strata_hterrain(ncoords
, dimension
, lacunarity
, depth
, offset
, distortion
, nbasis
)
609 elif ntype
in [10, 'ant_turbulence']:
610 value
= ant_turbulence(ncoords
, depth
, hardnoise
, nbasis
, amp
, freq
, distortion
)
612 elif ntype
in [11, 'vl_noise_turbulence']:
613 value
= vl_noise_turbulence(ncoords
, distortion
, depth
, nbasis
, vlbasis
, hardnoise
, amp
, freq
)
615 elif ntype
in [12, 'vl_hTerrain']:
616 value
= vl_hTerrain(ncoords
, dimension
, lacunarity
, depth
, offset
, nbasis
, vlbasis
, distortion
)
618 elif ntype
in [13, 'distorted_heteroTerrain']:
619 value
= distorted_heteroTerrain(ncoords
, dimension
, lacunarity
, depth
, offset
, distortion
, nbasis
, vlbasis
)
621 elif ntype
in [14, 'double_multiFractal']:
622 value
= double_multiFractal(ncoords
, dimension
, lacunarity
, depth
, offset
, gain
, nbasis
, vlbasis
)
624 elif ntype
in [15, 'rocks_noise']:
625 value
= rocks_noise(ncoords
, depth
, hardnoise
, nbasis
, distortion
)
627 elif ntype
in [16, 'slick_rock']:
628 value
= slick_rock(ncoords
,dimension
, lacunarity
, depth
, offset
, gain
, distortion
, nbasis
, vlbasis
)
630 elif ntype
in [17, 'planet_noise']:
631 value
= planet_noise(ncoords
, depth
, hardnoise
, nbasis
)[2] * 0.5 + 0.5
633 elif ntype
in [18, 'blender_texture']:
634 if texture_name
!= "" and texture_name
in bpy
.data
.textures
:
635 value
= bpy
.data
.textures
[texture_name
].evaluate(ncoords
)[3]
643 if fx_type
in [0,"0"]:
647 fxcoords
= Trans_Effect((x
, y
, z
), fx_size
, (fx_loc_x
, fx_loc_y
))
648 effect
= Effect_Function(fxcoords
, fx_type
, fx_bias
, fx_turb
, fx_depth
, fx_frequency
, fx_amplitude
)
649 effect
= Height_Scale(effect
, fx_height
, fx_offset
, fx_invert
)
650 fxval
= Mix_Modes(val
, effect
, fx_mixfactor
, fx_mix_mode
)
654 value
= Height_Scale(value
, height
, height_offset
, height_invert
)
659 ratio_x
, ratio_y
= abs(x
) * 2 / meshsize_x
, abs(y
) * 2 / meshsize_y
661 sqrt(ratio_y
**falloffsize_y
),
662 sqrt(ratio_x
**falloffsize_x
),
663 sqrt(ratio_x
**falloffsize_x
+ ratio_y
**falloffsize_y
)
665 dist
= fallofftypes
[falloff
]
668 dist
= (dist
* dist
* (3 - 2 * dist
))
669 value
= (value
- value
* dist
) + edge_level
673 # Strata / terrace / layers
674 if stratatype
not in [0, "0"]:
675 if stratatype
in [1, "1"]:
676 strata
= strata
/ height
678 steps
= (sin(value
* strata
* pi
) * (0.1 / strata
* pi
))
679 value
= (value
* 0.5 + steps
* 0.5) * 2.0
681 elif stratatype
in [2, "2"]:
682 strata
= strata
/ height
683 steps
= -abs(sin(value
* strata
* pi
) * (0.1 / strata
* pi
))
684 value
= (value
* 0.5 + steps
* 0.5) * 2.0
686 elif stratatype
in [3, "3"]:
687 strata
= strata
/ height
688 steps
= abs(sin(value
* strata
* pi
) * (0.1 / strata
* pi
))
689 value
= (value
* 0.5 + steps
* 0.5) * 2.0
691 elif stratatype
in [4, "4"]:
692 strata
= strata
/ height
693 value
= int( value
* strata
) * 1.0 / strata
695 elif stratatype
in [5, "5"]:
696 strata
= strata
/ height
697 steps
= (int( value
* strata
) * 1.0 / strata
)
698 value
= (value
* (1.0 - 0.5) + steps
* 0.5)
700 # Clamp height min max
701 if (value
< minimum
):
703 if (value
> maximum
):