Skip to content

[REGRESSION] We can't change the opacity of the globe #2561

@AnthonyGlt

Description

@AnthonyGlt

Since https://github.com/iTowns/itowns/pull/2374/files#diff-2cbabdb42b34b51f54a3ad1eabf469e8990ea4073b43780ff5f2cc6709e08847

It seems that we can't edit the opactiy of the globe anymore

Steps to Reproduce

  1. Use the following example code
  2. Click on the button "Change Tile Layer Opacity"

Expected Behavior

Image

Actual Behavior

Image

<html>
<head>
    <title>Itowns - Globe</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/example.css">
    <link rel="stylesheet" type="text/css" href="css/LoadingScreen.css">
    <link rel="stylesheet" type="text/css" href="css/widgets.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
    <style>
        #removeElevationBtn {
            position: absolute;
            top: 10px;
            left: 10px;
            z-index: 1000;
            padding: 10px 15px;
            background-color: #ff6b6b;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        #removeElevationBtn:hover {
            background-color: #ff5252;
        }
        #removeElevationBtn:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        #opacityBtn {
            position: absolute;
            top: 60px;
            left: 10px;
            z-index: 1000;
            padding: 10px 15px;
            background-color: #4caf50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        #opacityBtn:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div id="viewerDiv"></div>
<button id="removeElevationBtn" onclick="removeElevationLayer()">Remove Elevation Layer</button>
<button id="opacityBtn" onclick="changeTileLayerOpacity()">Change Tile Layer Opacity</button>

<!-- Import iTowns source code -->
<script src="../dist/itowns.js"></script>
<script src="../dist/debug.js"></script>
<!-- Import iTowns Widgets plugin -->
<script src="../dist/itowns_widgets.js"></script>
<!-- Import iTowns LoadingScreen and GuiTools plugins -->
<script src="js/GUI/GuiTools.js"></script>
<script src="js/GUI/LoadingScreen.js"></script>

<script type="text/javascript">

    // ---------- CREATE A GlobeView FOR SUPPORTING DATA VISUALIZATION : ----------
    // Define camera initial position
    const placement = {
        coord: new itowns.Coordinates('EPSG:4326', 2.351323, 48.856712),
        range: 2500,
    }
    // `viewerDiv` will contain iTowns' rendering area (`<canvas>`)
    const viewerDiv = document.getElementById('viewerDiv');
    // Create a GlobeView
    const view = new itowns.GlobeView(viewerDiv, placement);
    // Setup loading screen and debug menu
    setupLoadingScreen(viewerDiv, view);
    const debugMenu = new GuiTools('menuDiv', view);

    // Store elevation layer ID for removal
    let elevationLayerId = null;
    // Track current opacity for cycling through values
    let currentOpacity = 1.0;

    // ---------- DISPLAY ORTHO-IMAGES : ----------
    // Add one imagery layer to the scene. This layer's properties are defined in a json file, but it could be
    // defined as a plain js object. See `Layer` documentation for more info.
    itowns.Fetcher.json('./layers/JSONLayers/Ortho.json').then(function _(config) {
        config.source = new itowns.WMTSSource(config.source);
        view.addLayer(
            new itowns.ColorLayer('Ortho', config),
        ).then(debugMenu.addLayerGUI.bind(debugMenu));
    });

    // ---------- DISPLAY A DIGITAL ELEVATION MODEL : ----------
    // Add two elevation layers, each with a different level of detail. Here again, each layer's properties are
    // defined in a json file.
    function addElevationLayerFromConfig(config) {
        config.source = new itowns.WMTSSource(config.source);
        const elevationLayer = new itowns.ElevationLayer(config.id, config);
        elevationLayerId = config.id; // Store the layer ID
        view.addLayer(elevationLayer).then(debugMenu.addLayerGUI.bind(debugMenu));
    }
    // itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);
    itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json').then(addElevationLayerFromConfig);

    // Function to remove elevation layer
    function removeElevationLayer() {
        if (elevationLayerId) {
            view.removeLayer(elevationLayerId, true);
            console.log(`Elevation layer ${elevationLayerId} removed`);
            // Disable the button after removal
            document.getElementById('removeElevationBtn').disabled = true;
            document.getElementById('removeElevationBtn').textContent = 'Elevation Layer Removed';
        } else {
            console.log('No elevation layer ID found');
        }
    }

    // Function to change tile layer opacity
    function changeTileLayerOpacity() {
        // Cycle through opacity values: 1.0 -> 0.7 -> 0.5 -> 0.3 -> 1.0
        const opacityValues = [1.0, 0.7, 0.5, 0.3];
        const currentIndex = opacityValues.indexOf(currentOpacity);
        const nextIndex = (currentIndex + 1) % opacityValues.length;
        currentOpacity = opacityValues[nextIndex];

        // Apply the opacity change
        const opacity = currentOpacity;
        view.tileLayer.opacity = opacity;




            view.notifyChange(view.camera3D);

        console.log(`Tile layer opacity changed to: ${opacity}`);
        document.getElementById('opacityBtn').textContent = `Opacity: ${Math.round(opacity * 100)}%`;
    }

    // ---------- ADD SOME WIDGETS : ----------
    // ADD A SCALE :
    const scale = new itowns_widgets.Scale(view, { position: 'bottom-right', translate: { x: -80 } });
    // ADD A MINIMAP :
    const minimap = new itowns_widgets.Minimap(
        view,
        new itowns.ColorLayer('minimap', {
            source: new itowns.VectorTilesSource({
                style: "https://data.geopf.fr/annexes/ressources/vectorTiles/styles/PLAN.IGN/standard.json",
                // We don't display mountains and plot related data to ease visualisation
                filter: (layer) => !layer['source-layer'].includes('oro_')
                    && !layer['source-layer'].includes('parcellaire'),
            }),
            addLabelLayer: { performance: true },
        }),
        { cursor: '+' },
    );
    // ADD NAVIGATION TOOLS :
    const navigation = new itowns_widgets.Navigation(view, {
        position: 'bottom-right',
        translate: { y: -40 },
    });
    // ADD A SEARCH BAR :
    // You can find more precise explanation on searchbar options in the doc
    // (http://www.itowns-project.org/itowns/docs/#api/Widgets/Searchbar) and in the searchbar example
    // (https://www.itowns-project.org/itowns/examples/#widgets_searchbar)
    // Define options for geocoding service that should be used by the searchbar.
    const geocodingOptions = {
        url: new URL(
            'https://data.geopf.fr/geocodage/completion?' +
            'text=&type=StreetAddress,PositionOfInterest',
        ),
        parser: (response) => {
            const map = new Map();
            response.results.forEach(location => {
                map.set(location.fulltext, new itowns.Coordinates('EPSG:4326', location.x, location.y));
            });
            return map;
        },
        onSelected: (coordinates) => {
            view.controls.lookAtCoordinate({ coord: coordinates, range: 20000, tilt: 45, heading: 0 });
        },
    }
    // Create the searchbar
    const searchbar = new itowns_widgets.Searchbar(view, geocodingOptions, {
        maxSuggestionNumber: 15,
        placeholder: 'Search a location in France',
        position: 'top-right',
    });

    // ---------- DISPLAY ATMOSPHERIC LIGHTING : ----------
    const atmosphere = view.getLayerById('atmosphere');
    atmosphere.setRealisticOn(!view.isDebugMode);

    // ---------- DEBUG TOOLS : ----------
    // Toggle atmospheric lighting on/off.
    const cRL = debugMenu.addGUI('RealisticLighting', !view.isDebugMode, function (v) {
        atmosphere.setRealisticOn(v);
        view.notifyChange(atmosphere);
    });
    debug.createTileDebugUI(debugMenu.gui, view);
</script>
</body>
</html>

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions