I am making a portal system in godot 4. There are 2 worlds, everything in the normal world is on layer 1 or culling layer 1 (lights), and vice versa for altered world. The player model that moves around has two cameras, one's layer is the normal layer (the main one) and the other camera's layer is the altered layer. I have script that gets what the altered camera is seeing, and it gives that image to a shader to draw it.
gdscript:
extends MeshInstance3D
var portalTexture : Texture
var portalMaterial : ShaderMaterial
@onready var altered_camera= $"../../MainCharacter/altered_camera"
func _process(delta):
scale = Vector3(altered_camera.get_viewport().get_texture(
).get_size().x / 1000,altered_camera.get_viewport().get_texture().get_size(
).y / 1000,.1)
get_active_material(0).set_shader_parameter("portal_texture",
altered_camera.get_viewport().get_texture())
shader code:
shader_type spatial;
uniform sampler2D portal_texture;
void fragment() {
ALBEDO = texture(portal_texture, UV ).xyz;
}
When I look at the mesh, I see what the normal camera sees, instead of the altered one. I know this because there is a ball in front of the camera that is only in the altered world. What's going on?
I think it may be because the camera in the normal world is selected as the current one.
A
Viewporthas a single currentCamera3D, and that is what gets rendered.To get a second camera to render, you need a second
Viewport. Make the secondCamera3Da child of the secondViewportand make it current inside of it, then you can get what the secondCamera3Dsees by extracting the texture from the secondViewportthe one which has theCamera3Dyou want as child.