main.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. from direct.showbase.ShowBase import ShowBase
  3. from panda3d.core import Filename, Texture
  4. from panda3d.core import AmbientLight, DirectionalLight, PointLight
  5. from panda3d.core import NodePath, TextNode
  6. from direct.task.Task import Task
  7. from direct.actor.Actor import Actor
  8. from direct.gui.OnscreenText import OnscreenText
  9. import sys
  10. import os
  11. import random
  12. # Function to put instructions on the screen.
  13. def addInstructions(pos, msg):
  14. return OnscreenText(text=msg, style=1, fg=(1, 1, 1, 1), scale=.05,
  15. shadow=(0, 0, 0, 1), parent=base.a2dTopLeft,
  16. pos=(0.08, -pos - 0.04), align=TextNode.ALeft)
  17. # Function to put title on the screen.
  18. def addTitle(text):
  19. return OnscreenText(text=text, style=1, fg=(1, 1, 1, 1), scale=.08,
  20. parent=base.a2dBottomRight, align=TextNode.ARight,
  21. pos=(-0.1, 0.09), shadow=(0, 0, 0, 1))
  22. random.seed()
  23. class TeapotOnTVDemo(ShowBase):
  24. def __init__(self):
  25. ShowBase.__init__(self)
  26. self.disableMouse()
  27. self.setBackgroundColor((0, 0, 0, 1))
  28. # Post the instructions.
  29. self.title = addTitle("Panda3D: Tutorial - Using Render-to-Texture")
  30. self.inst1 = addInstructions(0.06, "ESC: Quit")
  31. self.inst2 = addInstructions(0.12, "Up/Down: Zoom in/out on the Teapot")
  32. self.inst3 = addInstructions(0.18, "Left/Right: Move teapot left/right")
  33. self.inst4 = addInstructions(0.24, "V: View the render-to-texture results")
  34. # we now get buffer thats going to hold the texture of our new scene
  35. altBuffer = self.win.makeTextureBuffer("hello", 256, 256)
  36. # now we have to setup a new scene graph to make this scene
  37. altRender = NodePath("new render")
  38. # this takes care of setting up ther camera properly
  39. self.altCam = self.makeCamera(altBuffer)
  40. self.altCam.reparentTo(altRender)
  41. self.altCam.setPos(0, -10, 0)
  42. # get the teapot and rotates it for a simple animation
  43. self.teapot = loader.loadModel('teapot')
  44. self.teapot.reparentTo(altRender)
  45. self.teapot.setPos(0, 0, -1)
  46. self.teapot.hprInterval(1.5, (360, 360, 360)).loop()
  47. # put some lighting on the teapot
  48. dlight = DirectionalLight('dlight')
  49. alight = AmbientLight('alight')
  50. dlnp = altRender.attachNewNode(dlight)
  51. alnp = altRender.attachNewNode(alight)
  52. dlight.setColor((0.8, 0.8, 0.5, 1))
  53. alight.setColor((0.2, 0.2, 0.2, 1))
  54. dlnp.setHpr(0, -60, 0)
  55. altRender.setLight(dlnp)
  56. altRender.setLight(alnp)
  57. # Put lighting on the main scene
  58. plight = PointLight('plight')
  59. plnp = render.attachNewNode(plight)
  60. plnp.setPos(0, 0, 10)
  61. render.setLight(plnp)
  62. render.setLight(alnp)
  63. # Panda contains a built-in viewer that lets you view the results of
  64. # your render-to-texture operations. This code configures the viewer.
  65. self.accept("v", self.bufferViewer.toggleEnable)
  66. self.accept("V", self.bufferViewer.toggleEnable)
  67. self.bufferViewer.setPosition("llcorner")
  68. self.bufferViewer.setCardSize(1.0, 0.0)
  69. # Create the tv-men. Each TV-man will display the
  70. # offscreen-texture on his TV screen.
  71. self.tvMen = []
  72. self.makeTvMan(-5, 30, 1, altBuffer.getTexture(), 0.9)
  73. self.makeTvMan(5, 30, 1, altBuffer.getTexture(), 1.4)
  74. self.makeTvMan(0, 23, -3, altBuffer.getTexture(), 2.0)
  75. self.makeTvMan(-5, 20, -6, altBuffer.getTexture(), 1.1)
  76. self.makeTvMan(5, 18, -5, altBuffer.getTexture(), 1.7)
  77. self.accept("escape", sys.exit, [0])
  78. self.accept("arrow_up", self.zoomIn)
  79. self.accept("arrow_down", self.zoomOut)
  80. self.accept("arrow_left", self.moveLeft)
  81. self.accept("arrow_right", self.moveRight)
  82. def makeTvMan(self, x, y, z, tex, playrate):
  83. man = Actor()
  84. man.loadModel('models/mechman_idle')
  85. man.setPos(x, y, z)
  86. man.reparentTo(render)
  87. faceplate = man.find("**/faceplate")
  88. faceplate.setTexture(tex, 1)
  89. man.setPlayRate(playrate, "mechman_anim")
  90. man.loop("mechman_anim")
  91. self.tvMen.append(man)
  92. def zoomIn(self):
  93. self.altCam.setY(self.altCam.getY() * 0.9)
  94. def zoomOut(self):
  95. self.altCam.setY(self.altCam.getY() * 1.2)
  96. def moveLeft(self):
  97. self.altCam.setX(self.altCam.getX() + 1)
  98. def moveRight(self):
  99. self.altCam.setX(self.altCam.getX() - 1)
  100. demo = TeapotOnTVDemo()
  101. demo.run()