Get Camera from React Component

Hi,

I’m using the React template for my project and I would like to access to the complete context of Needle from outside.
I succeed to get my needle domElement, I can access to my scene.

But I’m trying to make a switchCamera feature witch switch between Orthographic and perspective Camera, but I can’t access to needleDom.current._context.mainCamera or mainCameraComponent

Somebody has an idea ?

Original Post on Discord

by user 224464722878005248

You can get the context via await getContext() on the engine element from the outside (or the static Context.Current)

Oh thanks

And, it said the the context.mainCamera is in read only
I would like to do
this.context.mainCamera = this.orthocamera;

And I don’t see a set() function, how could I do ?

by user 224464722878005248

I believe it’s as simple as turning one cam off and the other on and both having the MainCamera tag

(similar to how it works in Unity with Camera.main)

Okay thanks !

by user 224464722878005248

The context has setCurrentCamera(yourCamera) for it

by user 224464722878005248

Okay because turn on/off doesn’t work
Thanks

by user 224464722878005248

You dont need to turn it off too if both have the msincamera tag, they stack.

turn it off => visible = false ?

by user 224464722878005248

if(this.originalCamera && this.orthographicCamera) {
                    // @ts-ignore
                    this.originalCamera.SetActive(false) 
                    // @ts-ignore
                    this.orthographicCamera.SetActive(true) 
                    // @ts-ignore
                    this.context.setCurrentCamera(this.orthographicCamera)
                }

Like it ?

by user 224464722878005248

by user 224464722878005248

I think I miss something

by user 224464722878005248

You enable or disable the camera object

this.context.setCurrentCamera(this.orthographicCamera)

Where this.orthographicCamera is a THREE.OrthographicCamera
It generate an error : cam.buildCamera is not a function

by user 224464722878005248

It needs to be the camera component, you have the threejs camera.

The camera component also has the enabled property

Okay so by using getComponent

Okay, I succeed, thank to you @marcel :cactus:

by user 224464722878005248

Great, happy to hear