【问题标题】:Resizing aframe object in the browser在浏览器中调整框架对象的大小
【发布时间】:2022-01-07 22:19:50
【问题描述】:

我正在使用 A-Frame 在 360 度视频中添加一些形状(圆形或矩形),我可以拖动形状以更改其在视频帧中的位置,但我还需要能够调整这些形状的大小以覆盖视频中的精确对象,假设我想用矩形覆盖视频帧中的绘画,所以我希望能够在网络浏览器模式下调整形状以正确覆盖绘画。我发现这个post 询问有关旋转对象的问题,他说他成功地进行了调整大小而没有解释他是如何做到的。有人知道怎么做吗?

【问题讨论】:

  • 你是如何拖动它们的,你想如何旋转/调整它们的大小?
  • 以下是在提到的帖子glitch.com/edit/#!/foul-pointy-collard?path=index.html%3A1%3A0 中提出轮换作为答案的方式
  • 基于此示例在我的应用程序中完成拖动jesstelford.github.io/aframe-click-drag-component/basic
  • 如果您看到旋转示例,您可以在浏览器中看到一个可以旋转的框,我想知道的是是否可以在浏览器模式下仅使用鼠标调整框的大小.. . 就像您在任何富文本编辑器(例如 MS Word)中调整任何形状的大小时所做的一样。我希望现在更清楚了:)
  • Can't make the drag component in a fiddle,所以我创建了一个关于如何从头开始重新缩放对象的答案。另请查看transform controls

标签: aframe


【解决方案1】:

您可以使用a-frame API 重新缩放元素:

element.setAttribute("scale", "1 1 1") // replace 1 1 1 with new values

或通过访问底层的THREE.js API:

element.object3D.scale.multiplyScalar(scaleFactor);

您可以轻松创建一个组件,该组件将重新调整“选定”元素的大小。运行 snipper,将鼠标悬停在一个元素上并使用滚轮:

<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
  // register component
  AFRAME.registerComponent("foo", {
    // init callback
    init: function() {
      // react to press / release events to know if the element is selected
      this.el.addEventListener("mousedown", e => {
        this.selected = true;
      })
      this.el.addEventListener("mouseup", e => {
        this.selected = false;
      })
      // if this is selected, scale up/down when the mouse is scrolled
      this.el.sceneEl.addEventListener("mousewheel", evt => {
        if (!this.selected) return; // ignore when not selected
        const scale_factor = evt.deltaY < 0 ? 1.1 : 0.9 // scale up or down depending on the scroll direction
        this.el.object3D.scale.multiplyScalar(scale_factor);
        evt.preventDefault();
      })
    }
  })

</script>
<a-scene cursor="rayOrigin: mouse" raycaster="objects: .interactable">
  <a-box class="interactable" position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9" foo></a-box>
  <a-sphere class="interactable" position="0 1.25 -5" radius="1.25" color="#EF2D5E" foo></a-sphere>
  <a-cylinder class="interactable" position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D" foo></a-cylinder>
  <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
</a-scene>

如果您正在寻找现成的解决方案,我建议您查看transform controls


如果您想通过移动顶点来调整平面大小以制作自定义多边形,您可以直接更改平面顶点位置:

<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
  AFRAME.registerComponent("foo", {
    init: function() {
      // grab the threejs mesh
      const mesh = this.el.getObject3D("mesh");
      // grab the vertices array
      // the array is looking like this: x1, y1, z1, x2, y2, z2, ... xn, yn, zn
      const vertices = mesh.geometry.attributes.position.array;
            
      // manipulate the vertices in fixed intervals 
      setInterval(evt => {
        vertices[0] = vertices[0] === -0.5 ? -1 : -0.5; // primitive x1 toggle
        mesh.geometry.attributes.position.needsUpdate = true; // toggle update flag
      }, 500);
      setInterval(evt => {
        vertices[1] = vertices[1] === 0.5 ? 1 : 0.5; // primitive y1 toggle
        mesh.geometry.attributes.position.needsUpdate = true; // toggle update flag
      }, 250);
    }
  })
</script>
<a-scene>
  <a-plane position="0 1.6 -2" color="green" foo></a-plane>
</a-scene>

Here is an example of moving the vertex around

【讨论】:

  • 感谢您提供信息丰富的回答,它阐明了事情是如何执行的,但我想要做的是使用鼠标光标缩放一个矩形以覆盖一幅画,正如我上面所说的。我的意思是像一个一个地移动矩形/多边形的角以将它们调整到预期的位置。最终用户只需要调整他的矩形/多边形的角,以便将它们放在他想要的位置。最终结果将类似于此视频中添加形状的操作方式youtube.com/watch?v=Yl5Ipe6t4Ew
  • 好的,所以不完全是重新缩放,而更像是重新定位顶点。我有一个飞机的想法,我会把它添加到答案中
  • 是的,您可以说顶点重新定位,在我的情况下,这将为用户提供更大的灵活性。感谢您的帮助,我将等待您对飞机的想法:)。
  • @Hibra 我已经更新了我的答案,请查看新的 sn-p(尤其是 cmets)和链接示例
  • 非常感谢@Piotr,这是一项了不起的工作,我认为这将解决我的问题,我会将问题标记为已回答,再次感谢:)
猜你喜欢
  • 1970-01-01
  • 2015-08-10
  • 2017-08-08
  • 2010-10-26
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
相关资源
最近更新 更多