main.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. """)
  19. # Initialize the showbase
  20. ShowBase.__init__(self)
  21. # Increase camera FOV aswell as the far plane
  22. self.camLens.set_fov(90)
  23. self.camLens.set_near_far(0.1, 50000)
  24. # Construct the terrain
  25. self.terrain_node = ShaderTerrainMesh()
  26. # Set a heightfield, the heightfield should be a 16-bit png and
  27. # have a quadratic size of a power of two.
  28. self.terrain_node.heightfield_filename = "heightfield.png"
  29. # Set the target triangle width. For a value of 10.0 for example,
  30. # the terrain will attempt to make every triangle 10 pixels wide on screen.
  31. self.terrain_node.target_triangle_width = 10.0
  32. # Generate the terrain
  33. self.terrain_node.generate()
  34. # Attach the terrain to the main scene and set its scale
  35. self.terrain = self.render.attach_new_node(self.terrain_node)
  36. self.terrain.set_scale(1024, 1024, 100)
  37. self.terrain.set_pos(-512, -512, -70.0)
  38. # Set a shader on the terrain. The ShaderTerrainMesh only works with
  39. # an applied shader. You can use the shaders used here in your own shaders
  40. terrain_shader = Shader.load(Shader.SL_GLSL, "terrain.vert.glsl", "terrain.frag.glsl")
  41. self.terrain.set_shader(terrain_shader)
  42. self.terrain.set_shader_input("camera", self.camera)
  43. # Set some texture on the terrain
  44. grass_tex = self.loader.loadTexture("textures/grass.png")
  45. grass_tex.set_minfilter(SamplerState.FT_linear_mipmap_linear)
  46. grass_tex.set_anisotropic_degree(16)
  47. self.terrain.set_texture(grass_tex)
  48. # Load some skybox - you can safely ignore this code
  49. skybox = self.loader.loadModel("models/skybox.bam")
  50. skybox.reparent_to(self.render)
  51. skybox.set_scale(20000)
  52. skybox_texture = self.loader.loadTexture("textures/skybox.jpg")
  53. skybox_texture.set_minfilter(SamplerState.FT_linear)
  54. skybox_texture.set_magfilter(SamplerState.FT_linear)
  55. skybox_texture.set_wrap_u(SamplerState.WM_repeat)
  56. skybox_texture.set_wrap_v(SamplerState.WM_mirror)
  57. skybox_texture.set_anisotropic_degree(16)
  58. skybox.set_texture(skybox_texture)
  59. skybox_shader = Shader.load(Shader.SL_GLSL, "skybox.vert.glsl", "skybox.frag.glsl")
  60. skybox.set_shader(skybox_shader)
  61. demo = ShaderTerrainDemo()
  62. demo.run()