Ask Your Question
1

How can I load splines in Nuxt?

asked 2021-11-06 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-10-28 03:00:00 +0000

nofretete gravatar image

To load splines in Nuxt, you can use the glslify-loader and the three library.

  1. Install the required packages:
npm install --save-dev glslify-loader three
  1. Create a new file in the plugins directory, for example threejs.js, and add the following code:
import * as THREE from 'three';
import glslify from 'glslify';

// Load require glslify shaders
THREE.ShaderChunk.spline_vert = glslify('./shaders/spline/vertex.glsl');
THREE.ShaderChunk.spline_frag = glslify('./shaders/spline/fragment.glsl');
  1. Create a new directory inside the static directory, for example shaders/spline, and add the vertex.glsl and fragment.glsl files inside this directory. These files should contain the code for the spline vertex shader and the spline fragment shader.

  2. Edit the nuxt.config.js file and add the following lines to the plugins array:

{
  src: '~/plugins/threejs.js',
  ssr: false
}

Now you can use the splines in your Vue components by adding a THREE.Line object to your scene with a THREE.CatmullRomCurve3 as its geometry. For example:

import * as THREE from 'three';

export default {
  mounted() {
    // Create a new scene
    const scene = new THREE.Scene();

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

    // Create a new renderer
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // Create a new curve
    const curve = new THREE.CatmullRomCurve3([
      new THREE.Vector3(-2, 0, 2),
      new THREE.Vector3(-1, 1, 1),
      new THREE.Vector3(0, 0, 0),
      new THREE.Vector3(1, -1, 1),
      new THREE.Vector3(2, 0, 2)
    ]);

    // Create a new material
    const material = new THREE.LineBasicMaterial({
      color: new THREE.Color('white'),
      linewidth: 3,
      linecap: 'round', // rounded ends
      linejoin: 'round' // rounded corners
    });

    // Create a new spline
    const spline = new THREE.Line(new THREE.BufferGeometry().setFromPoints(curve.getPoints(50)), material);

    // Add spline to scene
    scene.add(spline);

    // Animate scene
    const animate = function () {
      requestAnimationFrame(animate);

      renderer.render(scene, camera);
    };

    animate();
  }
}

This will create a new THREE.Line object with a THREE.CatmullRomCurve3 as its geometry, and add it to the scene. The animate function will keep rendering the scene.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-06 11:00:00 +0000

Seen: 7 times

Last updated: Oct 28 '22