如何通过移动或拖动鼠标在A-Frame中旋转三维模型?

时间:2017-05-04 16:22:12

标签: rotation aframe 3d-model

基于此How do I rotate a box in A-Frame by moving or dragging the mouse?

我怎么能用3D模型做到这一点?我需要在视图中旋转3d模型/对象

我已尝试过此代码,但无效

<html>
<head>
  <title>Rotation</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://aframe.io/releases/0.4.0/aframe.min.js"></script>
</head>
<body>
<script type="text/javascript">
    AFRAME.registerComponent('drag-rotate-component',{
      schema : { speed : {default:1}},
      init : function(){
        this.ifMouseDown = false;
        this.x_cord = 0;
        this.y_cord = 0;
        document.addEventListener('mousedown',this.OnDocumentMouseDown.bind(this));
        document.addEventListener('mouseup',this.OnDocumentMouseUp.bind(this));
        document.addEventListener('mousemove',this.OnDocumentMouseMove.bind(this));
      },
      OnDocumentMouseDown : function(event){
        this.ifMouseDown = true;
        this.x_cord = event.clientX;
        this.y_cord = event.clientY;
      },
      OnDocumentMouseUp : function(){
        this.ifMouseDown = false;
      },
      OnDocumentMouseMove : function(event)
      {
        if(this.ifMouseDown)
        {
          var temp_x = event.clientX-this.x_cord;
          var temp_y = event.clientY-this.y_cord;
          if(Math.abs(temp_y)<Math.abs(temp_x))
          {
            this.el.object3D.rotateY(temp_x*this.data.speed/1000);
          }
          else
          {
            this.el.object3D.rotateX(temp_y*this.data.speed/1000);
          }
          this.x_cord = event.clientX;
          this.y_cord = event.clientY;
        }
      }
    });
  </script>
  <a-scene>
   <a-assets>
      <a-asset-item id="man" src="./assets/models/man/man.dae"></a-asset-item>
   </a-assets>
   <a-collada-model src="#man" rotation="0 45 0"></a-model>
   <a-sky color="#ECECEC"></a-sky>
   <a-entity position="0 -0.5 1">
     <a-camera look-controls="enabled:false"></a-camera>
   </a-entity>
  </a-scene>
</body>
</html>

提前致谢!

也试过这个,但没有运气

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title - Demo 3D Model</title>
    <meta name="description" content="Inlabs demo - 3D Model">
        <script src="../../../dist/aframe-master.js"></script>

  </head>
  <body>
    <a-scene>
      <a-assets>
        <a-asset-item id="man" src="./assets/models/man/man.dae"></a-asset-item>
      </a-assets>

      <a-collada-model src="#man" rotation="0 45 0"></a-model>
      <a-sky color="#ECECEC"></a-sky>
      <a-entity position="0 -0.5 1">
        <a-camera drag-rotate-component look-controls="enabled:false"></a-camera>
      </a-entity>
    </a-scene>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

drag-rotate-component附加到模型而不是相机。

<a-collada-model drag-rotate-component>

此外,您不需要将-component添加到组件名称。

答案 1 :(得分:0)

编辑:对不起,我的不好,这不正确,因为它没有回答这个问题。

我认为您只是忘了将组件drag-rotate-component的名称作为属性附加到相机实体。请参阅此pen

<a-camera drag-rotate-component look-controls="enabled:false"></a-camera>
相关问题