scene.gd 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. extends Spatial
  2. # this is VERY BAD way to make portals, each portal render its scene in own viewport
  3. # rendering many portals at same time is big overhead
  4. # this can be optimized by creating simple "button" to enable portal
  5. # when player move close to door, this way will be rendered max 3 viewports at same time
  6. # I use it because it enought for this demo
  7. # why viewports per portal
  8. # because https://github.com/godotengine/godot/issues/19438
  9. # Godot do not implement Cull Mask for lights
  10. # if you do not need own unique light per portal scene
  11. # then all portals can exist in single viewport, single scene
  12. # (sorting by render priority outside/inside portal)
  13. # Panorama-shaders licence:
  14. # License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  15. # (credit) original shader author linked in *.shader files
  16. # same to static panorama images
  17. # this project will work in GLES3 render
  18. var mnodes=5
  19. var area_in_state=[false,false,false,false,false]
  20. var opt_portals=false #menu checkbox
  21. func _ready():
  22. pass
  23. func _process(delta):
  24. optimize_portals()
  25. portals_a_timers(delta)
  26. resolution_control()
  27. const STRETCH_MODE_NONE = 0
  28. const STRETCH_MODE_2D = 1
  29. const STRETCH_MODE_VIEWPORT = 2
  30. const STRETCH_ASPECT_IGNORE = 0
  31. const STRETCH_ASPECT_KEEP = 1
  32. var old_res=Vector2(1280,720)
  33. func resolution_control():
  34. var new_res=get_viewport().size
  35. if((int(old_res.x)!=int(new_res.x))||(int(old_res.y)!=int(new_res.y))):
  36. old_res=new_res
  37. for a in mnodes+1:
  38. get_node("p"+str(a)).size=new_res
  39. #get_tree().set_screen_stretch(STRETCH_MODE_NONE,STRETCH_ASPECT_IGNORE,Vector2(1280,720))
  40. func _on_Area_body_entered(body,id):
  41. if(body.is_in_group("spheres")):
  42. get_node("p"+str(id+1)+"/p"+str(id+1)+"/spheres/"+body.name).visible=true
  43. if(body.is_in_group("player")):
  44. get_node("sun_pos/b0/glow").material_override.render_priority=4
  45. get_node("sun_pos/b1/glow").material_override.render_priority=4
  46. get_node("sun_pos/b2/glow").material_override.render_priority=4
  47. var st=id
  48. for a in mnodes-1:
  49. st+=1
  50. if(st>=mnodes):st=0
  51. get_node("portals").get_child(st).get_node("wall/wall_out").material_override.render_priority=3
  52. get_node("portals").get_child(st).get_node("wall/wall_in").material_override.render_priority=1
  53. get_node("portals").get_child(st).get_node("wall/door_out").material_override.render_priority=4
  54. func _on_Area_body_exited(body,id):
  55. if(body.is_in_group("spheres")):
  56. get_node("p"+str(id+1)+"/p"+str(id+1)+"/spheres/"+body.name).visible=false
  57. if(body.is_in_group("player")):
  58. get_node("sun_pos/b0/glow").material_override.render_priority=11
  59. get_node("sun_pos/b1/glow").material_override.render_priority=11
  60. get_node("sun_pos/b2/glow").material_override.render_priority=11
  61. var st=id
  62. for a in mnodes-1:
  63. st+=1
  64. if(st>=mnodes):st=0
  65. get_node("portals").get_child(st).get_node("wall/wall_out").material_override.render_priority=10
  66. get_node("portals").get_child(st).get_node("wall/wall_in").material_override.render_priority=5
  67. get_node("portals").get_child(st).get_node("wall/door_out").material_override.render_priority=15
  68. func _on_Area2_body_entered(body):
  69. if(body.is_in_group("spheres")):
  70. body.visible=false
  71. func _on_Area2_body_exited(body):
  72. if(body.is_in_group("spheres")):
  73. body.visible=true
  74. var timers=[0,0,0,0,0]
  75. var iTime=0
  76. func portals_a_timers(delta):
  77. for a in mnodes:
  78. if(area_in_state[a]):
  79. timers[a]+=delta*0.5
  80. else:
  81. timers[a]=0
  82. get_node("portals").get_child(a).get_node("wall/door_out").material_override.set("shader_param/timer",timers[a])
  83. get_node("portals").get_child(a).get_node("wall/door_out").material_override.set("shader_param/is_opt",opt_portals)
  84. iTime+=delta
  85. func optimize_portals():
  86. if(!opt_portals):return
  87. if(area_in_state[0]):get_node("p1").set("render_target_update_mode",Viewport.UPDATE_WHEN_VISIBLE)
  88. else:get_node("p1").set("render_target_update_mode",Viewport.UPDATE_DISABLED)
  89. if(area_in_state[1]):get_node("p2").set("render_target_update_mode",Viewport.UPDATE_WHEN_VISIBLE)
  90. else:get_node("p2").set("render_target_update_mode",Viewport.UPDATE_DISABLED)
  91. if(area_in_state[2]):get_node("p3").set("render_target_update_mode",Viewport.UPDATE_WHEN_VISIBLE)
  92. else:get_node("p3").set("render_target_update_mode",Viewport.UPDATE_DISABLED)
  93. if(area_in_state[3]):get_node("p4").set("render_target_update_mode",Viewport.UPDATE_WHEN_VISIBLE)
  94. else:get_node("p4").set("render_target_update_mode",Viewport.UPDATE_DISABLED)
  95. if(area_in_state[4]):get_node("p5").set("render_target_update_mode",Viewport.UPDATE_WHEN_VISIBLE)
  96. else:get_node("p5").set("render_target_update_mode",Viewport.UPDATE_DISABLED)
  97. func _on_Area3_body_entered(body, id):
  98. if(body.is_in_group("player")):area_in_state[id]=true
  99. func _on_Area3_body_exited(body, id):
  100. if(body.is_in_group("player")):area_in_state[id]=false