three.js dev template
by MarkTheMule
HTML
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
CSS
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
JavaScript
// Simple three.js example
var mesh, renderer, scene, camera, controls;
init();
animate();
setTimeout(rigth, 2000);
function init() {
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
renderer.domElement.addEventListener(
'mousedown',
function(e){
if (e.buttons == 2)
camInfo('buttondown', camera)
});
// camera
camera = new THREE.OrthographicCamera(
window.innerWidth / -2, window.innerWidth / 2,
window.innerHeight / 2, window.innerHeight / -2,
-500, 500);
//camera = new THREE.PerspectiveCamera( 40,
// window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 20, 20, 20 );
camera.zoom = 5;
scene.addEventListener('mousedown',
function(e){ camInfo('mousedown', camera); });
// controls
controls = new THREE.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( 10 ) );
// geometry
var geometry = new THREE.SphereGeometry( 5, 12, 8 );
// material
var material = new THREE.MeshPhongMaterial( {
color: 0x00ffff,
flatShading: true,
transparent: true,
opacity: 0.7,
} );
// mesh
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
}
function animate() {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
}
function rigth(){
camera.position.set(0,0,20);
camera.lookAt(0,0,0);
camera.rotation.z += Math.PI/2
camera.updateProjectionMatrix();
}
function camInfo(label, camera) {
...