Advertisement
GlobalLiquidity

Untitled

Mar 12th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import * as bjs from 'babylonjs';
  3.  
  4. import {
  5.     generateSkybox,
  6.     generateWaterMaterial
  7. } from '../global/utils'
  8.  
  9. import {
  10.     ddsGc256SpecularHDR,
  11.     pngGlNormal,
  12. } from '../global/assets'
  13. import { create } from 'domain';
  14.  
  15.  
  16. export interface IScene
  17. {
  18.     title: string;
  19.     canvas : HTMLElement;
  20.     engine : bjs.Engine;
  21.     bjsScene : bjs.Scene;
  22.     camera : bjs.Camera;
  23.     light : bjs.PointLight;
  24. }
  25.  
  26.  
  27. export class Scene implements IScene
  28. {
  29.     engine : bjs.Engine;
  30.     bjsScene : bjs.Scene;
  31.     camera : bjs.ArcRotateCamera;
  32.     light : bjs.PointLight;
  33.     hdrTexture : bjs.CubeTexture;
  34.     hdrSkybox : bjs.Mesh;
  35.  
  36.     constructor(public title: string, public canvas : HTMLElement)
  37.     {
  38.         this.engine = new bjs.Engine(this.canvas as HTMLCanvasElement);
  39.         this.bjsScene = new bjs.Scene(this.engine);
  40.         this.createBaseScene();
  41.     }
  42.  
  43.     public createBaseScene () {
  44.         BABYLON.Tools.Log("Creating Base Scene");
  45.         this.bjsScene.clearColor = new bjs.Color4(0.05, 0.05, 0.1, 1.0);
  46.  
  47.         this.camera = new bjs.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 300, bjs.Vector3.Zero(), this.bjsScene);
  48.         this.camera.lowerBetaLimit = ((Math.PI /2) * 0.25);
  49.         this.camera.upperBetaLimit = (Math.PI /2) - ((Math.PI /2) * 0.025);
  50.         this.camera.lowerRadiusLimit = 30;
  51.         this.camera.upperRadiusLimit = 300;
  52.         this.camera.alpha=  Math.PI / 2 - (Math.PI * 0.2);
  53.         this.camera.beta = Math.PI / 2 - (Math.PI * 0.1);
  54.        
  55.         this.camera.attachControl(this.canvas, true);
  56.  
  57.         // Environment Texture
  58.         this.hdrTexture = bjs.CubeTexture.CreateFromPrefilteredData(ddsGc256SpecularHDR, this.bjsScene);
  59.    
  60.         this.bjsScene.imageProcessingConfiguration.exposure = 0.6;
  61.         this.bjsScene.imageProcessingConfiguration.contrast = 1.6;
  62.        
  63.         // Skybox
  64.         this.hdrSkybox = generateSkybox(1000.0, this.hdrTexture, this.bjsScene);
  65.         const light: bjs.HemisphericLight = new bjs.HemisphericLight("HemiLight", new bjs.Vector3(0.5, 1, 1), this.bjsScene);
  66.  
  67.         this.light = new bjs.PointLight("light", new bjs.Vector3(0, 0, 0), this.bjsScene);
  68.         this.light.diffuse = new bjs.Color3(1, 1, 1);
  69.         this.light.intensity = 1.0;
  70.  
  71.         this.bjsScene.activeCameras = [this.camera];
  72.  
  73.         this.bjsScene.registerBeforeRender(() => {
  74.             this.light.position = this.camera.position;
  75.         });
  76.  
  77.         this.engine.runRenderLoop(() => {
  78.             if (this.bjsScene) {
  79.                 this.light.position = this.camera.position;
  80.                 this.bjsScene.render();
  81.             }
  82.         });
  83.  
  84.         this.createScene();
  85.     }
  86.    
  87.     protected createScene()
  88.     {
  89.         //to be filled in by sublass
  90.     }
  91. }
  92.  
  93. export interface ISceneElement
  94. {
  95.     scene : IScene;
  96.     transform : bjs.TransformNode;
  97. }
  98.  
  99. export class SceneElement implements ISceneElement
  100. {
  101.     scene : Scene;
  102.     transform : bjs.TransformNode;
  103.  
  104.     constructor(public name:string, public x: number, public y: number, public z: number, scene:Scene)
  105.     {
  106.         BABYLON.Tools.Log("Scene Element Ctor");
  107.         this.scene = scene;
  108.         this.transform = new bjs.TransformNode(name);
  109.         this.transform.setAbsolutePosition(new bjs.Vector3(x,y,z));
  110.         this.create();
  111.     }
  112.  
  113.     protected create()
  114.     {
  115.  
  116.     }
  117.  
  118.     protected onRender()
  119.     {
  120.        
  121.     }
  122. }
  123.  
  124. export class GlobalLiqudityLogo extends SceneElement
  125. {
  126.     public logo: bjs.Mesh;
  127.  
  128.     protected create()
  129.     {
  130.         BABYLON.Tools.Log("Logo : create()");
  131.         const plasticMaterial: any = new bjs.PBRMaterial("plastic", this.scene.bjsScene);
  132.         plasticMaterial.albedoColor = bjs.Color3.White();
  133.         plasticMaterial.albedoColor =  new bjs.Color3(0, 0, 0.2);
  134.         plasticMaterial.normalTexture =  new bjs.Texture(pngGlNormal, this.scene.bjsScene);
  135.         plasticMaterial.microSurface = 0.75;
  136.         plasticMaterial.reflectivityColor = new bjs.Color3(0.215, 0.351, 0.727);
  137.         plasticMaterial.reflectionTexture = this.scene.hdrTexture;  
  138.         plasticMaterial.useLogarithmicDepth = false;
  139.  
  140.         BABYLON.Tools.Log("Loading Logo");
  141.         bjs.SceneLoader.ImportMesh("", "./dist/", 'gl.babylon', this.scene.bjsScene, (newMeshes: Array<bjs.AbstractMesh>) => {
  142.             newMeshes[0].position.set(0, 10, 0);
  143.             newMeshes[0].rotation.y = Math.PI;
  144.             newMeshes[0].scaling.set(0.125, 0.125, 0.125);
  145.             newMeshes[0].material = plasticMaterial;
  146.             this.logo = newMeshes[0] as bjs.Mesh;
  147.             this.logo.parent = this.transform;
  148.         });
  149.     }
  150. }
  151.  
  152. export class HomeScene extends Scene
  153. {
  154.     logo : GlobalLiqudityLogo;
  155.  
  156.     protected createScene()
  157.     {
  158.           // Water material
  159.           const waterMaterial: bjs.WaterMaterial = generateWaterMaterial(this.bjsScene);
  160.  
  161.           // Water mesh
  162.           const waterMesh: bjs.Mesh = bjs.Mesh.CreateGround("waterMesh", 512, 512, 32, this.bjsScene, false);
  163.           waterMesh.position.y = -35;
  164.           waterMesh.material = waterMaterial;
  165.           waterMaterial.addToRenderList(this.hdrSkybox);
  166.  
  167.           this.logo = new GlobalLiqudityLogo("logo",0,25,0,this);
  168.           waterMaterial.addToRenderList(this.logo.logo)
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement