Jack Spencer
Jack Spencer

Reputation: 13

How do I get what a camera in a different layer is seeing?

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.

Upvotes: 1

Views: 238

Answers (1)

Theraot
Theraot

Reputation: 40295

A Viewport has a single current Camera3D, and that is what gets rendered.

To get a second camera to render, you need a second Viewport. Make the second Camera3D a child of the second Viewport and make it current inside of it, then you can get what the second Camera3D sees by extracting the texture from the second Viewport the one which has the Camera3D you want as child.

Upvotes: 0

Related Questions