make_particle_scenes.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python3
  2. """make_particle_scenes.py <out dir>: one small scene per particle initializer, operator and emitter that no installed
  3. workshop item uses (found with a coverage build), each a single particle system on a grey background, for
  4. tools/analysis/coverage_testscenes.sh. Parameters are the keys Data/Parsers/ObjectParser.cpp reads, missing ones take
  5. the parser's defaults. Usage: make_particle_scenes.py ~/.local/share/we_live/tests/particles"""
  6. import json
  7. import os
  8. import sys
  9. BASE_INITIALIZERS = [
  10. {"name": "lifetimerandom", "min": 2, "max": 3},
  11. {"name": "sizerandom", "min": 30, "max": 50},
  12. {"name": "velocityrandom", "min": "-60 -60 0", "max": "60 60 0"},
  13. {"name": "colorrandom", "min": "255 255 255", "max": "255 200 100"},
  14. ]
  15. BASE_OPERATORS = [{"name": "movement", "gravity": "0 -20 0"}, {"name": "alphafade", "fadeintime": 0.1}]
  16. BASE_EMITTER = {"name": "sphererandom", "rate": 40, "origin": "0 0 0", "distancemin": 10, "distancemax": 120}
  17. CONTROL_POINTS = [{"id": 0, "flags": 0, "offset": "0 0 0"}, {"id": 1, "flags": 0, "offset": "200 100 0"}]
  18. INITIALIZERS = {
  19. "hsvcolorrandom": {"huemin": 0, "huemax": 1, "saturationmin": 0.5, "saturationmax": 1, "valuemin": 0.5, "valuemax": 1,
  20. "huesteps": 4},
  21. "colorlist": {"colors": ["1 0 0", "0 1 0", "0 0 1"], "huenoise": 0.1, "saturationnoise": 0.1, "valuenoise": 0.1},
  22. "turbulentvelocityrandom": {"speedmin": 20, "speedmax": 100, "scale": 1, "timescale": 1, "phasemin": 0,
  23. "phasemax": 1, "offset": 0, "forward": "0 1 0", "right": "1 0 0"},
  24. "positionoffsetrandom": {"distance": 60, "scale": 1, "octaves": 2, "directions": "1 1 0", "timescale": 1},
  25. "alpharandom": {"min": 0.3, "max": 1},
  26. "rotationrandom": {"min": "0 0 0", "max": "0 0 3.14"},
  27. "angularvelocityrandom": {"min": "0 0 -2", "max": "0 0 2"},
  28. "mapsequencearoundcontrolpoint": {"controlpoint": 1, "count": 4, "speedmin": 1, "speedmax": 2, "axis": "0 0 1"},
  29. "mapsequencebetweencontrolpoints": {"count": 4, "limitbehavior": 0, "controlpoint": 1},
  30. "remapinitialvalue": {},
  31. }
  32. OPERATORS = {
  33. "angularmovement": {"drag": 0.1, "force": "0 0 5"},
  34. "sizechange": {"startvalue": 1, "endvalue": 0.2, "starttime": 0, "endtime": 1},
  35. "alphachange": {"startvalue": 1, "endvalue": 0, "starttime": 0, "endtime": 1},
  36. "colorchange": {"startvalue": "1 1 1", "endvalue": "1 0 0", "starttime": 0, "endtime": 1},
  37. "turbulence": {"scale": 0.01, "speedmin": 20, "speedmax": 80, "timescale": 1, "phasemin": 0, "phasemax": 1},
  38. "vortex_v2": {"controlpoint": 0, "axis": "0 0 1", "distanceinner": 50, "distanceouter": 300, "speedinner": 300,
  39. "speedouter": 50, "centerforce": 5, "ringradius": 100, "ringwidth": 50},
  40. "controlpointattract": {"controlpoint": 1, "scale": 100, "threshold": 300, "deletethreshold": 5},
  41. "oscillatealpha": {"frequencymin": 1, "frequencymax": 3, "scalemin": 0.2, "scalemax": 1, "phasemin": 0, "phasemax": 3},
  42. "oscillatesize": {"frequencymin": 1, "frequencymax": 3, "scalemin": 0.5, "scalemax": 1.5, "phasemin": 0, "phasemax": 3},
  43. "oscillateposition": {"frequencymin": 1, "frequencymax": 3, "scalemin": 5, "scalemax": 30, "phasemin": 0,
  44. "phasemax": 3, "mask": "1 1 0"},
  45. "capvelocity": {"maxspeed": 40},
  46. "boids": {"separationthreshold": 20, "neighborthreshold": 100, "maxspeed": 100, "separationfactor": 1,
  47. "alignmentfactor": 1, "cohesionfactor": 1},
  48. "remapvalue": {},
  49. "maintaindistancetocontrolpoint": {"controlpoint": 1, "distance": 100, "scale": 5},
  50. "maintaindistancebetweencontrolpoints": {"controlpoint": 0, "controlpoint2": 1, "distance": 100, "scale": 5},
  51. "reducemovementnearcontrolpoint": {"controlpoint": 1, "distance": 150, "scale": 0.5},
  52. "collisionsphere": {"controlpoint": 1, "radius": 80, "restitution": 0.8},
  53. "collisionbox": {"controlpoint": 1, "size": "100 100 100", "restitution": 0.8},
  54. "collisionbounds": {"controlpoint": 1, "size": "300 300 300", "restitution": 0.8},
  55. "collisionquad": {"controlpoint": 1, "size": "200 200", "restitution": 0.8},
  56. "drag": {"drag": 0.5},
  57. }
  58. EMITTERS = {
  59. "boxrandom": {"distancemin": "10 10 0", "distancemax": "150 100 0"},
  60. "sphererandom": {"distancemin": 0, "distancemax": 200, "directions": "1 1 0"},
  61. "randomposition": {},
  62. }
  63. def scene(objects):
  64. return {
  65. "version": 3,
  66. "camera": {"center": "0 0 -1", "eye": "0 0 0", "up": "0 1 0"},
  67. "general": {"orthogonalprojection": {"width": 1920, "height": 1080}, "clearcolor": "0.1 0.1 0.15",
  68. "bloom": False, "cameraparallax": False},
  69. "objects": objects,
  70. }
  71. def write(out, name, particle, extra=None):
  72. folder = os.path.join(out, name)
  73. os.makedirs(os.path.join(folder, "particles"), exist_ok=True)
  74. for filename, content in (extra or {}).items():
  75. json.dump(content, open(os.path.join(folder, "particles", filename), "w"), indent=1)
  76. json.dump({"file": "scene.json", "type": "scene", "title": "particle " + name, "general": {"properties": {}}},
  77. open(os.path.join(folder, "project.json"), "w"))
  78. json.dump(particle, open(os.path.join(folder, "particles", "test.json"), "w"), indent=1)
  79. background = {"id": 1, "name": "bg", "image": "models/util/solidlayer.json", "size": "1920 1080",
  80. "origin": "960 540 0", "color": "0.3 0.3 0.3", "alpha": 1.0, "visible": True}
  81. system = {"id": 2, "name": "system", "particle": "particles/test.json", "origin": "960 540 0", "scale": "1 1 1",
  82. "angles": "0 0 0", "visible": True, "instanceoverride": None}
  83. json.dump(scene([background, system]), open(os.path.join(folder, "scene.json"), "w"), indent=1)
  84. def particle(initializers=(), operators=(), emitter=None):
  85. return {
  86. "material": "materials/particle/halo.json",
  87. "maxcount": 200,
  88. "starttime": 2,
  89. "emitter": [emitter or BASE_EMITTER],
  90. "initializer": BASE_INITIALIZERS + [dict(name=name, **values) for name, values in initializers],
  91. "operator": BASE_OPERATORS + [dict(name=name, **values) for name, values in operators],
  92. "controlpoint": CONTROL_POINTS,
  93. }
  94. REMAP_VALUES = ["lifetimefraction", "maxlifetime", "size", "opacity", "speed", "rotation", "angularspeed",
  95. "distancetocontrolpoint", "positionbetweentwocontrolpoints", "runtime", "timeofday",
  96. "particlesystemtime", "layertime", "color", "position", "velocity", "controlpoint",
  97. "deltatocontrolpoint", "directiontocontrolpoint", "layerorigin"]
  98. EVENT_INPUTS = ["setcolor", "multiplycolor", "setopacity", "multiplyopacity", "setcoloropacity", "multiplycoloropacity",
  99. "setvelocity", "addvelocity", "setsize", "multiplysize", "setrotation", "addrotation",
  100. "setangularvelocity", "addangularvelocity"]
  101. EVENT_CHILD_TYPES = ["eventfollow", "eventspawn", "eventdeath"]
  102. REMAP_OPERATIONS = ["remap", "multiply", "add", "subtract"]
  103. REMAP_COMPONENTS = ["all", "x", "y", "z", "sum", "average", "max", "min"]
  104. REMAP_TRANSFORMS = ["none", "sine", "square", "saw", "triangle", "simplexnoise", "fbmnoise"]
  105. def remap_entries(count):
  106. """a deterministic sweep over inputs, outputs, operations, components and transforms, a few per particle system"""
  107. for i in range(count):
  108. yield {
  109. "input": REMAP_VALUES[i % len(REMAP_VALUES)],
  110. "output": REMAP_VALUES[(i * 7 + 2) % len(REMAP_VALUES)],
  111. "operation": REMAP_OPERATIONS[i % len(REMAP_OPERATIONS)],
  112. "inputcomponent": REMAP_COMPONENTS[(i * 3) % len(REMAP_COMPONENTS)],
  113. "outputcomponent": REMAP_COMPONENTS[(i * 5 + 1) % len(REMAP_COMPONENTS)],
  114. "transformfunction": REMAP_TRANSFORMS[i % len(REMAP_TRANSFORMS)],
  115. "inputrangemin": 0,
  116. "inputrangemax": 1,
  117. "outputrangemin": 0.2,
  118. "outputrangemax": 1.5,
  119. "inputcontrolpoint0": 0,
  120. "inputcontrolpoint1": 1,
  121. "outputcontrolpoint0": 0,
  122. "outputcontrolpoint1": 1,
  123. "flags": 1 + (i % 3),
  124. }
  125. def main():
  126. out = sys.argv[1]
  127. entries = list(remap_entries(160))
  128. for number in range(0, len(entries), 6):
  129. group = entries[number:number + 6]
  130. write(out, "remapvalue-%02d" % (number // 6), particle(operators=[("remapvalue", e) for e in group]))
  131. write(out, "remapinitial-%02d" % (number // 6), particle(initializers=[("remapinitialvalue", e) for e in group]))
  132. for mode in (1, 2, 3):
  133. write(out, "turbulent-audio-%d" % mode, particle(initializers=[("turbulentvelocityrandom", dict(
  134. INITIALIZERS["turbulentvelocityrandom"], audioprocessingmode=mode, audioprocessingbounds="0 1",
  135. audioprocessingexponent=1.5, audioprocessingfrequencystart=2, audioprocessingfrequencyend=30))]))
  136. for name, values in INITIALIZERS.items():
  137. write(out, "init-" + name, particle(initializers=[(name, values)]))
  138. for name, values in OPERATORS.items():
  139. write(out, "op-" + name, particle(operators=[(name, values)]))
  140. for flags in (1, 2, 3):
  141. write(out, "op-controlpointattract-flags%d" % flags,
  142. particle(operators=[("controlpointattract", dict(OPERATORS["controlpointattract"], flags=flags))]))
  143. for shape in ("collisionsphere", "collisionbox", "collisionbounds", "collisionquad"):
  144. for behavior in ("slide", "stop", "delete"):
  145. write(out, "op-%s-%s" % (shape, behavior), particle(operators=[
  146. (shape, dict(OPERATORS[shape], collisionbehavior=behavior, flags=3))]))
  147. write(out, "op-%s-follow" % shape, particle(operators=[(shape, dict(OPERATORS[shape], flags=1))]))
  148. write(out, "op-collisionplane", particle(operators=[
  149. ("collisionplane", {"plane": "0 1 0", "distance": -100, "restitution": 0.6, "collisionbehavior": "slide"})]))
  150. for name in ("mapsequencearoundcontrolpoint", "mapsequencebetweencontrolpoints"):
  151. write(out, "init-%s-mirror" % name, particle(initializers=[
  152. (name, dict(INITIALIZERS[name], limitbehavior="mirror", count=3))]))
  153. for name, values in EMITTERS.items():
  154. write(out, "emit-" + name, particle(emitter=dict(BASE_EMITTER, name=name, **values)))
  155. for number, mode in enumerate(EVENT_INPUTS):
  156. kind = EVENT_CHILD_TYPES[number % len(EVENT_CHILD_TYPES)]
  157. child = particle(initializers=[("inheritinitialvaluefromevent", {"input": mode})],
  158. operators=[("inheritvaluefromevent", {"input": mode, "blendinstart": 0.1, "blendinend": 0.3,
  159. "blendoutstart": 0.7, "blendoutend": 0.9})])
  160. child["maxcount"] = 60
  161. parent = particle()
  162. parent["children"] = [{"type": kind, "name": "particles/child.json", "maxcount": 20, "probability": 1.0,
  163. "origin": "10 0 0", "scale": "1 1 1", "angles": "0 0 0.3"}]
  164. write(out, "event-%s-%s" % (kind, mode), parent, {"child.json": child})
  165. write(out, "all-operators", particle(operators=list(OPERATORS.items())))
  166. write(out, "all-initializers", particle(initializers=list(INITIALIZERS.items())))
  167. if __name__ == "__main__":
  168. main()