var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000);
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var light = new THREE.DirectionalLight(0xffffff, 1);
light.position.setScalar(10, 10, -10);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.25));
scene.add(new THREE.GridHelper(20, 20, 0x404040, 0x404040));
// island
var boxGeo = new THREE.BoxBufferGeometry();
boxGeo.translate(0, 0.5, 0);
var boxMat = new THREE.MeshLambertMaterial({
color: 0x909090
});
for (let x = -5; x < 6; x++) {
for (let y = -3; y < 4; y++) {
let box = new THREE.Mesh(boxGeo, boxMat);
box.scale.y = THREE.Math.randInt(1, 3);
box.position.set(x - 0.5, 0, y - 0.5);
scene.add(box);
let edgesGeo = new THREE.EdgesGeometry(boxGeo);
let edges = new THREE.LineSegments(edgesGeo, new THREE.LineBasicMaterial({
color: 0xaaaaaa
}));
box.add(edges);
}
}
//path
var curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 6, 6),
new THREE.Vector3(12, 6, 0),
new THREE.Vector3(0, 6, -6),
new THREE.Vector3(-12, 6, 0)
])
curve.closed = true;
curve.getPoint(0, camera.position);
camera.lookAt(scene.position);
var mouseX = 0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var currentPoint = 0;
var currPoint = 0;
document.addEventListener('mousedown', onDocumentMouseDown, false);
function onDocumentMouseDown(event) {
event.preventDefault();
document.addEventListener('mousemove', onDocumentMouseMove, false);
document.addEventListener('mouseup', onDocumentMouseUp, false);
document.addEventListener('mouseout', onDocumentMouseOut, false);
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
function onDocumentMouseMove(event) {
mouseX = event.clientX - windowHalfX;
targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
currPoint = (targetRotation - targetRotationOnMouseDown) * 0.05;
}
function onDocumentMouseUp(event) {
document.removeEventListener('mousemove', onDocumentMouseMove, false);
document.removeEventListener('mouseup', onDocumentMouseUp, false);
document.removeEventListener('mouseout', onDocumentMouseOut, false);
}
function onDocumentMouseOut(event) {
document.removeEventListener('mousemove', onDocumentMouseMove, false);
document.removeEventListener('mouseup', onDocumentMouseUp, false);
document.removeEventListener('mouseout', onDocumentMouseOut, false);
}
render();
function render() {
requestAnimationFrame(render);
curve.getPoint(currPoint, camera.position);
camera.lookAt(scene.position);
renderer.render(scene, camera);
}
body {
overflow: hidden;
margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/96/three.min.js"></script>