shadow_map.gd 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. extends Spatial
  2. # configure:
  3. # set all visible objects for volume lights, its VisualInstance Layer to 2
  4. export var color=Color("5b5aff")
  5. # this is not best solution, but it works
  6. # better solution can use single camera(instead of cubemap)
  7. # and make everything in DEPTH buffer
  8. # shader logic is "sampling depth arround light" in main camera view
  9. # minimal example of that logic https://www.shadertoy.com/view/XsKGRz
  10. # self https://github.com/danilw/godot-utils-and-other
  11. # Licence: no licence, use it as you wish.
  12. # twitter.com/AruGL
  13. # GLES2 build does not work in Web because this bug https://github.com/godotengine/godot/issues/36786
  14. # for Web-export used GLES3(WebGl2)
  15. # camera far
  16. const far=10
  17. onready var start_pos=self.get_node("../light_pos/light1").translation
  18. var iTime=0.0
  19. onready var cameras=Array()
  20. onready var control=self.get_node("../Control")
  21. func _ready():
  22. self.visible=true
  23. for a in self.get_child_count():
  24. if(self.get_child(a).get_child_count()>0):
  25. if(self.get_child(a).get_child(0) is Camera):
  26. cameras.append(self.get_child(a).get_child(0))
  27. for a in cameras:
  28. a.far=far
  29. a.translation=start_pos
  30. self.translation=start_pos
  31. update_color(color)
  32. self.get_node("../Control/elems/ColorPickerButton").color=color
  33. func update_color(colorx):
  34. self.get_node("OmniLight").light_color=colorx
  35. self.get_node("MeshInstance").material_override.set("shader_param/colorx",colorx)
  36. self.get_node("../Camera/vlights").material_override.set("shader_param/colorx",colorx)
  37. func _process(delta):
  38. if(!control.stop_all):
  39. iTime+=delta
  40. self.translation.x=start_pos.x+sin(iTime*0.5)
  41. self.translation.z=start_pos.z+2*cos(iTime*0.5)
  42. for a in cameras:
  43. a.translation.x=self.translation.x
  44. a.translation.z=self.translation.z