Addons
This commit is contained in:
1 parent
877262dfaf
commit
0abbcf96ff
346 files changed
+7047
No files matched your search
@@ -0,0 +1,161 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: advanced_portal_teleport.gd
|
||||
Description: An area which teleports a node through the parent node's portal. Checks entry velocity and handles RigidBody3D and CharacterBody3D physics. Can also handle a portal clone of the node if specified.
|
||||
Instructions: For detailed documentation, see the README or visit: https://github.com/Donitzo/godot-simple-portal-system
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends Area3D
|
||||
class_name AdvancedPortalTeleport
|
||||
|
||||
## Checks if the node is moving TOWARDS the portal before teleporting it.
|
||||
@export var velocity_check:bool = true
|
||||
## An additional velocity push given to RigidBody3Ds/CharacterBody3D exiting the portal.
|
||||
@export var exit_push_velocity:float = 0
|
||||
## Seconds to keep portal clones visible after the node leaves the teleporter.
|
||||
@export var clone_keep_alive_seconds:float = 0.1
|
||||
|
||||
var _parent_portal:Portal
|
||||
|
||||
# Info about the nodes currently crossing the portal
|
||||
var _crossing_nodes:Array = []
|
||||
|
||||
func _ready() -> void:
|
||||
_parent_portal = get_parent() as Portal
|
||||
if _parent_portal == null:
|
||||
push_error("The PortalTeleport \"%s\" is not a child of a Portal instance" % name)
|
||||
|
||||
connect("area_entered", _on_area_entered)
|
||||
connect("area_exited", _on_area_exited)
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
# Update nodes crossing the portal
|
||||
for i in range(_crossing_nodes.size() - 1, -1, -1):
|
||||
var crossing_node:Dictionary = _crossing_nodes[i]
|
||||
|
||||
if not is_instance_valid(crossing_node.node):
|
||||
# Node has been freed, remove crossing_node
|
||||
_crossing_nodes.remove_at(i)
|
||||
continue
|
||||
|
||||
# If the portal has yet to leave the enter portal area, try to teleport it
|
||||
if not crossing_node.left and _try_teleport(_crossing_nodes[i]):
|
||||
# Switch portals so that the portal clone is placed at the entrance portal instead
|
||||
crossing_node.clone_portal = _parent_portal.exit_portal
|
||||
crossing_node.left = true
|
||||
|
||||
# Move the clone to the exit portal
|
||||
if crossing_node.clone != null and crossing_node.clone_portal != null:
|
||||
crossing_node.clone.global_transform = crossing_node.clone_portal.real_to_exit_transform(crossing_node.node.global_transform)
|
||||
|
||||
# If the node has left the enter portal, keep it a bit longer before erasing it
|
||||
if crossing_node.left:
|
||||
crossing_node.keep_alive_seconds -= delta
|
||||
if crossing_node.keep_alive_seconds <= 0:
|
||||
# Hide portal clone
|
||||
if crossing_node.clone != null:
|
||||
crossing_node.clone.visible = false
|
||||
|
||||
_crossing_nodes.remove_at(i)
|
||||
|
||||
# Try to teleport the crossing node, and return false otherwise
|
||||
func _try_teleport(crossing_node:Dictionary) -> bool:
|
||||
var node:Node3D = crossing_node.node
|
||||
|
||||
# Check if the node is moving towards the portal
|
||||
if velocity_check:
|
||||
if node is RigidBody3D:
|
||||
var local_velocity:Vector3 = _parent_portal.global_transform.basis.inverse() * node.linear_velocity
|
||||
if local_velocity.z >= 0:
|
||||
return false
|
||||
elif node is CharacterBody3D:
|
||||
var local_velocity:Vector3 = _parent_portal.global_transform.basis.inverse() * node.velocity
|
||||
if local_velocity.z >= 0:
|
||||
return false
|
||||
else:
|
||||
var last_position = crossing_node.position
|
||||
crossing_node.position = _parent_portal.to_local(node.global_position)
|
||||
if last_position == null or last_position.z <= crossing_node.position.z:
|
||||
return false
|
||||
|
||||
# Handle RigidBody3D physics
|
||||
if node is RigidBody3D:
|
||||
# Rotate rotation and velocity
|
||||
node.linear_velocity = _parent_portal.real_to_exit_direction(node.linear_velocity)
|
||||
node.angular_velocity *= _parent_portal.real_to_exit_transform(Transform3D.IDENTITY).basis.inverse()
|
||||
|
||||
# Additional push when exiting the portal
|
||||
if exit_push_velocity > 0:
|
||||
var exit_forward:Vector3 = _parent_portal.exit_portal.global_transform.basis.z.normalized()
|
||||
node.linear_velocity += exit_forward * exit_push_velocity
|
||||
|
||||
# Handle CharacterBody3D physics
|
||||
elif node is CharacterBody3D:
|
||||
# Rotate velocity
|
||||
node.velocity = _parent_portal.real_to_exit_direction(node.velocity)
|
||||
|
||||
# Additional push when exiting the portal
|
||||
if exit_push_velocity > 0:
|
||||
var exit_forward:Vector3 = _parent_portal.exit_portal.global_transform.basis.z.normalized()
|
||||
node.velocity += exit_forward * exit_push_velocity
|
||||
|
||||
# Transform the position and orientation
|
||||
node.global_transform = _parent_portal.real_to_exit_transform(node.global_transform)
|
||||
|
||||
return true
|
||||
|
||||
func _on_area_entered(area:Area3D) -> void:
|
||||
if area.has_meta("teleportable_root"):
|
||||
# The node may not teleport immediately if it's not heading TOWARDS the portal,
|
||||
# so we keep a reference to it until it teleports or leaves.
|
||||
# This also allows us to hide its portal clone after it leaves.
|
||||
|
||||
var root:Node3D = area.get_node(area.get_meta("teleportable_root"))
|
||||
|
||||
var crossing_node:Dictionary
|
||||
if root.has_meta("crossing_node"):
|
||||
# Node is crossing another portal, erase it from that portal and start using this one instead
|
||||
crossing_node = root.get_meta("crossing_node")
|
||||
crossing_node.teleporter._crossing_nodes.erase(crossing_node)
|
||||
else:
|
||||
# First portal
|
||||
var clone:Node3D = area.get_node(area.get_meta("portal_clone")) if area.has_meta("portal_clone") else null
|
||||
crossing_node = {
|
||||
"node": root,
|
||||
"clone": clone,
|
||||
"clone_portal": null,
|
||||
"teleporter": null,
|
||||
"left": false,
|
||||
"keep_alive_seconds": 0.0,
|
||||
"position": null,
|
||||
}
|
||||
root.set_meta("crossing_node", crossing_node)
|
||||
|
||||
crossing_node.clone_portal = _parent_portal
|
||||
crossing_node.teleporter = self
|
||||
crossing_node.left = false
|
||||
crossing_node.keep_alive_seconds = clone_keep_alive_seconds
|
||||
crossing_node.position = null
|
||||
|
||||
# Show portal clone if it exists
|
||||
if crossing_node.clone != null:
|
||||
crossing_node.clone.visible = true
|
||||
|
||||
# Keep track of the node in this portal
|
||||
_crossing_nodes.push_back(crossing_node)
|
||||
|
||||
# Try an initial teleport
|
||||
if _try_teleport(crossing_node):
|
||||
crossing_node.clone_portal = _parent_portal.exit_portal
|
||||
crossing_node.left = true
|
||||
|
||||
func _on_area_exited(area:Area3D) -> void:
|
||||
if area.has_meta("teleportable_root"):
|
||||
var root:Node3D = area.get_node(area.get_meta("teleportable_root"))
|
||||
var crossing_node:Dictionary = root.get_meta("crossing_node")
|
||||
|
||||
if crossing_node.teleporter == self:
|
||||
# The node left the enter portal without teleporting (but don't erase it yet)
|
||||
crossing_node.left = true
|
||||
@@ -0,0 +1 @@
|
||||
uid://dssmha8ry203y
|
||||
@@ -0,0 +1,95 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: collision_disable_area.gd
|
||||
Description: An area in which collisions are disabled for physics bodies based on the "disabled_collision_masks" metadata array.
|
||||
Instructions: For detailed documentation, see the README or visit: https://github.com/Donitzo/godot-simple-portal-system
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends Area3D
|
||||
class_name CollisionDisableArea
|
||||
|
||||
## Seconds until collisions are re-enabled after the body leaves the area.
|
||||
@export var re_enable_delay_seconds:float = 0.1
|
||||
|
||||
# Info about the disabled bodies
|
||||
var _disables:Array = []
|
||||
|
||||
func _ready() -> void:
|
||||
connect("body_entered", _on_body_entered)
|
||||
connect("body_exited", _on_body_exited)
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
for i in range(_disables.size() - 1, -1, -1):
|
||||
var disable_info:Dictionary = _disables[i]
|
||||
|
||||
if not is_instance_valid(disable_info.body):
|
||||
# Body has been freed, remove disable info
|
||||
_disables.remove_at(i)
|
||||
continue
|
||||
|
||||
if not disable_info.left:
|
||||
# The body has yet to leave the area
|
||||
continue
|
||||
|
||||
# The body left the area, so reduce its timeout before removal
|
||||
disable_info.seconds_until_enable -= delta
|
||||
if disable_info.seconds_until_enable > 0:
|
||||
continue
|
||||
|
||||
_disables.remove_at(i)
|
||||
|
||||
# Re-enable collision masks
|
||||
for layer_number in disable_info.disabled_layers:
|
||||
# Only consider layers which were enabled to begin with
|
||||
if disable_info.disable_count.has(layer_number):
|
||||
# Decrement disables so only the final area actually disables the mask
|
||||
disable_info.disable_count[layer_number] -= 1
|
||||
if disable_info.disable_count[layer_number] == 0:
|
||||
# Final disable, so re-enable the collision mask
|
||||
disable_info.body.set_collision_mask_value(layer_number, true)
|
||||
|
||||
func _on_body_entered(body:PhysicsBody3D) -> void:
|
||||
if not body.has_meta("disabled_collision_masks"):
|
||||
return
|
||||
|
||||
# Is the body already disabled by this area?
|
||||
for disable_info in _disables:
|
||||
if disable_info.body == body:
|
||||
# Reset left and timeout
|
||||
disable_info.left = false
|
||||
disable_info.seconds_until_enable = re_enable_delay_seconds
|
||||
return
|
||||
|
||||
# Keep track of the number of times each collision mask is disabled in a meta field
|
||||
if not body.has_meta("collision_disable_count"):
|
||||
body.set_meta("collision_disable_count", {})
|
||||
var disable_count:Dictionary = body.get_meta("collision_disable_count")
|
||||
|
||||
# Disable the collision masks specified in the "disabled_collision_masks" metadata array
|
||||
var disabled_layers:Array = body.get_meta("disabled_collision_masks")
|
||||
for layer_number in disabled_layers:
|
||||
# Only consider layers which were enabled to begin with
|
||||
if disable_count.has(layer_number) or body.get_collision_mask_value(layer_number):
|
||||
# Increment disables so only the first area actually disables the mask
|
||||
disable_count[layer_number] = 1 if not disable_count.has(layer_number) else disable_count[layer_number] + 1
|
||||
if disable_count[layer_number] == 1:
|
||||
# First disable, so disable the collision mask
|
||||
body.set_collision_mask_value(layer_number, false)
|
||||
|
||||
# Keep a reference to all current disables in the area
|
||||
_disables.push_back({
|
||||
"body": body,
|
||||
"disabled_layers": disabled_layers,
|
||||
"disable_count": disable_count,
|
||||
"seconds_until_enable": re_enable_delay_seconds,
|
||||
"left": false,
|
||||
})
|
||||
|
||||
func _on_body_exited(body:PhysicsBody3D) -> void:
|
||||
if body.has_meta("disabled_collision_masks"):
|
||||
for disable_info in _disables:
|
||||
if disable_info.body == body:
|
||||
# Mark the body as having left the area
|
||||
disable_info.left = true
|
||||
@@ -0,0 +1 @@
|
||||
uid://b1ewd8uk44csw
|
||||
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: hoverable.gd
|
||||
Description: A hoverable object.
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends RigidBody3D
|
||||
class_name Hoverable
|
||||
|
||||
@onready var _mesh:MeshInstance3D = $Mesh
|
||||
|
||||
var hovered:bool
|
||||
|
||||
var _strength:float
|
||||
|
||||
func _ready() -> void:
|
||||
_mesh.material_override = _mesh.mesh.surface_get_material(0).duplicate()
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
_strength = min(1, _strength + delta * 10) if hovered else max(0, _strength - delta * 10)
|
||||
|
||||
_mesh.material_override.albedo_color = lerp(Color.WHITE, Color.RED, _strength)
|
||||
|
||||
hovered = false
|
||||
@@ -0,0 +1 @@
|
||||
uid://y4qe2vhyrnu8
|
||||
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: linerenderer.gd
|
||||
Description: Line drawing helper based on ImmediateMesh.
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends MeshInstance3D
|
||||
class_name LineRenderer
|
||||
|
||||
@export var material:Material
|
||||
|
||||
var _lines:Array
|
||||
var _dirty:bool
|
||||
|
||||
func _ready() -> void:
|
||||
mesh = ImmediateMesh.new()
|
||||
|
||||
func add_line(from:Vector3, to:Vector3, color:Color = Color.WHITE) -> void:
|
||||
_lines.push_back([from, to, color])
|
||||
_dirty = true
|
||||
|
||||
func clear_lines() -> void:
|
||||
_lines.clear()
|
||||
_dirty = true
|
||||
|
||||
func _process(_delta:float) -> void:
|
||||
if not _dirty:
|
||||
return
|
||||
_dirty = false
|
||||
|
||||
mesh.clear_surfaces()
|
||||
|
||||
mesh.surface_begin(PrimitiveMesh.PRIMITIVE_LINES, material)
|
||||
|
||||
for line in _lines:
|
||||
mesh.surface_set_color(line[2])
|
||||
mesh.surface_add_vertex(to_local(line[0]))
|
||||
mesh.surface_add_vertex(to_local(line[1]))
|
||||
|
||||
mesh.surface_end()
|
||||
@@ -0,0 +1 @@
|
||||
uid://ck0ext0mpgnun
|
||||
@@ -0,0 +1,114 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: player.gd
|
||||
Description: Simple controllable player with raycaster.
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends MeshInstance3D
|
||||
|
||||
const _MOUSE_PAN_THRESHOLD:float = 0.4;
|
||||
const _MOUSE_PAN_SPEED:float = 200
|
||||
|
||||
const _MOVE_SPEED:float = 4
|
||||
|
||||
var _mouse_look:Vector2
|
||||
|
||||
@onready var _line_renderer:LineRenderer = $"../LineRenderer"
|
||||
@onready var _camera:Camera3D = $Camera
|
||||
@onready var _pip_camera:Camera3D = $PipViewport1/Camera
|
||||
|
||||
@onready var _handle_raycast_callable:Callable = Callable(self, "_handle_raycast")
|
||||
|
||||
func _ready() -> void:
|
||||
set_layer_mask_value(1, false)
|
||||
set_layer_mask_value(2, true)
|
||||
|
||||
_camera.set_cull_mask_value(2, false)
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
var viewport:Viewport = get_viewport()
|
||||
var mouse_position:Vector2 = viewport.get_mouse_position()
|
||||
var viewport_size:Vector2i = viewport.size
|
||||
var normalized_mouse_position:Vector2 = mouse_position / Vector2(viewport_size)
|
||||
|
||||
var horizontal_speed:float = 0
|
||||
var vertical_speed:float = 0
|
||||
|
||||
if normalized_mouse_position.x > 0 and normalized_mouse_position.x < 1 and\
|
||||
normalized_mouse_position.y > 0 and normalized_mouse_position.y < 1:
|
||||
if normalized_mouse_position.x < _MOUSE_PAN_THRESHOLD:
|
||||
horizontal_speed = lerp(0, 1, _MOUSE_PAN_THRESHOLD - normalized_mouse_position.x)
|
||||
elif normalized_mouse_position.x > 1 - _MOUSE_PAN_THRESHOLD:
|
||||
horizontal_speed = lerp(0, -1, normalized_mouse_position.x - (1.0 - _MOUSE_PAN_THRESHOLD))
|
||||
if normalized_mouse_position.y < _MOUSE_PAN_THRESHOLD:
|
||||
vertical_speed = lerp(0, 1, _MOUSE_PAN_THRESHOLD - normalized_mouse_position.y)
|
||||
elif normalized_mouse_position.y > 1 - _MOUSE_PAN_THRESHOLD:
|
||||
vertical_speed = lerp(0, -1, normalized_mouse_position.y - (1.0 - _MOUSE_PAN_THRESHOLD))
|
||||
|
||||
_mouse_look.x = fmod(_mouse_look.x + horizontal_speed * _MOUSE_PAN_SPEED * delta + 360, 360)
|
||||
_mouse_look.y = clamp(_mouse_look.y + vertical_speed * _MOUSE_PAN_SPEED * delta, -80, 80)
|
||||
|
||||
rotation_degrees = Vector3(0, _mouse_look.x, 0)
|
||||
_camera.rotation_degrees = Vector3(_mouse_look.y, 0, 0)
|
||||
|
||||
var right:Vector3 = (global_transform.basis.x * Vector3(1, 0, 1)).normalized()
|
||||
var forward:Vector3 = (global_transform.basis.z * Vector3(1, 0, 1)).normalized()
|
||||
|
||||
if Input.is_key_pressed(KEY_LEFT) or Input.is_key_pressed(KEY_A):
|
||||
global_translate(-right * _MOVE_SPEED * delta)
|
||||
if Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_D):
|
||||
global_translate(right * _MOVE_SPEED * delta)
|
||||
if Input.is_key_pressed(KEY_UP) or Input.is_key_pressed(KEY_W):
|
||||
global_translate(-forward * _MOVE_SPEED * delta)
|
||||
if Input.is_key_pressed(KEY_DOWN) or Input.is_key_pressed(KEY_S):
|
||||
global_translate(forward * _MOVE_SPEED * delta)
|
||||
|
||||
if abs(position.z) > 4.4:
|
||||
position.x = clamp(position.x, -0.75, 0.75)
|
||||
else:
|
||||
position.x = clamp(position.x, -3.8, 3.8)
|
||||
if abs(position.x) > 0.75:
|
||||
position.z = clamp(position.z, -3.8, 3.8)
|
||||
|
||||
_pip_camera.global_position = _camera.to_global(Vector3(0, 1.5, 1.5))
|
||||
_pip_camera.global_rotation = _camera.global_rotation
|
||||
|
||||
var origin:Vector3 = _camera.project_ray_origin(mouse_position)
|
||||
var end:Vector3 = origin + _camera.project_ray_normal(mouse_position) * 100
|
||||
|
||||
_line_renderer.clear_lines()
|
||||
|
||||
Portal.raycast(get_tree(), origin, (end - origin).normalized(), _handle_raycast_callable)
|
||||
|
||||
func _handle_raycast(from:Vector3, dir:Vector3, segment_distance:float, _recursive_distance:float, recursions:int) -> bool:
|
||||
var distance:float = min(100, segment_distance)
|
||||
var target:Vector3 = from + dir * distance
|
||||
|
||||
_line_renderer.add_line(from, target, Color.GREEN)
|
||||
for i in 16:
|
||||
_line_renderer.add_line(target, target + Vector3(
|
||||
randf_range(-0.1, 0.1),
|
||||
randf_range(-0.1, 0.1),
|
||||
randf_range(-0.1, 0.1)), Color.RED)
|
||||
if recursions > 0:
|
||||
_line_renderer.add_line(from, from + Vector3(
|
||||
randf_range(-0.1, 0.1),
|
||||
randf_range(-0.1, 0.1),
|
||||
randf_range(-0.1, 0.1)), Color.BLUE)
|
||||
|
||||
var space_state:PhysicsDirectSpaceState3D = get_world_3d().direct_space_state
|
||||
var query:PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.create(from, from + dir * distance)
|
||||
var result:Dictionary = space_state.intersect_ray(query)
|
||||
if not result.is_empty() and result.collider is Hoverable:
|
||||
result.collider.hovered = true
|
||||
for i in 16:
|
||||
_line_renderer.add_line(result.position, result.position + Vector3(
|
||||
randf_range(-0.1, 0.1),
|
||||
randf_range(-0.1, 0.1),
|
||||
randf_range(-0.1, 0.1)), Color.GREEN)
|
||||
|
||||
return true
|
||||
|
||||
return false
|
||||
@@ -0,0 +1 @@
|
||||
uid://haakw1duibni
|
||||
@@ -0,0 +1,307 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: portal.gd
|
||||
Description: A simple portal system for viewport-based portals in Godot 4.
|
||||
Instructions: For detailed documentation, see the README or visit: https://github.com/Donitzo/godot-simple-portal-system
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends MeshInstance3D
|
||||
class_name Portal
|
||||
## The portal represents a single portal mesh in a pair of portals.
|
||||
|
||||
## The delay between the main viewport changing size and the portal viewport resizing.
|
||||
const _RESIZE_THROTTLE_SECONDS:float = 0.1
|
||||
|
||||
## The minimum camera near clipping distance.
|
||||
const _EXIT_CAMERA_NEAR_MIN:float = 0.01
|
||||
|
||||
## The portal mesh's local bounding box.
|
||||
@onready var _mesh_aabb:AABB = mesh.get_aabb()
|
||||
|
||||
## The vertical resolution of the portal viewport which covers the entire screen not just the portal mesh. Use 0 to use the real resolution.
|
||||
@export var vertical_viewport_resolution:int = 512
|
||||
|
||||
## Disable viewport distance. Portals further away than this won't have their viewports rendered.
|
||||
@export var disable_viewport_distance:float = 11
|
||||
## Whether to destroy the disabled viewport to save texture memory. Useful when you have a lot of portals. The viewport is re/-created when within disable_viewport_distance and visible.
|
||||
@export var destroy_disabled_viewport:bool = true
|
||||
|
||||
## The maximum fade-out distance.
|
||||
@export var fade_out_distance_max:float = 10
|
||||
## The minimum fade-out distance.
|
||||
@export var fade_out_distance_min:float = 8
|
||||
## The fade-out color.
|
||||
@export var fade_out_color:Color = Color.WHITE
|
||||
|
||||
## The scale of the exit side of the portal. < 1 means the exit is smaller than the entrance.
|
||||
@export var exit_scale:float = 1.0
|
||||
## A value subtracted from the exit camera near clipping plane. Useful for handling clipping issues.
|
||||
@export var exit_near_subtract:float = 0.05
|
||||
|
||||
## The main camera. Leave unset to use the default 3D camera.
|
||||
@export var main_camera:Camera3D
|
||||
|
||||
## An environment set for the exit camera. Leave unset to use the default environment.
|
||||
@export var exit_environment:Environment
|
||||
## The cull mask for the exit camera. Use it to hide certain objects in the exit camera.
|
||||
@export_flags_3d_render var exit_cull_mask:int = 0b11111111111111111111
|
||||
|
||||
## The exit portal. Leave unset to use this portal as an exit only.
|
||||
@export var exit_portal:Portal
|
||||
|
||||
## An environment set for the exit camera. Leave unset to use the default environment.
|
||||
@export var portal_shader:Shader = preload("res://addons/simple-portal-system/shaders/portal.gdshader")
|
||||
|
||||
# The viewport rendering the portal surface
|
||||
var _viewport:SubViewport
|
||||
|
||||
# The exit camera copies the main camera's position relative to the exit portal
|
||||
var _exit_camera:Camera3D
|
||||
|
||||
# The number of seconds until the viewport updates its size
|
||||
var _seconds_until_resize:float
|
||||
|
||||
func _ready() -> void:
|
||||
if not is_inside_tree():
|
||||
push_error("The portal \"%s\" is not inside a SceneTree." % name)
|
||||
|
||||
# An exit-free portal does not need to do anything
|
||||
if exit_portal == null:
|
||||
visible = false
|
||||
set_process(false)
|
||||
return
|
||||
|
||||
if not exit_portal.is_inside_tree() or exit_portal.get_tree() != get_tree():
|
||||
push_error("The exit_portal \"%s\" of \"%s\" is not inside the same SceneTree." % [exit_portal.name, name])
|
||||
|
||||
# Non-uniform parent scaling can introduce skew which isn't compensated for
|
||||
if get_parent() != null:
|
||||
var parent_scale:Vector3 = get_parent().global_transform.basis.get_scale()
|
||||
if abs(parent_scale.x - parent_scale.y) > 0.01 or abs(parent_scale.x - parent_scale.z) > 0.01:
|
||||
push_warning("The parent of \"%s\" is not uniformly scaled. The portal will not work correctly." % name)
|
||||
|
||||
# The portals should be updated last so the main camera has its final position
|
||||
process_priority = 1000
|
||||
|
||||
# Used in raycasting
|
||||
add_to_group("portals")
|
||||
|
||||
# Get the main camera
|
||||
if main_camera == null:
|
||||
main_camera = get_viewport().get_camera_3d()
|
||||
|
||||
# The portal shader renders the viewport on-top of the portal mesh in screen-space
|
||||
material_override = ShaderMaterial.new()
|
||||
material_override.shader = portal_shader
|
||||
material_override.set_shader_parameter("fade_out_distance_max", fade_out_distance_max)
|
||||
material_override.set_shader_parameter("fade_out_distance_min", fade_out_distance_min)
|
||||
material_override.set_shader_parameter("fade_out_color", fade_out_color)
|
||||
|
||||
# Create the viewport when _ready if it's not destroyed when disabled.
|
||||
# This may potentially get rid of the initial lag when the viewport is first created at the cost of texture memory.
|
||||
if not destroy_disabled_viewport:
|
||||
_create_viewport()
|
||||
|
||||
get_viewport().connect("size_changed", _handle_resize)
|
||||
|
||||
func _handle_resize() -> void:
|
||||
_seconds_until_resize = _RESIZE_THROTTLE_SECONDS
|
||||
|
||||
func _create_viewport() -> void:
|
||||
# Create the viewport for the portal surface
|
||||
_viewport = SubViewport.new()
|
||||
_viewport.name = "Viewport"
|
||||
_viewport.render_target_clear_mode = SubViewport.CLEAR_MODE_ONCE
|
||||
add_child(_viewport)
|
||||
material_override.set_shader_parameter("albedo", _viewport.get_texture())
|
||||
|
||||
# Create the exit camera which renders the portal surface for the viewport
|
||||
_exit_camera = Camera3D.new()
|
||||
_exit_camera.name = "Camera"
|
||||
_exit_camera.environment = exit_environment
|
||||
_exit_camera.cull_mask = exit_cull_mask
|
||||
_viewport.add_child(_exit_camera)
|
||||
|
||||
# Resize the viewport on the next _process
|
||||
_seconds_until_resize = 0
|
||||
|
||||
func _process(delta:float) -> void:
|
||||
# Disable the viewport if the portal is further away than disable_viewport_distance or if the portal is invisible in the scene tree
|
||||
var disable_viewport:bool = not is_visible_in_tree() or\
|
||||
main_camera.global_position.distance_squared_to(global_position) > disable_viewport_distance * disable_viewport_distance
|
||||
|
||||
# Enable or disable 3D rendering for the viewport (if it exists)
|
||||
if _viewport != null:
|
||||
_viewport.disable_3d = disable_viewport
|
||||
|
||||
if disable_viewport:
|
||||
# Destroy the disabled viewport to save memory
|
||||
if _viewport != null and destroy_disabled_viewport:
|
||||
material_override.set_shader_parameter("albedo", null)
|
||||
_viewport.queue_free()
|
||||
_viewport = null
|
||||
|
||||
# Ensure the portal can re-size the second it is enabled again
|
||||
if not is_nan(_seconds_until_resize):
|
||||
_seconds_until_resize = 0
|
||||
|
||||
# Don't process the rest if the viewport is disabled
|
||||
return
|
||||
|
||||
# Re/-Create viewport
|
||||
if _viewport == null:
|
||||
_create_viewport()
|
||||
|
||||
# Throttle the viewport resizing for better performance
|
||||
if not is_nan(_seconds_until_resize):
|
||||
_seconds_until_resize -= delta
|
||||
if _seconds_until_resize <= 0:
|
||||
_seconds_until_resize = NAN
|
||||
|
||||
var viewport_size:Vector2i = get_viewport().size
|
||||
if vertical_viewport_resolution == 0:
|
||||
# Resize the viewport to the main viewport size
|
||||
_viewport.size = viewport_size
|
||||
else:
|
||||
# Resize the viewport to the fixed height vertical_viewport_resolution and dynamic width
|
||||
var aspect_ratio:float = float(viewport_size.x) / viewport_size.y
|
||||
_viewport.size = Vector2i(int(vertical_viewport_resolution * aspect_ratio + 0.5), vertical_viewport_resolution)
|
||||
|
||||
# Move the exit camera relative to the exit portal based on the main camera's position relative to the entrance portal
|
||||
_exit_camera.global_transform = real_to_exit_transform(main_camera.global_transform)
|
||||
|
||||
# Get the four X, Y corners of the scaled entrance portal bounding box clamped to Z=0 (portal surface) relative to the exit portal.
|
||||
# The entrance portal bounding box is used since the entrance portal mesh does not need to match the exit portal mesh.
|
||||
var corner_1:Vector3 = exit_portal.to_global(Vector3(_mesh_aabb.position.x, _mesh_aabb.position.y, 0) * exit_scale)
|
||||
var corner_2:Vector3 = exit_portal.to_global(Vector3(_mesh_aabb.position.x + _mesh_aabb.size.x, _mesh_aabb.position.y, 0) * exit_scale)
|
||||
var corner_3:Vector3 = exit_portal.to_global(Vector3(_mesh_aabb.position.x + _mesh_aabb.size.x, _mesh_aabb.position.y + _mesh_aabb.size.y, 0) * exit_scale)
|
||||
var corner_4:Vector3 = exit_portal.to_global(Vector3(_mesh_aabb.position.x, _mesh_aabb.position.y + _mesh_aabb.size.y, 0) * exit_scale)
|
||||
|
||||
# Calculate the distance along the exit camera forward vector at which each of the portal corners projects
|
||||
var camera_forward:Vector3 = -_exit_camera.global_transform.basis.z.normalized()
|
||||
|
||||
var d_1:float = (corner_1 - _exit_camera.global_position).dot(camera_forward)
|
||||
var d_2:float = (corner_2 - _exit_camera.global_position).dot(camera_forward)
|
||||
var d_3:float = (corner_3 - _exit_camera.global_position).dot(camera_forward)
|
||||
var d_4:float = (corner_4 - _exit_camera.global_position).dot(camera_forward)
|
||||
|
||||
# The near clip distance is the shortest distance which still contains all the corners
|
||||
_exit_camera.near = max(_EXIT_CAMERA_NEAR_MIN, min(d_1, d_2, d_3, d_4) - exit_near_subtract)
|
||||
_exit_camera.far = main_camera.far
|
||||
_exit_camera.fov = main_camera.fov
|
||||
_exit_camera.keep_aspect = main_camera.keep_aspect
|
||||
|
||||
## Return a new Transform3D relative to the exit portal based on the real Transform3D relative to this portal.
|
||||
func real_to_exit_transform(real:Transform3D) -> Transform3D:
|
||||
# Convert from global space to local space at the entrance (this) portal
|
||||
var local:Transform3D = global_transform.affine_inverse() * real
|
||||
# Compensate for any scale the entrance portal may have
|
||||
var unscaled:Transform3D = local.scaled(global_transform.basis.get_scale())
|
||||
# Flip it (the portal always flips the view 180 degrees)
|
||||
var flipped:Transform3D = unscaled.rotated(Vector3.UP, PI)
|
||||
# Apply any scale the exit portal may have (and apply custom exit scale)
|
||||
var exit_scale_vector:Vector3 = exit_portal.global_transform.basis.get_scale()
|
||||
var scaled_at_exit:Transform3D = flipped.scaled(Vector3.ONE / exit_scale_vector * exit_scale)
|
||||
# Convert from local space at the exit portal to global space
|
||||
var local_at_exit:Transform3D = exit_portal.global_transform * scaled_at_exit
|
||||
return local_at_exit
|
||||
|
||||
## Return a new position relative to the exit portal based on the real position relative to this portal.
|
||||
func real_to_exit_position(real:Vector3) -> Vector3:
|
||||
# Convert from global space to local space at the entrance (this) portal
|
||||
var local:Vector3 = global_transform.affine_inverse() * real
|
||||
# Compensate for any scale the entrance portal may have
|
||||
var unscaled:Vector3 = local * global_transform.basis.get_scale()
|
||||
# Apply any scale the exit portal may have (and apply custom exit scale)
|
||||
var exit_scale_vector:Vector3 = Vector3(-1, 1, 1) * exit_portal.global_transform.basis.get_scale()
|
||||
var scaled_at_exit:Vector3 = unscaled / exit_scale_vector * exit_scale
|
||||
# Convert from local space at the exit portal to global space
|
||||
var local_at_exit:Vector3 = exit_portal.global_transform * scaled_at_exit
|
||||
return local_at_exit
|
||||
|
||||
## Return a new direction relative to the exit portal based on the real direction relative to this portal.
|
||||
func real_to_exit_direction(real:Vector3) -> Vector3:
|
||||
# Convert from global to local space at the entrance (this) portal
|
||||
var local:Vector3 = global_transform.basis.inverse() * real
|
||||
# Compensate for any scale the entrance portal may have
|
||||
var unscaled:Vector3 = local * global_transform.basis.get_scale()
|
||||
# Flip it (the portal always flips the view 180 degrees)
|
||||
var flipped:Vector3 = unscaled.rotated(Vector3.UP, PI)
|
||||
# Apply any scale the exit portal may have (and apply custom exit scale)
|
||||
var exit_scale_vector:Vector3 = exit_portal.global_transform.basis.get_scale()
|
||||
var scaled_at_exit:Vector3 = flipped / exit_scale_vector * exit_scale
|
||||
# Convert from local space at the exit portal to global space
|
||||
var local_at_exit:Vector3 = exit_portal.global_transform.basis * scaled_at_exit
|
||||
return local_at_exit
|
||||
|
||||
## Raycast against portals (See instructions).
|
||||
static func raycast(tree:SceneTree, from:Vector3, dir:Vector3, handle_raycast:Callable,
|
||||
max_distance:float = INF, max_recursions:int = 16, ignore_backside:bool = true) -> void:
|
||||
var portals:Array = tree.get_nodes_in_group("portals")
|
||||
var ignore_portal:Portal = null
|
||||
var recursive_distance:float = 0
|
||||
|
||||
for r in max_recursions + 1:
|
||||
var closest_hit:Vector3
|
||||
var closest_dir:Vector3
|
||||
var closest_portal:Portal
|
||||
var closest_distance_sqr:float = INF
|
||||
|
||||
# Find the closest portal the ray intersects
|
||||
for portal in portals:
|
||||
# Ignore exit portals and invisible portals
|
||||
if portal == ignore_portal or not portal.is_inside_tree() or tree != portal.get_tree() or not portal.is_visible_in_tree():
|
||||
continue
|
||||
|
||||
var local_from:Vector3 = portal.to_local(from)
|
||||
var local_dir:Vector3 = portal.global_transform.basis.inverse() * dir
|
||||
|
||||
# Check if ray is parallel to the portal
|
||||
if local_dir.z == 0:
|
||||
continue
|
||||
|
||||
# Ignore backside
|
||||
if local_dir.z > 0 and ignore_backside:
|
||||
continue
|
||||
|
||||
# Get the intersection point of the ray with the Z axis
|
||||
var t:float = -local_from.z / local_dir.z
|
||||
|
||||
# Is the intersection behind the start position?
|
||||
if t < 0:
|
||||
continue
|
||||
|
||||
# Check if the ray hit inside the portal bounding box (ignoring Z)
|
||||
var local_hit:Vector3 = local_from + t * local_dir
|
||||
var aabb:AABB = portal._mesh_aabb
|
||||
if local_hit.x < aabb.position.x or local_hit.x > aabb.position.x + aabb.size.x or\
|
||||
local_hit.y < aabb.position.y or local_hit.y > aabb.position.y + aabb.size.y:
|
||||
continue
|
||||
|
||||
# Check if this was the closest portal
|
||||
var hit:Vector3 = portal.to_global(local_hit)
|
||||
var distance_sqr:float = hit.distance_squared_to(from)
|
||||
if distance_sqr < closest_distance_sqr:
|
||||
closest_hit = hit
|
||||
closest_dir = dir
|
||||
closest_distance_sqr = distance_sqr
|
||||
closest_portal = portal
|
||||
|
||||
# Calculate the ray distance
|
||||
var hit_distance:float = INF if is_inf(closest_distance_sqr) else sqrt(closest_distance_sqr)
|
||||
|
||||
# Call the user-defined raycast function
|
||||
if handle_raycast.call(from, dir, hit_distance, recursive_distance, r):
|
||||
break
|
||||
|
||||
# Was no portal hit or was the maximum raycast distance reached?
|
||||
recursive_distance += hit_distance
|
||||
if is_inf(closest_distance_sqr) or recursive_distance >= max_distance:
|
||||
break
|
||||
|
||||
# Re-direct the ray through the portal
|
||||
from = closest_portal.real_to_exit_position(closest_hit)
|
||||
dir = closest_portal.real_to_exit_direction(closest_dir)
|
||||
ignore_portal = closest_portal.exit_portal
|
||||
@@ -0,0 +1 @@
|
||||
uid://d2bvvjsibau8c
|
||||
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
Asset: Godot Simple Portal System
|
||||
File: simple_portal_teleport.gd
|
||||
Description: An area which teleports a node through the parent node's portal.
|
||||
Instructions: For detailed documentation, see the README or visit: https://github.com/Donitzo/godot-simple-portal-system
|
||||
Repository: https://github.com/Donitzo/godot-simple-portal-system
|
||||
License: CC0 License
|
||||
"""
|
||||
|
||||
extends Area3D
|
||||
class_name SimplePortalTeleport
|
||||
|
||||
var _parent_portal:Portal
|
||||
|
||||
func _ready():
|
||||
_parent_portal = get_parent() as Portal
|
||||
if _parent_portal == null:
|
||||
push_error("The PortalTeleport \"%s\" is not a child of a Portal instance" % name)
|
||||
|
||||
connect("area_entered", _on_area_entered)
|
||||
|
||||
func _on_area_entered(area:Area3D):
|
||||
if area.has_meta("teleportable_root"):
|
||||
var root:Node3D = area.get_node(area.get_meta("teleportable_root"))
|
||||
root.global_transform = _parent_portal.real_to_exit_transform(root.global_transform)
|
||||
@@ -0,0 +1 @@
|
||||
uid://bkv7t4hw21byg
|
||||
Reference in new issue
Block a user