Scene.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Annotation} from "../Annotation.js";
  3. import {CameraMode} from "../defines.js";
  4. import {View} from "./View.js";
  5. import {Utils} from "../utils.js";
  6. import {EventDispatcher} from "../EventDispatcher.js";
  7. export class Scene extends EventDispatcher{
  8. constructor(){
  9. super();
  10. this.annotations = new Annotation();
  11. this.scene = new THREE.Scene();
  12. this.sceneBG = new THREE.Scene();
  13. this.scenePointCloud = new THREE.Scene();
  14. this.cameraP = new THREE.PerspectiveCamera(this.fov, 1, 0.1, 1000*1000);
  15. this.cameraO = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 1000*1000);
  16. this.cameraVR = new THREE.PerspectiveCamera();
  17. this.cameraBG = new THREE.Camera();
  18. this.cameraScreenSpace = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10);
  19. this.cameraMode = CameraMode.PERSPECTIVE;
  20. this.overrideCamera = null;
  21. this.pointclouds = [];
  22. this.measurements = [];
  23. this.profiles = [];
  24. this.volumes = [];
  25. this.polygonClipVolumes = [];
  26. this.cameraAnimations = [];
  27. this.orientedImages = [];
  28. this.images360 = [];
  29. this.geopackages = [];
  30. this.fpControls = null;
  31. this.orbitControls = null;
  32. this.earthControls = null;
  33. this.geoControls = null;
  34. this.deviceControls = null;
  35. this.inputHandler = null;
  36. this.view = new View();
  37. this.directionalLight = null;
  38. this.initialize();
  39. }
  40. estimateHeightAt (position) {
  41. let height = null;
  42. let fromSpacing = Infinity;
  43. for (let pointcloud of this.pointclouds) {
  44. if (pointcloud.root.geometryNode === undefined) {
  45. continue;
  46. }
  47. let pHeight = null;
  48. let pFromSpacing = Infinity;
  49. let lpos = position.clone().sub(pointcloud.position);
  50. lpos.z = 0;
  51. let ray = new THREE.Ray(lpos, new THREE.Vector3(0, 0, 1));
  52. let stack = [pointcloud.root];
  53. while (stack.length > 0) {
  54. let node = stack.pop();
  55. let box = node.getBoundingBox();
  56. let inside = ray.intersectBox(box);
  57. if (!inside) {
  58. continue;
  59. }
  60. let h = node.geometryNode.mean.z +
  61. pointcloud.position.z +
  62. node.geometryNode.boundingBox.min.z;
  63. if (node.geometryNode.spacing <= pFromSpacing) {
  64. pHeight = h;
  65. pFromSpacing = node.geometryNode.spacing;
  66. }
  67. for (let index of Object.keys(node.children)) {
  68. let child = node.children[index];
  69. if (child.geometryNode) {
  70. stack.push(node.children[index]);
  71. }
  72. }
  73. }
  74. if (height === null || pFromSpacing < fromSpacing) {
  75. height = pHeight;
  76. fromSpacing = pFromSpacing;
  77. }
  78. }
  79. return height;
  80. }
  81. getBoundingBox(pointclouds = this.pointclouds){
  82. let box = new THREE.Box3();
  83. this.scenePointCloud.updateMatrixWorld(true);
  84. this.referenceFrame.updateMatrixWorld(true);
  85. for (let pointcloud of pointclouds) {
  86. pointcloud.updateMatrixWorld(true);
  87. let pointcloudBox = pointcloud.pcoGeometry.tightBoundingBox ? pointcloud.pcoGeometry.tightBoundingBox : pointcloud.boundingBox;
  88. let boxWorld = Utils.computeTransformedBoundingBox(pointcloudBox, pointcloud.matrixWorld);
  89. box.union(boxWorld);
  90. }
  91. return box;
  92. }
  93. addPointCloud (pointcloud) {
  94. this.pointclouds.push(pointcloud);
  95. this.scenePointCloud.add(pointcloud);
  96. this.dispatchEvent({
  97. type: 'pointcloud_added',
  98. pointcloud: pointcloud
  99. });
  100. }
  101. addVolume (volume) {
  102. this.volumes.push(volume);
  103. this.dispatchEvent({
  104. 'type': 'volume_added',
  105. 'scene': this,
  106. 'volume': volume
  107. });
  108. }
  109. addOrientedImages(images){
  110. this.orientedImages.push(images);
  111. this.scene.add(images.node);
  112. this.dispatchEvent({
  113. 'type': 'oriented_images_added',
  114. 'scene': this,
  115. 'images': images
  116. });
  117. };
  118. removeOrientedImages(images){
  119. let index = this.orientedImages.indexOf(images);
  120. if (index > -1) {
  121. this.orientedImages.splice(index, 1);
  122. this.dispatchEvent({
  123. 'type': 'oriented_images_removed',
  124. 'scene': this,
  125. 'images': images
  126. });
  127. }
  128. };
  129. add360Images(images){
  130. this.images360.push(images);
  131. this.scene.add(images.node);
  132. this.dispatchEvent({
  133. 'type': '360_images_added',
  134. 'scene': this,
  135. 'images': images
  136. });
  137. }
  138. remove360Images(images){
  139. let index = this.images360.indexOf(images);
  140. if (index > -1) {
  141. this.images360.splice(index, 1);
  142. this.dispatchEvent({
  143. 'type': '360_images_removed',
  144. 'scene': this,
  145. 'images': images
  146. });
  147. }
  148. }
  149. addGeopackage(geopackage){
  150. this.geopackages.push(geopackage);
  151. this.scene.add(geopackage.node);
  152. this.dispatchEvent({
  153. 'type': 'geopackage_added',
  154. 'scene': this,
  155. 'geopackage': geopackage
  156. });
  157. };
  158. removeGeopackage(geopackage){
  159. let index = this.geopackages.indexOf(geopackage);
  160. if (index > -1) {
  161. this.geopackages.splice(index, 1);
  162. this.dispatchEvent({
  163. 'type': 'geopackage_removed',
  164. 'scene': this,
  165. 'geopackage': geopackage
  166. });
  167. }
  168. };
  169. removeVolume (volume) {
  170. let index = this.volumes.indexOf(volume);
  171. if (index > -1) {
  172. this.volumes.splice(index, 1);
  173. this.dispatchEvent({
  174. 'type': 'volume_removed',
  175. 'scene': this,
  176. 'volume': volume
  177. });
  178. }
  179. };
  180. addCameraAnimation(animation) {
  181. this.cameraAnimations.push(animation);
  182. this.dispatchEvent({
  183. 'type': 'camera_animation_added',
  184. 'scene': this,
  185. 'animation': animation
  186. });
  187. };
  188. removeCameraAnimation(animation){
  189. let index = this.cameraAnimations.indexOf(volume);
  190. if (index > -1) {
  191. this.cameraAnimations.splice(index, 1);
  192. this.dispatchEvent({
  193. 'type': 'camera_animation_removed',
  194. 'scene': this,
  195. 'animation': animation
  196. });
  197. }
  198. };
  199. addPolygonClipVolume(volume){
  200. this.polygonClipVolumes.push(volume);
  201. this.dispatchEvent({
  202. "type": "polygon_clip_volume_added",
  203. "scene": this,
  204. "volume": volume
  205. });
  206. };
  207. removePolygonClipVolume(volume){
  208. let index = this.polygonClipVolumes.indexOf(volume);
  209. if (index > -1) {
  210. this.polygonClipVolumes.splice(index, 1);
  211. this.dispatchEvent({
  212. "type": "polygon_clip_volume_removed",
  213. "scene": this,
  214. "volume": volume
  215. });
  216. }
  217. };
  218. addMeasurement(measurement){
  219. measurement.lengthUnit = this.lengthUnit;
  220. measurement.lengthUnitDisplay = this.lengthUnitDisplay;
  221. this.measurements.push(measurement);
  222. this.dispatchEvent({
  223. 'type': 'measurement_added',
  224. 'scene': this,
  225. 'measurement': measurement
  226. });
  227. };
  228. removeMeasurement (measurement) {
  229. let index = this.measurements.indexOf(measurement);
  230. if (index > -1) {
  231. this.measurements.splice(index, 1);
  232. this.dispatchEvent({
  233. 'type': 'measurement_removed',
  234. 'scene': this,
  235. 'measurement': measurement
  236. });
  237. }
  238. }
  239. addProfile (profile) {
  240. this.profiles.push(profile);
  241. this.dispatchEvent({
  242. 'type': 'profile_added',
  243. 'scene': this,
  244. 'profile': profile
  245. });
  246. }
  247. removeProfile (profile) {
  248. let index = this.profiles.indexOf(profile);
  249. if (index > -1) {
  250. this.profiles.splice(index, 1);
  251. this.dispatchEvent({
  252. 'type': 'profile_removed',
  253. 'scene': this,
  254. 'profile': profile
  255. });
  256. }
  257. }
  258. removeAllMeasurements () {
  259. while (this.measurements.length > 0) {
  260. this.removeMeasurement(this.measurements[0]);
  261. }
  262. while (this.profiles.length > 0) {
  263. this.removeProfile(this.profiles[0]);
  264. }
  265. while (this.volumes.length > 0) {
  266. this.removeVolume(this.volumes[0]);
  267. }
  268. }
  269. removeAllClipVolumes(){
  270. let clipVolumes = this.volumes.filter(volume => volume.clip === true);
  271. for(let clipVolume of clipVolumes){
  272. this.removeVolume(clipVolume);
  273. }
  274. while(this.polygonClipVolumes.length > 0){
  275. this.removePolygonClipVolume(this.polygonClipVolumes[0]);
  276. }
  277. }
  278. getActiveCamera() {
  279. if(this.overrideCamera){
  280. return this.overrideCamera;
  281. }
  282. if(this.cameraMode === CameraMode.PERSPECTIVE){
  283. return this.cameraP;
  284. }else if(this.cameraMode === CameraMode.ORTHOGRAPHIC){
  285. return this.cameraO;
  286. }else if(this.cameraMode === CameraMode.VR){
  287. return this.cameraVR;
  288. }
  289. return null;
  290. }
  291. initialize(){
  292. this.referenceFrame = new THREE.Object3D();
  293. this.referenceFrame.matrixAutoUpdate = false;
  294. this.scenePointCloud.add(this.referenceFrame);
  295. this.cameraP.up.set(0, 0, 1);
  296. this.cameraP.position.set(1000, 1000, 1000);
  297. this.cameraO.up.set(0, 0, 1);
  298. this.cameraO.position.set(1000, 1000, 1000);
  299. //this.camera.rotation.y = -Math.PI / 4;
  300. //this.camera.rotation.x = -Math.PI / 6;
  301. this.cameraScreenSpace.lookAt(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -1), new THREE.Vector3(0, 1, 0));
  302. this.directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  303. this.directionalLight.position.set( 10, 10, 10 );
  304. this.directionalLight.lookAt( new THREE.Vector3(0, 0, 0));
  305. this.scenePointCloud.add( this.directionalLight );
  306. let light = new THREE.AmbientLight( 0x555555 ); // soft white light
  307. this.scenePointCloud.add( light );
  308. { // background
  309. let texture = Utils.createBackgroundTexture(512, 512);
  310. texture.minFilter = texture.magFilter = THREE.NearestFilter;
  311. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  312. let bg = new THREE.Mesh(
  313. new THREE.PlaneBufferGeometry(2, 2, 1),
  314. new THREE.MeshBasicMaterial({
  315. map: texture
  316. })
  317. );
  318. bg.material.depthTest = false;
  319. bg.material.depthWrite = false;
  320. this.sceneBG.add(bg);
  321. }
  322. // { // lights
  323. // {
  324. // let light = new THREE.DirectionalLight(0xffffff);
  325. // light.position.set(10, 10, 1);
  326. // light.target.position.set(0, 0, 0);
  327. // this.scene.add(light);
  328. // }
  329. // {
  330. // let light = new THREE.DirectionalLight(0xffffff);
  331. // light.position.set(-10, 10, 1);
  332. // light.target.position.set(0, 0, 0);
  333. // this.scene.add(light);
  334. // }
  335. // {
  336. // let light = new THREE.DirectionalLight(0xffffff);
  337. // light.position.set(0, -10, 20);
  338. // light.target.position.set(0, 0, 0);
  339. // this.scene.add(light);
  340. // }
  341. // }
  342. }
  343. addAnnotation(position, args = {}){
  344. if(position instanceof Array){
  345. args.position = new THREE.Vector3().fromArray(position);
  346. } else if (position.x != null) {
  347. args.position = position;
  348. }
  349. let annotation = new Annotation(args);
  350. this.annotations.add(annotation);
  351. return annotation;
  352. }
  353. getAnnotations () {
  354. return this.annotations;
  355. };
  356. removeAnnotation(annotationToRemove) {
  357. this.annotations.remove(annotationToRemove);
  358. }
  359. };