| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- import * as THREE from "../../libs/three.js/build/three.module.js";
- import {Annotation} from "../Annotation.js";
- import {Measure} from "../utils/Measure.js";
- import {CameraAnimation} from "../modules/CameraAnimation/CameraAnimation.js";
- import {Utils} from "../utils.js";
- import {PointSizeType} from "../defines.js";
- function loadPointCloud(viewer, data){
- let loadMaterial = (target) => {
- if(data.material){
- if(data.material.activeAttributeName != null){
- target.activeAttributeName = data.material.activeAttributeName;
- }
- if(data.material.ranges != null){
- for(let range of data.material.ranges){
- if(range.name === "elevationRange"){
- target.elevationRange = range.value;
- }else if(range.name === "intensityRange"){
- target.intensityRange = range.value;
- }else{
- target.setRange(range.name, range.value);
- }
- }
- }
- if(data.material.size != null){
- target.size = data.material.size;
- }
- if(data.material.minSize != null){
- target.minSize = data.material.minSize;
- }
- if(data.material.pointSizeType != null){
- target.pointSizeType = PointSizeType[data.material.pointSizeType];
- }
- if(data.material.matcap != null){
- target.matcap = data.material.matcap;
- }
- }else if(data.activeAttributeName != null){
- target.activeAttributeName = data.activeAttributeName;
- }else{
- // no material data
- }
- };
- const promise = new Promise((resolve) => {
- const names = viewer.scene.pointclouds.map(p => p.name);
- const alreadyExists = names.includes(data.name);
- if(alreadyExists){
- resolve();
- return;
- }
- Potree.loadPointCloud(data.url, data.name, (e) => {
- const {pointcloud} = e;
- pointcloud.position.set(...data.position);
- pointcloud.rotation.set(...data.rotation);
- pointcloud.scale.set(...data.scale);
- loadMaterial(pointcloud.material);
- viewer.scene.addPointCloud(pointcloud);
- resolve(pointcloud);
- });
- });
- return promise;
- }
- function loadMeasurement(viewer, data){
- const duplicate = viewer.scene.measurements.find(measure => measure.uuid === data.uuid);
- if(duplicate){
- return;
- }
- const measure = new Measure();
- measure.uuid = data.uuid;
- measure.name = data.name;
- measure.showDistances = data.showDistances;
- measure.showCoordinates = data.showCoordinates;
- measure.showArea = data.showArea;
- measure.closed = data.closed;
- measure.showAngles = data.showAngles;
- measure.showHeight = data.showHeight;
- measure.showCircle = data.showCircle;
- measure.showAzimuth = data.showAzimuth;
- measure.showEdges = data.showEdges;
- // color
- for(const point of data.points){
- const pos = new THREE.Vector3(...point);
- measure.addMarker(pos);
- }
- viewer.scene.addMeasurement(measure);
- }
- function loadVolume(viewer, data){
- const duplicate = viewer.scene.volumes.find(volume => volume.uuid === data.uuid);
- if(duplicate){
- return;
- }
- let volume = new Potree[data.type];
- volume.uuid = data.uuid;
- volume.name = data.name;
- volume.position.set(...data.position);
- volume.rotation.set(...data.rotation);
- volume.scale.set(...data.scale);
- volume.visible = data.visible;
- volume.clip = data.clip;
- viewer.scene.addVolume(volume);
- }
- function loadCameraAnimation(viewer, data){
- const duplicate = viewer.scene.cameraAnimations.find(a => a.uuid === data.uuid);
- if(duplicate){
- return;
- }
- const animation = new CameraAnimation(viewer);
- animation.uuid = data.uuid;
- animation.name = data.name;
- animation.duration = data.duration;
- animation.t = data.t;
- animation.curveType = data.curveType;
- animation.visible = data.visible;
- animation.controlPoints = [];
- for(const cpdata of data.controlPoints){
- const cp = animation.createControlPoint();
- cp.position.set(...cpdata.position);
- cp.target.set(...cpdata.target);
- }
- viewer.scene.addCameraAnimation(animation);
- }
- function loadOrientedImages(viewer, images){
- const {cameraParamsPath, imageParamsPath} = images;
- const duplicate = viewer.scene.orientedImages.find(i => i.imageParamsPath === imageParamsPath);
- if(duplicate){
- return;
- }
- Potree.OrientedImageLoader.load(cameraParamsPath, imageParamsPath, viewer).then( images => {
- viewer.scene.addOrientedImages(images);
- });
- }
- function loadGeopackage(viewer, geopackage){
- const path = geopackage.path;
- const duplicate = viewer.scene.geopackages.find(i => i.path === path);
- if(duplicate){
- return;
- }
- const projection = viewer.getProjection();
- proj4.defs("WGS84", "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
- proj4.defs("pointcloud", projection);
- const transform = proj4("WGS84", "pointcloud");
- const params = {
- transform: transform,
- };
- Potree.GeoPackageLoader.loadUrl(path, params).then(data => {
- viewer.scene.addGeopackage(data);
- });
-
- }
- function loadSettings(viewer, data){
- if(!data){
- return;
- }
- viewer.setPointBudget(data.pointBudget);
- viewer.setFOV(data.fov);
- viewer.setEDLEnabled(data.edlEnabled);
- viewer.setEDLRadius(data.edlRadius);
- viewer.setEDLStrength(data.edlStrength);
- viewer.setBackground(data.background);
- viewer.setMinNodeSize(data.minNodeSize);
- viewer.setShowBoundingBox(data.showBoundingBoxes);
- }
- function loadView(viewer, view){
- viewer.scene.view.position.set(...view.position);
- viewer.scene.view.lookAt(...view.target);
- }
- function loadAnnotationItem(item){
- const annotation = new Annotation({
- position: item.position,
- title: item.title,
- cameraPosition: item.cameraPosition,
- cameraTarget: item.cameraTarget,
- });
- annotation.description = item.description;
- annotation.uuid = item.uuid;
- if(item.offset){
- annotation.offset.set(...item.offset);
- }
- return annotation;
- }
- function loadAnnotations(viewer, data){
- if(!data){
- return;
- }
- const findDuplicate = (item) => {
- let duplicate = null;
- viewer.scene.annotations.traverse( a => {
- if(a.uuid === item.uuid){
- duplicate = a;
- }
- });
- return duplicate;
- };
- const traverse = (item, parent) => {
- const duplicate = findDuplicate(item);
- if(duplicate){
- return;
- }
- const annotation = loadAnnotationItem(item);
- for(const childItem of item.children){
- traverse(childItem, annotation);
- }
- parent.add(annotation);
- };
- for(const item of data){
- traverse(item, viewer.scene.annotations);
- }
- }
- function loadProfile(viewer, data){
-
- const {name, points} = data;
- const duplicate = viewer.scene.profiles.find(profile => profile.uuid === data.uuid);
- if(duplicate){
- return;
- }
- let profile = new Potree.Profile();
- profile.name = name;
- profile.uuid = data.uuid;
- profile.setWidth(data.width);
- for(const point of points){
- profile.addMarker(new THREE.Vector3(...point));
- }
-
- viewer.scene.addProfile(profile);
- }
- function loadClassification(viewer, data){
- if(!data){
- return;
- }
- const classifications = data;
- viewer.setClassifications(classifications);
- }
- export async function loadProject(viewer, data){
- if(data.type !== "Potree"){
- console.error("not a valid Potree project");
- return;
- }
- loadSettings(viewer, data.settings);
- loadView(viewer, data.view);
- const pointcloudPromises = [];
- for(const pointcloud of data.pointclouds){
- const promise = loadPointCloud(viewer, pointcloud);
- pointcloudPromises.push(promise);
- }
- for(const measure of data.measurements){
- loadMeasurement(viewer, measure);
- }
- for(const volume of data.volumes){
- loadVolume(viewer, volume);
- }
- for(const animation of data.cameraAnimations){
- loadCameraAnimation(viewer, animation);
- }
- for(const profile of data.profiles){
- loadProfile(viewer, profile);
- }
- if(data.orientedImages){
- for(const images of data.orientedImages){
- loadOrientedImages(viewer, images);
- }
- }
- loadAnnotations(viewer, data.annotations);
- loadClassification(viewer, data.classification);
- // need to load at least one point cloud that defines the scene projection,
- // before we can load stuff in other projections such as geopackages
- //await Promise.any(pointcloudPromises); // (not yet supported)
- Utils.waitAny(pointcloudPromises).then( () => {
- if(data.geopackages){
- for(const geopackage of data.geopackages){
- loadGeopackage(viewer, geopackage);
- }
- }
- });
- await Promise.all(pointcloudPromises);
- }
|