@@ -100,6 +100,7 @@ class OrbitControls extends EventDispatcher<StandardControlsEventMap> {
100100 setPolarAngle : ( x : number ) => void
101101 setAzimuthalAngle : ( x : number ) => void
102102 getDistance : ( ) => number
103+ setDistance : ( x : number ) => void
103104 // Not used in most scenarios, however they can be useful for specific use cases
104105 getZoomScale : ( ) => number
105106
@@ -181,6 +182,21 @@ class OrbitControls extends EventDispatcher<StandardControlsEventMap> {
181182
182183 this . getDistance = ( ) : number => scope . object . position . distanceTo ( scope . target )
183184
185+ this . setDistance = ( value : number ) : void => {
186+ const clamped = Math . max ( scope . minDistance , Math . min ( scope . maxDistance , value ) )
187+ const current = scope . object . position . distanceTo ( scope . target )
188+ if ( current === 0 ) return
189+ if ( ( scope . object as OrthographicCamera ) . isOrthographicCamera ) {
190+ // z position has no effect on a parallel projection, so adjust zoom instead
191+ const orthoCamera = scope . object as OrthographicCamera
192+ orthoCamera . zoom = Math . max ( scope . minZoom , Math . min ( scope . maxZoom , ( orthoCamera . zoom * current ) / clamped ) )
193+ orthoCamera . updateProjectionMatrix ( )
194+ } else {
195+ scope . object . position . sub ( scope . target ) . setLength ( clamped ) . add ( scope . target )
196+ }
197+ scope . update ( )
198+ }
199+
184200 this . listenToKeyEvents = ( domElement : HTMLElement ) : void => {
185201 domElement . addEventListener ( 'keydown' , onKeyDown )
186202 this . _domElementKeyEvents = domElement
0 commit comments