Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>My first three.js app</title>
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body { margin: 0; }
- canvas { width: 100%; height: 100% }
- </style>
- </head>
- <body>
- <script src="js/three.js"></script>
- <script src="js/webvr.js"></script>
- <script>
- var loader = new THREE.TextureLoader();
- var texture = loader.load( "assets/UV_Grid_Sm.jpg" );
- texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
- texture.repeat.set( 0.008, 0.008 );
- var scene = new THREE.Scene();
- var camera = new THREE.PerspectiveCamera( 45, window.innerWidth/window.innerHeight, 0.1, 1000 );
- var renderer = new THREE.WebGLRenderer();
- renderer.setSize( window.innerWidth, window.innerHeight );
- var y_val = 1;
- var gravity = 9.8;
- var Bottom, Sphere;
- var coor = {x: 0, y: y_val, z: 0, deg: 0, ydeg: 0, frame: 0, v_x: 0, v_z: 0};
- var moving = {doing: false, ori_x: 0, ori_y: 0, ori_deg: 0};
- var press = {left: false, right: false, up: false, down: false};
- var moveScale = {move: 6, deg: 2};
- var cursor_now = {x: 0, y: 0};
- var jump = {jumping: false, time: 0};
- var prev_time = 0;
- function init(){
- WEBVR.checkAvailability().catch( function( message ) {
- document.body.appendChild( WEBVR.getMessageContainer( message ) );
- } );
- document.body.appendChild( renderer.domElement );
- renderer.vr.enabled = true;
- WEBVR.getVRDisplay( function ( display ) {
- renderer.vr.setDevice( display );
- document.body.appendChild( WEBVR.getButton( display, renderer.domElement ) );
- } );
- window.addEventListener( 'resize', onWindowResize, false );
- window.addEventListener( 'keyup', onKeyUp, true);
- window.addEventListener( 'keydown', onKeyDown, true);
- window.addEventListener( 'mousemove', onMouseMove, true);
- window.addEventListener( 'mousedown', onMouseDown, true);
- var sqPts = []
- sqPts.push(new THREE.Vector2(-15,-15));
- sqPts.push(new THREE.Vector2(-15, 15));
- sqPts.push(new THREE.Vector2(15, 15));
- sqPts.push(new THREE.Vector2(15, -15));
- sqPts.push(new THREE.Vector2(-15, -15));
- var square = new THREE.Shape(sqPts);
- var Bottom = new THREE.Mesh(new THREE.ShapeBufferGeometry(square), new THREE.MeshBasicMaterial( {map: texture} ));
- Bottom.position.set(0, 0, 0);
- Bottom.rotation.x = -Math.PI/2;
- scene.add(Bottom);
- var geometry = new THREE.SphereGeometry( 0.2, 32, 32 );
- var material = new THREE.MeshBasicMaterial( {color: 0xff0000} );
- Sphere = new THREE.Mesh( geometry, material );
- scene.add( Sphere );
- }
- function animate(){
- var movingTask = function(){
- requestAnimationFrame( animate );
- camera.position.x = coor.x;
- camera.position.z = coor.z;
- var plus_y = 0;
- var now_time = new Date().getTime();
- var delta_time = (now_time - prev_time) / 1000.0;
- prev_time = now_time;
- if(press.up || press.down || press.left || press.right) coor.frame += 1;
- if(jump.jumping){
- var now_time = (now_time - jump.time) / 1000.0;
- if(now_time > 2.0 * Math.PI / 9.8){
- jump.jumping = false;
- }else{
- plus_y = Math.PI * now_time - 0.5 * gravity * now_time * now_time;
- }
- }
- coor.v_x = 0; coor.v_z = 0;
- if(press.up){
- coor.v_z -= moveScale.move * Math.cos(coor.deg);
- coor.v_x += moveScale.move * Math.sin(coor.deg);
- }
- if(press.down){
- coor.v_z += moveScale.move * Math.cos(coor.deg);
- coor.v_x -= moveScale.move * Math.sin(coor.deg);
- }
- if(press.left){
- coor.v_x -= moveScale.move * Math.cos(coor.deg) / 2;
- coor.v_z -= moveScale.move * Math.sin(coor.deg) / 2;
- }
- if(press.right){
- coor.v_x += moveScale.move * Math.cos(coor.deg) / 2;
- coor.v_z += moveScale.move * Math.sin(coor.deg) / 2;
- }
- //console.log(delta_time, coor.v_x, coor.v_z);
- coor.x += delta_time * coor.v_x; coor.z += delta_time * coor.v_z;
- //press.up = false; press.down = false; press.left = false; press.right = false;
- var real_y = (y_val - 0.1) + 0.1 * Math.cos(coor.frame / 6.0);
- camera.position.y = real_y + plus_y;
- camera.lookAt(new THREE.Vector3(coor.x + 100 * Math.sin(coor.deg), (y_val + plus_y) + 100 * Math.sin(coor.ydeg), coor.z - 100 * Math.cos(coor.deg)));
- Sphere.position.x = coor.x + 100 * Math.sin(coor.deg);
- Sphere.position.y = (y_val + plus_y) + 100 * Math.sin(coor.ydeg);
- Sphere.position.z = coor.z - 100 * Math.cos(coor.deg);
- if(moving.doing) document.body.style.cursor = "none";
- else document.body.style.cursor = "default";
- var width = window.innerWidth; var height = window.innerHeight;
- if(moving.doing){
- if(cursor_now.x <= 0.02 * width){
- moving.ori_deg -= moveScale.deg * Math.PI / 180.0;
- coor.deg -= moveScale.deg * Math.PI / 180.0;
- }if(cursor_now.x >= 0.98 * width){
- moving.ori_deg += moveScale.deg * Math.PI / 180.0;
- coor.deg += moveScale.deg * Math.PI / 180.0;
- }
- }
- }
- movingTask();
- renderer.render(scene, camera);
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onMouseMove(e){
- cursor_now.x = e.clientX;
- cursor_now.y = e.clientY;
- if(moving.doing){
- //console.log(e.clientX, e.clientY);
- var new_x = e.clientX, new_y = e.clientY;
- var width = window.innerWidth; var height = window.innerHeight;
- coor.ydeg = Math.PI / 2.0 * Math.min(0.5, Math.max(-0.5, (moving.ori_y - new_y)/ height));
- coor.deg = moving.ori_deg - Math.PI * Math.min(0.5, Math.max(-0.5, (moving.ori_x - new_x)/ width));
- //console.log(coor.ydeg);
- }
- }
- function onMouseDown(e){
- if(!moving.doing){
- moving.doing = true;
- var width = window.innerWidth; var height = window.innerHeight;
- moving.ori_x = 0.5 * width;//e.clientX;
- moving.ori_y = 0.5 * height;
- moving.ori_deg = coor.deg;
- }
- }
- function onKeyDown(e){
- //console.log("keydown", e.keyCode);
- if(moving.doing){
- // Up
- if(e.keyCode == 38 || e.keyCode == 87){
- if(!(press.up)){
- press.up = true;
- }
- }
- // Down
- if(e.keyCode == 40 || e.keyCode == 83){
- if(!(press.down)){
- press.down = true;
- }
- }
- // Left
- if(e.keyCode == 37 || e.keyCode == 65){
- if(!(press.left)){
- press.left = true;
- }
- //coor.deg -= moveScale.deg * Math.PI / 180.0;
- }
- // Right
- if(e.keyCode == 39 || e.keyCode == 68){
- if(!(press.right)){
- press.right = true;
- }
- }
- // Esc
- if(e.keyCode == 27){
- moving.doing = false;
- }
- // Space
- if(e.keyCode == 32){
- if(!jump.jumping){
- jump.jumping = true;
- jump.time = new Date().getTime();
- }
- }
- }
- }
- function onKeyUp(e){
- //console.log("keyup", e.keyCode);
- if(moving.doing){
- // Up
- if(e.keyCode == 38 || e.keyCode == 87){
- if((press.up)){
- press.up = false;
- }
- }
- // Down
- if(e.keyCode == 40 || e.keyCode == 83){
- if((press.down)){
- press.down = false;
- }
- }
- // Left
- if(e.keyCode == 37 || e.keyCode == 65){
- if((press.left)){
- press.left = false;
- }
- //coor.deg -= moveScale.deg * Math.PI / 180.0;
- }
- // Right
- if(e.keyCode == 39 || e.keyCode == 68){
- if((press.right)){
- press.right = false;
- }
- }
- }
- }
- init();
- animate();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement