Remove setInterval implementations from camera controls (#4255)
* remove setInterval implementations from camera controls * fmt
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								..env.development.local.swp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								..env.development.local.swp
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@ -64,6 +64,27 @@ export type ReactCameraProperties =
 | 
			
		||||
 | 
			
		||||
const lastCmdDelay = 50
 | 
			
		||||
 | 
			
		||||
class CameraRateLimiter {
 | 
			
		||||
  lastSend?: Date = undefined
 | 
			
		||||
  rateLimitMs: number = 16 //60 FPS
 | 
			
		||||
 | 
			
		||||
  send = (f: () => void) => {
 | 
			
		||||
    let now = new Date()
 | 
			
		||||
 | 
			
		||||
    if (
 | 
			
		||||
      this.lastSend === undefined ||
 | 
			
		||||
      now.getTime() - this.lastSend.getTime() > this.rateLimitMs
 | 
			
		||||
    ) {
 | 
			
		||||
      f()
 | 
			
		||||
      this.lastSend = now
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  reset = () => {
 | 
			
		||||
    this.lastSend = undefined
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class CameraControls {
 | 
			
		||||
  engineCommandManager: EngineCommandManager
 | 
			
		||||
  syncDirection: 'clientToEngine' | 'engineToClient' = 'engineToClient'
 | 
			
		||||
@ -77,9 +98,8 @@ export class CameraControls {
 | 
			
		||||
  enableRotate = true
 | 
			
		||||
  enablePan = true
 | 
			
		||||
  enableZoom = true
 | 
			
		||||
  zoomDataFromLastFrame?: number = undefined
 | 
			
		||||
  // holds coordinates, and interaction
 | 
			
		||||
  moveDataFromLastFrame?: [number, number, string] = undefined
 | 
			
		||||
  moveSender: CameraRateLimiter = new CameraRateLimiter()
 | 
			
		||||
  zoomSender: CameraRateLimiter = new CameraRateLimiter()
 | 
			
		||||
  lastPerspectiveFov: number = 45
 | 
			
		||||
  pendingZoom: number | null = null
 | 
			
		||||
  pendingRotation: Vector2 | null = null
 | 
			
		||||
@ -171,6 +191,36 @@ export class CameraControls {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  doMove = (interaction: any, coordinates: any) => {
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | 
			
		||||
    this.engineCommandManager.sendSceneCommand({
 | 
			
		||||
      type: 'modeling_cmd_req',
 | 
			
		||||
      cmd: {
 | 
			
		||||
        type: 'camera_drag_move',
 | 
			
		||||
        interaction: interaction,
 | 
			
		||||
        window: {
 | 
			
		||||
          x: coordinates[0],
 | 
			
		||||
          y: coordinates[1],
 | 
			
		||||
        },
 | 
			
		||||
      },
 | 
			
		||||
      cmd_id: uuidv4(),
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  doZoom = (zoom: number) => {
 | 
			
		||||
    this.handleStart()
 | 
			
		||||
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | 
			
		||||
    this.engineCommandManager.sendSceneCommand({
 | 
			
		||||
      type: 'modeling_cmd_req',
 | 
			
		||||
      cmd: {
 | 
			
		||||
        type: 'default_camera_zoom',
 | 
			
		||||
        magnitude: (-1 * zoom) / window.devicePixelRatio,
 | 
			
		||||
      },
 | 
			
		||||
      cmd_id: uuidv4(),
 | 
			
		||||
    })
 | 
			
		||||
    this.handleEnd()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  constructor(
 | 
			
		||||
    isOrtho = false,
 | 
			
		||||
    domElement: HTMLCanvasElement,
 | 
			
		||||
@ -258,49 +308,6 @@ export class CameraControls {
 | 
			
		||||
      this.onCameraChange()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Our stream is never more than 60fps.
 | 
			
		||||
    // We can get away with capping our "virtual fps" to 60 then.
 | 
			
		||||
    const FPS_VIRTUAL = 60
 | 
			
		||||
 | 
			
		||||
    const doZoom = () => {
 | 
			
		||||
      if (this.zoomDataFromLastFrame !== undefined) {
 | 
			
		||||
        this.handleStart()
 | 
			
		||||
        // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | 
			
		||||
        this.engineCommandManager.sendSceneCommand({
 | 
			
		||||
          type: 'modeling_cmd_req',
 | 
			
		||||
          cmd: {
 | 
			
		||||
            type: 'default_camera_zoom',
 | 
			
		||||
            magnitude:
 | 
			
		||||
              (-1 * this.zoomDataFromLastFrame) / window.devicePixelRatio,
 | 
			
		||||
          },
 | 
			
		||||
          cmd_id: uuidv4(),
 | 
			
		||||
        })
 | 
			
		||||
        this.handleEnd()
 | 
			
		||||
      }
 | 
			
		||||
      this.zoomDataFromLastFrame = undefined
 | 
			
		||||
    }
 | 
			
		||||
    setInterval(doZoom, 1000 / FPS_VIRTUAL)
 | 
			
		||||
 | 
			
		||||
    const doMove = () => {
 | 
			
		||||
      if (this.moveDataFromLastFrame !== undefined) {
 | 
			
		||||
        // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | 
			
		||||
        this.engineCommandManager.sendSceneCommand({
 | 
			
		||||
          type: 'modeling_cmd_req',
 | 
			
		||||
          cmd: {
 | 
			
		||||
            type: 'camera_drag_move',
 | 
			
		||||
            interaction: this.moveDataFromLastFrame[2] as any,
 | 
			
		||||
            window: {
 | 
			
		||||
              x: this.moveDataFromLastFrame[0],
 | 
			
		||||
              y: this.moveDataFromLastFrame[1],
 | 
			
		||||
            },
 | 
			
		||||
          },
 | 
			
		||||
          cmd_id: uuidv4(),
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
      this.moveDataFromLastFrame = undefined
 | 
			
		||||
    }
 | 
			
		||||
    setInterval(doMove, 1000 / FPS_VIRTUAL)
 | 
			
		||||
 | 
			
		||||
    setTimeout(() => {
 | 
			
		||||
      this.engineCommandManager.subscribeTo({
 | 
			
		||||
        event: 'camera_drag_end',
 | 
			
		||||
@ -386,7 +393,9 @@ export class CameraControls {
 | 
			
		||||
      if (interaction === 'none') return
 | 
			
		||||
 | 
			
		||||
      if (this.syncDirection === 'engineToClient') {
 | 
			
		||||
        this.moveDataFromLastFrame = [event.clientX, event.clientY, interaction]
 | 
			
		||||
        this.moveSender.send(() => {
 | 
			
		||||
          this.doMove(interaction, [event.clientX, event.clientY])
 | 
			
		||||
        })
 | 
			
		||||
        return
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@ -459,7 +468,9 @@ export class CameraControls {
 | 
			
		||||
 | 
			
		||||
    if (this.syncDirection === 'engineToClient') {
 | 
			
		||||
      if (interaction === 'zoom') {
 | 
			
		||||
        this.zoomDataFromLastFrame = event.deltaY
 | 
			
		||||
        this.zoomSender.send(() => {
 | 
			
		||||
          this.doZoom(event.deltaY)
 | 
			
		||||
        })
 | 
			
		||||
      } else {
 | 
			
		||||
        // This case will get handled when we add pan and rotate using Apple trackpad.
 | 
			
		||||
        console.error(
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user