Compare commits

...
3 Commits
Author SHA1 Message Date
Vojta 79ce6cc145 Update portal plugin to 1.0.1
Includes the flickering fix!
2025-07-08 08:21:16 +02:00
Vojta d52d760b18 Update plugin 2025-05-20 21:22:18 +02:00
Vojta d5b4f22a9d Remove DS_Store 2025-05-20 20:49:04 +02:00
7 changed files with 93 additions and 51 deletions

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
# Godot 4+ specific ignores # Godot 4+ specific ignores
.godot/ .godot/
/android/ /android/
.DS_Store
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
### 1.0.1
- Fix flickering when going sideways through a portal. PR #4
# 1.0.0
First public release! This plugin was developed as part of my master's thesis. As such, the thesis
itself is the best documentation at the moment:
[https://is.muni.cz/th/ydltb](https://is.muni.cz/th/ydltb/?lang=en).
Also check out the included README.md for tips and potentially also the
[GitHub repository](https://github.com/VojtaStruhar/godot-portals-plugin)
for some showcase levels.
+21 -24
View File
@@ -4,28 +4,26 @@ This plugin enables you to easily create seamless plugins.
## Documentation ## Documentation
For documentation about `Portal3D`, see the portal script itself. For documentation about `Portal3D`, see the portal script itself. Everything is properly documented
Everything is properly documented and viewable in the default and viewable in the default documentation window. Go to the script editor, click _Search Help_ in
documentation window. Click _Script > Search Help_ and type the top bar and search for "Portal3D".
"Portal3D" in there.
For everything else, there is this README. For everything else, there is this README.
## Materials ## Guides
### Portals ### Customize portals in the editor
The portal mesh has a custom shader material assigned to it at runtime, The portal mesh has a custom shader material assigned to it at runtime (defined in
(defined in `materials/portal_shader.gdshader`), but in editor, it uses `materials/portal_shader.gdshader`), but in editor, it uses a regular material -- find it at
a regular material -- find it at `materials/editor-preview-portal-material.tres`. `materials/editor-preview-portal-material.tres`. You can edit this material to customize how
You can edit this material to customize how portals look in the editor portals look in the editor (in case the default gray color blends in too much).
(in case the default gray color blends in too much).
### Smooth teleportation ### Smooth teleportation
The Portal3D script provides a mechanism for smooth teleportation. In order to be The Portal3D script provides a mechanism for smooth teleportation. In order to be able to create
able to create smooth portal transitions, you need to implement a clipping shader smooth portal transitions, you need to put a clipping shader onto all meshes that are supposed to
on all meshes that are supposed to participate in the teleportation. participate in the smooth teleportation.
**How to convert a regular mesh to a clippable one?** Like this: **How to convert a regular mesh to a clippable one?** Like this:
@@ -43,22 +41,21 @@ shader_type spatial;
PORTALCLIP_UNIFORMS PORTALCLIP_UNIFORMS
void vertex() { void vertex() {
// ... // ...
PORTALCLIP_VERTEX PORTALCLIP_VERTEX
} }
void fragment() { void fragment() {
// ... // ...
PORTALCLIP_FRAGMENT PORTALCLIP_FRAGMENT
} }
``` ```
And that's it! Now look for `DUPLICATE_MESHES_CALLBACK` in the Portal3D script, you And that's it! Now look for `DUPLICATE_MESHES_CALLBACK` in the Portal3D script, you are ready to
are ready to get going with smooth teleportation! get going with smooth teleportation!
## Gizmos ## Gizmos
This plugin includes couple of custom gizmos. One gives a This plugin includes couple of custom gizmos. One gives a connected portal an outline and the
connected portal an outline and the second one visualizes portal's second one visualizes portal's front direction. You can configure the color of both gizmos in
front direction. You can configure the color of both gizmos in _Project Settings / Addons / Portals_ or turn them off altogether.
_Project Settings > Addons > Portals_ or turn them off altogether.
+3 -3
View File
@@ -1,7 +1,7 @@
[plugin] [plugin]
name="3D Portals" name="Portals 3D"
description="Implements seamless portals in 3D" description="Seamless portals plugin in 3D"
author="Vojtech Struhar" author="Vojtech Struhar"
version="0.1" version="1.0.1"
script="plugin.gd" script="plugin.gd"
+55 -24
View File
@@ -344,6 +344,9 @@ var portal_viewport: SubViewport = null
class TeleportableMeta: class TeleportableMeta:
## Forward distance from the portal ## Forward distance from the portal
var forward: float = 0 var forward: float = 0
## True only if the [member Portal3D.player_camera] is a child of the object being teleported.
## In that case, we consider it the player.
var is_player: bool = false
## Meshes that the object gave for duplication. Retrieved by the ## Meshes that the object gave for duplication. Retrieved by the
## [constant Portal3D.DUPLICATE_MESHES_CALLBACK] callback. ## [constant Portal3D.DUPLICATE_MESHES_CALLBACK] callback.
var meshes: Array[MeshInstance3D] = [] var meshes: Array[MeshInstance3D] = []
@@ -468,7 +471,7 @@ func _ready() -> void:
teleport_area.collision_mask = teleport_collision_mask teleport_area.collision_mask = teleport_collision_mask
func _process(delta: float) -> void: func _process(_delta: float) -> void:
if Engine.is_editor_hint(): if Engine.is_editor_hint():
return return
@@ -552,14 +555,13 @@ func _process_teleports() -> void:
on_teleport.emit(teleportable) on_teleport.emit(teleportable)
exit_portal.on_teleport_receive.emit(teleportable) exit_portal.on_teleport_receive.emit(teleportable)
# Force the cameras to refresh if we just teleported a player
var was_player := not str(teleportable.get_path_to(player_camera)).begins_with(".") if tp_meta.is_player:
if was_player:
_process_cameras() _process_cameras()
exit_portal._process_cameras() exit_portal._process_cameras()
# Resolve teleport interactions # Resolve teleport interactions
if was_player and _check_tp_interaction(TeleportInteractions.PLAYER_UPRIGHT): if tp_meta.is_player and _check_tp_interaction(TeleportInteractions.PLAYER_UPRIGHT):
get_tree().create_tween().tween_property(teleportable, "rotation:x", 0, 0.3) get_tree().create_tween().tween_property(teleportable, "rotation:x", 0, 0.3)
get_tree().create_tween().tween_property(teleportable, "rotation:z", 0, 0.3) get_tree().create_tween().tween_property(teleportable, "rotation:z", 0, 0.3)
@@ -691,11 +693,22 @@ func _on_window_resize() -> void:
#region UTILS #region UTILS
func _construct_tp_metadata(node: Node3D) -> void: func _construct_tp_metadata(node: Node3D) -> void:
var teleportable = node.get_node(node.get_meta(TELEPORT_ROOT_META, ".")) # Usually the node itself
var meta = TeleportableMeta.new() var meta = TeleportableMeta.new()
meta.forward = forward_distance(node) meta.forward = forward_distance(node)
meta.is_player = not str(teleportable.get_path_to(player_camera)).begins_with(".")
if _check_tp_interaction(TeleportInteractions.DUPLICATE_MESHES) and \ ## This is a workaround to prevent flickering when traversing portals.
node.has_method(DUPLICATE_MESHES_CALLBACK): ## There is a bit of lag when restarting RTT when the exit portal becomes physically visible.
## Ensuring both portals are updated regardless of visibility while in the portals prevents flickering.
## More info: https://github.com/VojtaStruhar/godot-portals-plugin/pull/4
if meta.is_player:
_set_portal_pair_update_mode(SubViewport.UPDATE_ALWAYS)
if _check_tp_interaction(TeleportInteractions.DUPLICATE_MESHES)\
and node.has_method(DUPLICATE_MESHES_CALLBACK):
meta.meshes = node.call(DUPLICATE_MESHES_CALLBACK) meta.meshes = node.call(DUPLICATE_MESHES_CALLBACK)
for m: MeshInstance3D in meta.meshes: for m: MeshInstance3D in meta.meshes:
var dupe = m.duplicate(0) var dupe = m.duplicate(0)
@@ -711,11 +724,40 @@ func _erase_tp_metadata(node_id: int) -> void:
var meta = _watchlist_teleportables.get(node_id) var meta = _watchlist_teleportables.get(node_id)
if meta != null: if meta != null:
meta = meta as TeleportableMeta meta = meta as TeleportableMeta
if meta.is_player:
_set_portal_pair_update_mode(SubViewport.UPDATE_WHEN_VISIBLE)
for m in meta.meshes: _disable_mesh_clipping(m) for m in meta.meshes: _disable_mesh_clipping(m)
for c in meta.mesh_clones: c.queue_free() for c in meta.mesh_clones: c.queue_free()
_watchlist_teleportables.erase(node_id) _watchlist_teleportables.erase(node_id)
func _transfer_tp_metadata_to_exit(for_body: Node3D) -> void:
if not exit_portal.is_teleport:
return # One-way teleport scenario
var body_id = for_body.get_instance_id()
var tp_meta = _watchlist_teleportables[body_id]
assert(tp_meta != null, "Attempted to trasfer teleport metadata for a node that is not being watched.")
tp_meta.forward = exit_portal.forward_distance(for_body)
_enable_mesh_clipping(tp_meta, exit_portal) # Switch, the main mesh is clipped by exit portal!
exit_portal._watchlist_teleportables.set(body_id, tp_meta)
if tp_meta.is_player and exit_portal.exit_portal != self:
# Not a portal pair - the transition isn't seamless anyways. Flip the update
# mode of this portal "manually" and enable the next portal pair, since `_construct_tp_metadata`
# will not get called there. Usually portals are symmetric, though.
portal_viewport.set_update_mode(SubViewport.UPDATE_WHEN_VISIBLE)
exit_portal._set_portal_pair_update_mode(SubViewport.UPDATE_ALWAYS)
# NOTE: Not using '_erase_tp_metadata' here, as it also frees the cloned meshes!
_watchlist_teleportables.erase(body_id)
func _enable_mesh_clipping(meta: TeleportableMeta, along_portal: Portal3D) -> void: func _enable_mesh_clipping(meta: TeleportableMeta, along_portal: Portal3D) -> void:
for mi: MeshInstance3D in meta.meshes: for mi: MeshInstance3D in meta.meshes:
var clip_normal = signf(meta.forward) * along_portal.global_basis.z var clip_normal = signf(meta.forward) * along_portal.global_basis.z
@@ -733,23 +775,6 @@ func _enable_mesh_clipping(meta: TeleportableMeta, along_portal: Portal3D) -> vo
func _disable_mesh_clipping(mi: MeshInstance3D) -> void: func _disable_mesh_clipping(mi: MeshInstance3D) -> void:
mi.set_instance_shader_parameter("portal_clip_active", false) mi.set_instance_shader_parameter("portal_clip_active", false)
func _transfer_tp_metadata_to_exit(for_body: Node3D) -> void:
if not exit_portal.is_teleport:
return # One-way teleport scenario
var body_id = for_body.get_instance_id()
var tp_meta = _watchlist_teleportables[body_id]
if tp_meta == null:
push_error("Attempted to trasfer teleport metadata for a node that is not being watched.")
return
tp_meta.forward = exit_portal.forward_distance(for_body)
_enable_mesh_clipping(tp_meta, exit_portal) # Switch, the main mesh is clipped by exit portal!
exit_portal._watchlist_teleportables.set(body_id, tp_meta)
# NOTE: Not using '_erase_tp_metadata' here, as it also frees the cloned meshes!
_watchlist_teleportables.erase(body_id)
## [b]Crucial[/b] piece of a portal - transforming where objects should appear ## [b]Crucial[/b] piece of a portal - transforming where objects should appear
## on the other side. Used for both cameras and teleports. ## on the other side. Used for both cameras and teleports.
func to_exit_transform(g_transform: Transform3D) -> Transform3D: func to_exit_transform(g_transform: Transform3D) -> Transform3D:
@@ -828,6 +853,12 @@ func _calculate_viewport_size() -> Vector2i:
func _check_tp_interaction(flag: int) -> bool: func _check_tp_interaction(flag: int) -> bool:
return (teleport_interactions & flag) > 0 return (teleport_interactions & flag) > 0
func _set_portal_pair_update_mode(mode: SubViewport.UpdateMode) -> void:
assert(is_instance_valid(exit_portal))
self.portal_viewport.set_update_mode(mode)
if exit_portal.portal_viewport:
exit_portal.portal_viewport.set_update_mode(mode)
## Get a point where the portal plane intersects a line. Line [param start] and [param end] ## Get a point where the portal plane intersects a line. Line [param start] and [param end]
## are in global coordinates and so is the result. Used for forwarding raycast queries. ## are in global coordinates and so is the result. Used for forwarding raycast queries.
func line_intersection(start: Vector3, end: Vector3) -> Vector3: func line_intersection(start: Vector3, end: Vector3) -> Vector3: