javascript - 请问如何旋转和移动一个构件?
PHPz
PHPz 2017-05-19 10:10:06
0
1
480

请问有办法在选中一个构件后让他移动或旋转吗?例如选中后依某个轴向旋转一角度,或者是选中后让构件移动到其他位置?

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 );
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板