Skip to content

Commit

Permalink
fix(fetcher): add sanity check for texture data
Browse files Browse the repository at this point in the history
  • Loading branch information
Desplandis committed Feb 14, 2025
1 parent 4ee3c7f commit 798e00a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions packages/Main/src/Provider/Fetcher.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TextureLoader, DataTexture, RedFormat, FloatType } from 'three';

const TEXTURE_TILE_DIM = 256;
const TEXTURE_TILE_SIZE = TEXTURE_TILE_DIM * TEXTURE_TILE_DIM;

const textureLoader = new TextureLoader();
const SIZE_TEXTURE_TILE = 256;

function checkResponse(response) {
if (!response.ok) {
const error = new Error(`Error loading ${response.url}: status ${response.status}`);
Expand All @@ -15,13 +18,6 @@ const arrayBuffer = (url, options = {}) => fetch(url, options).then((response) =
return response.arrayBuffer();
});

function getTextureFloat(buffer) {
const texture = new DataTexture(buffer, SIZE_TEXTURE_TILE, SIZE_TEXTURE_TILE, RedFormat, FloatType);
texture.internalFormat = 'R32F';
texture.needsUpdate = true;
return texture;
}

/**
* Utilitary to fetch resources from a server using the [fetch API](
* https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch).
Expand Down Expand Up @@ -137,8 +133,13 @@ export default {
*/
textureFloat(url, options = {}) {
return arrayBuffer(url, options).then((buffer) => {
const floatArray = new Float32Array(buffer);
const texture = getTextureFloat(floatArray);
if (buffer.byteLength !== TEXTURE_TILE_SIZE * Float32Array.BYTES_PER_ELEMENT) {
throw new Error(`Invalid float data from URL: \`${url}\``);
}
const data = new Float32Array(buffer);
const texture = new DataTexture(data, TEXTURE_TILE_DIM, TEXTURE_TILE_DIM, RedFormat, FloatType);
texture.internalFormat = 'R32F';
texture.needsUpdate = true;
return texture;
});
},
Expand Down
4 changes: 2 additions & 2 deletions packages/Main/src/Renderer/RasterTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ export class RasterElevationTile extends RasterTile {
zmax: this.layer.zmax,
});
if (this.min != min || this.max != max) {
this.min = min;
this.max = max;
this.min = isNaN(min) ? this.min : min;
this.max = isNaN(max) ? this.max : max;
}
}
}
Expand Down

0 comments on commit 798e00a

Please sign in to comment.