three.js dev template - module
HTML
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.151/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.151/examples/jsm/"
}
}
</script>
CSS
body {
margin: 0px;
}
JavaScript
// Simple three.js example
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { ParametricGeometry } from 'three/addons/geometries/ParametricGeometry.js';
import { ParametricGeometries } from 'three/addons/geometries/ParametricGeometries.js';
let mesh, renderer, scene, camera, controls;
init();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
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
const light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 20, 20, 0 );
scene.add( light );
// axes
scene.add( new THREE.AxesHelper( 20 ) );
// geometry
const geometry = new ParametricGeometry( radialWave, 20, 20 );
// material
const material = new THREE.MeshPhongMaterial( {
color: 0x00ffff,
side: THREE.DoubleSide
} );
// mesh
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
function radialWave( u, v, target ) {
const r = 10;
const x = Math.sin( u ) * r;
const z = Math.sin( v / 2 ) * 2 * r;
const y = ( Math.sin( u * 4 * Math.PI ) + Math.cos( v * 2 * Math.PI ) ) * 2.8;
return target.set( x, y, z );
}