Skip to content

Commit

Permalink
add first-person example
Browse files Browse the repository at this point in the history
  • Loading branch information
yomotsu committed Apr 30, 2020
1 parent 4a34f20 commit 6663412
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions examples/first-person.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
<label><input type="checkbox" onchange="cameraControls.verticalDragToForward = this.checked">vertical drag to move forward</label>
<br>
<label><input type="checkbox" onchange="cameraControls.dollyToCursor = this.checked">dolly to cursor</label>
<br>
<button onclick="cameraControls.rotate( 45 * THREE.Math.DEG2RAD, 0, true )">rotate theta 45deg</button>
<button onclick="cameraControls.rotate( -90 * THREE.Math.DEG2RAD, 0, true )">rotate theta -90deg</button>
<button onclick="cameraControls.rotate( 360 * THREE.Math.DEG2RAD, 0, true )">rotate theta 360deg</button>
<button onclick="cameraControls.rotate( 0, 20 * THREE.Math.DEG2RAD, true )">rotate phi 20deg</button>
<br>
<button onclick="cameraControls.truck( 1, 0, true)">truck( 1, 0 )</button>
<button onclick="cameraControls.truck( 0, 1, true)">truck( 0, 1 )</button>
<button onclick="cameraControls.truck( -1, -1, true )">truck( -1, -1 )</button>
<br>
<button onclick="cameraControls.zoom( camera.zoom / 2, true )">zoom `camera.zoom / 2`</button>
<button onclick="cameraControls.zoom( -camera.zoom / 2, true )">zoom `- camera.zoom / 2`</button>
<br>
<button onclick="cameraControls.moveTo( 3, 5, 2, true )">move to( 3, 5, 2 )</button>
<button onclick="cameraControls.fitTo( mesh, true )">fit to the bounding box of the mesh</button>
<button onclick="cameraControls.reset( true )">reset</button>
</div>

<script src="https://unpkg.com/three@0.115.0/build/three.min.js"></script>
<script src="../dist/camera-controls.js"></script>
<script>
CameraControls.install( { THREE: THREE } );

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );

const EPS = 1e-5;
// in order to archive FPS look, set EPSILON for the distance to the center
camera.position.set( 0, 0, EPS );
const renderer = new THREE.WebGLRenderer( { antialias: true, stencil: false } );
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );
// cameraControls.maxDistance = EPS;
cameraControls.azimuthRotateSpeed = - 0.3; // negative value to invert rotation direction
cameraControls.polarRotateSpeed = - 0.3; // negative value to invert rotation direction
cameraControls.truckSpeed = 1 / EPS * 3;
cameraControls.mouseButtons.wheel = CameraControls.ACTION.ZOOM;
cameraControls.touches.two = CameraControls.ACTION.TOUCH_ZOOM_TRUCK;
cameraControls.saveState();

const mesh = new THREE.Mesh(
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

renderer.render( scene, camera );

( function anim () {

const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
const updated = cameraControls.update( delta );

// if ( elapsed > 30 ) { return; }

requestAnimationFrame( anim );

if ( updated ) {

renderer.render( scene, camera );
console.log( 'rendered' );

}

} )();
</script>

</body>
</html>
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A camera control for three.js, similar to THREE.OrbitControls yet supports smoot
- [combined gestures](https://yomotsu.github.io/camera-controls/examples/combined-gestures.html)
- [keyboard events](https://yomotsu.github.io/camera-controls/examples/keyboard.html)
- [collision](https://yomotsu.github.io/camera-controls/examples/collision.html)
- [first-person](https://yomotsu.github.io/camera-controls/examples/first-person.html)
- [rotate with duration and easing](https://yomotsu.github.io/camera-controls/examples/easing.html) (with [tween.js](https://github.com/tweenjs/tween.js))
- [path animation](https://yomotsu.github.io/camera-controls/examples/path-animation.html) (with [tween.js](https://github.com/tweenjs/tween.js))

Expand Down

0 comments on commit 6663412

Please sign in to comment.