5.12.0

Extending the Catalogue

Learn how to add custom Three.js classes and third-party libraries to TresJS using the extend function.

The TresJS Catalogue

TresJS automatically generates Vue components for all classes exported from the three package. This catalogue maps component names to Three.js constructors:

ComponentThree.js Class
<TresMesh />THREE.Mesh
<TresBoxGeometry />THREE.BoxGeometry
<TresPerspectiveCamera />THREE.PerspectiveCamera

But Three.js has many useful classes in three/addons/ that aren't included by default, plus third-party libraries you might want to use.

The extend Function

The extend function adds new classes to the catalogue, making them available as Tres components:

import { extend } from '@tresjs/core'
import { TextGeometry } from 'three/addons/geometries/TextGeometry'

// Add to catalogue
extend({ TextGeometry })

// Now available as <TresTextGeometry />

Common Extensions

Controls

<script setup lang="ts">
import { OrbitControls } from '@tresjs/cientos'
</script>

<template>
  <TresCanvas>
    <TresPerspectiveCamera :position="[5, 5, 5]" />
    <OrbitControls />
    <!-- ... -->
  </TresCanvas>
</template>
Controls such as Three.js' OrbitControls require the active camera and renderer DOM element as constructor arguments. Prefer the @tresjs/cientos component shown above, which reads both from the canvas context. If you extend a control class directly, wrap it in a component rendered inside <TresCanvas> and pass those values through the args prop.

To extend the Three.js class directly, create a child component such as ExtendedOrbitControls.vue:

<script setup lang="ts">
import { extend, useTres } from '@tresjs/core'
import { OrbitControls } from 'three/addons/controls/OrbitControls'

const { camera, renderer } = useTres()

extend({ OrbitControls })
</script>

<template>
  <TresOrbitControls
    v-if="camera"
    :args="[camera, renderer.domElement]"
  />
</template>

Render that component as a child of <TresCanvas> so useTres() can access the canvas context.

Geometries

<script setup lang="ts">
import { extend } from '@tresjs/core'
import { TextGeometry } from 'three/addons/geometries/TextGeometry'
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry'

extend({ TextGeometry, RoundedBoxGeometry })
</script>

<template>
  <TresCanvas>
    <TresMesh>
      <TresRoundedBoxGeometry :args="[1, 1, 1, 4, 0.1]" />
      <TresMeshStandardMaterial />
    </TresMesh>
  </TresCanvas>
</template>

Loaders

<script setup lang="ts">
import { extend } from '@tresjs/core'
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader'
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader'

extend({ GLTFLoader, DRACOLoader })
</script>

Post-Processing

<script setup lang="ts">
import { extend } from '@tresjs/core'
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer'
import { RenderPass } from 'three/addons/postprocessing/RenderPass'
import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass'

extend({ EffectComposer, RenderPass, UnrealBloomPass })
</script>
For post-processing, consider using which provides ready-to-use effect components.

Third-Party Libraries

Using drei-style Libraries

<script setup lang="ts">
import { extend } from '@tresjs/core'
import { MeshWobbleMaterial } from 'some-threejs-library'

extend({ MeshWobbleMaterial })
</script>

<template>
  <TresCanvas>
    <TresMesh>
      <TresBoxGeometry />
      <TresMeshWobbleMaterial color="hotpink" :factor="1" :speed="2" />
    </TresMesh>
  </TresCanvas>
</template>

Custom Classes

You can extend with your own Three.js classes:

<script setup lang="ts">
import { extend } from '@tresjs/core'
import * as THREE from 'three'

// Custom geometry class
class StarGeometry extends THREE.BufferGeometry {
  constructor(innerRadius = 0.5, outerRadius = 1, points = 5) {
    super()
    // ... geometry creation logic
  }
}

extend({ StarGeometry })
</script>

<template>
  <TresCanvas>
    <TresMesh>
      <TresStarGeometry :args="[0.5, 1, 5]" />
      <TresMeshStandardMaterial color="gold" />
    </TresMesh>
  </TresCanvas>
</template>

Default attach

Materials, geometries and fog attach to their parent automatically. Other classes that are not an Object3D connect only through the attach prop. To give a class a default attach, pass an entry with object and attach to extend:

import { extend } from '@tresjs/core'
import { FollowBody } from 'some-camera-library'

// `setBody` returns a function that removes the body again
extend({
  FollowBody: {
    object: FollowBody,
    attach: (parent, child) => parent.setBody(child),
  },
})

Now <TresFollowBody /> attaches itself without an attach prop. The default can be a string, such as 'body', or a function that returns a detach function. An attach prop on the Tres component still overrides the default. If the prop is null or undefined, the default applies, because Vue sets a removed prop to null.

Props that Tres assigns

When a prop changes and the current value is a math object, such as a Vector3 or a Color, Tres writes the new values into the current object. It does not replace the object. This keeps objects that the class creates itself, such as mesh.position, in place.

If the current value is the object from the previous prop value, Tres does not change that object. When you change :target="pointA" to :target="pointB", Tres assigns pointB. The instance holds pointB, and pointA keeps its values.

If the new value is an array or a color string, Tres writes it into a clone of the current object. Later arrays and color strings go into the same clone. When the next value is an object again, Tres assigns it.

Tres cannot know that a value from args is your object. To make Tres assign a prop in that case too, list the prop in assign:

import { extend } from '@tresjs/core'
import { Follower } from 'some-camera-library'

extend({
  Follower: {
    object: Follower,
    assign: ['target'],
  },
})
Do not use a pierced prop together with an object prop for the same field, such as target-x with :target="pointA". Tres writes the pierced value into the object you pass, so pointA.x changes. When the object changes, Tres does not apply the pierced value again.

Naming Convention

The component name is derived from the class name you provide to extend:

extend({ RoundedBoxGeometry }) // <TresRoundedBoxGeometry />
extend({ TextGeometry })       // <TresTextGeometry />
extend({ MyCustomMaterial })   // <TresMyCustomMaterial />

You can also use custom names:

extend({
  CustomGeometry: RoundedBoxGeometry // <TresCustomGeometry />
})

Global vs Local Extension

Extend in your main app entry or a plugin:

// plugins/tres.ts or main.ts
import { extend } from '@tresjs/core'
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry'
import { TextGeometry } from 'three/addons/geometries/TextGeometry'

extend({ RoundedBoxGeometry, TextGeometry })

Local Extension (Component-Specific)

Extend in the component that needs it:

<script setup lang="ts">
import { extend } from '@tresjs/core'
import { SomeSpecialGeometry } from 'special-library'

// Only this component uses this class
extend({ SomeSpecialGeometry })
</script>

TypeScript Support

For type safety with extended components, declare the types:

// types/tres.d.ts
import type { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry'

declare module '@tresjs/core' {
  interface TresObjectMap {
    RoundedBoxGeometry: typeof RoundedBoxGeometry
  }
}

Complete Example

<script setup lang="ts">
import { OrbitControls } from '@tresjs/cientos'
import { extend } from '@tresjs/core'
import { TextGeometry } from 'three/addons/geometries/TextGeometry'
import { FontLoader } from 'three/addons/loaders/FontLoader'

extend({ TextGeometry })

// Load font for TextGeometry
const fontLoader = new FontLoader()
const font = await new Promise((resolve) => {
  fontLoader.load('/fonts/helvetiker_regular.typeface.json', resolve)
})

const fontOptions = {
  font,
  size: 1,
  height: 0.2,
  curveSegments: 12,
}
</script>

<template>
  <TresCanvas shadows>
    <TresPerspectiveCamera :position="[5, 5, 5]" />
    <OrbitControls />

    <TresMesh :position="[0, 0, 0]">
      <TresTextGeometry :args="['TresJS', fontOptions]" center />
      <TresMeshStandardMaterial color="#82dbc5" />
    </TresMesh>

    <TresAmbientLight :intensity="0.5" />
    <TresDirectionalLight :position="[10, 10, 10]" />
  </TresCanvas>
</template>

Key Takeaways

extend() Function

Use extend() to add any Three.js class to the TresJS catalogue.

Addons Support

Classes from three/addons/ aren't included by default - extend them as needed.

Custom Classes

Your own Three.js classes can be extended and used as Vue components.

Tres Prefix

Extended classes become available with the Tres prefix: TextGeometry → <TresTextGeometry />.