To load splines in Nuxt, you can use the glslify-loader
and the three
library.
npm install --save-dev glslify-loader three
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');
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.
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.
Asked: 2021-11-06 11:00:00 +0000
Seen: 8 times
Last updated: Oct 28 '22