mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 02:01:22 +01:00
feat: 3d mode - improve water bodies mesh
This commit is contained in:
parent
0eaabcb051
commit
585f3c34b1
3 changed files with 48 additions and 6 deletions
|
|
@ -5993,6 +5993,11 @@
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div data-tip="Toggle wireframe mode" style="margin: 0.6em 0 0.3em -0.2em">
|
||||||
|
<input id="options3dMeshWireframeMode" class="checkbox" type="checkbox" />
|
||||||
|
<label for="options3dMeshWireframeMode" class="checkbox-label"><i>Show wireframe</i></label>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div data-tip="Set sky and water color" id="options3dColorSection" style="display: none">
|
<div data-tip="Set sky and water color" id="options3dColorSection" style="display: none">
|
||||||
<span>Sky:</span
|
<span>Sky:</span
|
||||||
><input
|
><input
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ window.ThreeD = (function () {
|
||||||
let labels = [];
|
let labels = [];
|
||||||
let icons = [];
|
let icons = [];
|
||||||
let lines = [];
|
let lines = [];
|
||||||
|
let gridToPackCellMap = null; // Map from grid cell index to pack cell index
|
||||||
|
|
||||||
const context2d = document.createElement("canvas").getContext("2d");
|
const context2d = document.createElement("canvas").getContext("2d");
|
||||||
|
|
||||||
|
|
@ -486,8 +487,20 @@ window.ThreeD = (function () {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a mesh from pixel data
|
// create a mesh from pixel data
|
||||||
async function createMesh(width, height, segmentsX, segmentsY) {
|
async function createMesh(width, height, segmentsX, segmentsY) {
|
||||||
|
// Build lookup map from grid cell index to pack cell index
|
||||||
|
gridToPackCellMap = new Map();
|
||||||
|
if (pack.cells?.g && pack.cells?.i) {
|
||||||
|
for (const packCellIndex of pack.cells.i) {
|
||||||
|
const gridCellIndex = pack.cells.g[packCellIndex];
|
||||||
|
if (!gridToPackCellMap.has(gridCellIndex)) {
|
||||||
|
gridToPackCellMap.set(gridCellIndex, packCellIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (texture) texture.dispose();
|
if (texture) texture.dispose();
|
||||||
if (!options.wireframe) {
|
if (!options.wireframe) {
|
||||||
texture = new THREE.TextureLoader().load(await createMeshTextureUrl(), render);
|
texture = new THREE.TextureLoader().load(await createMeshTextureUrl(), render);
|
||||||
|
|
@ -541,9 +554,32 @@ window.ThreeD = (function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const LOWER_BY_WATER = 18;
|
||||||
|
const DIVIDER = 100 - LOWER_BY_WATER;
|
||||||
|
|
||||||
function getMeshHeight(i) {
|
function getMeshHeight(i) {
|
||||||
const h = grid.cells.h[i];
|
const height = grid.cells.h[i];
|
||||||
return h < 20 ? 0 : ((h - 18) / 82) * options.scale;
|
|
||||||
|
let waterCellId = null;
|
||||||
|
if (height < 20) {
|
||||||
|
waterCellId = i;
|
||||||
|
} else if (grid.cells.c[i]) {
|
||||||
|
waterCellId = grid.cells.c[i].find(c => grid.cells.h[c] < 20) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If water vertex, get uniform elevation
|
||||||
|
if (waterCellId !== null) {
|
||||||
|
const packCellIndex = gridToPackCellMap.get(waterCellId);
|
||||||
|
const featureId = pack.cells.f[packCellIndex];
|
||||||
|
if (featureId === undefined) return 0;
|
||||||
|
|
||||||
|
const feature = pack.features[featureId];
|
||||||
|
const waterHeight = feature.type === "lake" && feature.height ? feature.height : 20;
|
||||||
|
return ((waterHeight - LOWER_BY_WATER) / DIVIDER) * options.scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Land vertex
|
||||||
|
return ((height - LOWER_BY_WATER) / DIVIDER) * options.scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extendWater(width, height) {
|
function extendWater(width, height) {
|
||||||
|
|
@ -699,6 +735,7 @@ window.ThreeD = (function () {
|
||||||
script.onerror = () => resolve(false);
|
script.onerror = () => resolve(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function OrbitControls(camera, domElement) {
|
function OrbitControls(camera, domElement) {
|
||||||
if (THREE.OrbitControls) return new THREE.OrbitControls(camera, domElement);
|
if (THREE.OrbitControls) return new THREE.OrbitControls(camera, domElement);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1051,7 +1051,7 @@ function toggle3dOptions() {
|
||||||
byId("options3dMeshSky").addEventListener("input", changeColors);
|
byId("options3dMeshSky").addEventListener("input", changeColors);
|
||||||
byId("options3dMeshWater").addEventListener("input", changeColors);
|
byId("options3dMeshWater").addEventListener("input", changeColors);
|
||||||
byId("options3dGlobeResolution").addEventListener("change", changeResolution);
|
byId("options3dGlobeResolution").addEventListener("change", changeResolution);
|
||||||
// byId("options3dMeshWireframeMode").addEventListener("change",toggleWireframe3d);
|
byId("options3dMeshWireframeMode").addEventListener("change", toggleWireframe3d);
|
||||||
byId("options3dSunColor").addEventListener("input", changeSunColor);
|
byId("options3dSunColor").addEventListener("input", changeSunColor);
|
||||||
byId("options3dSubdivide").addEventListener("change", toggle3dSubdivision);
|
byId("options3dSubdivide").addEventListener("change", toggle3dSubdivision);
|
||||||
|
|
||||||
|
|
@ -1116,9 +1116,9 @@ function toggle3dOptions() {
|
||||||
ThreeD.toggle3dSubdivision();
|
ThreeD.toggle3dSubdivision();
|
||||||
}
|
}
|
||||||
|
|
||||||
// function toggleWireframe3d() {
|
function toggleWireframe3d() {
|
||||||
// ThreeD.toggleWireframe();
|
ThreeD.toggleWireframe();
|
||||||
// }
|
}
|
||||||
|
|
||||||
function toggleSkyMode() {
|
function toggleSkyMode() {
|
||||||
const hide = ThreeD.options.extendedWater;
|
const hide = ThreeD.options.extendedWater;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue