TroisJS: Three.js for Vue 3

0.3.4 · active · verified Tue Apr 21

TroisJS is a powerful and lightweight library designed to facilitate the creation of 3D graphics and animations within Vue 3 applications, leveraging the capabilities of Three.js and the performance benefits of Vite. Currently stable at version 0.3.4, the library maintains a relatively active release cadence, frequently pushing bug fixes and minor features as seen in recent patch versions (e.g., 0.3.4, 0.3.2). Its primary differentiator is providing a `react-three-fiber`-like component-based API specifically tailored for the Vue ecosystem, allowing developers to compose complex Three.js scenes declaratively using Vue components. It ships with comprehensive TypeScript types, ensuring robust development and better maintainability for large-scale projects. The library aims to abstract away much of the boilerplate associated with direct Three.js integration into a Vue environment, making 3D development more accessible to Vue developers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic TroisJS setup using Vue 3's `createApp` and `script setup` composition API. It initializes a 3D scene with a camera, light, and a rotating box, showing how to register TroisJS components globally via `TroisJSVuePlugin` and access Three.js objects for animation.

import { createApp } from 'vue'
import { TroisJSVuePlugin } from 'troisjs'
import { ref, onMounted } from 'vue'

const app = createApp({
  template: `
    <renderer ref="rendererRef" antialias orbit-ctrl resize="window">
      <camera :position="{ z: 10 }"></camera>
      <scene>
        <point-light :position="{ y: 50, z: 50 }"></point-light>
        <box ref="boxRef" :rotation="{ y: rotation.y, z: rotation.z }">
          <lambert-material></lambert-material>
        </box>
      </scene>
    </renderer>
  `,
  setup() {
    const rendererRef = ref(null)
    const boxRef = ref(null)
    const rotation = ref({ y: Math.PI / 4, z: Math.PI / 4 })

    onMounted(() => {
      // Access the Three.js scene, camera, etc. via rendererRef.value.scene, etc.
      if (rendererRef.value && boxRef.value) {
        console.log('Renderer and Box components mounted.')
        // Example: animate box rotation
        rendererRef.value.onBeforeRender(() => {
          rotation.value.y += 0.01
          rotation.value.z += 0.005
        })
      }
    })

    return { rendererRef, boxRef, rotation }
  }
})

app.use(TroisJSVuePlugin).mount('#app')

view raw JSON →