iTowns / itowns

A Three.js-based framework written in Javascript/WebGL for visualizing 3D geospatial data
http://www.itowns-project.org
Other
1.07k stars 290 forks source link

[BUG] PlanarControls startPosition not set properly with proj4 projection info #2302

Open HoloTheDrunk opened 3 months ago

HoloTheDrunk commented 3 months ago

Your Environment

Context

Steps to Reproduce (for bugs)

HTML

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body style="margin: auto">
        <div id="viewerDiv" style="width: 100vw; height: 100vh"></div>
    </body>
</html>

TypeScript

import * as itowns from "itowns";
import * as three from "three";
import proj4 from "proj4";

const viewerDiv = document.getElementById("viewerDiv") as HTMLDivElement;

proj4.defs(
    "EPSG:3946",
    "+proj=lcc +lat_0=46 +lon_0=3 +lat_1=45.25 +lat_2=46.75 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs",
);

const view = new itowns.View("EPSG:3946", viewerDiv);
view.mainLoop.gfxEngine.renderer.setClearColor(three.Color.NAMES.black, 1.0);

const controls = new itowns.PlanarControls(view as itowns.PlanarView);

let eptSource: itowns.EntwinePointTileSource;
let eptLayer: itowns.EntwinePointTileLayer;

function onLayerReady() {
    const lookAt = new three.Vector3();
    const size = new three.Vector3();
    eptLayer.root.bbox.getSize(size);
    eptLayer.root.bbox.getCenter(lookAt);

    (view.camera3D as three.PerspectiveCamera).far = size.length() * 2.0;

    controls.groundLevel = eptLayer.root.bbox.min.z;
    const position = eptLayer.root.bbox.min
        .clone()
        .add(size.multiply({ x: 0, y: 0, z: size.x / size.z }));

    view.camera3D.position.copy(position);
    view.camera3D.lookAt(lookAt);
    (view.camera3D as itowns.OrientedImageCamera).updateProjectionMatrix();

    view.notifyChange(view.camera3D);
}

function loadEPT(url: string) {
    eptSource = new itowns.EntwinePointTileSource({ url });

    if (eptLayer) {
        view.removeLayer("Entwine Point Tile");
        view.notifyChange();
        eptLayer.delete();
    }

    const config = {
        source: eptSource,
        crs: view.referenceCrs,
    };

    eptLayer = new itowns.EntwinePointTileLayer("Entwine Point Tile", config);

    view.addLayer(eptLayer).then(onLayerReady);
}

loadEPT(
    "https://download.data.grandlyon.com/files/grandlyon/imagerie/mnt2018/lidar/ept/",
);

Expected Behavior

Pressing the Y key should make the camera travel to the position it was visually spawned at.

Actual Behavior

Pressing the Y key makes the camera travel to (0, 0, 0). Start of travel (visual spawn position based on proj4 def): image End of travel: image

Possible Cause/Fix/Solution

The startPosition and startQuaternion are likely not set properly once the desired projection is applied.