fireball.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. from random import choice
  3. from panda3d.core import Point3, Vec4
  4. from direct.showbase.ShowBase import ShowBase
  5. from direct.motiontrail.MotionTrail import MotionTrail
  6. from direct.interval.LerpInterval import LerpPosInterval, LerpHprInterval
  7. from direct.interval.LerpInterval import LerpScaleInterval
  8. from direct.interval.LerpInterval import LerpTexOffsetInterval
  9. from direct.interval.IntervalGlobal import Sequence
  10. base = ShowBase()
  11. base.set_background_color(0.1, 0.1, 0.1, 1)
  12. base.cam.set_pos(0, -128, 32)
  13. base.cam.look_at(render)
  14. flame_colors = (
  15. Vec4(1.0, 0.0, 0.0, 1),
  16. Vec4(1.0, 0.2, 0.0, 1),
  17. Vec4(1.0, 0.7, 0.0, 1),
  18. Vec4(0.0, 0.0, 0.2, 1),
  19. )
  20. # A NodePath, rotating in empty space.
  21. pivot = render.attach_new_node("pivot")
  22. pivot.hprInterval(3, (360, 0, 0)).loop()
  23. Sequence( # Bobs up and down
  24. LerpPosInterval(pivot, 0.3, (0, 0,-2), (0, 0, 1), blendType="easeInOut"),
  25. LerpPosInterval(pivot, 0.5, (0, 0, 1), (0, 0,-2), blendType="easeInOut")
  26. ).loop()
  27. # A little chunk of charcoal that rotates along the pivot with an offset.
  28. charcoal = loader.load_model("models/smiley").copy_to(pivot)
  29. charcoal.set_texture(loader.load_texture("models/plasma.png"), 1)
  30. charcoal.set_color(flame_colors[0] * 1.5)
  31. charcoal.set_x(-32)
  32. # It leaves a trail of flames.
  33. fire_trail = MotionTrail("fire trail", charcoal)
  34. fire_trail.register_motion_trail()
  35. fire_trail.geom_node_path.reparent_to(render)
  36. fire_trail.set_texture(loader.load_texture("models/plasma.png"))
  37. fire_trail.time_window = 3 # Length of trail
  38. # A circle as the trail's shape, by plotting a NodePath in a circle.
  39. center = render.attach_new_node("center")
  40. around = center.attach_new_node("around")
  41. around.set_z(1)
  42. res = 8 # Amount of angles in "circle". Higher is smoother.
  43. for i in range(res + 1):
  44. center.set_r((360 / res) * i)
  45. vertex_pos = around.get_pos(render)
  46. fire_trail.add_vertex(vertex_pos)
  47. start_color = flame_colors[i % len(flame_colors)] * 1.7
  48. end_color = Vec4(1, 1, 0, 1)
  49. fire_trail.set_vertex_color(i, start_color, end_color)
  50. '''
  51. # A simple flat line, tron lightcycle-style, would be like so:
  52. fire_trail.add_vertex(Point3(0, 0, 1))
  53. fire_trail.add_vertex(Point3(0, 0,-1))
  54. fire_trail.set_vertex_color(0, flame_colors[0], flame_colors[0])
  55. fire_trail.set_vertex_color(1, flame_colors[1], flame_colors[1])
  56. '''
  57. fire_trail.update_vertices()
  58. # Adding intervals to the trail to give it swoops and bends.
  59. LerpHprInterval(fire_trail, 2, (0, 0, -360)).loop()
  60. LerpTexOffsetInterval(fire_trail.geom_node_path, 4, (1, 1), (1, 0)).loop()
  61. Sequence( # Grow and shrink
  62. LerpScaleInterval(fire_trail, 0.3, 1.4, 0.4, blendType="easeInOut"),
  63. LerpScaleInterval(fire_trail, 0.5, 0.4, 1.4, blendType="easeInOut")
  64. ).loop()
  65. base.run()