ExtendScene.js 6.2 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.overlayScene = new THREE.Scene();
  10. viewer.addEventListener("render.pass.perspective_overlay", this.renderOverlay.bind(this));
  11. this.cameraP = new THREE.PerspectiveCamera(this.fov, 1, Potree.config.view.near, Potree.config.view.cameraFar);
  12. this.cameraO = new THREE.OrthographicCamera(-1, 1, 1, -1, Potree.config.view.near, Potree.settings.cameraFar);
  13. this.cameraP.limitFar = true//add
  14. this.initializeExtend();
  15. //-------------
  16. this.axisArrow = new Axis();
  17. this.scene.add(this.axisArrow)
  18. if(!Potree.settings.isDebug && !Potree.settings.showAxis)this.axisArrow.visible = false
  19. Potree.Utils.setObjectLayers(this.axisArrow, 'bothMapAndScene' )
  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. getActiveCamera() {
  84. return viewer.mainViewport.camera
  85. }
  86. initialize(){//不用旧的 因为还没创建完变量
  87. }
  88. initializeExtend(){//add 新的initialize
  89. this.referenceFrame = new THREE.Object3D();
  90. this.referenceFrame.matrixAutoUpdate = false;
  91. this.scenePointCloud.add(this.referenceFrame);
  92. if(window.axisYup){
  93. }else{
  94. this.cameraP.up.set(0, 0, 1);
  95. this.cameraO.up.set(0, 0, 1);
  96. }
  97. this.cameraP.position.set(1000, 1000, 1000);
  98. this.cameraO.position.set(1000, 1000, 1000);
  99. //this.camera.rotation.y = -Math.PI / 4;
  100. //this.camera.rotation.x = -Math.PI / 6;
  101. this.cameraScreenSpace.lookAt(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, -1), new THREE.Vector3(0, 1, 0));
  102. this.directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  103. this.directionalLight.position.set( 10, 10, 10 );
  104. this.directionalLight.lookAt( new THREE.Vector3(0, 0, 0));
  105. this.scenePointCloud.add( this.directionalLight );
  106. let light = new THREE.AmbientLight( 0x555555 ); // soft white light
  107. this.scenePointCloud.add( light );
  108. //add:------给空间模型的box 或其他obj------
  109. let light2 = new THREE.AmbientLight( 16777215, 0.5 );
  110. Potree.Utils.setObjectLayers(light2, 'light'/* 'bothMapAndScene' */)
  111. this.scene.add(light2)
  112. let light3 = new THREE.DirectionalLight( 16777215, 0.7);
  113. light3.position.set( 10, 10, 10 );
  114. light3.lookAt( new THREE.Vector3(0, 0, 0));
  115. Potree.Utils.setObjectLayers(light3, 'light')
  116. this.scene.add(light3)
  117. let light4 = new THREE.DirectionalLight( 16777215, 0.3); //补光
  118. light4.position.set( -10, -5, -7 );
  119. light4.lookAt( new THREE.Vector3(0, 0, 0));
  120. Potree.Utils.setObjectLayers(light4, 'light')
  121. this.scene.add(light4)
  122. //--------------------------------------------
  123. { // background
  124. let texture = Utils.createBackgroundTexture(512, 512);
  125. texture.minFilter = texture.magFilter = THREE.NearestFilter;
  126. texture.minFilter = texture.magFilter = THREE.LinearFilter;
  127. let bg = new THREE.Mesh(
  128. new THREE.PlaneBufferGeometry(2, 2, 1),
  129. new THREE.MeshBasicMaterial({
  130. map: texture
  131. })
  132. );
  133. bg.material.depthTest = false;
  134. bg.material.depthWrite = false;
  135. bg.name = 'bg'
  136. //this.sceneBG.add(bg);
  137. this.scene.add(bg)
  138. bg.layers.set(Potree.config.renderLayers.bg)
  139. }
  140. { // background color
  141. let bg2 = new THREE.Mesh(
  142. new THREE.PlaneBufferGeometry(2, 2, 1),
  143. new THREE.MeshBasicMaterial({
  144. transparent : true
  145. })
  146. );
  147. bg2.material.depthTest = false;
  148. bg2.material.depthWrite = false;
  149. bg2.name = 'bg2'
  150. this.scene.add(bg2)
  151. bg2.layers.set(Potree.config.renderLayers.bg2)
  152. this.bg2 = bg2
  153. }
  154. }
  155. renderOverlay(o={}){// measure tag
  156. if(this.overlayScene.children.filter(e=>e.visible).length == 0)return
  157. let renderer = o.renderer || this.viewer.renderer
  158. Potree.Utils.setCameraLayers(o.camera, ['sceneObjects'])
  159. viewer.dispatchEvent({type: "render.begin2" , name:'overlays', viewport:o.viewport, renderer:o.renderer })
  160. renderer.render(this.overlayScene, o.camera );
  161. }
  162. };
  163. //Object.assign( ExtendScene.prototype, THREE.EventDispatcher.prototype );
  164. export {ExtendScene}