LoadProject.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Annotation} from "../Annotation.js";
  3. import {Measure} from "../utils/Measure.js";
  4. import {CameraAnimation} from "../modules/CameraAnimation/CameraAnimation.js";
  5. import {Utils} from "../utils.js";
  6. import {PointSizeType} from "../defines.js";
  7. function loadPointCloud(viewer, data){
  8. let loadMaterial = (target) => {
  9. if(data.material){
  10. if(data.material.activeAttributeName != null){
  11. target.activeAttributeName = data.material.activeAttributeName;
  12. }
  13. if(data.material.ranges != null){
  14. for(let range of data.material.ranges){
  15. if(range.name === "elevationRange"){
  16. target.elevationRange = range.value;
  17. }else if(range.name === "intensityRange"){
  18. target.intensityRange = range.value;
  19. }else{
  20. target.setRange(range.name, range.value);
  21. }
  22. }
  23. }
  24. if(data.material.size != null){
  25. target.size = data.material.size;
  26. }
  27. if(data.material.minSize != null){
  28. target.minSize = data.material.minSize;
  29. }
  30. if(data.material.pointSizeType != null){
  31. target.pointSizeType = PointSizeType[data.material.pointSizeType];
  32. }
  33. if(data.material.matcap != null){
  34. target.matcap = data.material.matcap;
  35. }
  36. }else if(data.activeAttributeName != null){
  37. target.activeAttributeName = data.activeAttributeName;
  38. }else{
  39. // no material data
  40. }
  41. };
  42. const promise = new Promise((resolve) => {
  43. const names = viewer.scene.pointclouds.map(p => p.name);
  44. const alreadyExists = names.includes(data.name);
  45. if(alreadyExists){
  46. resolve();
  47. return;
  48. }
  49. Potree.loadPointCloud(data.url, data.name, (e) => {
  50. const {pointcloud} = e;
  51. pointcloud.position.set(...data.position);
  52. pointcloud.rotation.set(...data.rotation);
  53. pointcloud.scale.set(...data.scale);
  54. loadMaterial(pointcloud.material);
  55. viewer.scene.addPointCloud(pointcloud);
  56. resolve(pointcloud);
  57. });
  58. });
  59. return promise;
  60. }
  61. function loadMeasurement(viewer, data){
  62. const duplicate = viewer.scene.measurements.find(measure => measure.uuid === data.uuid);
  63. if(duplicate){
  64. return;
  65. }
  66. const measure = new Measure();
  67. measure.uuid = data.uuid;
  68. measure.name = data.name;
  69. measure.showDistances = data.showDistances;
  70. measure.showCoordinates = data.showCoordinates;
  71. measure.showArea = data.showArea;
  72. measure.closed = data.closed;
  73. measure.showAngles = data.showAngles;
  74. measure.showHeight = data.showHeight;
  75. measure.showCircle = data.showCircle;
  76. measure.showAzimuth = data.showAzimuth;
  77. measure.showEdges = data.showEdges;
  78. // color
  79. for(const point of data.points){
  80. const pos = new THREE.Vector3(...point);
  81. measure.addMarker(pos);
  82. }
  83. viewer.scene.addMeasurement(measure);
  84. }
  85. function loadVolume(viewer, data){
  86. const duplicate = viewer.scene.volumes.find(volume => volume.uuid === data.uuid);
  87. if(duplicate){
  88. return;
  89. }
  90. let volume = new Potree[data.type];
  91. volume.uuid = data.uuid;
  92. volume.name = data.name;
  93. volume.position.set(...data.position);
  94. volume.rotation.set(...data.rotation);
  95. volume.scale.set(...data.scale);
  96. volume.visible = data.visible;
  97. volume.clip = data.clip;
  98. viewer.scene.addVolume(volume);
  99. }
  100. function loadCameraAnimation(viewer, data){
  101. const duplicate = viewer.scene.cameraAnimations.find(a => a.uuid === data.uuid);
  102. if(duplicate){
  103. return;
  104. }
  105. const animation = new CameraAnimation(viewer);
  106. animation.uuid = data.uuid;
  107. animation.name = data.name;
  108. animation.duration = data.duration;
  109. animation.t = data.t;
  110. animation.curveType = data.curveType;
  111. animation.visible = data.visible;
  112. animation.controlPoints = [];
  113. for(const cpdata of data.controlPoints){
  114. const cp = animation.createControlPoint();
  115. cp.position.set(...cpdata.position);
  116. cp.target.set(...cpdata.target);
  117. }
  118. viewer.scene.addCameraAnimation(animation);
  119. }
  120. function loadOrientedImages(viewer, images){
  121. const {cameraParamsPath, imageParamsPath} = images;
  122. const duplicate = viewer.scene.orientedImages.find(i => i.imageParamsPath === imageParamsPath);
  123. if(duplicate){
  124. return;
  125. }
  126. Potree.OrientedImageLoader.load(cameraParamsPath, imageParamsPath, viewer).then( images => {
  127. viewer.scene.addOrientedImages(images);
  128. });
  129. }
  130. function loadGeopackage(viewer, geopackage){
  131. const path = geopackage.path;
  132. const duplicate = viewer.scene.geopackages.find(i => i.path === path);
  133. if(duplicate){
  134. return;
  135. }
  136. const projection = viewer.getProjection();
  137. proj4.defs("WGS84", "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs");
  138. proj4.defs("pointcloud", projection);
  139. const transform = proj4("WGS84", "pointcloud");
  140. const params = {
  141. transform: transform,
  142. };
  143. Potree.GeoPackageLoader.loadUrl(path, params).then(data => {
  144. viewer.scene.addGeopackage(data);
  145. });
  146. }
  147. function loadSettings(viewer, data){
  148. if(!data){
  149. return;
  150. }
  151. viewer.setPointBudget(data.pointBudget);
  152. viewer.setFOV(data.fov);
  153. viewer.setEDLEnabled(data.edlEnabled);
  154. viewer.setEDLRadius(data.edlRadius);
  155. viewer.setEDLStrength(data.edlStrength);
  156. viewer.setBackground(data.background);
  157. viewer.setMinNodeSize(data.minNodeSize);
  158. viewer.setShowBoundingBox(data.showBoundingBoxes);
  159. }
  160. function loadView(viewer, view){
  161. viewer.scene.view.position.set(...view.position);
  162. viewer.scene.view.lookAt(...view.target);
  163. }
  164. function loadAnnotationItem(item){
  165. const annotation = new Annotation({
  166. position: item.position,
  167. title: item.title,
  168. cameraPosition: item.cameraPosition,
  169. cameraTarget: item.cameraTarget,
  170. });
  171. annotation.description = item.description;
  172. annotation.uuid = item.uuid;
  173. if(item.offset){
  174. annotation.offset.set(...item.offset);
  175. }
  176. return annotation;
  177. }
  178. function loadAnnotations(viewer, data){
  179. if(!data){
  180. return;
  181. }
  182. const findDuplicate = (item) => {
  183. let duplicate = null;
  184. viewer.scene.annotations.traverse( a => {
  185. if(a.uuid === item.uuid){
  186. duplicate = a;
  187. }
  188. });
  189. return duplicate;
  190. };
  191. const traverse = (item, parent) => {
  192. const duplicate = findDuplicate(item);
  193. if(duplicate){
  194. return;
  195. }
  196. const annotation = loadAnnotationItem(item);
  197. for(const childItem of item.children){
  198. traverse(childItem, annotation);
  199. }
  200. parent.add(annotation);
  201. };
  202. for(const item of data){
  203. traverse(item, viewer.scene.annotations);
  204. }
  205. }
  206. function loadProfile(viewer, data){
  207. const {name, points} = data;
  208. const duplicate = viewer.scene.profiles.find(profile => profile.uuid === data.uuid);
  209. if(duplicate){
  210. return;
  211. }
  212. let profile = new Potree.Profile();
  213. profile.name = name;
  214. profile.uuid = data.uuid;
  215. profile.setWidth(data.width);
  216. for(const point of points){
  217. profile.addMarker(new THREE.Vector3(...point));
  218. }
  219. viewer.scene.addProfile(profile);
  220. }
  221. function loadClassification(viewer, data){
  222. if(!data){
  223. return;
  224. }
  225. const classifications = data;
  226. viewer.setClassifications(classifications);
  227. }
  228. export async function loadProject(viewer, data){
  229. if(data.type !== "Potree"){
  230. console.error("not a valid Potree project");
  231. return;
  232. }
  233. loadSettings(viewer, data.settings);
  234. loadView(viewer, data.view);
  235. const pointcloudPromises = [];
  236. for(const pointcloud of data.pointclouds){
  237. const promise = loadPointCloud(viewer, pointcloud);
  238. pointcloudPromises.push(promise);
  239. }
  240. for(const measure of data.measurements){
  241. loadMeasurement(viewer, measure);
  242. }
  243. for(const volume of data.volumes){
  244. loadVolume(viewer, volume);
  245. }
  246. for(const animation of data.cameraAnimations){
  247. loadCameraAnimation(viewer, animation);
  248. }
  249. for(const profile of data.profiles){
  250. loadProfile(viewer, profile);
  251. }
  252. if(data.orientedImages){
  253. for(const images of data.orientedImages){
  254. loadOrientedImages(viewer, images);
  255. }
  256. }
  257. loadAnnotations(viewer, data.annotations);
  258. loadClassification(viewer, data.classification);
  259. // need to load at least one point cloud that defines the scene projection,
  260. // before we can load stuff in other projections such as geopackages
  261. //await Promise.any(pointcloudPromises); // (not yet supported)
  262. Utils.waitAny(pointcloudPromises).then( () => {
  263. if(data.geopackages){
  264. for(const geopackage of data.geopackages){
  265. loadGeopackage(viewer, geopackage);
  266. }
  267. }
  268. });
  269. await Promise.all(pointcloudPromises);
  270. }