three.js dev template - module

by aDeveloperCase

HTML

<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
    
<script type="importmap">
	{
		"imports": {
			"three": "https://unpkg.com/three/build/three.module.js"
		}
	}
</script>

CSS

body {
	margin: 0px;
}

JavaScript

// Simple three.js example

import * as THREE from "three";
import { OrbitControls } from "https://unpkg.com/three/examples/jsm/controls/OrbitControls.js";

var axisMesh, axisAngle, oldAxisAngle, rotAxis, red, blue, renderer, scene, camera, controls;

init();
animate();

function init() {

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

    // scene
    scene = new THREE.Scene();
    
    // camera
    camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
    camera.position.set( 20, 20, 20 );

    // controls
    controls = new OrbitControls( camera, renderer.domElement );
    
    // ambient
    scene.add( new THREE.AmbientLight( 0x222222 ) );
    
    // light
    var light = new THREE.DirectionalLight( 0xffffff, 1 );
    light.position.set( 20,20, 0 );
    scene.add( light );
    
    // axes
    scene.add( new THREE.AxesHelper( 20 ) );

    // geometry
    var geo1 = new THREE.BoxGeometry(5, 1, 1);
    
    // material
    var mat1 = new THREE.MeshPhongMaterial( {
        color: 0xff0000, 
        flatShading: true,
        transparent: true,
        opacity: 0.7,
    } );
    var mat2 = new THREE.MeshPhongMaterial( {
        color: 0x0000ff, 
        flatShading: true,
        transparent: true,
        opacity: 0.7,
    } );
    
    var axisGeo = new THREE.BoxGeometry(0.2, 0.2, 30);
    var axisMat = new THREE.MeshPhongMaterial({
        color: 0xffff00, 
        flatShading: true,
        transparent: true,
        opacity: 0.7,
    });
    
    // mesh
    red = new THREE.Mesh( geo1, mat1 );
    blue = new THREE.Mesh(geo1, mat2);
    
    axisMesh = new THREE.Mesh(axisGeo, axisMat);
    
   	blue.position.set(10, 0, 0)
    scene.add( red, blue, axisMesh );
    
    axisAngle = new...