main.py 3.7 KB

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