A basic THREE.js scene with shadows, lights, camera rotation, and frame rate stats.
Last active
June 18, 2019 02:08
-
-
Save HarryStevens/ae33383726f17ab959413422d6e11daa to your computer and use it in GitHub Desktop.
Sphere
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> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/69/three.js"></script> | |
<script src="https://unpkg.com/[email protected]/examples/js/libs/stats.min.js"></script> | |
<script src="https://unpkg.com/[email protected]/build/geometric.min.js"></script> | |
<style> | |
body { | |
margin: 0; | |
overflow: hidden; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="stats"></div> | |
<div id="scene"></div> | |
<script> | |
const stats = (_ => { | |
const stats = new Stats(); | |
stats.setMode(0); | |
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(-40, 20, -20); | |
camera.lookAt(scene.position); | |
return camera; | |
})(); | |
const renderer = (_ => { | |
const renderer = new THREE.WebGLRenderer(); | |
renderer.setClearColor("#eee"); | |
renderer.setSize(innerWidth, innerHeight); | |
renderer.shadowMapEnabled = true; | |
document.getElementById("scene").appendChild(renderer.domElement); | |
return renderer; | |
})(); | |
const plane = (_ => { | |
const geo = new THREE.PlaneBufferGeometry(30, 30); | |
const mat = new THREE.MeshLambertMaterial({ color: "#fff" }); | |
const plane = new THREE.Mesh(geo, mat); | |
plane.rotation.x = -.5 * Math.PI; | |
plane.receiveShadow = true; | |
scene.add(plane); | |
return plane; | |
})(); | |
const sphere = (_ => { | |
const geo = new THREE.SphereGeometry(4, 32, 32); | |
const mat = new THREE.MeshPhongMaterial({ color: "steelblue"}); | |
const sphere = new THREE.Mesh(geo, mat); | |
sphere.position.y = 4; | |
sphere.castShadow = true; | |
scene.add(sphere); | |
return sphere; | |
})(); | |
const spotlight = (_ => { | |
const spotlight = new THREE.SpotLight("#fff"); | |
spotlight.position.set(-50, 50, -50); | |
spotlight.castShadow = true; | |
scene.add(spotlight); | |
return spotlight; | |
})(); | |
function render() { | |
requestAnimationFrame(render); | |
stats.update(); | |
[camera.position.x, camera.position.z] = geometric.pointRotate([camera.position.x, camera.position.z], 0.5); | |
camera.lookAt(scene.position); | |
renderer.render(scene, camera); | |
} | |
render(); | |
function resize(){ | |
camera.aspect = innerWidth / innerHeight; | |
camera.updateProjectionMatrix(); | |
renderer.setSize(innerWidth, innerHeight); | |
} | |
onresize = resize; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment