particle_builder.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """按模板 + 参数生成 Cocos ParticleSystem2D 配置(JSON)。
  2. 纯本地、无需 API。运行时把 JSON 喂给 ParticleSystem2D 即可(或转成 .plist)。
  3. """
  4. import json
  5. import os
  6. # 几套基础粒子模板(可继续加)。坐标/数值为 Cocos ParticleSystem2D 字段。
  7. TEMPLATES = {
  8. "rain": {
  9. "emitterType": 0, "duration": -1, "emissionRate": 80, "life": 2.2, "lifeVar": 0.4,
  10. "angle": 270, "angleVar": 10, "speed": 220, "speedVar": 40,
  11. "gravityY": -300, "gravityX": 0,
  12. "startSize": 36, "startSizeVar": 8, "endSize": 36,
  13. "startSpin": 0, "startSpinVar": 180, "endSpin": 0, "endSpinVar": 180,
  14. "posVarX": 360, "posVarY": 0,
  15. },
  16. "burst": {
  17. "emitterType": 0, "duration": 0.4, "emissionRate": 600, "life": 0.8, "lifeVar": 0.2,
  18. "angle": 90, "angleVar": 180, "speed": 320, "speedVar": 80,
  19. "gravityY": -120, "gravityX": 0,
  20. "startSize": 28, "startSizeVar": 10, "endSize": 0,
  21. "posVarX": 8, "posVarY": 8,
  22. },
  23. "glow": {
  24. "emitterType": 0, "duration": -1, "emissionRate": 24, "life": 1.4, "lifeVar": 0.3,
  25. "angle": 90, "angleVar": 360, "speed": 30, "speedVar": 10,
  26. "gravityY": 0, "gravityX": 0,
  27. "startSize": 60, "startSizeVar": 14, "endSize": 0,
  28. "posVarX": 30, "posVarY": 30,
  29. },
  30. "confetti": {
  31. "emitterType": 0, "duration": 0.6, "emissionRate": 300, "life": 2.0, "lifeVar": 0.5,
  32. "angle": 90, "angleVar": 60, "speed": 400, "speedVar": 120,
  33. "gravityY": -260, "gravityX": 0,
  34. "startSize": 24, "startSizeVar": 8, "endSize": 16,
  35. "startSpin": 0, "startSpinVar": 360, "endSpin": 0, "endSpinVar": 360,
  36. "posVarX": 20, "posVarY": 0,
  37. },
  38. }
  39. def build_particle(vfx_id, template, color, out_dir, texture="particle.png"):
  40. base = TEMPLATES.get(template)
  41. if base is None:
  42. raise ValueError(f"未知粒子模板: {template} (可选 {list(TEMPLATES)})")
  43. r, g, b = (color + [255, 255, 255])[:3]
  44. cfg = dict(base)
  45. cfg.update({
  46. "id": vfx_id,
  47. "texture": texture,
  48. "startColor": [r, g, b, 255],
  49. "startColorVar": [0, 0, 0, 0],
  50. "endColor": [r, g, b, 0],
  51. "endColorVar": [0, 0, 0, 0],
  52. "totalParticles": max(64, int(base["emissionRate"] * base["life"])),
  53. "blendFunc": {"src": 770, "dst": 1}, # 加色,适合金币/光效
  54. })
  55. os.makedirs(out_dir, exist_ok=True)
  56. path = os.path.join(out_dir, f"{vfx_id}.particle.json")
  57. with open(path, "w", encoding="utf-8") as f:
  58. json.dump(cfg, f, ensure_ascii=False, indent=2)
  59. return path