TresJS provides an autogenerated catalogue of Vue components that map directly to Three.js classes. Any class exported from the three package is automatically available as a Vue component with the Tres prefix.
| Vue Component | Three.js Class |
|---|---|
<TresPerspectiveCamera /> | new THREE.PerspectiveCamera() |
<TresMesh /> | new THREE.Mesh() |
<TresBoxGeometry /> | new THREE.BoxGeometry() |
<TresMeshBasicMaterial /> | new THREE.MeshBasicMaterial() |
<TresAmbientLight /> | new THREE.AmbientLight() |
Unlike vanilla Three.js where you must import each class:
import { PerspectiveCamera, Mesh, BoxGeometry } from 'three'
With TresJS, components are available automatically:
<template>
<TresCanvas>
<TresPerspectiveCamera :position="[5, 5, 5]" />
<TresMesh>
<TresBoxGeometry />
<TresMeshBasicMaterial color="red" />
</TresMesh>
</TresCanvas>
</template>
TresJS components accept two types of configuration:
args)Pass arguments to the Three.js constructor using the args prop:
<template>
<!-- Creates: new THREE.BoxGeometry(2, 2, 2) -->
<TresBoxGeometry :args="[2, 2, 2]" />
<!-- Creates: new THREE.PerspectiveCamera(75, 1, 0.1, 1000) -->
<TresPerspectiveCamera :args="[75, 1, 0.1, 1000]" />
</template>
Set Three.js object properties using Vue props:
<script setup lang="ts">
const rotation = ref([0, Math.PI, 0])
const scale = ref(2)
</script>
<template>
<TresMesh
:position="[0, 1, 0]"
:rotation="rotation"
:scale
:cast-shadow="true"
>
<TresBoxGeometry />
<TresMeshStandardMaterial color="blue" :metalness="0.5" />
</TresMesh>
</template>
Add Three.js addons or custom classes using extend():
<script setup lang="ts">
import { extend } from '@tresjs/core'
import { OrbitControls } from 'three/addons/controls/OrbitControls'
extend({ OrbitControls })
</script>
<template>
<TresCanvas>
<TresPerspectiveCamera :position="[5, 5, 5]" />
<TresOrbitControls />
</TresCanvas>
</template>