This is my boilerplate template for getting started with a THREE.js scene.
Last active
July 4, 2019 03:20
-
-
Save HarryStevens/b35577f9df63934616da48114649004b to your computer and use it in GitHub Desktop.
THREE.js Boilerplate
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
license: gpl-3.0 |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
margin: 0; | |
overflow: hidden; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="scene"></div> | |
<div id="stats"></div> | |
<script src="https://unpkg.com/[email protected]/build/three.js"></script> | |
<script src="https://unpkg.com/[email protected]/examples/js/libs/stats.min.js"></script> | |
<script src="https://unpkg.com/[email protected]/examples/js/libs/dat.gui.min.js"></script> | |
<script> | |
const controls = {} | |
const gui = (_ => { | |
const gui = new dat.GUI(); | |
return gui; | |
})(); | |
const stats = (_ => { | |
const stats = new Stats(); | |
stats.domElement.style.position = "absolute"; | |
stats.domElement.style.left = "0px"; | |
stats.domElement.style.top = "0px"; | |
document.getElementById("stats").appendChild(stats.domElement); | |
return stats; | |
})(); | |
const scene = new THREE.Scene(); | |
const camera = (_ => { | |
const camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 0.1, 1000); | |
camera.position.set(20, 20, 20); | |
camera.lookAt(scene.position); | |
return camera; | |
})(); | |
const renderer = (_ => { | |
const renderer = new THREE.WebGLRenderer(); | |
renderer.setClearColor("#fff"); | |
renderer.setPixelRatio(devicePixelRatio); | |
renderer.shadowMap.enabled = true; | |
renderer.shadowMap.type = THREE.PCFSoftShadowMap; | |
renderer.gammaOutput = true; | |
renderer.gammaFactor = 2.2; | |
document.getElementById("scene").appendChild(renderer.domElement); | |
return renderer; | |
})(); | |
function animate(){ | |
requestAnimationFrame(animate); | |
stats.update(); | |
renderer.render(scene, camera); | |
} | |
animate(); | |
function size(){ | |
camera.aspect = innerWidth / innerHeight; | |
camera.updateProjectionMatrix(); | |
renderer.setSize(innerWidth, innerHeight); | |
} | |
size(); | |
onresize = size; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment