steam_example.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # Author: Shao Zhang and Phil Saltzman
  3. # Last Updated: 2015-03-13
  4. #
  5. # This tutorial shows how to take an existing particle effect taken from a
  6. # .ptf file and run it in a general Panda project.
  7. from direct.showbase.ShowBase import ShowBase
  8. from panda3d.core import TextNode
  9. from panda3d.core import AmbientLight, DirectionalLight
  10. from panda3d.core import LPoint3, LVector3
  11. from panda3d.core import Filename
  12. from panda3d.physics import BaseParticleEmitter, BaseParticleRenderer
  13. from panda3d.physics import PointParticleFactory, SpriteParticleRenderer
  14. from panda3d.physics import LinearNoiseForce, DiscEmitter
  15. from direct.particles.Particles import Particles
  16. from direct.particles.ParticleEffect import ParticleEffect
  17. from direct.particles.ForceGroup import ForceGroup
  18. from direct.gui.OnscreenText import OnscreenText
  19. import sys
  20. HELP_TEXT = """
  21. 1: Load Steam
  22. 2: Load Dust
  23. 3: Load Fountain
  24. 4: Load Smoke
  25. 5: Load Smokering
  26. 6: Load Fireish
  27. ESC: Quit
  28. """
  29. class ParticleDemo(ShowBase):
  30. def __init__(self):
  31. ShowBase.__init__(self)
  32. # Standard title and instruction text
  33. self.title = OnscreenText(
  34. text="Panda3D: Tutorial - Particles",
  35. parent=base.a2dBottomCenter,
  36. style=1, fg=(1, 1, 1, 1), pos=(0, 0.1), scale=.08)
  37. self.escapeEvent = OnscreenText(
  38. text=HELP_TEXT, parent=base.a2dTopLeft,
  39. style=1, fg=(1, 1, 1, 1), pos=(0.06, -0.06),
  40. align=TextNode.ALeft, scale=.05)
  41. # More standard initialization
  42. self.accept('escape', sys.exit)
  43. self.accept('1', self.loadParticleConfig, ['steam.ptf'])
  44. self.accept('2', self.loadParticleConfig, ['dust.ptf'])
  45. self.accept('3', self.loadParticleConfig, ['fountain.ptf'])
  46. self.accept('4', self.loadParticleConfig, ['smoke.ptf'])
  47. self.accept('5', self.loadParticleConfig, ['smokering.ptf'])
  48. self.accept('6', self.loadParticleConfig, ['fireish.ptf'])
  49. self.accept('escape', sys.exit)
  50. base.disableMouse()
  51. base.camera.setPos(0, -20, 2)
  52. base.camLens.setFov(25)
  53. base.setBackgroundColor(0, 0, 0)
  54. # This command is required for Panda to render particles
  55. base.enableParticles()
  56. self.t = loader.loadModel("teapot")
  57. self.t.setPos(0, 10, 0)
  58. self.t.reparentTo(render)
  59. self.setupLights()
  60. self.p = ParticleEffect()
  61. self.loadParticleConfig('steam.ptf')
  62. def loadParticleConfig(self, filename):
  63. # Start of the code from steam.ptf
  64. self.p.cleanup()
  65. self.p = ParticleEffect()
  66. self.p.loadConfig(Filename(filename))
  67. # Sets particles to birth relative to the teapot, but to render at
  68. # toplevel
  69. self.p.start(self.t)
  70. self.p.setPos(3.000, 0.000, 2.250)
  71. # Setup lighting
  72. def setupLights(self):
  73. ambientLight = AmbientLight("ambientLight")
  74. ambientLight.setColor((.4, .4, .35, 1))
  75. directionalLight = DirectionalLight("directionalLight")
  76. directionalLight.setDirection(LVector3(0, 8, -2.5))
  77. directionalLight.setColor((0.9, 0.8, 0.9, 1))
  78. # Set lighting on teapot so steam doesn't get affected
  79. self.t.setLight(self.t.attachNewNode(directionalLight))
  80. self.t.setLight(self.t.attachNewNode(ambientLight))
  81. demo = ParticleDemo()
  82. demo.run()