Advertisement
Masterchoc

Untitled

Dec 25th, 2017
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {EventEmitter} from 'events'
  2. import Worker from '../workers/Terrain.worker'
  3.  
  4. export default class Terrain extends EventEmitter {
  5.     constructor(name, x, z, heightmap, blendmap, scene) {
  6.         super();
  7.         this.name = name;
  8.         this.scene = scene;
  9.         this.x = x;
  10.         this.z = z;
  11.         this.mesh = null;
  12.         this.worker = new Worker();
  13.         this.heightmap = heightmap;
  14.         this.blendmap = blendmap;
  15.         this.subdivisions = 64;
  16.         this.filter = [1, 1, 1];
  17.         this.heightmapBuffer = null;
  18.         this.blendmapBuffer = null;
  19.         this.SIZE = 32.5;
  20.         this.MIN_HEIGHT = 0;
  21.         this.MAX_HEIGHT = 1;
  22.  
  23.         this.getHeightmapPixels(heightmap, blendmap);
  24.  
  25.         this.worker.onmessage = event => {
  26.             if(event.data.generated) {
  27.                 this.heights = event.data.heights;
  28.                 this.mesh = this.scene.engine.loader.loadToVAO(
  29.                     this.name,
  30.                     event.data.vertices,
  31.                     event.data.indices,
  32.                     event.data.texCoords,
  33.                     event.data.normals
  34.                 );
  35.                 this.emit('ready', event.data);
  36.             }
  37.         };
  38.     }
  39.  
  40.     getHeights(positions) {
  41.         return new Promise((res, rej) => {
  42.             this.worker.postMessage({
  43.                 getHeights: positions
  44.             });
  45.  
  46.             this.worker.onmessage = event => {
  47.                 if(event.data) {
  48.                     res(event.data)
  49.                 }
  50.             };
  51.         });
  52.     }
  53.  
  54.     getHeightmapPixels(heightmap, blendmap) {
  55.         if(blendmap && heightmap) {
  56.             let canvas = document.createElement('canvas');
  57.             let canvas2 = document.createElement('canvas');
  58.  
  59.             canvas.width = canvas2.width =  heightmap.image.width;
  60.             canvas.height = canvas2.height =  heightmap.image.height;
  61.             let ctx = canvas.getContext('2d');
  62.             let ctx2 = canvas2.getContext('2d');
  63.             // ctx.translate(0, height);
  64.             // ctx.scale(1, -1);
  65.             ctx.drawImage(heightmap.image, 0, 0);
  66.  
  67.             // ctx2.translate(blendmap.image.width/2, blendmap.image.height/2);
  68.             // ctx2.scale(-1, -1);
  69.             ctx2.drawImage(blendmap.image, 0, 0);
  70.  
  71.             this.heightmapBuffer = ctx.getImageData(0, 0, heightmap.image.width, heightmap.image.height).data;
  72.             this.blendmapBuffer = ctx2.getImageData(0, 0,  blendmap.image.width, blendmap.image.height).data;
  73.             this.worker.postMessage({
  74.  
  75.                 config: {
  76.                     heightmap: this.heightmapBuffer,
  77.                     blendmap: this.blendmapBuffer,
  78.                     heightmapWidth: heightmap.image.width,
  79.                     heightmapHeight: heightmap.image.height,
  80.                     SIZE: this.SIZE,
  81.                     subdivisions: this.subdivisions,
  82.                     filter: this.filter,
  83.                     MAX_HEIGHT: this.MAX_HEIGHT,
  84.                     MIN_HEIGHT: this.MIN_HEIGHT
  85.                 }
  86.             })
  87.         }
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement