main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. # Author: tobspr
  3. #
  4. # Last Updated: 2016-04-30
  5. #
  6. # This tutorial provides an example of using the ShaderTerrainMesh class
  7. from direct.showbase.ShowBase import ShowBase
  8. from panda3d.core import ShaderTerrainMesh, Shader, load_prc_file_data
  9. from panda3d.core import SamplerState
  10. class ShaderTerrainDemo(ShowBase):
  11. def __init__(self):
  12. # Load some configuration variables, its important for this to happen
  13. # before the ShowBase is initialized
  14. load_prc_file_data("", """
  15. textures-power-2 none
  16. gl-coordinate-system default
  17. window-title Panda3D ShaderTerrainMesh Demo
  18. filled-wireframe-apply-shader true
  19. """)
  20. # Initialize the showbase
  21. ShowBase.__init__(self)
  22. # Increase camera FOV as well as the far plane
  23. self.camLens.set_fov(90)
  24. self.camLens.set_near_far(0.1, 50000)
  25. # Construct the terrain
  26. self.terrain_node = ShaderTerrainMesh()
  27. # Set a heightfield, the heightfield should be a 16-bit png and
  28. # have a quadratic size of a power of two.
  29. heightfield = self.loader.loadTexture("heightfield.png")
  30. heightfield.wrap_u = SamplerState.WM_clamp
  31. heightfield.wrap_v = SamplerState.WM_clamp
  32. self.terrain_node.heightfield = heightfield
  33. # Set the target triangle width. For a value of 10.0 for example,
  34. # the terrain will attempt to make every triangle 10 pixels wide on screen.
  35. self.terrain_node.target_triangle_width = 10.0
  36. # Generate the terrain
  37. self.terrain_node.generate()
  38. # Attach the terrain to the main scene and set its scale. With no scale
  39. # set, the terrain ranges from (0, 0, 0) to (1, 1, 1)
  40. self.terrain = self.render.attach_new_node(self.terrain_node)
  41. self.terrain.set_scale(1024, 1024, 100)
  42. self.terrain.set_pos(-512, -512, -70.0)
  43. # Set a shader on the terrain. The ShaderTerrainMesh only works with
  44. # an applied shader. You can use the shaders used here in your own application
  45. terrain_shader = Shader.load(Shader.SL_GLSL, "terrain.vert.glsl", "terrain.frag.glsl")
  46. self.terrain.set_shader(terrain_shader)
  47. self.terrain.set_shader_input("camera", self.camera)
  48. # Shortcut to view the wireframe mesh
  49. self.accept("f3", self.toggleWireframe)
  50. # Set some texture on the terrain
  51. grass_tex = self.loader.loadTexture("textures/grass.png")
  52. grass_tex.set_minfilter(SamplerState.FT_linear_mipmap_linear)
  53. grass_tex.set_anisotropic_degree(16)
  54. self.terrain.set_texture(grass_tex)
  55. # Load a skybox - you can safely ignore this code
  56. skybox = self.loader.loadModel("models/skybox.bam")
  57. skybox.reparent_to(self.render)
  58. skybox.set_scale(20000)
  59. skybox_texture = self.loader.loadTexture("textures/skybox.jpg")
  60. skybox_texture.set_minfilter(SamplerState.FT_linear)
  61. skybox_texture.set_magfilter(SamplerState.FT_linear)
  62. skybox_texture.set_wrap_u(SamplerState.WM_repeat)
  63. skybox_texture.set_wrap_v(SamplerState.WM_mirror)
  64. skybox_texture.set_anisotropic_degree(16)
  65. skybox.set_texture(skybox_texture)
  66. skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl", "skybox.frag.glsl")
  67. skybox.set_shader(skybox_shader)
  68. ShaderTerrainDemo().run()