import * as THREE from "../../../libs/three.js/build/three.module.js"; import Common from '../utils/Common.js' export default class Viewport extends THREE.EventDispatcher{ constructor( view, camera, prop={}){//目前不支持换camera super() this.left = prop.left; this.bottom = prop.bottom; this.width = prop.width; this.height = prop.height; this.name = prop.name this.view = view this.camera = camera this.active = true this.unableChangePos = false this.noPointcloud; //this.keys = [...] firstPersonCtl.... this.resolution = new THREE.Vector2; this.resolution2 = new THREE.Vector2; this.offset = new THREE.Vector2; //viewportOffset 范围从0-整个画布的像素 this.extraEnableLayers = prop.extraEnableLayers || [];//额外可展示的层 this.cameraLayers = prop.cameraLayers this.pixelRatio = prop.pixelRatio //如果规定pixelRatio的话要传,这样就覆盖devicePicelRatio, 如magnifier } clone(){ return Common.CloneClassObject(this) } getMoveSpeed(){ return this.moveSpeed } setMoveSpeed(e){ this.moveSpeed = e } layersAdd(name){ this.extraEnableLayers.includes(name) || this.extraEnableLayers.push(name) } layersRemove(name){ let index = this.extraEnableLayers.indexOf(name) if(index > -1){ this.extraEnableLayers.splice(index, 1) } } cameraChanged() { var copy = ()=>{ this.previousState = { projectionMatrix: this.camera.projectionMatrix.clone(),//worldMatrix在this.control时归零了所以不用了吧,用position和qua也一样 position: this.camera.position.clone(), quaternion: this.camera.quaternion.clone(), active:this.active, resolution:this.resolution.clone(), resolution2:this.resolution2.clone(), //有时clientWidth没变但是ratio缩放了 }; } let projectionChanged = true, positionChanged = true, quaternionChanged = true, activeChanged = true, resolutionChanged = true let getChanged = ()=>{ return { projectionChanged,positionChanged,quaternionChanged, activeChanged, resolutionChanged, changed:projectionChanged || positionChanged || quaternionChanged || activeChanged || resolutionChanged } } if (this.previousState){ projectionChanged = !this.camera.projectionMatrix.equals(this.previousState.projectionMatrix) positionChanged = !this.camera.position.equals(this.previousState.position) quaternionChanged = !this.camera.quaternion.equals(this.previousState.quaternion) activeChanged = this.active != this.previousState.active resolutionChanged = !this.resolution.equals(this.previousState.resolution) || !this.resolution2.equals(this.previousState.resolution2) } copy() return getChanged() } setResolution(w,h, wholeW=0, wholeH=0){ this.resolution.set(w,h);//是client的width height this.resolution2.copy(this.resolution).multiplyScalar(this.pixelRatio || window.devicePixelRatio) this.offset.set(wholeW,wholeH).multiply(new THREE.Vector2(this.left,this.bottom))//.multiplyScalar(window.devicePixelRatio) this.dispatchEvent({type:'resize'}) } }