☑ How to set camera viewport programmatically?

Hi there,

This does not work for me.
this.camera.addComponent(“camera”, { viewport: { x: 0.2, y: 0.2, w: 0.5, h: 0.5}});

Also CameraComponent does not seem to have a viewport in the documentation.
http://developer.playcanvas.com/en/api/pc.CameraComponent.html

However I can set the viewport on the camera if I use the editor, it is a property of the camera.

Also I can change the viewport programmaticallyI found by hacking but the object looks squashed horizontally and of course this looks like the wrong way to do it.
var c = this.camera.camera.data.camera;
c._rect.x = 0.5;
c._rect.width = 0.5;

Thanks,
Philip

Sorry I can answer my own question, the camera property name is called “rect”, I discovered by looking in the editor and mouse over the property. By setting rect with an array of 4 floats it works.

It was just that I was looking for a name viewport but the name is rect.

1 Like