ExtendScene.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Utils} from "../utils.js";
  3. import Axis from '../custom/objects/Axis.js'
  4. import {Scene} from './Scene.js'
  5. class ExtendScene extends Scene{
  6. constructor(){
  7. super();
  8. delete this.sceneBG;
  9. this.cameraP = new THREE.PerspectiveCamera(this.fov, 1, Potree.config.view.near, Potree.config.view.near);
  10. this.cameraO = new THREE.OrthographicCamera(-1, 1, 1, -1, Potree.config.view.near, Potree.settings.cameraFar);
  11. this.cameraP.limitFar = true//add
  12. this.initializeExtend();
  13. //-------------
  14. this.axisArrow = new Axis();
  15. this.scene.add(this.axisArrow)
  16. if(!Potree.settings.isDebug)this.axisArrow.visible = false
  17. viewer.setObjectLayers(this.axisArrow, 'bothMapAndScene' )
  18. this.tags = new THREE.Object3D;
  19. this.scene.add(this.tags)
  20. }
  21. estimateHeightAt (position) {
  22. let height = null;
  23. let fromSpacing = Infinity;
  24. for (let pointcloud of this.pointclouds) {
  25. if (pointcloud.root.geometryNode === undefined) {
  26. continue;
  27. }
  28. let pHeight = null;
  29. let pFromSpacing = Infinity;
  30. let lpos = position.clone().sub(pointcloud.position);
  31. lpos.z = 0;
  32. let ray = new THREE.Ray(lpos, new THREE.Vector3(0, 0, 1));
  33. let stack = [pointcloud.root];
  34. while (stack.length > 0) {
  35. let node = stack.pop();
  36. let box = node.getBoundingBox();
  37. let inside = ray.intersectBox(box);
  38. if (!inside) {
  39. continue;
  40. }
  41. let h = node.geometryNode.mean.z +
  42. pointcloud.position.z +
  43. node.geometryNode.boundingBox.min.z;
  44. if (node.geometryNode.spacing <= pFromSpacing) {
  45. pHeight = h;
  46. pFromSpacing = node.geometryNode.spacing;
  47. }
  48. for (let index of Object.keys(node.children)) {
  49. let child = node.children[index];
  50. if (child.geometryNode) {
  51. stack.push(node.children[index]);
  52. }
  53. }
  54. }
  55. if (height === null || pFromSpacing < fromSpacing) {
  56. height = pHeight;
  57. fromSpacing = pFromSpacing;
  58. }
  59. }
  60. return height;
  61. }
  62. //add:
  63. removePointCloud (pointcloud) {
  64. let index = this.pointclouds.indexOf(pointcloud);
  65. if(index == -1)return
  66. this.pointclouds.splice(index, 1)
  67. this.scenePointCloud.remove(pointcloud);
  68. pointcloud.panos.forEach(pano=>{
  69. pano.dispose()
  70. })
  71. }
  72. removeCameraAnimation(animation){
  73. let index = this.cameraAnimations.indexOf(animation);
  74. if (index > -1) {
  75. this.cameraAnimations.splice(index, 1);
  76. this.dispatchEvent({
  77. 'type': 'camera_animation_removed',
  78. 'scene': this,
  79. 'animation': animation
  80. });
  81. }
  82. };
  83. initialize(){//不用旧的 因为还没创建完变量
  84. }
  85. initializeExtend(){//add 新的initialize
  86. this.referenceFrame = new THREE.Object3D();
  87. this.referenceFrame.matrixAutoUpdate = false;
  88. this.scenePointCloud.add(this.referenceFrame);
  89. if(window.axisYup){
  90. }else{
  91. this.cameraP.up.set(0, 0, 1);
  92. this.cameraO.up.set(0, 0, 1);
  93. }
  94. this.cameraP.position.set(1000, 1000, 1000);
  95. this.cameraO.position.set(1000, 1000, 1000);
  96. //this.camera.rotation.y = -Math.PI / 4;
  97. //this.camera.rotation.x = -Math.PI / 6;
  98. this.cameraScreenSpace.lookAt(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -1), new THREE.Vector3(0, 1, 0));
  99. this.directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  100. this.directionalLight.position.set( 10, 10, 10 );
  101. this.directionalLight.lookAt( new THREE.Vector3(0, 0, 0));
  102. this.scenePointCloud.add( this.directionalLight );
  103. let light = new THREE.AmbientLight( 0x555555 ); // soft white light
  104. this.scenePointCloud.add( light );
  105. //add:------给空间模型的box 或其他obj------
  106. let light2 = new THREE.AmbientLight( 16777215, 1 );
  107. viewer.setObjectLayers(light2, 'bothMapAndScene')
  108. this.scene.add(light2)
  109. let light3 = new THREE.DirectionalLight( 16777215, 1);
  110. light3.position.set( 10, 10, 10 );
  111. light3.lookAt( new THREE.Vector3(0, 0, 0));
  112. viewer.setObjectLayers(light3, 'bothMapAndScene')
  113. this.scene.add(light3)
  114. //--------------------------------------------
  115. { // background
  116. let texture = Utils.createBackgroundTexture(512, 512);
  117. texture.minFilter = texture.magFilter = THREE.NearestFilter;
  118. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  119. let bg = new THREE.Mesh(
  120. new THREE.PlaneBufferGeometry(2, 2, 1),
  121. new THREE.MeshBasicMaterial({
  122. map: texture
  123. })
  124. );
  125. bg.material.depthTest = false;
  126. bg.material.depthWrite = false;
  127. bg.name = 'bg'
  128. //this.sceneBG.add(bg);
  129. this.scene.add(bg)
  130. bg.layers.set(Potree.config.renderLayers.bg)
  131. }
  132. { // background color
  133. let bg2 = new THREE.Mesh(
  134. new THREE.PlaneBufferGeometry(2, 2, 1),
  135. new THREE.MeshBasicMaterial({
  136. transparent : true
  137. })
  138. );
  139. bg2.material.depthTest = false;
  140. bg2.material.depthWrite = false;
  141. bg2.name = 'bg2'
  142. this.scene.add(bg2)
  143. bg2.layers.set(Potree.config.renderLayers.bg2)
  144. this.bg2 = bg2
  145. }
  146. // { // lights
  147. // {
  148. // let light = new THREE.DirectionalLight(0xffffff);
  149. // light.position.set(10, 10, 1);
  150. // light.target.position.set(0, 0, 0);
  151. // this.scene.add(light);
  152. // }
  153. // {
  154. // let light = new THREE.DirectionalLight(0xffffff);
  155. // light.position.set(-10, 10, 1);
  156. // light.target.position.set(0, 0, 0);
  157. // this.scene.add(light);
  158. // }
  159. // {
  160. // let light = new THREE.DirectionalLight(0xffffff);
  161. // light.position.set(0, -10, 20);
  162. // light.target.position.set(0, 0, 0);
  163. // this.scene.add(light);
  164. // }
  165. // }
  166. }
  167. };
  168. Object.assign( ExtendScene.prototype, THREE.EventDispatcher.prototype );
  169. export {ExtendScene}