From a9d9cb991bd30bd593fd94dd641b2c949cea7232 Mon Sep 17 00:00:00 2001 From: "Lukas Rieger (Blue)" Date: Mon, 15 Aug 2022 10:02:10 +0200 Subject: [PATCH] Fix hight-map readings --- src/map/Map.js | 12 ++++++++++-- src/util/Utils.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/map/Map.js b/src/map/Map.js index 43f4372..15e5b3b 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -405,8 +405,16 @@ export class Map { const texture = tile.model.material.uniforms?.textureImage?.value?.image; if (texture == null) continue; - const color = getPixel(texture, x - tileX * scaledTileSize.x, z - tileZ * scaledTileSize.z + this.data.lowres.tileSize.z); - return color[2]; + const color = getPixel(texture, x - tileX * scaledTileSize.x, z - tileZ * scaledTileSize.z + this.data.lowres.tileSize.z + 1); + + let heightUnsigned = color[1] * 256.0 + color[2]; + if (heightUnsigned >= 32768.0) { + return -(65535.0 - heightUnsigned); + } else { + return heightUnsigned; + } + + } return false; diff --git a/src/util/Utils.js b/src/util/Utils.js index 2bffb68..be3c946 100644 --- a/src/util/Utils.js +++ b/src/util/Utils.js @@ -387,6 +387,6 @@ pixel.height = 1; const pixelContext = pixel.getContext('2d'); export const getPixel = (img, x, y) => { - pixelContext.drawImage(img, x, y, 1, 1, 0, 0, 1, 1); + pixelContext.drawImage(img, Math.floor(x), Math.floor(y), 1, 1, 0, 0, 1, 1); return pixelContext.getImageData(0, 0, 1, 1).data; }