Files
godot-demo-projects/3d/occlusion_culling_mesh_lod/door.gd
Hugo Locurcio 4f866f2a9a Add doors as an example of dynamic occluders in the Occlusion culling demo (#807)
The occluders don't actually move, but are toggled when the door starts
opening and finishes closing to avoid unnecessary BVH rebuilds.
2024-04-11 21:07:55 -07:00

31 lines
984 B
GDScript

extends Node3D
var open := false
func _input(event: InputEvent):
if event.is_action_pressed("toggle_doors"):
if open:
# Close the door.
# The occluder will be re-enabled when the animation ends
# using `_on_animation_player_animation_finished()`.
$AnimationPlayer.play_backwards("open")
open = false
else:
# Open the door.
$AnimationPlayer.play("open")
open = true
# Disable the occluder as soon as the door starts opening.
# The occluder is not part of the pivot to prevent it from having its
# position changed every frame, which causes the occlusion culling BVH
# to be rebuilt each frame. This causes a CPU performance penalty.
$OccluderInstance3D.visible = false
func _on_animation_player_animation_finished(_anim_name):
if not open:
# Re-enable the occluder when the door is done closing.
# To prevent overocclusion, the door must be fully closed before the occluder can be re-enabled.
$OccluderInstance3D.visible = true