Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {EventEmitter} from 'events'
- import Worker from '../workers/Terrain.worker'
- export default class Terrain extends EventEmitter {
- constructor(name, x, z, heightmap, blendmap, scene) {
- super();
- this.name = name;
- this.scene = scene;
- this.x = x;
- this.z = z;
- this.mesh = null;
- this.worker = new Worker();
- this.heightmap = heightmap;
- this.blendmap = blendmap;
- this.subdivisions = 64;
- this.filter = [1, 1, 1];
- this.heightmapBuffer = null;
- this.blendmapBuffer = null;
- this.SIZE = 32.5;
- this.MIN_HEIGHT = 0;
- this.MAX_HEIGHT = 1;
- this.getHeightmapPixels(heightmap, blendmap);
- this.worker.onmessage = event => {
- if(event.data.generated) {
- this.heights = event.data.heights;
- this.mesh = this.scene.engine.loader.loadToVAO(
- this.name,
- event.data.vertices,
- event.data.indices,
- event.data.texCoords,
- event.data.normals
- );
- this.emit('ready', event.data);
- }
- };
- }
- getHeights(positions) {
- return new Promise((res, rej) => {
- this.worker.postMessage({
- getHeights: positions
- });
- this.worker.onmessage = event => {
- if(event.data) {
- res(event.data)
- }
- };
- });
- }
- getHeightmapPixels(heightmap, blendmap) {
- if(blendmap && heightmap) {
- let canvas = document.createElement('canvas');
- let canvas2 = document.createElement('canvas');
- canvas.width = canvas2.width = heightmap.image.width;
- canvas.height = canvas2.height = heightmap.image.height;
- let ctx = canvas.getContext('2d');
- let ctx2 = canvas2.getContext('2d');
- // ctx.translate(0, height);
- // ctx.scale(1, -1);
- ctx.drawImage(heightmap.image, 0, 0);
- // ctx2.translate(blendmap.image.width/2, blendmap.image.height/2);
- // ctx2.scale(-1, -1);
- ctx2.drawImage(blendmap.image, 0, 0);
- this.heightmapBuffer = ctx.getImageData(0, 0, heightmap.image.width, heightmap.image.height).data;
- this.blendmapBuffer = ctx2.getImageData(0, 0, blendmap.image.width, blendmap.image.height).data;
- this.worker.postMessage({
- config: {
- heightmap: this.heightmapBuffer,
- blendmap: this.blendmapBuffer,
- heightmapWidth: heightmap.image.width,
- heightmapHeight: heightmap.image.height,
- SIZE: this.SIZE,
- subdivisions: this.subdivisions,
- filter: this.filter,
- MAX_HEIGHT: this.MAX_HEIGHT,
- MIN_HEIGHT: this.MIN_HEIGHT
- }
- })
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement