main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. # Author: tobspr
  3. #
  4. # Last Updated: 2016-02-13
  5. #
  6. # This tutorial provides an example of using the ShaderTerrainMesh class
  7. import os, sys, math, random
  8. from direct.showbase.ShowBase import ShowBase
  9. from panda3d.core import ShaderTerrainMesh, Shader, load_prc_file_data
  10. from panda3d.core import SamplerState
  11. class ShaderTerrainDemo(ShowBase):
  12. def __init__(self):
  13. # Load some configuration variables, its important for this to happen
  14. # before the ShowBase is initialized
  15. load_prc_file_data("", """
  16. textures-power-2 none
  17. window-title Panda3D Shader Terrain Demo
  18. gl-coordinate-system default
  19. """)
  20. # Initialize the showbase
  21. ShowBase.__init__(self)
  22. # Increase camera FOV aswell 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. self.terrain_node.heightfield_filename = "heightfield.png"
  30. # Set the target triangle width. For a value of 10.0 for example,
  31. # the terrain will attempt to make every triangle 10 pixels wide on screen.
  32. self.terrain_node.target_triangle_width = 10.0
  33. # Generate the terrain
  34. self.terrain_node.generate()
  35. # Attach the terrain to the main scene and set its scale
  36. self.terrain = self.render.attach_new_node(self.terrain_node)
  37. self.terrain.set_scale(1024, 1024, 100)
  38. self.terrain.set_pos(-512, -512, -70.0)
  39. # Set a shader on the terrain. The ShaderTerrainMesh only works with
  40. # an applied shader. You can use the shaders used here in your own shaders
  41. terrain_shader = Shader.load(Shader.SL_GLSL, "terrain.vert.glsl", "terrain.frag.glsl")
  42. self.terrain.set_shader(terrain_shader)
  43. self.terrain.set_shader_input("camera", self.camera)
  44. # Set some texture on the terrain
  45. grass_tex = self.loader.loadTexture("textures/grass.png")
  46. grass_tex.set_minfilter(SamplerState.FT_linear_mipmap_linear)
  47. grass_tex.set_anisotropic_degree(16)
  48. self.terrain.set_texture(grass_tex)
  49. # Load some skybox - you can safely ignore this code
  50. skybox = self.loader.loadModel("models/skybox.bam")
  51. skybox.reparent_to(self.render)
  52. skybox.set_scale(20000)
  53. skybox_texture = self.loader.loadTexture("textures/skybox.jpg")
  54. skybox_texture.set_minfilter(SamplerState.FT_linear)
  55. skybox_texture.set_magfilter(SamplerState.FT_linear)
  56. skybox_texture.set_wrap_u(SamplerState.WM_repeat)
  57. skybox_texture.set_wrap_v(SamplerState.WM_mirror)
  58. skybox_texture.set_anisotropic_degree(16)
  59. skybox.set_texture(skybox_texture)
  60. skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl", "skybox.frag.glsl")
  61. skybox.set_shader(skybox_shader)
  62. demo = ShaderTerrainDemo()
  63. demo.run()