Viewport.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as THREE from "../../../libs/three.js/build/three.module.js";
  2. import Common from '../utils/Common.js'
  3. export default class Viewport extends THREE.EventDispatcher{
  4. constructor( view, camera, prop={}){//目前不支持换camera
  5. super()
  6. this.left = prop.left;
  7. this.bottom = prop.bottom;
  8. this.width = prop.width;
  9. this.height = prop.height;
  10. this.name = prop.name
  11. this.view = view
  12. this.camera = camera
  13. this.active = true
  14. this.unableChangePos = false
  15. this.noPointcloud;
  16. //this.keys = [...] firstPersonCtl....
  17. this.resolution = new THREE.Vector2;
  18. this.resolution2 = new THREE.Vector2;
  19. this.offset = new THREE.Vector2; //viewportOffset 范围从0-整个画布的像素
  20. this.extraEnableLayers = prop.extraEnableLayers || [];//额外可展示的层
  21. this.cameraLayers = prop.cameraLayers
  22. this.pixelRatio = prop.pixelRatio //如果规定pixelRatio的话要传,这样就覆盖devicePicelRatio, 如magnifier
  23. }
  24. clone(){
  25. return Common.CloneClassObject(this)
  26. }
  27. getMoveSpeed(){
  28. return this.moveSpeed
  29. }
  30. setMoveSpeed(e){
  31. this.moveSpeed = e
  32. }
  33. layersAdd(name){
  34. this.extraEnableLayers.includes(name) || this.extraEnableLayers.push(name)
  35. }
  36. layersRemove(name){
  37. let index = this.extraEnableLayers.indexOf(name)
  38. if(index > -1){
  39. this.extraEnableLayers.splice(index, 1)
  40. }
  41. }
  42. cameraChanged() {
  43. var copy = ()=>{
  44. this.previousState = {
  45. projectionMatrix: this.camera.projectionMatrix.clone(),//worldMatrix在this.control时归零了所以不用了吧,用position和qua也一样
  46. position: this.camera.position.clone(),
  47. quaternion: this.camera.quaternion.clone(),
  48. active:this.active,
  49. resolution:this.resolution.clone(),
  50. resolution2:this.resolution2.clone(), //有时clientWidth没变但是ratio缩放了
  51. };
  52. }
  53. let projectionChanged = true, positionChanged = true, quaternionChanged = true, activeChanged = true, resolutionChanged = true
  54. let getChanged = ()=>{
  55. return {
  56. projectionChanged,positionChanged,quaternionChanged, activeChanged, resolutionChanged,
  57. changed:projectionChanged || positionChanged || quaternionChanged || activeChanged || resolutionChanged
  58. }
  59. }
  60. if (this.previousState){
  61. projectionChanged = !this.camera.projectionMatrix.equals(this.previousState.projectionMatrix)
  62. positionChanged = !this.camera.position.equals(this.previousState.position)
  63. quaternionChanged = !this.camera.quaternion.equals(this.previousState.quaternion)
  64. activeChanged = this.active != this.previousState.active
  65. resolutionChanged = !this.resolution.equals(this.previousState.resolution) || !this.resolution2.equals(this.previousState.resolution2)
  66. }
  67. copy()
  68. return getChanged()
  69. }
  70. setResolution(w,h, wholeW=0, wholeH=0){
  71. this.resolution.set(w,h);//是client的width height
  72. this.resolution2.copy(this.resolution).multiplyScalar(this.pixelRatio || window.devicePixelRatio)
  73. this.offset.set(wholeW,wholeH).multiply(new THREE.Vector2(this.left,this.bottom))//.multiplyScalar(window.devicePixelRatio)
  74. this.dispatchEvent({type:'resize'})
  75. }
  76. }