OrthoPerspectiveCamera
đŊī¸ Managing Multiple Viewsâ
Perspective view adds depth and realism, which helps in creating visually compelling representations in 3D scenes.đ¤ī¸ While, Orthographic view is important for precise measurements and proportions.đ
đ If you haven't started there, check out that tutorial first!
We'll be using an advanced camera component for this tutorial. OrthoPerspectiveCamera makes it simple to use Orthographic and Perspective projections.
đ˛ Creating a Cube Meshâ
First, let's create a simple Cube, which will render differently depending on the projection you choose.đ§
We will create a Cube
with 3x3x3
dimensions and use red color for the material.đī¸
const cubeGeometry = new THREE.BoxGeometry(3, 3, 3);
const redMaterial = new THREE.MeshStandardMaterial({color: "red"});
const redCube = new THREE.Mesh(cubeGeometry, redMaterial);
redCube.position.set(0, 1.5, 0);
Now, we will add the Cube to the Scene
. We must also add the cube to components.meshes
,
which is simply an array of all the meshes in the Scene đī¸.
components.meshes
acts as a store to help you manage your elements centrally.
scene.add(redCube);
components.meshes.push(redCube);
đī¸ Developing an OrthoPerspective Cameraâ
We will create OrthoPerspectiveCamera by passing components
as an argument to it.đī¸
The OrthoPerspective Camera extends the SimpleCamera by providing you with extra controls.
We will then configure the camera location and update the look at target using setLookAt()
API.đ
const camera = new OBC.OrthoPerspectiveCamera(components);
components.camera = camera;
camera.controls.setLookAt(10, 10, 10, 0, 0, 0);
components.init();
đĨ Whenever the components like scene, camera are created, you need to initialize the component library. Check out components.init() for more info!đ
đšī¸ Changing Views and Navigationâ
Now, that our camera setup is done, we need to manage the camera projection on demand.
Toggling Orthographic View and Perspective Viewâ
Let's create a simple method toggleProjection()
which toggles the Camera View using camera.toggleProjection
.
Alternatively, you can also use camera.setProjection()
and pass 'Orthographic'
or 'Perspective'
to manage the views.đĄ
function toggleProjection() {
camera.toggleProjection();
}
Managing Navigation Modesâ
Along with projection, we can also manage Navigation modes using OrthoPerspective camera.
To update navigation modes, we will use camera.setNavigationMode('Orbit' | 'FirstPerson' | 'Plan')
- Orbit - Orbit Mode helps us to easily navigate around the 3D Elements.
- FirstPerson - It helps you to visualize scene from your own perspective. First Person mode is only available for Perspective Projection.
- Plan - This mode helps you to easily navigate in 2D Projections.
function setNavigationMode(navMode){
camera.setNavigationMode(navMode);
}
𧎠OrthoPerspective Camera also provides you an option to adjust your camera to fit the 3D elements. You can simply use fitModelToFrame(mesh) and provide the mesh which you want to fit to your window frame
Congratulations đ on completing this tutorial! Now you can add Advance Camera System to your web-app in minutes using OrthoPerspectiveCamera âđŊī¸ Let's keep it up and check out another tutorial! đ