Masterchoc

Untitled

Dec 14th, 2017
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Maths from '../core/Maths'
  2. import Camera from './Camera'
  3. import {vec2, vec3, mat4, glMatrix} from 'gl-matrix'
  4.  
  5. export default class FlyingCamera extends Camera {
  6.     constructor(scene) {
  7.         super(scene);
  8.         this.position = vec3.create();
  9.         this.rotation = vec3.create();
  10.         this.velocity = vec3.create();
  11.         this.friction = vec3.fromValues(.9, .9, .9);
  12.         this.positionSpeed = 0.1;
  13.         this.rotationSpeed = 0.1;
  14.         this.wheelPrecision = 0.5;
  15.  
  16.         scene.engine.input.on('mousewheel', delta => {
  17.             this.positionSpeed += delta * this.wheelPrecision;
  18.             if (this.positionSpeed < 0)
  19.                 this.positionSpeed = 0;
  20.         });
  21.  
  22.         scene.engine.input.on('mousemove', (position, delta) => {
  23.             this.pointer(delta);
  24.         })
  25.     }
  26.  
  27.     update() {
  28.         this.move();
  29.         this.view();
  30.     }
  31.  
  32.     move() {
  33.         let direction = vec3.create();
  34.         let moveSpeed = this.positionSpeed * this.scene.engine.clock.deltaTime;
  35.         let rotSpeed = this.rotationSpeed * this.scene.engine.clock.deltaTime * 10;
  36.  
  37.         if (this.keys.left
  38.             || this.keys.right
  39.             || this.keys.shift
  40.             || this.keys.space
  41.             || this.keys.up
  42.             || this.keys.down)
  43.             this.emit('playermoved');
  44.  
  45.         if (this.keys.left)  direction[0] += moveSpeed;
  46.         if (this.keys.right) direction[0] -= moveSpeed;
  47.         if (this.keys.shift) direction[1] += moveSpeed;
  48.         if (this.keys.space) direction[1] -= moveSpeed;
  49.         if (this.keys.up)    direction[2] += moveSpeed;
  50.         if (this.keys.down)  direction[2] -= moveSpeed;
  51.  
  52.         if (this.keys.arrow_up) this.rotation[0] -= rotSpeed;
  53.         if (this.keys.arrow_down) this.rotation[0] += rotSpeed;
  54.         if (this.keys.arrow_left) this.rotation[1] -= rotSpeed;
  55.         if (this.keys.arrow_right) this.rotation[1] += rotSpeed;
  56.  
  57.         this.limitRotation();
  58.  
  59.         mat4.identity(this.viewMatrix);
  60.         mat4.rotateX(this.viewMatrix, this.viewMatrix, this.rotation[0]);
  61.         mat4.rotateY(this.viewMatrix, this.viewMatrix, this.rotation[1]);
  62.         mat4.invert(this.viewMatrix, this.viewMatrix);
  63.  
  64.         Maths.mat4MulVec3(this.viewMatrix, direction);
  65.  
  66.         vec3.add(this.velocity, this.velocity, direction);
  67.         vec3.mul(this.velocity, this.velocity, this.friction);
  68.         vec3.add(this.position, this.position, this.velocity);
  69.     }
  70.  
  71.     view() {
  72.         mat4.identity(this.viewMatrix);
  73.         mat4.rotateX(this.viewMatrix, this.viewMatrix, this.rotation[0]);
  74.         mat4.rotateY(this.viewMatrix, this.viewMatrix, this.rotation[1]);
  75.         mat4.translate(this.viewMatrix, this.viewMatrix, this.position);
  76.     }
  77.  
  78.     limitRotation() {
  79.         if (this.rotation[1] < 0) this.rotation[1] += Math.PI * 2;
  80.         if (this.rotation[1] >= Math.PI * 2) this.rotation[1] -= Math.PI * 2;
  81.  
  82.         if (this.rotation[0] < -Math.PI / 2) this.rotation[0] = -Math.PI / 2;
  83.         if (this.rotation[0] > Math.PI / 2) this.rotation[0] = Math.PI / 2;
  84.     }
  85.  
  86.     pointer(delta) {
  87.         vec2.add(this.mouse, this.mouse, delta);
  88.         this.rotation[1] += glMatrix.toRadian(this.mouse[0]) * this.rotationSpeed;
  89.         this.rotation[0] += glMatrix.toRadian(this.mouse[1]) * this.rotationSpeed;
  90.  
  91.         this.limitRotation();
  92.     }
  93. }
RAW Paste Data