【问题标题】:How do I rotate a 3d model in A-Frame by moving or dragging the mouse?如何通过移动或拖动鼠标在 A-Frame 中旋转 3d 模型?
【发布时间】:2017-10-02 22:42:36
【问题描述】:

基于此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>

【问题讨论】:

    标签: rotation aframe 3d-model


    【解决方案1】:

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

    &lt;a-collada-model drag-rotate-component&gt;

    另外,您不需要在组件名称中添加-component

    【讨论】:

    • 感谢您的帮助!!现在它的工作,@ 987654321@ 通过相同的主题有没有办法改变旋转轴的中心?这样可以像示例一样旋转模型,轴位于中心 sketchfab.com/models/e44c452648ab42d2bd5b87ff2f5937b5 ,而不是轴位于模型的角上作为代码笔
    • 此组件在 0.9.0 版本中无法正常工作。有人有解决办法吗?
    【解决方案2】:

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

    我认为您只是忘记将组件名称drag-rotate-component 作为属性附加到相机实体。看到这个pen

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

    【讨论】:

    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2015-08-21
    • 2021-08-08
    • 2011-03-27
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多