Ausnahme gefangen: SSL certificate problem: certificate is not yet valid 📌 Build 3D Scenes Declaratively with TresJS using Vue

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Build 3D Scenes Declaratively with TresJS using Vue


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

For a more interactive experience, read this article here alvarosaburido.dev.

What if I tell you that this scene right here:

Low Poly Planet Scene

It's entirely done using Vue components, yes, Vue components 🤯.

<script setup lang="ts">
import { useGLTF, useTweakPane } from '@tresjs/cientos'
import { useRenderLoop } from '@tresjs/core'
import { shallowRef, watch } from 'vue'
import Airplane from './Airplane.vue'
import Cloud from './Cloud.vue'

const { scene: planet } = await useGLTF(
  'https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/low-poly/planet.gltf',
)

const { pane } = useTweakPane()

const planetRef = shallowRef()

planet.traverse(child => {
  if (child.isMesh) {
    child.receiveShadow = true
  }
})

watch(
  () => planetRef.value,
  planet => {
    if (!planet) return
    pane.addInput(planetRef.value, 'visible', { label: 'Planet' })
  },
)

const { onLoop } = useRenderLoop()

onLoop(({ delta }) => {
  if (!planet) return
  planet.rotation.y += delta * 0.04
  planet.rotation.z += delta * 0.02
  planet.rotation.x += delta * 0.05
  planet.updateMatrixWorld()
})
</script>
<template>
  <TresMesh ref="planetRef" v-bind="planet" />
  <Airplane :planet="planetRef" />
  <Cloud v-for="cloud of [1, 2, 3, 4, 5, 6, 7, 8, 9]" :key="cloud" :planet="planetRef" />
</template>

That's the magic of TresJS, a declarative way of using ThreeJS with Vue components! If you've ever tried to create 3D experiences on the web with ThreeJS, you know how powerful it is but also how complex it can be. TresJS aims to simplify the process and make it more approachable by leveraging the power of Vue's declarative syntax.

Whether you're a seasoned developer or a beginner, TresJS can help you create stunning 3D visuals with less code and less hassle. In this tutorial, we'll cover the basics of TresJS and show you how to create your first 3D scene using Vue components.

By the end of this tutorial, you'll have a solid understanding of how TresJS works and how to use it to create 3D scenes with ease. So, let's get started and see what TresJS can do for you!

You can find the documentation here.

The mighty getting started step

pnpm add three @tresjs/core -D

You can install TresJS as any other Vue plugin

import { createApp } from 'vue'
import App from './App.vue'

import Tres from '@tresjs/core'

export const app = createApp(App)

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

Setting up the experience Canvas

Before we can create a Scene, we need somewhere to display it. Using plain ThreeJS we would need to create a canvas HTML element to mount the WebglRenderer :

const canvas = document.querySelector('canvas'):

const renderer = new THREE.WebGLRenderer(canvas);

With TresJS you only need to add the default component <TresCanvas /> to the template of your Vue component.

<template>
  <TresCanvas> // Your scene is going to live here 
  </TresCanvas>
</template

The TresCanvas component is going to do some setup work behind the scene:

  • 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.

Creating a scene

Scene diagram

We need 3 core elements to create a 3D experience:

With plain ThreeJS we would initialise a scene and a camera instance and then pass them to the renderer method render

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

// And then we render a frame
renderer.render( scene, camera );

With TresJS you can create a Scene using the <TresScene /> component.

<template>
  <TresCanvas>
    <TresScene>
      <!-- Your scene goes here -->
    </TresScene>
  </TresCanvas>
</template>

Then you can add a PerspectiveCamera using the <TresPerspectiveCamera /> component.

<template>
  <TresCanvas>
    <TresPerspectiveCamera />
    <TresScene>
      <!-- Your scene goes here -->
    </TresScene>
  </TresCanvas>
</template>

But wait! What if I want to initialize the camera with some default parameters, like the field of view (fov) THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

Arguments

Some ThreeJS objects have arguments, for example, the PerspectiveCamera constructor has the following arguments:

  • fov - Camera frustum vertical field of view.
  • aspect - Camera frustum aspect ratio.
  • near - Camera frustum near plane.
  • far - Camera frustum far plane.

To pass these arguments to the TresPerspectiveCamera component, you can use the args prop:

<template>
  <TresCanvas>
    <TresPerspectiveCamera :args="[45, 1, 0.1, 1000]" />
    <!-- Your scene goes here -->
  </TresCanvas>
</template>

Adding a Donut 🍩

That scene looks a little empty, let's add a basic object. If we where using plain ThreeJS we would need to create a Mesh object and attach to it a Material and a Geometry like this:

const geometry = new THREE.TorusGeometry(1, 0.5, 16, 32)
const material = new THREE.MeshBasicMaterial({ color: 'orange' })
const donut = new THREE.Mesh(geometry, material)
scene.add(donut)

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 <TresMesh /> component, and between the default slots, we are going to pass a <TresTorusGeometry /> and a <TresMeshBasicMaterial />.

<template>
  <TresCanvas>
    <TresPerspectiveCamera :args="[45, 1, 0.1, 1000]" />
    <TresScene>
      <TresMesh>
        <TresTorusGeometry />
        <TresMeshBasicMaterial color="orange" />
      </TresMesh>
    </TresScene>
  </TresCanvas>
</template>

Notice that we don't need to import anything, thats because TresJS automatically generate a Vue Component based of the Three Object you want to use in CamelCase with a Tres prefix. For example, if you want to use a AmbientLight you would use <TresAmbientLight /> component.

Cheff kiss

Props

You can also pass props to the component, for example, the TresAmbientLight has a intensity property, so you can pass it to the component like this:

<TresAmbientLight :intensity="0.5" />

Set

All properties whose underlying object has a .set() the method has a shortcut to receive the value as an array. For example, the TresPerspectiveCamera has a position property, which is a Vector3 object, so you can pass it to the component like this:

<TresPerspectiveCamera :position="[1, 2, 3]" />

Scalar

Another shortcut you can use is to pass a scalar value to a property that expects a Vector3 object, using the same value for the rest of the Vector:

<TresPerspectiveCamera :position="5" /><TresPerspectiveCamera :position="[5, 5, 5]" />

Color

You can pass colors to the components using the color prop, which accepts a string with the color name or a hex value:

<TresAmbientLight color="teal" /><TresAmbientLight color="#008080" />

And with that, you have everything to get started with TresJS and its cool features. Stay tuned for more articles where we will discover more awesome stuff.

FAQ

Is TresJS open source?

Not yet, but it will be soon, we are still figuring out proper CI/CD and better Typing support

Is there a Nuxt module?

Not yet, but is planned for earlier this year (2023). For now, you can extend nuxtApp.vueApp context with a Nuxt plugin

Can I contribute?

Yeeees 🔥! Just drop me a DM on Twitter @alvarosabu

Video

If you prefer a video format for this tutorial, you can visit it here

...



📌 Build 3D Scenes Declaratively with TresJS using Vue


📈 119.83 Punkte

📌 Moving From Vue 1 To Vue 2 To Vue 3: A Case Study Of Migrating A Headless CMS System


📈 45.61 Punkte

📌 NativeScript-Vue 2.0 erschienen: Native Mobile-Apps mit Vue


📈 30.41 Punkte

📌 Vue i18n – So erstellst Du mehrsprachige Vue.js Anwendungen!


📈 30.41 Punkte

📌 Easy Vue internationalization - guide to the Vue I18n plugin


📈 30.41 Punkte

📌 500+ Vue.js Tailwind CSS UI Components - TailGrids Vue


📈 30.41 Punkte

📌 Vue Designer 1.4 - Desktop editor to visually design Vue applications.


📈 30.41 Punkte

📌 1/7 Learn how to build virtual reality scenes on the web with WebVR and JavaScript


📈 21.58 Punkte

📌 With Codex, creators can use natural language to build 3D scenes


📈 21.58 Punkte

📌 You'll soon be able to build your own scenes for Microsoft Teams


📈 21.58 Punkte

📌 Google Helps Police Identify Devices Close to Crime Scenes Using Location Data


📈 21.29 Punkte

📌 Using scenes in Actions Builder (AoG Pro Tips)


📈 21.29 Punkte

📌 Create custom mockups using 500+ pre-made scenes


📈 21.29 Punkte

📌 Cropping Landsat Scenes from their Bounding Box using Python


📈 21.29 Punkte

📌 Vite.js: Rasantes Build-Tool aus der Vue.js-Schmiede


📈 20.61 Punkte

📌 Vue.js Apps ohne Node, Webpack, npm, Build Tools oder CLI erstellen


📈 20.61 Punkte

📌 Vue 3.1: Migration-Build ermöglicht graduelles Upgraden


📈 20.61 Punkte

📌 Build a Simple CRUD App with Spring Boot and Vue.js


📈 20.61 Punkte

📌 Build complex SPAs quickly with vue-element-admin


📈 20.61 Punkte

📌 Build your own Vue UI library with Unstyled PrimeVue Core and Tailwind CSS


📈 20.61 Punkte

📌 Build and Deploy E-commerce Website with Laravel and Vue.js


📈 20.61 Punkte

📌 Using Vue's v-model With Objects


📈 20.32 Punkte

📌 Essential Advantages of Using Vue.JS for Building Applications


📈 20.32 Punkte

📌 Skeleton Loader using Vue & Tailwind


📈 20.32 Punkte

📌 How To Create A Mobile App Using Vite, Vue and Ionic Capacitor In 8 Minutes Including Explanation


📈 20.32 Punkte

📌 The declarative approach in Vue 3, think twice before using `watch` or mutating your state


📈 20.32 Punkte

📌 How to create a vue project using vite


📈 20.32 Punkte

📌 The declarative approach in Vue 3, think twice before using `watch` or mutating your state


📈 20.32 Punkte

📌 Create a Carousel with Progress Indicators using Tailwind and Vue


📈 20.32 Punkte

📌 Component Testing in Vue: Using routing for states


📈 20.32 Punkte

📌 Using Google Gemini With Flask, Vue and Vite


📈 20.32 Punkte

📌 Behind the Scenes 2015 - Hak5 1919


📈 16.17 Punkte

📌 Behind the Scenes at a Capture the Flag (CTF) Competition


📈 16.17 Punkte











matomo