Hello,
I followed the directions in the installation manual. Then I followed tutorials from other sides. All in hopes to get three.js properly up and running.
I am using Atom to code and the atom-live-server to host.
What worked:
- creating a folder for my project
- creating a “js” folder and putting the three.js file inside
- creating an “index.html” file with script tags to the three.js file and my scene.js file
- creating a scene.js file with the first example (solid rotating green cube) from the manual
What didn’t work:
- I installed three in my project folder via npm
- I deleted the script tag references to three.js
- I added type = “module” to the script tag referencing my scene.js
- I added “import * as THREE from ‘three’;” to my scene.js file
- the example-code does not work any more.
This is my code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type = "module" src="scene.js"></script>
</body>
</html>
scene.js
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
const animate = function () { requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
This is my folder structure:
project/
--node_modules/
----three/
----.package-lock.json
--index.html
--package-lock.json
--package.json
--scene.js
Can anyone help me?