簡體   English   中英

ThreeJS如何設置OrbitControls的位置

[英]ThreeJS How to set the position of the OrbitControls

我正在使用.getAzimuthalAngle()和.getPolarAngle()存儲OrbitControls的位置,稍后我希望能夠設置getAzimuthalAngle和getPolarAngle,但它們的值略有不同。

我使用getAzimuthalAngle和getPolarAngle的原因是因為我需要計算旋轉的百分比,例如,如果旋轉180%,則為50%。 與背景一樣,我將2D地圖的位置設置為鏡像相同的位置。 我已經以一種方式使它工作了,但是由於無法設置OrbitControls的位置,我陷入了困境。

我設法通過復制OrbitControl更新函數並直接設置theta和phi值(而不是以增量方式作為更新函數)來破解一個解決方案

this.setPhiTheta = function(t, p) {
    var position = this.object.position;

    offset.copy( position ).sub( this.target );

    // rotate offset to "y-axis-is-up" space
    offset.applyQuaternion( quat );

    // angle from z-axis around y-axis

    theta = Math.atan2( offset.x, offset.z );

    // angle from y-axis

    phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );

    if ( this.autoRotate && state === STATE.NONE ) {

        this.rotateLeft( getAutoRotationAngle() );

    }

    /*theta = 3.105;
    phi = 1.48;*/
    theta = t;
    phi = p
    //console.log("theta " + theta)
    // restrict theta to be between desired limits
    theta = Math.max( this.minAzimuthAngle, Math.min( this.maxAzimuthAngle, theta ) );

    // restrict phi to be between desired limits
    phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );

    // restrict phi to be betwee EPS and PI-EPS
    phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );

    var radius = offset.length() * scale;

    // restrict radius to be between desired limits
    radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );

    // move target to panned location
    this.target.add( pan );

    offset.x = radius * Math.sin( phi ) * Math.sin( theta );
    offset.y = radius * Math.cos( phi );
    offset.z = radius * Math.sin( phi ) * Math.cos( theta );

    // rotate offset back to "camera-up-vector-is-up" space
    offset.applyQuaternion( quatInverse );

    position.copy( this.target ).add( offset );

    this.object.lookAt( this.target );

    //thetaDelta /= 1.2;
    //phiDelta /= 1.2;
    scale = 1;
    pan.set( 0, 0, 0 );

    // update condition is:
    // min(camera displacement, camera rotation in radians)^2 > EPS
    // using small-angle approximation cos(x/2) = 1 - x^2 / 8

    if ( lastPosition.distanceToSquared( this.object.position ) > EPS
        || 8 * (1 - lastQuaternion.dot(this.object.quaternion)) > EPS ) {

        this.dispatchEvent( changeEvent );

        lastPosition.copy( this.object.position );
        lastQuaternion.copy (this.object.quaternion );

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM