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 noise_basis_default
= "BLENDER"
44 ("BLENDER", "Blender", "Blender default noise", 0),
45 ("PERLIN_ORIGINAL", "Perlin", "Perlin noise", 1),
46 ("PERLIN_NEW", "New Perlin", "New Perlin noise", 2),
47 ("VORONOI_F1", "Voronoi F1", "Voronoi F1", 3),
48 ("VORONOI_F2", "Voronoi F2", "Voronoi F2", 4),
49 ("VORONOI_F3", "Voronoi F3", "Voronoi F3", 5),
50 ("VORONOI_F4", "Voronoi F4", "Voronoi F4", 6),
51 ("VORONOI_F2F1", "Voronoi F2-F1", "Voronoi F2-F1", 7),
52 ("VORONOI_CRACKLE", "Voronoi Crackle", "Voronoi Crackle", 8),
53 ("CELLNOISE", "Cell Noise", "Cell noise", 9)
56 # ------------------------------------------------------------
58 def Height_Scale(input, iscale
, offset
, invert
):
60 return (1.0 - input) * iscale
+ offset
62 return input * iscale
+ offset
65 # Functions for marble_noise and effects:
68 return sqrt((x
* x
) + (y
* y
))
72 return 0.5 + 0.5 * sin(a
)
76 return 0.5 + 0.5 * cos(a
)
81 a
= 1 - 2 * abs(floor((a
* (1 / b
)) + 0.5) - (a
* (1 / b
)))
103 return sharp(sharp(a
))
110 def shapes(x
, y
, z
, shape
=0):
116 s
= cos(x
**2 + y
**2) / (x
**2 + y
**2 + 0.5)
121 s
= ((x
* sin(x
* x
+ y
* y
) + y
* cos(x
* x
+ y
* y
)) / (x
**2 + y
**2 + 0.5))
127 s
= 1 - ((cos(x
* p
) + cos(y
* p
) + cos(z
* p
)) - 0.5)
144 s
= ((x
+ y
+ z
) * 5)
149 def marble_noise(x
, y
, z
, origin
, size
, shape
, bias
, sharpnes
, turb
, depth
, hard
, basis
, amp
, freq
):
151 s
= shapes(x
, y
, z
, shape
)
155 value
= s
+ turb
* turbulence_vector((x
, y
, z
), depth
, hard
, noise_basis
=basis
)[1]
158 value
= cos_bias(value
)
160 value
= tri_bias(value
)
162 value
= saw_bias(value
)
164 value
= sin_bias(value
)
167 value
= 1.0 - sharp(value
)
169 value
= 1.0 - sharper(value
)
175 value
= sharper(value
)
177 value
= 1.0 - soft(value
)
182 # vl_noise_turbulence:
183 def vlnTurbMode(coords
, distort
, basis
, vlbasis
, hardnoise
):
186 return (abs(-variable_lacunarity(coords
, distort
, noise_type1
=basis
, noise_type2
=vlbasis
)))
189 return variable_lacunarity(coords
, distort
, noise_type1
=basis
, noise_type2
=vlbasis
)
192 def vl_noise_turbulence(coords
, distort
, depth
, basis
, vlbasis
, hardnoise
, amp
, freq
):
194 value
= vlnTurbMode(coords
, distort
, basis
, vlbasis
, hardnoise
)
196 for i
in range(depth
):
198 value
+= vlnTurbMode((x
* (freq
* i
), y
* (freq
* i
), z
* (freq
* i
)), distort
, basis
, vlbasis
, hardnoise
) * (amp
* 0.5 / i
)
203 def double_multiFractal(coords
, H
, lacunarity
, octaves
, offset
, gain
, basis
, vlbasis
):
205 n1
= multi_fractal((x
* 1.5 + 1, y
* 1.5 + 1, z
* 1.5 + 1), 1.0, 1.0, 1.0, noise_basis
=basis
) * (offset
* 0.5)
206 n2
= multi_fractal((x
- 1, y
- 1, z
- 1), H
, lacunarity
, octaves
, noise_basis
=vlbasis
) * (gain
* 0.5)
207 return (n1
* n1
+ n2
* n2
) * 0.5
210 ## distorted_heteroTerrain:
211 def distorted_heteroTerrain(coords
, H
, lacunarity
, octaves
, offset
, distort
, basis
, vlbasis
):
213 h1
= (hetero_terrain((x
, y
, z
), 1.0, 2.0, 1.0, 1.0, noise_basis
=basis
) * 0.5)
215 h2
= (hetero_terrain((x
+ d
, y
+ d
, z
+ d
), H
, lacunarity
, octaves
, offset
, noise_basis
=vlbasis
) * 0.25)
216 return (h1
* h1
+ h2
* h2
) * 0.5
220 def slick_rock(coords
, H
, lacunarity
, octaves
, offset
, gain
, distort
, basis
, vlbasis
):
222 n
= multi_fractal((x
,y
,z
), 1.0, 2.0, 2.0, noise_basis
=basis
) * distort
* 0.25
223 r
= ridged_multi_fractal((x
+ n
, y
+ n
, z
+ n
), H
, lacunarity
, octaves
, offset
+ 0.1, gain
* 2, noise_basis
=vlbasis
)
224 return (n
+ (n
* r
)) * 0.5
228 def vl_hTerrain(coords
, H
, lacunarity
, octaves
, offset
, basis
, vlbasis
, distort
):
230 ht
= hetero_terrain((x
, y
, z
), H
, lacunarity
, octaves
, offset
, noise_basis
=basis
) * 0.25
231 vl
= ht
* variable_lacunarity((x
, y
, z
), distort
, noise_type1
=basis
, noise_type2
=vlbasis
) * 0.5 + 0.5
236 def ant_turbulence(coords
, depth
, hardnoise
, nbasis
, amp
, freq
, distortion
):
238 t
= turbulence_vector((x
/2, y
/2, z
/2), depth
, 0, noise_basis
=nbasis
, amplitude_scale
=amp
, frequency_scale
=freq
) * 0.5 * distortion
239 return turbulence((t
[0], t
[1], t
[2]), 2, hardnoise
, noise_basis
="VORONOI_F1") * 0.5 + 0.5
243 def rocks_noise(coords
, depth
, hardnoise
, nbasis
, distortion
):
245 p
= turbulence((x
, y
, z
), 4, 0, noise_basis
='BLENDER') * 0.125 * distortion
247 a
= turbulence((xx
+ p
, yy
+ p
, zz
), 2, 0, noise_basis
='VORONOI_F2F1')
248 pa
= a
* 0.1875 * distortion
249 b
= turbulence((x
, y
, z
+ pa
), depth
, hardnoise
, noise_basis
=nbasis
)
250 return ((a
+ 0.5 * (b
- a
)) * 0.5 + 0.5)
253 # shattered_hterrain:
254 def shattered_hterrain(coords
, H
, lacunarity
, octaves
, offset
, distort
, basis
):
256 d
= (turbulence_vector(coords
, 6, 0)[0] * 0.5 + 0.5) * distort
* 0.5
257 t1
= (turbulence_vector((x
+ d
, y
+ d
, z
+ d
), 0, 0, noise_basis
='VORONOI_F2F1')[0] + 0.5)
258 t2
= (hetero_terrain((x
* 2, y
* 2, z
* 2), H
, lacunarity
, octaves
, offset
, noise_basis
=basis
) * 0.5)
259 return ((t1
* t2
) + t2
* 0.5) * 0.5
263 def strata_hterrain(coords
, H
, lacunarity
, octaves
, offset
, distort
, basis
):
265 value
= hetero_terrain((x
, y
, z
), H
, lacunarity
, octaves
, offset
, noise_basis
=basis
) * 0.5
266 steps
= (sin(value
* (distort
* 5) * pi
) * (0.1 / (distort
* 5) * pi
))
267 return (value
* (1.0 - 0.5) + steps
* 0.5)
270 # Planet Noise by: Farsthary
271 # https://farsthary.com/2010/11/24/new-planet-procedural-texture/
272 def planet_noise(coords
, oct=6, hard
=0, noisebasis
='PERLIN_ORIGINAL', nabla
=0.001):
275 offset
= nabla
* 1000
276 x
= turbulence((x
, y
, z
), oct, hard
, noise_basis
=noisebasis
)
277 y
= turbulence((x
+ offset
, y
, z
), oct, hard
, noise_basis
=noisebasis
)
278 z
= turbulence((x
, y
+ offset
, z
), oct, hard
, noise_basis
=noisebasis
)
279 xdy
= x
- turbulence((x
, y
+ d
, z
), oct, hard
, noise_basis
=noisebasis
)
280 xdz
= x
- turbulence((x
, y
, z
+ d
), oct, hard
, noise_basis
=noisebasis
)
281 ydx
= y
- turbulence((x
+ d
, y
, z
), oct, hard
, noise_basis
=noisebasis
)
282 ydz
= y
- turbulence((x
, y
, z
+ d
), oct, hard
, noise_basis
=noisebasis
)
283 zdx
= z
- turbulence((x
+ d
, y
, z
), oct, hard
, noise_basis
=noisebasis
)
284 zdy
= z
- turbulence((x
, y
+ d
, z
), oct, hard
, noise_basis
=noisebasis
)
285 return (zdy
- ydz
), (zdx
- xdz
), (ydx
- xdy
)
288 ###----------------------------------------------------------------------
289 # v.1.04 Effect functions:
301 def Mix_Modes(a
, b
, mixfactor
, mode
):
303 a
= a
* (1.0 - mixfactor
)
304 b
= b
* (1.0 + mixfactor
)
307 return (a
* (1.0 - 0.5) + b
* 0.5)
322 return 1.0 - ((1.0 - a
) * (1.0 - b
) / 1.0)
336 Bias_Types
= [sin_bias
, cos_bias
, tri_bias
, saw_bias
, no_bias
]
337 Sharp_Types
= [soft
, sharp
, sharper
]
340 # Transpose effect coords:
341 def Trans_Effect(coords
, size
, loc
):
343 x
= (x
* 2.0 / size
+ loc
[0])
344 y
= (y
* 2.0 / size
+ loc
[1])
348 # Effect_Basis_Function:
349 def Effect_Basis_Function(coords
, type, bias
):
358 effect
= offset
+ iscale
* (Bias_Types
[bias
](x
+ y
))
361 effect
= offset
+ iscale
* 0.5 * (Bias_Types
[bias
](x
* pi
) + Bias_Types
[bias
](y
* pi
))
364 effect
= offset
+ iscale
* Bias_Types
[bias
](offset
+ iscale
* sin(x
* pi
+ sin(y
* pi
)))
367 effect
= offset
+ iscale
* (Bias_Types
[bias
](cos(x
) + sin(y
) + cos(x
* 2 + y
* 2) - sin(-x
* 4 + y
* 4)))
370 effect
= offset
+ iscale
* 1 - Bias_Types
[bias
]((sin(x
* pi
) + sin(y
* pi
)))
373 effect
= offset
+ iscale
* (Bias_Types
[bias
](x
* pi
* 2) * Bias_Types
[bias
](y
* pi
* 2)) - 0.5
376 effect
= offset
+ iscale
* (Bias_Types
[bias
](1.0 - (x
* x
+ y
* y
)))
379 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
380 ## square / piramide:
382 effect
= offset
+ iscale
* Bias_Types
[bias
](1.0 - sqrt((x
* x
)**10 + (y
* y
)**10)**0.1)
385 effect
= (0.5 - max(Bias_Types
[bias
](x
* pi
) , Bias_Types
[bias
](y
* pi
)))
388 effect
= offset
+ iscale
* effect
391 effect
= (0.025 - min(Bias_Types
[bias
](x
* pi
), Bias_Types
[bias
](y
* pi
)))
394 effect
= offset
+ iscale
* effect
397 a
= max(Bias_Types
[bias
](x
* pi
), Bias_Types
[bias
](y
* pi
))
398 b
= max(Bias_Types
[bias
](x
* pi
* 2 + 2), Bias_Types
[bias
](y
* pi
* 2 + 2))
399 effect
= min(Bias_Types
[bias
](a
), Bias_Types
[bias
](b
)) * 3.0 - 2.0
402 effect
= offset
+ iscale
* effect
405 t
= turbulence((x
, y
, 0), 6, 0, noise_basis
="BLENDER") * 0.25
406 effect
= variable_lacunarity((x
, y
, t
), 0.25, noise_type2
='VORONOI_CRACKLE')
409 effect
= offset
+ iscale
* effect
410 ## sparse cracks noise:
412 effect
= 2.5 * abs(noise((x
, y
, 0), noise_basis
="PERLIN_ORIGINAL")) - 0.1
415 effect
= offset
+ iscale
* (effect
* 2.5)
416 ## shattered rock noise:
418 effect
= 0.5 + noise((x
, y
, 0), noise_basis
="VORONOI_F2F1")
421 effect
= offset
+ iscale
* effect
424 effect
= 0.25 + 1.5 * voronoi((x
, y
, 0), distance_metric
='DISTANCE_SQUARED')[0][0]
427 effect
= offset
+ iscale
* effect
* 2
430 effect
= cos(5 * noise((x
, y
, 0), noise_basis
="BLENDER"))
431 effect
= offset
+ iscale
* (effect
* 0.5)
434 n
= 0.5 + 0.5 * turbulence((x
* 5, y
* 5, 0), 8, 0, noise_basis
="BLENDER")
435 effect
= ((n
* n
)**5)
436 effect
= offset
+ iscale
* effect
439 effect
= offset
+ iscale
* (noise((x
* 2, y
* 2, 0), noise_basis
="BLENDER") * 1.5 - 0.75)
442 t
= turbulence((x
, y
, 0), 6, 0, noise_basis
="BLENDER")
446 effect
= offset
+ iscale
* effect
449 t
= 1 - voronoi((x
, y
, 0), distance_metric
='DISTANCE_SQUARED')[0][0]
453 effect
= offset
+ iscale
* effect
463 # fractalize Effect_Basis_Function: ------------------------------
464 def Effect_Function(coords
, type, bias
, turb
, depth
, frequency
, amplitude
):
469 t
= turb
* ( 0.5 + 0.5 * turbulence(coords
, 6, 0, noise_basis
="BLENDER"))
474 result
= Effect_Basis_Function((x
, y
, z
), type, bias
) * amplitude
478 for i
in range(depth
):
482 result
+= Effect_Basis_Function((x
, y
, z
), type, bias
) * amplitude
/ i
487 # ------------------------------------------------------------
489 def noise_gen(coords
, props
):
491 terrain_name
= props
[0]
498 texture_name
= props
[7]
501 meshsize_x
= props
[10]
502 meshsize_y
= props
[11]
515 distortion
= props
[24]
516 hardnoise
= int(props
[25])
520 dimension
= props
[29]
521 lacunarity
= props
[30]
524 marblebias
= int(props
[33])
525 marblesharpnes
= int(props
[34])
526 marbleshape
= int(props
[35])
528 height_invert
= props
[37]
529 height_offset
= props
[38]
532 falloff
= int(props
[41])
533 edge_level
= props
[42]
534 falloffsize_x
= props
[43]
535 falloffsize_y
= props
[44]
536 stratatype
= props
[45]
539 waterlevel
= props
[48]
540 vert_group
= props
[49]
541 remove_double
= props
[50]
542 fx_mixfactor
= props
[51]
543 fx_mix_mode
= props
[52]
548 fx_frequency
= props
[57]
549 fx_amplitude
= props
[58]
553 fx_height
= props
[62]
554 fx_offset
= props
[63]
555 fx_invert
= props
[64]
561 origin
= x_offset
, y_offset
, z_offset
570 origin
= random_unit_vector()
571 ox
= (origin
[0] * o_range
)
572 oy
= (origin
[1] * o_range
)
574 origin_x
= (ox
- (ox
* 0.5)) + x_offset
575 origin_y
= (oy
- (oy
* 0.5)) + y_offset
576 origin_z
= oz
+ z_offset
578 ncoords
= (x
/ (nsize
* size_x
) + origin_x
, y
/ (nsize
* size_y
) + origin_y
, z
/ (nsize
* size_z
) + origin_z
)
581 if ntype
in [0, 'multi_fractal']:
582 value
= multi_fractal(ncoords
, dimension
, lacunarity
, depth
, noise_basis
=nbasis
) * 0.5
584 elif ntype
in [1, 'ridged_multi_fractal']:
585 value
= ridged_multi_fractal(ncoords
, dimension
, lacunarity
, depth
, offset
, gain
, noise_basis
=nbasis
) * 0.5
587 elif ntype
in [2, 'hybrid_multi_fractal']:
588 value
= hybrid_multi_fractal(ncoords
, dimension
, lacunarity
, depth
, offset
, gain
, noise_basis
=nbasis
) * 0.5
590 elif ntype
in [3, 'hetero_terrain']:
591 value
= hetero_terrain(ncoords
, dimension
, lacunarity
, depth
, offset
, noise_basis
=nbasis
) * 0.25
593 elif ntype
in [4, 'fractal']:
594 value
= fractal(ncoords
, dimension
, lacunarity
, depth
, noise_basis
=nbasis
)
596 elif ntype
in [5, 'turbulence_vector']:
597 value
= turbulence_vector(ncoords
, depth
, hardnoise
, noise_basis
=nbasis
, amplitude_scale
=amp
, frequency_scale
=freq
)[0]
599 elif ntype
in [6, 'variable_lacunarity']:
600 value
= variable_lacunarity(ncoords
, distortion
, noise_type1
=nbasis
, noise_type2
=vlbasis
)
602 elif ntype
in [7, 'marble_noise']:
603 value
= marble_noise(
604 (ncoords
[0] - origin_x
+ x_offset
),
605 (ncoords
[1] - origin_y
+ y_offset
),
606 (ncoords
[2] - origin_z
+ z_offset
),
607 (origin
[0] + x_offset
, origin
[1] + y_offset
, origin
[2] + z_offset
), nsize
,
608 marbleshape
, marblebias
, marblesharpnes
,
609 distortion
, depth
, hardnoise
, nbasis
, amp
, freq
611 elif ntype
in [8, 'shattered_hterrain']:
612 value
= shattered_hterrain(ncoords
, dimension
, lacunarity
, depth
, offset
, distortion
, nbasis
)
614 elif ntype
in [9, 'strata_hterrain']:
615 value
= strata_hterrain(ncoords
, dimension
, lacunarity
, depth
, offset
, distortion
, nbasis
)
617 elif ntype
in [10, 'ant_turbulence']:
618 value
= ant_turbulence(ncoords
, depth
, hardnoise
, nbasis
, amp
, freq
, distortion
)
620 elif ntype
in [11, 'vl_noise_turbulence']:
621 value
= vl_noise_turbulence(ncoords
, distortion
, depth
, nbasis
, vlbasis
, hardnoise
, amp
, freq
)
623 elif ntype
in [12, 'vl_hTerrain']:
624 value
= vl_hTerrain(ncoords
, dimension
, lacunarity
, depth
, offset
, nbasis
, vlbasis
, distortion
)
626 elif ntype
in [13, 'distorted_heteroTerrain']:
627 value
= distorted_heteroTerrain(ncoords
, dimension
, lacunarity
, depth
, offset
, distortion
, nbasis
, vlbasis
)
629 elif ntype
in [14, 'double_multiFractal']:
630 value
= double_multiFractal(ncoords
, dimension
, lacunarity
, depth
, offset
, gain
, nbasis
, vlbasis
)
632 elif ntype
in [15, 'rocks_noise']:
633 value
= rocks_noise(ncoords
, depth
, hardnoise
, nbasis
, distortion
)
635 elif ntype
in [16, 'slick_rock']:
636 value
= slick_rock(ncoords
,dimension
, lacunarity
, depth
, offset
, gain
, distortion
, nbasis
, vlbasis
)
638 elif ntype
in [17, 'planet_noise']:
639 value
= planet_noise(ncoords
, depth
, hardnoise
, nbasis
)[2] * 0.5 + 0.5
641 elif ntype
in [18, 'blender_texture']:
642 if texture_name
!= "" and texture_name
in bpy
.data
.textures
:
643 value
= bpy
.data
.textures
[texture_name
].evaluate(ncoords
)[3]
651 if fx_type
in [0,"0"]:
655 fxcoords
= Trans_Effect((x
, y
, z
), fx_size
, (fx_loc_x
, fx_loc_y
))
656 effect
= Effect_Function(fxcoords
, fx_type
, fx_bias
, fx_turb
, fx_depth
, fx_frequency
, fx_amplitude
)
657 effect
= Height_Scale(effect
, fx_height
, fx_offset
, fx_invert
)
658 fxval
= Mix_Modes(val
, effect
, fx_mixfactor
, fx_mix_mode
)
662 value
= Height_Scale(value
, height
, height_offset
, height_invert
)
667 ratio_x
, ratio_y
= abs(x
) * 2 / meshsize_x
, abs(y
) * 2 / meshsize_y
669 sqrt(ratio_y
**falloffsize_y
),
670 sqrt(ratio_x
**falloffsize_x
),
671 sqrt(ratio_x
**falloffsize_x
+ ratio_y
**falloffsize_y
)
673 dist
= fallofftypes
[falloff
]
676 dist
= (dist
* dist
* (3 - 2 * dist
))
677 value
= (value
- value
* dist
) + edge_level
681 # Strata / terrace / layers
682 if stratatype
not in [0, "0"]:
683 if stratatype
in [1, "1"]:
684 strata
= strata
/ height
686 steps
= (sin(value
* strata
* pi
) * (0.1 / strata
* pi
))
687 value
= (value
* 0.5 + steps
* 0.5) * 2.0
689 elif stratatype
in [2, "2"]:
690 strata
= strata
/ height
691 steps
= -abs(sin(value
* strata
* pi
) * (0.1 / strata
* pi
))
692 value
= (value
* 0.5 + steps
* 0.5) * 2.0
694 elif stratatype
in [3, "3"]:
695 strata
= strata
/ height
696 steps
= abs(sin(value
* strata
* pi
) * (0.1 / strata
* pi
))
697 value
= (value
* 0.5 + steps
* 0.5) * 2.0
699 elif stratatype
in [4, "4"]:
700 strata
= strata
/ height
701 value
= int( value
* strata
) * 1.0 / strata
703 elif stratatype
in [5, "5"]:
704 strata
= strata
/ height
705 steps
= (int( value
* strata
) * 1.0 / strata
)
706 value
= (value
* (1.0 - 0.5) + steps
* 0.5)
708 # Clamp height min max
709 if (value
< minimum
):
711 if (value
> maximum
):