1

我正在使用AR.js,并且在标记组件中放置了一个球体。

<body style="margin : 0px; overflow: hidden;">
    <a-scene vr-mode-ui="enabled: false" embedded="" arjs="sourceType: webcam; debugUIEnabled: false;">
        <a-marker markerhandler id="marker" emitevents="true" cursor="rayOrigin: mouse" preset="hiro">
            <a-sphere id="sphere" color="green" radius="0.3" position="0 1 0" ></a-sphere>
        </a-marker>
        <!-- use this <a-entity camera> to support multiple-markers, otherwise use <a-marker-camera> instead of </a-marker>-->
        <a-entity camera="" id="camera">
                <a-entity geometry="primitive: plane; height: 0.1; width: 0.1" position="0.4 -0.2 -1"
                material="color: gray; opacity: 0.5"></a-entity>
                <a-entity id="sphere-button" geometry="primitive: plane; height: 0.1; width: 0.1" position="-0.4 -0.2 -1"
                material="color: green; opacity: 0.5"></a-entity>
        </a-entity>
    </a-scene>  
  </body>

单击#sphere-button时,球体应与相机分离并附加到相机。在 DOM 中重定位期间,位置应该保持不变,但事实并非如此。我试过这个:

let v = new THREE.Vector3();
v.copy(sphere.object3D.position);
sphere.object3D.localToWorld(v);
camera.object3D.worldToLocal(v);
sphere.parentNode.removeChild(entity);
camera.appendChild(sphere);
entity.setAttribute('position', v);

我如何正确转换两个父母a-cameraa-marker之间的位置?

4

2 回答 2

3

对于reparenting,我现在会在three.js 级别进行,而不是使用DOM。在 DOM atm 上分离和附加将重新初始化所有内容,并且会一团糟。

let v = new THREE.Vector3();
v.copy(sphere.object3D.position);
sphere.object3D.localToWorld(v);
camera.object3D.worldToLocal(v);
camera.object3D.add(sphere.object3D);
sphere.object3D.position.copy(v);
于 2018-09-13T21:28:01.930 回答
0

我尝试了一种不同的方法,使用object3D.attach(如@prisoner849 在评论中建议的那样)获取与预期父级相关的三个对象的位置、旋转等,然后使用该位置、旋转克隆实体。 .. 在预期的父级下(最后,删除原始实体)。

您可以在“AFrame:重新设置元素保持其世界位置,旋转”的答案中查看实现此方法的组件。

于 2021-01-03T20:44:43.297 回答