javascript - 請問如何旋轉和移動一個構件?
PHPz
PHPz 2017-05-19 10:10:06
0
1
482

請問有辦法在選取一個構件後讓他移動或旋轉嗎?例如選取後依某個軸向旋轉一角度,或是選取後讓構件移動到其他位置?

PHPz
PHPz

学习是最好的投资!

全部回覆(1)
给我你的怀抱

可以的,你可以這樣來達成:

  1. 依 Y 軸旋轉 180 度(以下為ES2015的程式碼範例):

class RotateExt extends Autodesk.Viewing.Extension {
  constructor( viewer, options ) {
    super();
  }

   load() {
    viewer.addEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
    return true;
   }

   unload() {
    viewer.removeEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
    return true;
   }
   
  /**!
   * 关键函数
   */
   onSelectionChanged = ( event ) => {
       
    const quaternion = new THREE.Quaternion();
    // 设置旋转量 - 依 Y 轴旋转构件 180 度
    quaternion.setFromAxisAngle( new THREE.Vector3( 0,1,0 ), Math.PI );

    const model = event.model;
    const fragIdsArray = event.fragIdsArray;

    fragIdsArray.forEach( ( fragId, idx ) => {
      const fragProxy = this.viewer.impl.getFragmentProxy( model, fragId );

      fragProxy.getAnimTransform();

      const position = new THREE.Vector3( fragProxy.position.x, fragProxy.position.y, fragProxy.position.z );
      position.applyQuaternion( quaternion );

      fragProxy.position = position;
      fragProxy.quaternion.multiplyQuaternions( quaternion, fragProxy.quaternion );

      if( idx === 0 ) {
        const euler = new THREE.Euler();
        euler.setFromQuaternion( fragProxy.quaternion, 0 );
      }

      fragProxy.updateAnimTransform();
    });
    
    this.viewer.impl.sceneUpdated( true );
  };
}

Autodesk.Viewing.theExtensionManager.registerExtension( 'Autodesk.ADN.Viewing.Extension.RotateTool', RotateExt );
  1. 沿X軸移動 -100 單位(以下為ES2015的代碼範例):

class TranslateExt extends Autodesk.Viewing.Extension {
  constructor( viewer, options ) {
    super();
  }

   load() {
    viewer.addEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
    return true;
   }

   unload() {
    viewer.removeEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, this.onSelectionChanged );
    return true;
   }

  /**!
   * 关键函数
   */
   onSelectionChanged = ( event ) => {
    
    // 设置移动量 - 向 X 轴移动 -100 单位
    const offset = new THREE.Vector3( -100, 0 , 0 );

    const model = event.model;
    const fragIdsArray = event.fragIdsArray;

    fragIdsArray.forEach( ( fragId, idx ) => {
      const fragProxy = this.viewer.impl.getFragmentProxy( model, fragId );

      fragProxy.getAnimTransform();

      const position = new THREE.Vector3(
        fragProxy.position.x + offset.x,
        fragProxy.position.y + offset.y,
        fragProxy.position.z + offset.z
      );

      fragProxy.position = position;

      fragProxy.updateAnimTransform();
    });
    
    this.viewer.impl.sceneUpdated( true );
  };
}

Autodesk.Viewing.theExtensionManager.registerExtension( 'Autodesk.ADN.Viewing.Extension.TranslateTool', TranslateExt );
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板