threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OrbitControls2.ts 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {Camera, PerspectiveCamera, Vector3} from 'three'
  2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
  3. const offset2 = new Vector3()
  4. const targetDeltaX = new Vector3()
  5. const targetDeltaY = new Vector3()
  6. const targetDeltaZ = new Vector3()
  7. const targetDelta = new Vector3()
  8. const panOffset2 = new Vector3()
  9. let scaleOffset = 1
  10. const upVec = new Vector3(0, 1, 0)
  11. export class OrbitControls2 extends OrbitControls {
  12. throttleUpdate = 60
  13. constructor(object: Camera, domElement: HTMLElement) {
  14. super(object, domElement)
  15. const sup = this.update
  16. this.update = ()=>this._update(sup)
  17. }
  18. readonly targetOffset = new Vector3(0, 0, 0)
  19. private _update(sup: ()=>boolean): boolean {
  20. this.target.add(this.targetOffset)
  21. offset2.copy(this.object.position).sub(this.target)
  22. scaleOffset = offset2.length()
  23. panOffset2.copy(this.target)
  24. const ret = sup()
  25. panOffset2.sub(this.target) // get the panOffset of this frame from OrbitControls
  26. // if (panOffset2.length() > 0.0001)
  27. // console.log(panOffset2.toArray())
  28. // console.log(offset2.clone().normalize().cross(upVec))
  29. offset2.copy(this.object.position).sub(this.target)
  30. // panOffset2.multiplyScalar(-1)
  31. // panOffset3.x = panOffset3.z
  32. // console.log(panOffset3.z)
  33. scaleOffset /= offset2.length()
  34. this.target.add(panOffset2)
  35. this.object.position.copy(this.target).add(offset2)
  36. offset2.normalize()
  37. targetDeltaX.crossVectors(upVec, offset2).normalize()
  38. targetDeltaY.crossVectors(offset2, targetDeltaX).normalize()
  39. targetDeltaZ.crossVectors(targetDeltaX, targetDeltaY).normalize().negate()
  40. if (targetDeltaX.length() > 0.1) // check if not 0
  41. this.object.up.crossVectors(offset2.clone().normalize(), targetDeltaX)
  42. if (this.enablePan) {
  43. targetDelta.set(0, 0, 0)
  44. .addScaledVector(targetDeltaX, panOffset2.x)
  45. .addScaledVector(targetDeltaY, panOffset2.y)
  46. .addScaledVector(targetDeltaZ, panOffset2.z)
  47. this.targetOffset.add(targetDelta)
  48. this.targetOffset.multiplyScalar(1. / scaleOffset)
  49. }
  50. targetDelta.set(0, 0, 0)
  51. .addScaledVector(targetDeltaX, -this.targetOffset.x)
  52. .addScaledVector(targetDeltaY, -this.targetOffset.y)
  53. .addScaledVector(targetDeltaZ, -this.targetOffset.z)
  54. // console.log(targetDelta)
  55. this.object.lookAt(targetDelta.add(this.target))
  56. this.object.updateMatrixWorld()
  57. if ((this.object as PerspectiveCamera).isCamera) {
  58. (this.object as PerspectiveCamera).updateProjectionMatrix()
  59. }
  60. this.target.sub(this.targetOffset)
  61. return ret
  62. }
  63. }