Merge branch 'master' into blender2.8
[blender-addons.git] / ant_landscape / mesh_ant_displace.py
blobfb691abfc1f1889597e60c14ada2958e09783695
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 - Mesh Displace
20 # Jimmy Hazevoet
22 # ------------------------------------------------------------
23 # import modules
24 import bpy
25 from bpy.props import (
26 BoolProperty,
27 EnumProperty,
28 FloatProperty,
29 IntProperty,
30 StringProperty,
31 FloatVectorProperty,
33 from .ant_functions import (
34 draw_ant_refresh,
35 draw_ant_main,
36 draw_ant_noise,
37 draw_ant_displace,
39 from .ant_noise import noise_gen
41 # ------------------------------------------------------------
42 # Do vert displacement
43 class AntMeshDisplace(bpy.types.Operator):
44 bl_idname = "mesh.ant_displace"
45 bl_label = "Another Noise Tool - Displace"
46 bl_description = "Displace mesh vertices"
47 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
49 ant_terrain_name = StringProperty(
50 name="Name",
51 default="Landscape"
53 land_material = StringProperty(
54 name='Material',
55 default="",
56 description="Terrain material"
58 water_material = StringProperty(
59 name='Material',
60 default="",
61 description="Water plane material"
63 texture_block = StringProperty(
64 name="Texture",
65 default=""
67 at_cursor = BoolProperty(
68 name="Cursor",
69 default=True,
70 description="Place at cursor location",
72 smooth_mesh = BoolProperty(
73 name="Smooth",
74 default=True,
75 description="Shade smooth"
77 tri_face = BoolProperty(
78 name="Triangulate",
79 default=False,
80 description="Triangulate faces"
82 sphere_mesh = BoolProperty(
83 name="Sphere",
84 default=False,
85 description="Generate uv sphere - remove doubles when ready"
87 subdivision_x = IntProperty(
88 name="Subdivisions X",
89 default=128,
90 min=4,
91 max=6400,
92 description="Mesh X subdivisions"
94 subdivision_y = IntProperty(
95 default=128,
96 name="Subdivisions Y",
97 min=4,
98 max=6400,
99 description="Mesh Y subdivisions"
101 mesh_size = FloatProperty(
102 default=2.0,
103 name="Mesh Size",
104 min=0.01,
105 max=100000.0,
106 description="Mesh size"
108 mesh_size_x = FloatProperty(
109 default=2.0,
110 name="Mesh Size X",
111 min=0.01,
112 description="Mesh x size"
114 mesh_size_y = FloatProperty(
115 name="Mesh Size Y",
116 default=2.0,
117 min=0.01,
118 description="Mesh y size"
121 random_seed = IntProperty(
122 name="Random Seed",
123 default=0,
124 min=0,
125 description="Randomize noise origin"
127 noise_offset_x = FloatProperty(
128 name="Offset X",
129 default=0.0,
130 description="Noise X Offset"
132 noise_offset_y = FloatProperty(
133 name="Offset Y",
134 default=0.0,
135 description="Noise Y Offset"
137 noise_offset_z = FloatProperty(
138 name="Offset Z",
139 default=0.0,
140 description="Noise Z Offset"
142 noise_size_x = FloatProperty(
143 default=1.0,
144 name="Size X",
145 min=0.01,
146 max=1000.0,
147 description="Noise x size"
149 noise_size_y = FloatProperty(
150 name="Size Y",
151 default=1.0,
152 min=0.01,
153 max=1000.0,
154 description="Noise y size"
156 noise_size_z = FloatProperty(
157 name="Size Z",
158 default=1.0,
159 min=0.01,
160 max=1000.0,
161 description="Noise Z size"
163 noise_size = FloatProperty(
164 name="Noise Size",
165 default=0.25,
166 min=0.01,
167 max=1000.0,
168 description="Noise size"
170 noise_type = EnumProperty(
171 name="Noise Type",
172 default='hetero_terrain',
173 description="Noise type",
174 items = [
175 ('multi_fractal', "Multi Fractal", "Blender: Multi Fractal algorithm", 0),
176 ('ridged_multi_fractal', "Ridged MFractal", "Blender: Ridged Multi Fractal", 1),
177 ('hybrid_multi_fractal', "Hybrid MFractal", "Blender: Hybrid Multi Fractal", 2),
178 ('hetero_terrain', "Hetero Terrain", "Blender: Hetero Terrain", 3),
179 ('fractal', "fBm Fractal", "Blender: fBm - Fractional Browninian motion", 4),
180 ('turbulence_vector', "Turbulence", "Blender: Turbulence Vector", 5),
181 ('variable_lacunarity', "Distorted Noise", "Blender: Distorted Noise", 6),
182 ('marble_noise', "Marble", "A.N.T.: Marble Noise", 7),
183 ('shattered_hterrain', "Shattered hTerrain", "A.N.T.: Shattered hTerrain", 8),
184 ('strata_hterrain', "Strata hTerrain", "A.N.T: Strata hTerrain", 9),
185 ('ant_turbulence', "Another Noise", "A.N.T: Turbulence variation", 10),
186 ('vl_noise_turbulence', "vlNoise turbulence", "A.N.T: Real vlNoise turbulence", 11),
187 ('vl_hTerrain', "vlNoise hTerrain", "A.N.T: vlNoise hTerrain", 12),
188 ('distorted_heteroTerrain', "Distorted hTerrain", "A.N.T distorted hTerrain", 13),
189 ('double_multiFractal', "Double MultiFractal", "A.N.T: double multiFractal", 14),
190 ('rocks_noise', "Noise Rocks", "A.N.T: turbulence variation", 15),
191 ('slick_rock', "Slick Rock", "A.N.T: slick rock", 16),
192 ('planet_noise', "Planet Noise", "Planet Noise by: Farsthary", 17),
193 ('blender_texture', "Blender Texture - Texture Nodes", "Blender texture data block", 18)]
195 basis_type = EnumProperty(
196 name="Noise Basis",
197 default="0",
198 description="Noise basis algorithms",
199 items = [
200 ("0", "Blender", "Blender default noise", 0),
201 ("1", "Perlin", "Perlin noise", 1),
202 ("2", "New Perlin", "New Perlin noise", 2),
203 ("3", "Voronoi F1", "Voronoi F1", 3),
204 ("4", "Voronoi F2", "Voronoi F2", 4),
205 ("5", "Voronoi F3", "Voronoi F3", 5),
206 ("6", "Voronoi F4", "Voronoi F4", 6),
207 ("7", "Voronoi F2-F1", "Voronoi F2-F1", 7),
208 ("8", "Voronoi Crackle", "Voronoi Crackle", 8),
209 ("9", "Cell Noise", "Cell noise", 9)]
211 vl_basis_type = EnumProperty(
212 name="vlNoise Basis",
213 default="0",
214 description="VLNoise basis algorithms",
215 items = [
216 ("0", "Blender", "Blender default noise", 0),
217 ("1", "Perlin", "Perlin noise", 1),
218 ("2", "New Perlin", "New Perlin noise", 2),
219 ("3", "Voronoi F1", "Voronoi F1", 3),
220 ("4", "Voronoi F2", "Voronoi F2", 4),
221 ("5", "Voronoi F3", "Voronoi F3", 5),
222 ("6", "Voronoi F4", "Voronoi F4", 6),
223 ("7", "Voronoi F2-F1", "Voronoi F2-F1", 7),
224 ("8", "Voronoi Crackle", "Voronoi Crackle", 8),
225 ("9", "Cell Noise", "Cell noise", 9)]
227 distortion = FloatProperty(
228 name="Distortion",
229 default=1.0,
230 min=0.01,
231 max=100.0,
232 description="Distortion amount"
234 hard_noise = EnumProperty(
235 name="Soft Hard",
236 default="0",
237 description="Soft Noise, Hard noise",
238 items = [
239 ("0", "Soft", "Soft Noise", 0),
240 ("1", "Hard", "Hard noise", 1)]
242 noise_depth = IntProperty(
243 name="Depth",
244 default=8,
245 min=0,
246 max=16,
247 description="Noise Depth - number of frequencies in the fBm"
249 amplitude = FloatProperty(
250 name="Amp",
251 default=0.5,
252 min=0.01,
253 max=1.0,
254 description="Amplitude"
256 frequency = FloatProperty(
257 name="Freq",
258 default=2.0,
259 min=0.01,
260 max=5.0,
261 description="Frequency"
263 dimension = FloatProperty(
264 name="Dimension",
265 default=1.0,
266 min=0.01,
267 max=2.0,
268 description="H - fractal dimension of the roughest areas"
270 lacunarity = FloatProperty(
271 name="Lacunarity",
272 min=0.01,
273 max=6.0,
274 default=2.0,
275 description="Lacunarity - gap between successive frequencies"
277 offset = FloatProperty(
278 name="Offset",
279 default=1.0,
280 min=0.01,
281 max=6.0,
282 description="Offset - raises the terrain from sea level"
284 gain = FloatProperty(
285 name="Gain",
286 default=1.0,
287 min=0.01,
288 max=6.0,
289 description="Gain - scale factor"
291 marble_bias = EnumProperty(
292 name="Bias",
293 default="0",
294 description="Marble bias",
295 items = [
296 ("0", "Sin", "Sin", 0),
297 ("1", "Cos", "Cos", 1),
298 ("2", "Tri", "Tri", 2),
299 ("3", "Saw", "Saw", 3)]
301 marble_sharp = EnumProperty(
302 name="Sharp",
303 default="0",
304 description="Marble sharpness",
305 items = [
306 ("0", "Soft", "Soft", 0),
307 ("1", "Sharp", "Sharp", 1),
308 ("2", "Sharper", "Sharper", 2),
309 ("3", "Soft inv.", "Soft", 3),
310 ("4", "Sharp inv.", "Sharp", 4),
311 ("5", "Sharper inv.", "Sharper", 5)]
313 marble_shape = EnumProperty(
314 name="Shape",
315 default="0",
316 description="Marble shape",
317 items= [
318 ("0", "Default", "Default", 0),
319 ("1", "Ring", "Ring", 1),
320 ("2", "Swirl", "Swirl", 2),
321 ("3", "Bump", "Bump", 3),
322 ("4", "Wave", "Wave", 4),
323 ("5", "Z", "Z", 5),
324 ("6", "Y", "Y", 6),
325 ("7", "X", "X", 7)]
327 height = FloatProperty(
328 name="Height",
329 default=0.25,
330 min=-10000.0,
331 max=10000.0,
332 description="Noise intensity scale"
334 height_invert = BoolProperty(
335 name="Invert",
336 default=False,
337 description="Height invert",
339 height_offset = FloatProperty(
340 name="Offset",
341 default=0.0,
342 min=-10000.0,
343 max=10000.0,
344 description="Height offset"
347 fx_mixfactor = FloatProperty(
348 name="Mix Factor",
349 default=0.0,
350 min=-1.0,
351 max=1.0,
352 description="Effect mix factor: -1.0 = Noise, +1.0 = Effect"
354 fx_mix_mode = EnumProperty(
355 name="Effect Mix",
356 default="0",
357 description="Effect mix mode",
358 items = [
359 ("0", "Mix", "Mix", 0),
360 ("1", "Add", "Add", 1),
361 ("2", "Sub", "Subtract", 2),
362 ("3", "Mul", "Multiply", 3),
363 ("4", "Abs", "Absolute", 4),
364 ("5", "Scr", "Screen", 5),
365 ("6", "Mod", "Modulo", 6),
366 ("7", "Min", "Minimum", 7),
367 ("8", "Max", "Maximum", 8)
370 fx_type = EnumProperty(
371 name="Effect Type",
372 default="0",
373 description="Effect type",
374 items = [
375 ("0", "None", "No effect", 0),
376 ("1", "Gradient", "Gradient", 1),
377 ("2", "Waves", "Waves - Bumps", 2),
378 ("3", "Zigzag", "Zigzag", 3),
379 ("4", "Wavy", "Wavy", 4),
380 ("5", "Bump", "Bump", 5),
381 ("6", "Dots", "Dots", 6),
382 ("7", "Rings", "Rings", 7),
383 ("8", "Spiral", "Spiral", 8),
384 ("9", "Square", "Square", 9),
385 ("10", "Blocks", "Blocks", 10),
386 ("11", "Grid", "Grid", 11),
387 ("12", "Tech", "Tech", 12),
388 ("13", "Crackle", "Crackle", 13),
389 ("14", "Cracks", "Cracks", 14),
390 ("15", "Rock", "Rock noise", 15),
391 ("16", "Lunar", "Craters", 16),
392 ("17", "Cosine", "Cosine", 17),
393 ("18", "Spikey", "Spikey", 18),
394 ("19", "Stone", "Stone", 19),
395 ("20", "Flat Turb", "Flat turbulence", 20),
396 ("21", "Flat Voronoi", "Flat voronoi", 21)
399 fx_bias = EnumProperty(
400 name="Effect Bias",
401 default="0",
402 description="Effect bias type",
403 items = [
404 ("0", "Sin", "Sin", 0),
405 ("1", "Cos", "Cos", 1),
406 ("2", "Tri", "Tri", 2),
407 ("3", "Saw", "Saw", 3),
408 ("4", "None", "None", 4)
411 fx_turb = FloatProperty(
412 name="Distortion",
413 default=0.0,
414 min=0.0,
415 max=1000.0,
416 description="Effect turbulence distortion"
418 fx_depth = IntProperty(
419 name="Depth",
420 default=0,
421 min=0,
422 max=16,
423 description="Effect depth - number of frequencies"
425 fx_amplitude = FloatProperty(
426 name="Amp",
427 default=0.5,
428 min=0.01,
429 max=1.0,
430 description="Amplitude"
432 fx_frequency = FloatProperty(
433 name="Freq",
434 default=2.0,
435 min=0.01,
436 max=5.0,
437 description="Frequency"
439 fx_size = FloatProperty(
440 name="Effect Size",
441 default=1.0,
442 min=0.01,
443 max=1000.0,
444 description="Effect size"
446 fx_loc_x = FloatProperty(
447 name="Offset X",
448 default=0.0,
449 description="Effect x offset"
451 fx_loc_y = FloatProperty(
452 name="Offset Y",
453 default=0.0,
454 description="Effect y offset"
456 fx_height = FloatProperty(
457 name="Intensity",
458 default=1.0,
459 min=-1000.0,
460 max=1000.0,
461 description="Effect intensity scale"
463 fx_invert = BoolProperty(
464 name="Invert",
465 default=False,
466 description="Effect invert"
468 fx_offset = FloatProperty(
469 name="Offset",
470 default=0.0,
471 min=-1000.0,
472 max=1000.0,
473 description="Effect height offset"
476 edge_falloff = EnumProperty(
477 name="Falloff",
478 default="0",
479 description="Flatten edges",
480 items = [
481 ("0", "None", "None", 0),
482 ("1", "Y", "Y Falloff", 1),
483 ("2", "X", "X Falloff", 2),
484 ("3", "X Y", "X Y Falloff", 3)]
486 falloff_x = FloatProperty(
487 name="Falloff X",
488 default=4.0,
489 min=0.1,
490 max=100.0,
491 description="Falloff x scale"
493 falloff_y = FloatProperty(
494 name="Falloff Y",
495 default=4.0,
496 min=0.1,
497 max=100.0,
498 description="Falloff y scale"
500 edge_level = FloatProperty(
501 name="Edge Level",
502 default=0.0,
503 min=-10000.0,
504 max=10000.0,
505 description="Edge level, sealevel offset"
507 maximum = FloatProperty(
508 name="Maximum",
509 default=1.0,
510 min=-10000.0,
511 max=10000.0,
512 description="Maximum, flattens terrain at plateau level"
514 minimum = FloatProperty(
515 name="Minimum",
516 default=-1.0,
517 min=-10000.0,
518 max=10000.0,
519 description="Minimum, flattens terrain at seabed level"
521 vert_group = StringProperty(
522 name="Vertex Group",
523 default=""
525 strata = FloatProperty(
526 name="Amount",
527 default=5.0,
528 min=0.01,
529 max=1000.0,
530 description="Strata layers / terraces"
532 strata_type = EnumProperty(
533 name="Strata",
534 default="0",
535 description="Strata types",
536 items = [
537 ("0", "None", "No strata", 0),
538 ("1", "Smooth", "Smooth transitions", 1),
539 ("2", "Sharp Sub", "Sharp substract transitions", 2),
540 ("3", "Sharp Add", "Sharp add transitions", 3),
541 ("4", "Quantize", "Quantize", 4),
542 ("5", "Quantize Mix", "Quantize mixed", 5)]
544 water_plane = BoolProperty(
545 name="Water Plane",
546 default=False,
547 description="Add water plane"
549 water_level = FloatProperty(
550 name="Level",
551 default=0.01,
552 min=-10000.0,
553 max=10000.0,
554 description="Water level"
556 remove_double = BoolProperty(
557 name="Remove Doubles",
558 default=False,
559 description="Remove doubles"
561 direction = EnumProperty(
562 name="Direction",
563 default="NORMAL",
564 description="Displacement direction",
565 items = [
566 ("NORMAL", "Normal", "Displace along vertex normal direction", 0),
567 ("Z", "Z", "Displace in the Z direction", 1),
568 ("Y", "Y", "Displace in the Y direction", 2),
569 ("X", "X", "Displace in the X direction", 3)]
571 show_main_settings = BoolProperty(
572 name="Main Settings",
573 default=True,
574 description="Show settings"
576 show_noise_settings = BoolProperty(
577 name="Noise Settings",
578 default=True,
579 description="Show noise settings"
581 show_displace_settings = BoolProperty(
582 name="Displace Settings",
583 default=True,
584 description="Show terrain settings"
586 refresh = BoolProperty(
587 name="Refresh",
588 default=False,
589 description="Refresh"
591 auto_refresh = BoolProperty(
592 name="Auto",
593 default=False,
594 description="Automatic refresh"
597 def draw(self, context):
598 draw_ant_refresh(self, context)
599 draw_ant_noise(self, context, generate=False)
600 draw_ant_displace(self, context, generate=False)
603 @classmethod
604 def poll(cls, context):
605 ob = context.object
606 return (ob and ob.type == 'MESH')
609 def invoke(self, context, event):
610 self.refresh = True
611 return self.execute(context)
614 def execute(self, context):
615 if not self.refresh:
616 return {'PASS_THROUGH'}
618 # turn off undo
619 undo = bpy.context.user_preferences.edit.use_global_undo
620 bpy.context.user_preferences.edit.use_global_undo = False
622 ob = context.object
624 # Properties:
625 props = [
626 self.ant_terrain_name,
627 self.at_cursor,
628 self.smooth_mesh,
629 self.tri_face,
630 self.sphere_mesh,
631 self.land_material,
632 self.water_material,
633 self.texture_block,
634 self.subdivision_x,
635 self.subdivision_y,
636 self.mesh_size_x,
637 self.mesh_size_y,
638 self.mesh_size,
639 self.random_seed,
640 self.noise_offset_x,
641 self.noise_offset_y,
642 self.noise_offset_z,
643 self.noise_size_x,
644 self.noise_size_y,
645 self.noise_size_z,
646 self.noise_size,
647 self.noise_type,
648 self.basis_type,
649 self.vl_basis_type,
650 self.distortion,
651 self.hard_noise,
652 self.noise_depth,
653 self.amplitude,
654 self.frequency,
655 self.dimension,
656 self.lacunarity,
657 self.offset,
658 self.gain,
659 self.marble_bias,
660 self.marble_sharp,
661 self.marble_shape,
662 self.height,
663 self.height_invert,
664 self.height_offset,
665 self.maximum,
666 self.minimum,
667 self.edge_falloff,
668 self.edge_level,
669 self.falloff_x,
670 self.falloff_y,
671 self.strata_type,
672 self.strata,
673 self.water_plane,
674 self.water_level,
675 self.vert_group,
676 self.remove_double,
677 self.fx_mixfactor,
678 self.fx_mix_mode,
679 self.fx_type,
680 self.fx_bias,
681 self.fx_turb,
682 self.fx_depth,
683 self.fx_frequency,
684 self.fx_amplitude,
685 self.fx_size,
686 self.fx_loc_x,
687 self.fx_loc_y,
688 self.fx_height,
689 self.fx_offset,
690 self.fx_invert
693 # do displace
694 mesh = ob.data
696 if self.vert_group != "" and self.vert_group in ob.vertex_groups:
697 vertex_group = ob.vertex_groups[self.vert_group]
699 if vertex_group:
700 gi = vertex_group.index
701 if self.direction == "X":
702 for v in mesh.vertices:
703 for g in v.groups:
704 if g.group == gi:
705 v.co[0] += vertex_group.weight(v.index) * noise_gen(v.co, props)
707 if self.direction == "Y":
708 for v in mesh.vertices:
709 for g in v.groups:
710 if g.group == gi:
711 v.co[1] += vertex_group.weight(v.index) * noise_gen(v.co, props)
713 if self.direction == "Z":
714 for v in mesh.vertices:
715 for g in v.groups:
716 if g.group == gi:
717 v.co[2] += vertex_group.weight(v.index) * noise_gen(v.co, props)
719 else:
720 for v in mesh.vertices:
721 for g in v.groups:
722 if g.group == gi:
723 v.co += vertex_group.weight(v.index) * v.normal * noise_gen(v.co, props)
725 else:
726 if self.direction == "X":
727 for v in mesh.vertices:
728 v.co[0] += noise_gen(v.co, props)
730 elif self.direction == "Y":
731 for v in mesh.vertices:
732 v.co[1] += noise_gen(v.co, props)
734 elif self.direction == "Z":
735 for v in mesh.vertices:
736 v.co[2] += noise_gen(v.co, props)
738 else:
739 for v in mesh.vertices:
740 v.co += v.normal * noise_gen(v.co, props)
742 mesh.update()
744 if self.auto_refresh is False:
745 self.refresh = False
747 # restore pre operator undo state
748 context.user_preferences.edit.use_global_undo = undo
750 return {'FINISHED'}