Advertisement
GlobalLiquidity

Untitled

Mar 24th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as bjs from 'babylonjs';
  2.  
  3. import {
  4.     Scene,
  5. } from '../Scene'
  6.  
  7. import { SolidParticleSystemElement } from './SolidParticleSystemElement';
  8. import { OrderBook } from '../../market/orderbook';
  9. import { MarketSide } from '../../market/enums';
  10.  
  11. export interface IValueFieldUpdateStrategy
  12. {
  13.     valueField : ValueField
  14.  
  15.     preCalculate();
  16.     updateParticle(particle : bjs.SolidParticle)
  17. }
  18.  
  19. export class ValueFieldUpdateStrategy implements IValueFieldUpdateStrategy
  20. {
  21.     constructor(public valueField:ValueField)
  22.     {
  23.      
  24.     }
  25.  
  26.     public preCalculate()
  27.     {
  28.         this.onPreCalculate();
  29.     }
  30.  
  31.     public updateParticle(particle : bjs.SolidParticle)
  32.     {
  33.         this.onUpdateParticle(particle);
  34.     }
  35.  
  36.     protected onPreCalculate()
  37.     {
  38.  
  39.     }
  40.  
  41.     protected onUpdateParticle(particle : bjs.SolidParticle)
  42.     {
  43.        
  44.     }
  45. }
  46.  
  47. export class SineWaveOscillator extends ValueFieldUpdateStrategy
  48. {
  49.     protected theta: number = 0;
  50.  
  51.     constructor(valueField: ValueField, public speed: number, public frequency: number)
  52.     {
  53.         super(valueField);
  54.     }
  55.  
  56.     protected onPreCalculate()
  57.     {
  58.         console.log('SineWaveOscillator :  onPreCalculate()');
  59.         this.theta += this.speed/60;
  60.     }
  61.  
  62.     protected onUpdateParticle(particle : bjs.SolidParticle)
  63.     {
  64.         console.log('SineWaveOscillator :  onUpdateParticle()');
  65.  
  66.         const currentRow: number = particle.idx / this.valueField.columns;
  67.         const currentColumn: number = particle.idx % this.valueField.columns;  
  68.         let cellOffset: number = currentRow * - this.frequency;
  69.         particle.scaling.y = particle.scaling.y +  (Math.sin(this.theta + cellOffset) * 5);
  70.         particle.position.y = particle.scaling.y * 0.5;
  71.     }
  72. }
  73.  
  74. export class ValueField extends SolidParticleSystemElement
  75. {
  76.     private cellHeight: number = 5;
  77.     private cellWidth: number = 1;
  78.     private cellDepth: number = 1;
  79.  
  80.     private updateStrategy:ValueFieldUpdateStrategy;
  81.    
  82.  
  83.     constructor(name:string, public x: number, public y: number, public z: number, scene:Scene, public rows:number, public columns: number)
  84.     {
  85.        
  86.         super(
  87.             name,
  88.             x,
  89.             y,
  90.             z,
  91.             scene,
  92.             bjs.MeshBuilder.CreateBox("box", { height: 1, width: 0.9, depth:0.9 }, scene.bjsScene),
  93.             new bjs.PBRMaterial(name + "-material", scene.bjsScene),
  94.             rows * columns,
  95.         );
  96.  
  97.         console.log('ValueField :  constructor()');
  98.         console.log('ValueField :  creating ValueField with ' + rows + ' rows, and ' + columns + ' columns.');
  99.         this.create();
  100.     }
  101.    
  102.     protected create()
  103.     {
  104.         console.log('ValueField : create()');
  105.  
  106.         this.material.reflectionTexture = this.scene.hdrTexture;
  107.         this.material.microSurface = 0.7;
  108.         this.material.reflectivityColor = new bjs.Color3(0.85, 0.85, 0.85);
  109.         this.material.albedoColor = new bjs.Color3(0.01, 0.01, 0.01);
  110.  
  111.         this.updateStrategy = this.createUpdateStrategy(this);
  112.        
  113.  
  114.         this.posOptions = {
  115.             positionFunction:  this.onSetInitialParticlePosition
  116.         }
  117.  
  118.         this.init();
  119.  
  120.         this.mesh.position.set(0, 0, 0);
  121.    
  122.         this.sps.computeParticleTexture = false;        // prevents from computing particle.uvs
  123.         this.sps.computeParticleColor = false;          // prevents from computing particle.color
  124.         this.sps.computeParticleVertex = false;         // prevents from calling the custom updateParticleVertex() function
  125.     }
  126.  
  127.     protected createUpdateStrategy(valueField : ValueField): ValueFieldUpdateStrategy
  128.     {
  129.         throw new Error("Implement in subclass. Factory Method.");
  130.         return null;
  131.     }
  132.  
  133.     protected onSetInitialParticlePosition = (particle: bjs.SolidParticle, i: number) =>
  134.     {
  135.         console.log('ValueField : onSetInitialParticlePosition()');
  136.         const currentRow = i / this.columns;
  137.         const currentColumn = i % this.columns;
  138.         particle.position.set(currentColumn * this.cellWidth, particle.scaling.y * 0.5, currentRow * this.cellDepth * -1);
  139.  
  140.     }
  141.  
  142.     protected onPreRender()
  143.     {
  144.         console.log('ValueField : onPreRender()');
  145.         this.updateStrategy.preCalculate();
  146.         super.onPreRender();
  147.     }
  148.  
  149.     protected onRender()
  150.     {
  151.        
  152.     }
  153.  
  154.     protected onUpdateParticle = (particle : bjs.SolidParticle) =>
  155.     {
  156.         console.log('ValueField : onUpdateParticle()');
  157.         this.updateStrategy.updateParticle(particle);
  158.         return particle;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement