const renderer = new THREE.WebGLRenderer({antialias: true, alpha: true})
document.body.appendChild(renderer.domElement)
renderer.setSize(200, 200)
renderer.setClearColor(0x55ccff)
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(28, 1, 1, 1000)
const geo = new THREE.SphereBufferGeometry(10, 32, 32)
const leftMat = new THREE.MeshBasicMaterial({color:"red"})
const rightMat = new THREE.MeshBasicMaterial({color:"blue"})
const frontMat = new THREE.MeshBasicMaterial({color:"green"})
const left = new THREE.Mesh(geo, leftMat)
left.position.set(-50, 0, 0)
const right = new THREE.Mesh(geo, rightMat)
right.position.set(50, 0, 0)
const front = new THREE.Mesh(geo, frontMat)
front.position.set(0, 0, -50)
const light = new THREE.PointLight(0xffffff, 1)
scene.add(left, right, light, front, camera)
function render(){
renderer.render(scene, camera)
}
render()
document.getElementById("reset").addEventListener("click", ()=>{
camera.lookAt(front.position)
render()
})
document.getElementById("leftLook").addEventListener("click", ()=>{
camera.lookAt(left.position)
render()
})
document.getElementById("rightLook").addEventListener("click", ()=>{
camera.lookAt(right.position)
render()
})
const axis = new THREE.Vector3().set(0, 1, 0)
document.getElementById("leftTurn").addEventListener("click", ()=>{
camera.rotateOnAxis(axis, Math.PI / 2)
render()
})
document.getElementById("rightTurn").addEventListener("click", ()=>{
camera.rotateOnAxis(axis, Math.PI / -2)
render()
})
.test input {
width: 150px;
}
.right {
float: right;
}
.test {
width: 100%;
}
.test:after {
content: " "
clear: both;
}
canvas {
margin-left: auto;
margin-right: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/103/three.min.js"></script>
<div class="test">
<input id="leftLook" type="button" value="LEFT" /><input id="rightLook" type="button" value="RIGHT" />
</div>
<div class="test">
<input id="leftTurn" type="button" value="LEFT" /><input id="rightTurn" type="button" value="RIGHT" />
</div>
<input id="reset" type="button" value="RESET" /><br />