Skip to content

Commit

Permalink
feat(LayeredMaterial): migrate to TypeScript
Browse files Browse the repository at this point in the history
Squashed commits (oldest first):
- fix(tests): add new empty methods to mock Material
- refactor: use interface where possible
- fix: use setUniform for material showOutline set
- fix(elevation): correct antilogy
- refactor: rename layer to tile where relevant
  • Loading branch information
HoloTheDrunk committed Jan 16, 2025
1 parent c4629d6 commit b237291
Show file tree
Hide file tree
Showing 22 changed files with 624 additions and 353 deletions.
6 changes: 3 additions & 3 deletions src/Converter/convertToTile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as THREE from 'three';
import TileMesh from 'Core/TileMesh';
import LayeredMaterial from 'Renderer/LayeredMaterial';
import { LayeredMaterial } from 'Renderer/LayeredMaterial';
import { newTileGeometry } from 'Core/Prefab/TileBuilder';
import ReferLayerProperties from 'Layer/ReferencingLayerProperties';
import { geoidLayerIsVisible } from 'Layer/GeoidLayer';
Expand All @@ -13,7 +13,7 @@ function setTileFromTiledLayer(tile, tileLayer) {
}

if (__DEBUG__) {
tile.material.showOutline = tileLayer.showOutline || false;
tile.material.setUniform('showOutline', tileLayer.showOutline || false);
}

if (tileLayer.isGlobeLayer) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export default {
tile.geoidHeight = parent.geoidHeight;
const geoidHeight = geoidLayerIsVisible(layer) ? tile.geoidHeight : 0;
tile.setBBoxZ({ min: parent.obb.z.min, max: parent.obb.z.max, geoidHeight });
tile.material.geoidHeight = geoidHeight;
tile.material.setUniform('geoidHeight', geoidHeight);
}

return tile;
Expand Down
8 changes: 4 additions & 4 deletions src/Core/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class View extends THREE.EventDispatcher {
this.mainLoop.gfxEngine.getWindowSize().y,
options.camera);

this._frameRequesters = { };
this._frameRequesters = {};

this._resizeListener = () => this.resize();
window.addEventListener('resize', this._resizeListener, false);
Expand All @@ -217,8 +217,8 @@ class View extends THREE.EventDispatcher {
// all layers must be ready
const allReady = this.getLayers().every(layer => layer.ready);
if (allReady &&
this.mainLoop.scheduler.commandsWaitingExecutionCount() == 0 &&
this.mainLoop.renderingState == RENDERING_PAUSED) {
this.mainLoop.scheduler.commandsWaitingExecutionCount() == 0 &&
this.mainLoop.renderingState == RENDERING_PAUSED) {
this.dispatchEvent({ type: VIEW_EVENTS.LAYERS_INITIALIZED });
this.removeFrameRequester(MAIN_LOOP_EVENTS.UPDATE_END, this._allLayersAreReadyCallback);
}
Expand Down Expand Up @@ -954,7 +954,7 @@ class View extends THREE.EventDispatcher {
continue;
}

for (const materialLayer of tile.object.material.getLayers(layers)) {
for (const materialLayer of tile.object.material.getTiles(layers)) {
for (const texture of materialLayer.textures) {
if (!texture.features) {
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/Layer/ColorLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ class ColorLayer extends RasterLayer {
* @return {RasterColorTile} The raster color node added.
*/
setupRasterNode(node) {
const rasterColorNode = new RasterColorTile(node.material, this);
const rasterColorTile = new RasterColorTile(node.material, this);

node.material.addLayer(rasterColorNode);
node.material.addColorTile(rasterColorTile);
// set up ColorLayer ordering.
node.material.setSequence(this.parent.colorLayersOrder);
node.material.setColorTileIds(this.parent.colorLayersOrder);

return rasterColorNode;
return rasterColorTile;
}

update(context, layer, node, parent) {
Expand Down
4 changes: 2 additions & 2 deletions src/Layer/ElevationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class ElevationLayer extends RasterLayer {
setupRasterNode(node) {
const rasterElevationNode = new RasterElevationTile(node.material, this);

node.material.addLayer(rasterElevationNode);
node.material.setSequenceElevation(this.id);
node.material.setElevationTile(rasterElevationNode);
node.material.setElevationTileId(this.id);
// bounding box initialisation
const updateBBox = () => node.setBBoxZ({
min: rasterElevationNode.min, max: rasterElevationNode.max, scale: this.scale,
Expand Down
4 changes: 2 additions & 2 deletions src/Layer/GeoidLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class GeoidLayer extends Layer {
}

updateNodeZ(node) {
node.material.geoidHeight = this.visible ? node.geoidHeight : 0;
node.obb.updateZ({ geoidHeight: node.material.geoidHeight });
node.material.setUniform('geoidHeight', this.visible ? node.geoidHeight : 0);
node.obb.updateZ({ geoidHeight: node.material.getUniform('geoidHeight') });
}

update(context, layer, node, parent) {
Expand Down
4 changes: 2 additions & 2 deletions src/Layer/LabelLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ class LabelLayer extends GeometryLayer {
// Necessary event listener, to remove any Label attached to
node.addEventListener('removed', () => this.removeNodeDomElement(node));

if (labelsNode.needsAltitude && node.material.getElevationLayer()) {
node.material.getElevationLayer().addEventListener('rasterElevationLevelChanged', () => { labelsNode.needsUpdate = true; });
if (labelsNode.needsAltitude && node.material.getElevationTile()) {
node.material.getElevationTile().addEventListener('rasterElevationLevelChanged', () => { labelsNode.needsUpdate = true; });
}

if (this.performance) {
Expand Down
4 changes: 2 additions & 2 deletions src/Layer/RasterLayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Layer from 'Layer/Layer';
import { removeLayeredMaterialNodeLayer } from 'Process/LayeredMaterialNodeProcessing';
import { removeLayeredMaterialNodeTile } from 'Process/LayeredMaterialNodeProcessing';
import textureConverter from 'Converter/textureConverter';
import { CACHE_POLICIES } from 'Core/Scheduler/Cache';

Expand Down Expand Up @@ -34,7 +34,7 @@ class RasterLayer extends Layer {
this.cache.clear();
}
for (const root of this.parent.level0Nodes) {
root.traverse(removeLayeredMaterialNodeLayer(this.id));
root.traverse(removeLayeredMaterialNodeTile(this.id));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Layer/TiledGeometryLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class TiledGeometryLayer extends GeometryLayer {
*/
static hasEnoughTexturesToSubdivide(context, node) {
const layerUpdateState = node.layerUpdateState || {};
let nodeLayer = node.material.getElevationLayer();
let nodeLayer = node.material.getElevationTile();

for (const e of context.elevationLayers) {
const extents = node.getExtentsByProjection(e.crs);
Expand Down Expand Up @@ -403,7 +403,7 @@ class TiledGeometryLayer extends GeometryLayer {
if (layerUpdateState[c.id] && layerUpdateState[c.id].inError()) {
continue;
}
nodeLayer = node.material.getLayer(c.id);
nodeLayer = node.material.getColorTile(c.id);
if (c.source.extentInsideLimit(node.extent, zoom) && (!nodeLayer || nodeLayer.level < 0)) {
return false;
}
Expand Down
34 changes: 17 additions & 17 deletions src/Process/LayeredMaterialNodeProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function refinementCommandCancellationFn(cmd) {
// Cancel the command if the tile already has a better texture.
// This is only needed for elevation layers, because we may have several
// concurrent layers but we can only use one texture.
if (cmd.layer.isElevationLayer && cmd.requester.material.getElevationLayer() &&
cmd.targetLevel <= cmd.requester.material.getElevationLayer().level) {
if (cmd.layer.isElevationLayer && cmd.requester.material.getElevationTile() &&
cmd.targetLevel <= cmd.requester.material.getElevationTile().level) {
return true;
}

Expand Down Expand Up @@ -55,7 +55,7 @@ export function updateLayeredMaterialNodeImagery(context, layer, node, parent) {
return;
}

let nodeLayer = material.getLayer(layer.id);
let nodeLayer = material.getTile(layer.id);

// Initialisation
if (node.layerUpdateState[layer.id] === undefined) {
Expand All @@ -67,8 +67,8 @@ export function updateLayeredMaterialNodeImagery(context, layer, node, parent) {
// parent texture
if (!layer.noTextureParentOutsideLimit &&
parent.material &&
parent.material.getLayer &&
parent.material.getLayer(layer.id)) {
parent.material.getTile &&
parent.material.getTile(layer.id)) {
// ok, we're going to inherit our parent's texture
} else {
node.layerUpdateState[layer.id].noMoreUpdatePossible();
Expand All @@ -81,7 +81,7 @@ export function updateLayeredMaterialNodeImagery(context, layer, node, parent) {
nodeLayer = layer.setupRasterNode(node);

// Init the node by parent
const parentLayer = parent.material?.getLayer(layer.id);
const parentLayer = parent.material?.getTile(layer.id);
nodeLayer.initFromParent(parentLayer, extentsDestination);
}

Expand Down Expand Up @@ -171,15 +171,15 @@ export function updateLayeredMaterialNodeElevation(context, layer, node, parent)
return;
}
// Init elevation layer, and inherit from parent if possible
let nodeLayer = material.getElevationLayer();
let nodeLayer = material.getElevationTile();
if (!nodeLayer) {
nodeLayer = layer.setupRasterNode(node);
}

if (node.layerUpdateState[layer.id] === undefined) {
node.layerUpdateState[layer.id] = new LayerUpdateState();

const parentLayer = parent.material?.getLayer(layer.id);
const parentLayer = parent.material?.getTile(layer.id);
nodeLayer.initFromParent(parentLayer, extentsDestination);

if (nodeLayer.level >= layer.source.zoom.min) {
Expand All @@ -190,8 +190,8 @@ export function updateLayeredMaterialNodeElevation(context, layer, node, parent)

// Possible conditions to *not* update the elevation texture
if (layer.frozen ||
!material.visible ||
!node.layerUpdateState[layer.id].canTryUpdate()) {
!material.visible ||
!node.layerUpdateState[layer.id].canTryUpdate()) {
return;
}

Expand Down Expand Up @@ -232,19 +232,19 @@ export function updateLayeredMaterialNodeElevation(context, layer, node, parent)
err => handlingError(err, node, layer, targetLevel, context.view));
}

export function removeLayeredMaterialNodeLayer(layerId) {
export function removeLayeredMaterialNodeTile(tileId) {
/**
* @param {TileMesh} node - The node to udpate.
*/
return function removeLayeredMaterialNodeLayer(node) {
if (node.material?.removeLayer) {
if (node.material.elevationLayerIds.indexOf(layerId) > -1) {
return function removeLayeredMaterialNodeTile(node) {
if (node.material?.removeTile) {
if (node.material.elevationTile !== undefined) {
node.setBBoxZ({ min: 0, max: 0 });
}
node.material.removeLayer(layerId);
node.material.removeTile(tileId);
}
if (node.layerUpdateState && node.layerUpdateState[layerId]) {
delete node.layerUpdateState[layerId];
if (node.layerUpdateState && node.layerUpdateState[tileId]) {
delete node.layerUpdateState[tileId];
}
};
}
13 changes: 8 additions & 5 deletions src/Renderer/ColorLayersOrdering.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ImageryLayers } from 'Layer/Layer';
function updateLayersOrdering(geometryLayer, imageryLayers) {
const sequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
const cO = function cO(object) {
if (object.material?.setSequence) {
object.material.setSequence(sequence);
if (object.material?.setColorTileIds) {
object.material.setColorTileIds(sequence);
}
};

Expand Down Expand Up @@ -38,7 +38,8 @@ export default {
const previousSequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
ImageryLayers.moveLayerUp(layer, imageryLayers);
updateLayersOrdering(view.tileLayer, imageryLayers);
view.dispatchEvent({ type: COLOR_LAYERS_ORDER_CHANGED,
view.dispatchEvent({
type: COLOR_LAYERS_ORDER_CHANGED,
previous: { sequence: previousSequence },
new: { sequence: ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers) },
});
Expand All @@ -64,7 +65,8 @@ export default {
const previousSequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
ImageryLayers.moveLayerDown(layer, imageryLayers);
updateLayersOrdering(view.tileLayer, imageryLayers);
view.dispatchEvent({ type: COLOR_LAYERS_ORDER_CHANGED,
view.dispatchEvent({
type: COLOR_LAYERS_ORDER_CHANGED,
previous: { sequence: previousSequence },
new: { sequence: ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers) },
});
Expand All @@ -91,7 +93,8 @@ export default {
const previousSequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
ImageryLayers.moveLayerToIndex(layer, index, imageryLayers);
updateLayersOrdering(view.tileLayer, imageryLayers);
view.dispatchEvent({ type: COLOR_LAYERS_ORDER_CHANGED,
view.dispatchEvent({
type: COLOR_LAYERS_ORDER_CHANGED,
previous: { sequence: previousSequence },
new: { sequence: ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers) },
});
Expand Down
Loading

0 comments on commit b237291

Please sign in to comment.