Your first scene
This guide will help you to create your first Tres scene. 🍩
Setting up the experience Canvas
Before we can create a ThreeJS
Scene
, we need a space to display it. Using plain
ThreeJS
we would need to create a
canvas
HTML element to mount the
WebglRenderer
and initialize the
Scene
.
With
TresJS
you only need to import the default component
<TresCanvas />
and add it to the template of your Vue component.
WARNING
It's important that all components related to the scene live between the
<TresCanvas />
component. Otherwise, they will be not rendered.
The
TresCanvas
component is going to do some setup work behind the scenes:
- It creates a WebGLRenderer that automatically updates every frame.
- It sets the render loop to be called on every frame based on the browser refresh rate.
Canvas size
By default,
TresCanvas
component will take the
parent's width and height
, if you are experiencing a blank page make sure the parent element has a proper size.
If your scene is not gonna be part of a UI, you can also force the canvas to take the width and height of the full window by using the
window-size
prop like this:
Creating a scene
We need 4 core elements to create a 3D experience:
- A Scene to hold the camera and the object(s) together.
- A Renderer to render the scene into the DOM.
- A Camera
- An Object
With
TresJS
you only need to add the
<TresCanvas />
component to the template of your Vue component and it will automatically create a
Renderer
(
canvas
DOM Element) and
Scene
for you.
Then you can add a
PerspectiveCamera
using the
<TresPerspectiveCamera />
component.
WARNING
A common issue is that the camera default position is the origin of the scene (0,0,0). TresJS will automatically set the position of your camera to
[3,3,3]
if the prop
position
is not set by you. If no camera is defined in you scene, a perspective camera is added automatically.
Adding a 🍩
That scene looks a little empty, let's add a basic object. If we were using plain ThreeJS we would need to create a Mesh object and attach to it a Material and a Geometry like this:
A Mesh is a basic scene object in three.js, and it's used to hold the geometry and the material needed to represent a shape in 3D space.
Now let's see how we can easily achieve the same with
TresJS
. To do that we are going to use the
<TresMesh />
component, and between the default slots, we are going to pass a
<TresTorusGeometry />
and a
<TresMeshBasicMaterial />
component.
INFO
Notice that we don't need to import anything, that's because
TresJS
automatically generate a
Vue Component based on the three objects you want to use in PascalCase with a Tres prefix
. For example, if you want to use an
AmbientLight
you would use the
<TresAmbientLight />
component.