Advertisement
Guest User

engine.js

a guest
May 27th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { GridMaterial } from "@babylonjs/materials"
  2.  
  3.  
  4. export default class Engine{
  5.     constructor(){
  6.         // Scene setup
  7.         this.canvas = document.getElementById("renderCanvas");
  8.         this.engine = new BABYLON.Engine(this.canvas);      
  9.         this.scene = new BABYLON.Scene(this.engine);
  10.         this.active = false;
  11.        
  12.         // Camera setup
  13.         this.camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, 2.01, -8), this.scene);        
  14.         this.camera.attachControl(this.canvas, true);
  15.         this.camera.speed = 0.2;      
  16.         this.camera.ellipsoid = new BABYLON.Vector3(1, 1, 1); // Collision box for the camera
  17.         this.camera.checkCollisions = true;
  18.         this.camera.applyGravity = true;
  19.  
  20.         // Enable collisions and gravity in scene
  21.         this.scene.collisionsEnabled = true
  22.         this.scene.gravity = new BABYLON.Vector3(0, -0.05, 0)
  23. }
  24.  
  25.     assetManager(){
  26.  
  27.         var camera = this.camera;
  28.         var scene = this.scene;
  29.  
  30.         // Add lights to the scene
  31.         var light0 = new BABYLON.DirectionalLight("Omni", new BABYLON.Vector3(-2, -5, 2), scene)
  32.         var light1 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(2, -5, -2), scene)
  33.  
  34.         var assetsManager = new BABYLON.AssetsManager(scene);    
  35.         // Called when a single task has been sucessfull
  36.         assetsManager.onTaskSuccessObservable.add(function(task) {        
  37.             //console.log("task successful", task);            
  38.  
  39.             // Setting ground material
  40.             var ground = scene.getMeshByName("ground");          
  41.             ground.material = new GridMaterial("groundMaterial", scene)    
  42.             ground.material.diffuseColor = new BABYLON.Color3(1, 1, 1)
  43.             ground.material.backFaceCulling = false
  44.            
  45.             var gun = scene.getMeshByName("SMDImport");// Pistol                    
  46.             gun.parent = camera;        
  47.             gun.rotation.z =  Math.PI;        
  48.             gun.rotation.y = -Math.PI;
  49.        
  50.             gun.scaling = new BABYLON.Vector3( 0.1, 0.1, 0.1);
  51.             gun.position = new BABYLON.Vector3(1, -1, 1);
  52.            
  53.         });
  54.        
  55.         // Called when all tasks in the assetsManger are done
  56.         assetsManager.onTasksDoneObservable.add(function(tasks) {
  57.  
  58.             var errors = tasks.filter(function(task) {return task.taskState === BABYLON.AssetTaskState.ERROR});
  59.             var successes = tasks.filter(function(task) {return task.taskState !== BABYLON.AssetTaskState.ERROR});
  60.             //console.log(tasks);
  61.         });
  62.  
  63.         // We add single tasks to the assetsManager
  64.         assetsManager.addMeshTask("task", "", "../assets/models/", "test6.babylon");
  65.         assetsManager.addMeshTask("task", "", "../assets/models/", "deagle.obj");
  66.  
  67.         // Now let the assetsManger load/excecute every task
  68.         assetsManager.load();
  69.     }
  70.  
  71.  
  72.     pointerLock(){
  73.         var canvas = this.canvas;
  74.         var scene = this.scene;
  75.         var camera = this.camera;
  76.         var isLocked = false
  77.    
  78.         scene.onPointerDown = function (evt) {
  79.    
  80.             if (document.pointerLockElement !== canvas) {
  81.                 console.log("Was Already locked: ", document.pointerLockElement === canvas)
  82.    
  83.                 if (!isLocked) {
  84.                     canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock || false
  85.                    
  86.                     if (canvas.requestPointerLock) {
  87.                         canvas.requestPointerLock()
  88.                     }
  89.                 }
  90.             }
  91.             //continue with shooting requests or whatever :P
  92.             //evt === 0 (left mouse click)
  93.             //evt === 1 (mouse wheel click (not scrolling))
  94.             //evt === 2 (right mouse click)
  95.  
  96.             if(evt.button == 0){
  97.  
  98.                 let array = camera.getChildren();
  99.                 //console.log(array[0]);
  100.                  
  101.                 let ray = camera.getForwardRay(10000);
  102.                 let hit = scene.pickWithRay(ray);
  103.                 let model = hit.pickedMesh;                  
  104.        
  105.                 if(hit !== null && model !== null){
  106.                     console.log(model.name);
  107.                     scene.getMeshByName(model.name).dispose();      
  108.                 }                              
  109.             }            
  110.         }
  111.        
  112.  
  113.         // Event listener when the pointerlock is updated (or removed by pressing ESC for example).
  114.         var pointerlockchange = function () {
  115.             var controlEnabled = document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement || document.msPointerLockElement || false
  116.    
  117.             // If the user is already locked
  118.             if (!controlEnabled) {
  119.                 camera.detachControl(canvas)                
  120.                 isLocked = false
  121.                 this.active = false
  122.                 //console.log(this.active);
  123.                
  124.             } else {
  125.                 camera.attachControl(canvas)                
  126.                 isLocked = true
  127.                 this.active = true;
  128.                 //console.log(this.active);
  129.             }
  130.         }
  131.    
  132.         // Attach events to the document
  133.         document.addEventListener("pointerlockchange", pointerlockchange, false)
  134.         document.addEventListener("mspointerlockchange", pointerlockchange, false)
  135.         document.addEventListener("mozpointerlockchange", pointerlockchange, false)
  136.         document.addEventListener("webkitpointerlockchange", pointerlockchange, false)
  137.     }
  138.  
  139.     render(){
  140.         // Render every frame
  141.         this.engine.runRenderLoop(() => {
  142.             this.scene.render()                        
  143.         })    
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement