Sunday, 27 October 2019

Path for Three.js using node module

Im learning Three.js and have set up an basic project running a node.js server and importing Three.js as a node moudule.

Actually my setup works but Im a little bit confused if this is a good setup?

The thing I am thinking about is basically the long path for my node_module. On some pages Three.js is getting imported just through:

import * as THREE from 'three';

but in my case I have to write the full path:

import * as THREE from './node_modules/three/build/three.module.js';

Is this correct implementation?

Here is my full code:

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <title>three.js webgl</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <style>
        body {
            background-color: #cce0ff;
            color: #000;
        }

        a {
            color: #080;
        }
    </style>
</head>

<body>
    <script type="module" src="index.js"></script>
</body>

</html>

index.js

**import * as THREE from './node_modules/three/build/three.module.js';**

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

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);

camera.position.z = 5;

var animate = function () {
    requestAnimationFrame(animate);

    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;

    renderer.render(scene, camera);
};

animate();

Do i need to use webpack to bundle. Can that solve that it cant find the path to my nodemodules?



from Path for Three.js using node module

No comments:

Post a Comment