Commit c0303f6e authored by iSergio's avatar iSergio
Browse files

Added CustomHeightmapTerrainProvider, a simple TerrainProvider that gets...

Added CustomHeightmapTerrainProvider, a simple TerrainProvider that gets height values from a callback function.
parent 96531379
Loading
Loading
Loading
Loading
+151 −0
Original line number Diff line number Diff line
/*
 * Copyright 2021 iserge.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cesiumjs.cs.core.providers;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsProperty;
import jsinterop.annotations.JsType;
import org.cesiumjs.cs.core.*;
import org.cesiumjs.cs.js.JsObject;
import org.cesiumjs.cs.promise.Promise;

/**
 * A simple {@link TerrainProvider} that gets height values from a callback function. It can be used for procedurally
 * generated terrain or as a way to load custom heightmap data without creating a subclass of {@link TerrainProvider}.
 * There are some limitations such as no water mask, no vertex normals, and no availability, so a full-fledged {@link TerrainProvider}
 * subclass is better suited for these more sophisticated use cases.
 */
@JsType(isNative = true, namespace = "Cesium", name = "CustomHeightmapTerrainProvider")
public class CustomHeightmapTerrainProvider implements TerrainProvider {
    /**
     * Gets the credit to display when this terrain provider is active.
     * Typically this is used to credit the source of the terrain.
     */
    @JsProperty(name =  "credit")
    public native Credit credit();
    /**
     * Gets an event that is raised when the terrain provider encounters an asynchronous error. By subscribing to the event,
     * you will be notified of the error and can potentially recover from it.
     * Event listeners are passed an instance of TileProviderError.
     */
    @JsProperty(name =  "errorEvent")
    public native Event errorEvent();
    /**
     * Gets a value indicating whether or not the requested tiles include vertex normals.
     * Vertex normals are not supported by CustomHeightmapTerrainProvider, so the return value will always be false.
     */
    @JsProperty(name =  "hasVertexNormals")
    public native boolean hasVertexNormals();
    /**
     * Gets a value indicating whether or not the provider includes a water mask. The water mask indicates which
     * areas of the globe are water rather than land, so they can be rendered as a reflective surface with
     * animated waves. Water mask is not supported by CustomHeightmapTerrainProvider,
     * so the return value will always be false.
     */
    @JsProperty(name =  "hasWaterMask")
    public native boolean hasWaterMask();
    /**
     * Gets the number of rows per heightmap tile.
     */
    @JsProperty(name =  "height")
    public native Number height();
    /**
     * Gets a value indicating whether or not the provider is ready for use.
     */
    @JsProperty(name =  "ready")
    public native boolean ready();
    /**
     * Gets a promise that resolves to true when the provider is ready for use.
     */
    @JsProperty(name =  "readyPromise")
    public native Promise<Boolean, Void> readyPromise();
    /**
     * Gets the tiling scheme used by this provider.
     */
    @JsProperty(name =  "tilingScheme")
    public native TilingScheme tilingScheme();
    /**
     * Gets the number of columns per heightmap tile.
     */
    @JsProperty(name =  "width")
    public native Number width();

    /**
     * Gets the maximum geometric error allowed in a tile at a given level. This
     * function should not be called before {@link GeoserverTerrainProvider#ready}
     * returns true.
     *
     * @param level The tile level for which to get the maximum geometric error.
     * @return The maximum geometric error.
     */
    @Override
    @JsMethod
    public native double getLevelMaximumGeometricError(int level);

    /**
     * Determines whether data for a tile is available to be loaded.
     *
     * @param x     The X coordinate of the tile for which to request geometry.
     * @param y     The Y coordinate of the tile for which to request geometry.
     * @param level The level of the tile for which to request geometry.
     * @return Undefined if not supported by the terrain provider, otherwise true or
     * false.
     */
    @Override
    @JsMethod
    public native boolean getTileDataAvailable(int x, int y, int level);

    /**
     * Makes sure we load availability data for a tile.
     * @param x The X coordinate of the tile for which to request geometry.
     * @param y The Y coordinate of the tile for which to request geometry.
     * @param level The level of the tile for which to request geometry.
     * @return Undefined if nothing need to be loaded or a Promise that resolves when all required tiles are loaded
     */
    @JsMethod
    public native Promise<Void, Void> loadTileDataAvailability(int x, int y, int level);

    /**
     * Requests the geometry for a given tile. This function should not be called
     * before {@link GeoserverTerrainProvider#ready} returns true. The result must
     * include terrain data and may optionally include a water mask and an
     * indication of which child tiles are available.
     *
     * @param x       The X coordinate of the tile for which to request geometry.
     * @param y       The Y coordinate of the tile for which to request geometry.
     * @param level   The level of the tile for which to request geometry.
     * @param request The request object. Intended for internal use only.
     * @return A promise for the requested geometry. If this method returns
     * undefined instead of a promise, it is an indication that too many
     * requests are already pending and the request will be retried later.
     */
    @Override
    @JsMethod
    public native Promise<TerrainData, Void> requestTileGeometry(int x, int y, int level, Request request);

    public interface GeometryCallback {
        /**
         *
         * @param x The X coordinate of the tile for which to request geometry.
         * @param y The Y coordinate of the tile for which to request geometry.
         * @param level The level of the tile for which to request geometry.
         * @return An array or a promise to an array of heights in row-major order. If undefined,
         * the globe will render the parent tile.
         */
        JsObject callback(int x, int y, int level);
    }
}
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright 2021 iserge.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cesiumjs.cs.core.providers.options;

import jsinterop.annotations.JsConstructor;
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsProperty;
import org.cesiumjs.cs.core.Credit;
import org.cesiumjs.cs.core.Ellipsoid;
import org.cesiumjs.cs.core.TilingScheme;
import org.cesiumjs.cs.core.providers.CustomHeightmapTerrainProvider;

/**
 * Options for {@link CustomHeightmapTerrainProvider}.
 */
public class CustomHeightmapTerrainProviderOptions {
    /**
     * The callback function for requesting tile geometry.
     */
    @JsProperty
    public CustomHeightmapTerrainProvider.GeometryCallback callback;
    /**
     *
     */
    @JsProperty
    public int width;
    /**
     * The number of rows per heightmap tile.
     */
    @JsProperty
    public int height;
    /**
     * The tiling scheme specifying how the ellipsoidal surface is broken into tiles. If this parameter is not provided,
     * a GeographicTilingScheme is used.
     */
    @JsProperty
    public TilingScheme tilingScheme;
    /**
     * The ellipsoid. If the tilingScheme is specified, this parameter is ignored and the tiling scheme's ellipsoid is
     * used instead. If neither parameter is specified, the WGS84 ellipsoid is used.
     */
    @JsProperty
    public Ellipsoid ellipsoid;
    /**
     * A credit for the data source, which is displayed on the canvas.
     */
    @JsProperty
    public Credit credit;

    @JsConstructor
    private CustomHeightmapTerrainProviderOptions() {}

    @JsOverlay
    public static CustomHeightmapTerrainProviderOptions create(CustomHeightmapTerrainProvider.GeometryCallback callback, int width, int height) {
        CustomHeightmapTerrainProviderOptions options = new CustomHeightmapTerrainProviderOptions();
        options.callback = callback;
        options.width = width;
        options.height = height;
        return options;
    }
}