/
RaskolnickOFF
/
3D
Обзор
Документация
Войти
/
RaskolnickOFF
/
3D
Код
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/source/world/HeightmapData.js
51 строка
2 KB
RaskolnickOFF
fixes
19 июн 2026, 19:05
19 июн 2026, 19:05
84fe909
Код
Авторство
О чём код?
// js/source/world/HeightmapData.js — единый источник правды для высот export class HeightmapData { constructor({ size, segments, initialHeight = 0 }) { this.size = size; this.segments = segments; this.vertexCount = segments + 1; this.cellSize = size / segments; this._vertices = new Float32Array(this.vertexCount * this.vertexCount); this._vertices.fill(initialHeight); } getVertex(x, z) { if (x < 0 || x > this.segments || z < 0 || z > this.segments) return 0; return this._vertices[z * this.vertexCount + x]; } setVertex(x, z, height) { if (x < 0 || x > this.segments || z < 0 || z > this.segments) return; this._vertices[z * this.vertexCount + x] = height; } getHeightAt(worldX, worldZ) { const halfSize = this.size / 2; const localX = worldX + halfSize; const localZ = worldZ + halfSize; const gx = localX / this.cellSize; const gz = localZ / this.cellSize; const x0 = Math.max(0, Math.min(this.segments, Math.floor(gx))); const z0 = Math.max(0, Math.min(this.segments, Math.floor(gz))); const x1 = Math.min(x0 + 1, this.segments); const z1 = Math.min(z0 + 1, this.segments); const fx = gx - x0; const fz = gz - z0; const h00 = this.getVertex(x0, z0); const h10 = this.getVertex(x1, z0); const h01 = this.getVertex(x0, z1); const h11 = this.getVertex(x1, z1); const h0 = h00 + (h10 - h00) * fx; const h1 = h01 + (h11 - h01) * fx; return h0 + (h1 - h0) * fz; } getVerticesArray() { return this._vertices; } reset(height = 0) { this._vertices.fill(height); } }