main.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. # As an optimization, set this to the maximum number of cameras
  19. # or lights that will be rendering the terrain at any given time.
  20. stm-max-views 8
  21. # Further optimize the performance by reducing this to the max
  22. # number of chunks that will be visible at any given time.
  23. stm-max-chunk-count 2048
  24. """)
  25. # Initialize the showbase
  26. ShowBase.__init__(self)
  27. # Increase camera FOV as well as the far plane
  28. self.camLens.set_fov(90)
  29. self.camLens.set_near_far(0.1, 50000)
  30. # Construct the terrain
  31. self.terrain_node = ShaderTerrainMesh()
  32. # Set a heightfield, the heightfield should be a 16-bit png and
  33. # have a quadratic size of a power of two.
  34. heightfield = self.loader.loadTexture("heightfield.png")
  35. heightfield.wrap_u = SamplerState.WM_clamp
  36. heightfield.wrap_v = SamplerState.WM_clamp
  37. self.terrain_node.heightfield = heightfield
  38. # Set the target triangle width. For a value of 10.0 for example,
  39. # the terrain will attempt to make every triangle 10 pixels wide on screen.
  40. self.terrain_node.target_triangle_width = 10.0
  41. # Generate the terrain
  42. self.terrain_node.generate()
  43. # Attach the terrain to the main scene and set its scale. With no scale
  44. # set, the terrain ranges from (0, 0, 0) to (1, 1, 1)
  45. self.terrain = self.render.attach_new_node(self.terrain_node)
  46. self.terrain.set_scale(1024, 1024, 100)
  47. self.terrain.set_pos(-512, -512, -70.0)
  48. # Set a shader on the terrain. The ShaderTerrainMesh only works with
  49. # an applied shader. You can use the shaders used here in your own application
  50. terrain_shader = Shader.load(Shader.SL_GLSL, "terrain.vert.glsl", "terrain.frag.glsl")
  51. self.terrain.set_shader(terrain_shader)
  52. self.terrain.set_shader_input("camera", self.camera)
  53. # Shortcut to view the wireframe mesh
  54. self.accept("f3", self.toggleWireframe)
  55. # Set some texture on the terrain
  56. grass_tex = self.loader.loadTexture("textures/grass.png")
  57. grass_tex.set_minfilter(SamplerState.FT_linear_mipmap_linear)
  58. grass_tex.set_anisotropic_degree(16)
  59. self.terrain.set_texture(grass_tex)
  60. # Load a skybox - you can safely ignore this code
  61. skybox = self.loader.loadModel("models/skybox.bam")
  62. skybox.reparent_to(self.render)
  63. skybox.set_scale(20000)
  64. skybox_texture = self.loader.loadTexture("textures/skybox.jpg")
  65. skybox_texture.set_minfilter(SamplerState.FT_linear)
  66. skybox_texture.set_magfilter(SamplerState.FT_linear)
  67. skybox_texture.set_wrap_u(SamplerState.WM_repeat)
  68. skybox_texture.set_wrap_v(SamplerState.WM_mirror)
  69. skybox_texture.set_anisotropic_degree(16)
  70. skybox.set_texture(skybox_texture)
  71. skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl", "skybox.frag.glsl")
  72. skybox.set_shader(skybox_shader)
  73. ShaderTerrainDemo().run()