When doing raycasts directly with PhysicsDirectSpaceState3D.IntersectRay(), implement a method that checks your collisions and if a portal is hit, pass the PhysicsRayQueryParameters3D to this method.
If you are using RayCast3D for raycasting see ForwardRaycast()
public Dictionary ForwardRaycastQuery(PhysicsRayQueryParameters3D parameters)
| parameter | description |
|---|---|
| parameters | The ray query parameters of your raycast. |
A dictionary of the raycast parameters or an empty dictionary if nothings was intersected.