0

Based on this How do I rotate a box in A-Frame by moving or dragging the mouse?

how i can do it with a 3d model? i need rotate a 3d model/object in a view

i have tried this code, but doesn't works

<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>

Thanks in advance!

Also have tried this, but not luck

<!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>
Community
  • 1
  • 1
Cesar
  • 3
  • 1
  • 5

2 Answers2

1

Attach the drag-rotate-component to the model not the camera.

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

Also, you don't need to add -component to the component name.

ngokevin
  • 12,980
  • 2
  • 38
  • 84
  • Thanks for help!! now its working, https://codepen.io/djcesar/pen/EmwKQV By the same topic is there any way to change the center of rotation axis? this to rotate the model like the example with axis on the center https://sketchfab.com/models/e44c452648ab42d2bd5b87ff2f5937b5 , not with axis on the corner of the model as the codepen – Cesar May 04 '17 at 20:34
  • This component is somehow not working at Version 0.9.0. Has anybody a solution? – pr0cz Sep 30 '19 at 07:02
0

EDIT: sorry, my bad, this is not correct as it doesn't answer the question.

I think you simply forgot to append the name of the component drag-rotate-component as an attribute to the camera entity. See this pen.

<a-camera drag-rotate-component look-controls="enabled:false"></a-camera>
a.barbieri
  • 2,398
  • 3
  • 30
  • 58
  • Thanks for response, it does not works for my case, works as the pen but the desired function is rotate the model, not the current view. i need do something like this https://sketchfab.com/models/e44c452648ab42d2bd5b87ff2f5937b5 – Cesar May 04 '17 at 17:51