Getting Started with 3D on Vue using TresJS

Posted by

TresJS – First steps with 3D on Vue

If you’re looking to add 3D functionality to your Vue.js application, TresJS is a great choice. TresJS is a 3D graphics engine for the web that is built on top of Three.js and Vue. It allows you to easily create and manipulate 3D objects in your Vue.js application.

To get started with TresJS, you’ll first need to install the package. You can do this using npm by running the following command:

“`html
npm install tres
“`

Once the package is installed, you can start using it in your Vue.js application. To create a simple 3D scene with TresJS, you’ll need to import the necessary components and set up a basic scene.

“`html

import { Scene, PerspectiveCamera, DirectionalLight, AmbientLight, Mesh, Material, MeshBasicMaterial, SphereGeometry, MeshStandardMaterial } from ‘tres’;

export default {
mounted() {
const scene = new Scene();

const camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;

const renderer = new Renderer({
canvas: this.$refs.renderer
});

const light = new DirectionalLight(0xffffff, 1);
light.position.set(0, 0, 1).normalize();
scene.add(light);

const ambientLight = new AmbientLight(0x404040);
scene.add(ambientLight);

const geometry = new SphereGeometry(1, 32, 32);
const material = new MeshStandardMaterial({
color: 0x00ff00
});

const sphere = new Mesh(geometry, material);
scene.add(sphere);

renderer.render(scene, camera);
}
};

“`

In this example, we import the necessary components from TresJS and then set up a simple 3D scene. We create a scene, add a camera and light, and then create a simple 3D object (in this case, a sphere) and add it to the scene. Finally, we create a renderer and use it to render the scene.

This is just a basic example of how to get started with TresJS, but it should give you a good idea of how to start working with 3D graphics in your Vue.js application. TresJS provides a lot of flexibility and power for creating 3D scenes, so you can use it to create anything from simple 3D animations to complex 3D games.

I hope this article has given you a good introduction to getting started with TresJS in your Vue.js applications. With TresJS, adding 3D functionality to your web application has never been easier.

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sarwannizamani9963
7 months ago

Your videos are great, but I am unable to find props for the different components like for example TresBoxGeometry, TresMesh etc, I am unable to look for different components there.

@RobertWildling
7 months ago

Nice tutorials, even though a bit confusing. There is no package.json (but you later mention that you have a basic setup with vite). Also: what are "nr" and "ni"? When I search npm I get really old results (like 9 and 10 years old). And when I install those packages globally and use them, all I get is errors over errors. Could you please share a little info about "nr" and "ni"? Thank you! 🙂

@caioalves8319
7 months ago

Hello, would you be able to make a video on how to use Histoire in Nuxt 3?

@FernandoHernandez-pz9im
7 months ago

Dude you're awesome! Great job, love vue and I wanted to learn three js but the documentation of trees is a lil limit and is kinda hard translate the three's docs into trees js.
Love it thank you so much, I will follow all the videos please don't stop to upload this kind of content ❤

@einl12345
7 months ago

Looks great! Thanks 🙂

@thomasfichtner9738
7 months ago

Hey Alvaro, loving the framework your creating. One question is there a function or a way to fully or partly clean a scene and dispose items?

Iam currently doing it like:
if (this.renderer?.renderer && this.scene && this.canvas) {

this.scene.traverse((object) => {

if (object instanceof THREE.Mesh) {

object.geometry.dispose();

object.material.dispose();

object.material.map?.dispose();

}

});

this.renderer && this.renderer.renderer.dispose();

this.canvas.removeChild(this.renderer.renderer.domElement);

}

But is there a better way that the TresJS provides?
Thank you <3

@LNSFLIVE
7 months ago

doubt you can use fbx files, but can you still use glb files? maybe do a video for that