Изменить родителя компонента и сохранить позицию - PullRequest
0 голосов
/ 13 сентября 2018

Я использую 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>

Когда нажата # кнопка сферы , сфера должна отсоединиться от и прикрепить к камере. Во время перемещения в 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-camera и a-marker ?

1 Ответ

0 голосов
/ 14 сентября 2018

Для воспитания, я бы сейчас делал это на уровне three.js и не использовал бы DOM.Отсоединение и присоединение к атому DOM перезапустит все и станет беспорядком.

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);
...