Last active
October 23, 2018 18:57
-
-
Save pyadav/7d9a77b35ecd755644c033f7ec236f5c to your computer and use it in GitHub Desktop.
A basic three.js setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(){ | |
| var camera, scene, renderer; | |
| var geometry, material, mesh; | |
| // Set the scene size. | |
| const WIDTH = window.innerWidth; | |
| const HEIGHT = window.innerHeight; | |
| // Set some camera attributes. | |
| const VIEW_ANGLE = 70; // FOV (field of view) | |
| const ASPECT = WIDTH / HEIGHT; | |
| const NEAR = 0.01; | |
| const FAR = 10; | |
| // Set up the box geometry attributes | |
| const BOX_WIDTH = 0.2; | |
| const BOX_HEIGHT = 0.2; | |
| const BOX_DEPTH = 0.2; | |
| init(); | |
| animate(); | |
| function init() { | |
| // Create an empty scene | |
| scene = new THREE.Scene(); | |
| // Create a WebGL renderer with Antialiasing and add into DOM to display | |
| renderer = new THREE.WebGLRenderer( { antialias: true } ); | |
| renderer.setSize( WIDTH, HEIGHT ); | |
| document.body.appendChild( renderer.domElement ); | |
| // Create a basic perspective camera | |
| camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR ); | |
| camera.position.z = 1; | |
| // Create a geometry and basic material to apply on mesh | |
| geometry = new THREE.BoxGeometry( BOX_WIDTH, BOX_HEIGHT, BOX_DEPTH ); | |
| material = new THREE.MeshNormalMaterial(); | |
| // Create a mesh object using geometry & material | |
| mesh = new THREE.Mesh( geometry, material ); | |
| scene.add( mesh ); | |
| } | |
| // Render Loop to animate | |
| function animate() { | |
| requestAnimationFrame( animate ); | |
| mesh.rotation.x += 0.01; | |
| mesh.rotation.y += 0.02; | |
| renderer.render( scene, camera ); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment