Calculate the position of an element inside a photosphere

前端 未结 2 1722
野趣味
野趣味 2021-01-25 20:01

Im using an a-sky element to display a 360° photosphere.

My problem is that I have to place inside the photosphere some elements that must act like \'markers\'; but I do

相关标签:
2条回答
  • 2021-01-25 20:43

    If you want to calculate position on the a-sky sphere surface, I have a method. make a ray from camera's position, and this ray will hit on sphere. this hit point is the position that we want to calculate. the code is like:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/aframe/0.3.2/aframe.min.js"></script>
    <script src="//cdn.rawgit.com/donmccurdy/aframe-extras/v2.5.2/dist/aframe-extras.js"></script>
    <!DOCTYPE html>  
    <html>  
    <head>
    <title>sphere position</title>
    <script>
      document.addEventListener("click",function(){
        var camera = document.querySelector("a-camera");
        var cursor = document.querySelector("a-cursor");
        var camera_pos = new THREE.Vector3().copy(camera.object3D.getWorldPosition()); // get camera's world position
        var cursor_pos = new THREE.Vector3().copy(cursor.object3D.getWorldPosition()); // get cursor's world position
        var direction = new THREE.Vector3().subVectors(cursor_pos,camera_pos); //calculate direction from camera to cursor
        var raycaster = new THREE.Raycaster(camera_pos,direction.normalize()); // make raycaster 
        var sky = document.querySelector("a-sky");
        var intersects = raycaster.intersectObject(sky.object3D.children[0]); //let raycaster intersect the 'a-sky' sphere
        console.log(intersects[0].point); 
    
      });
      </script>
    </head>
    <body>
    <a-scene>
      <a-camera universal-controls position="0 1.6 0">
        <a-cursor></a-cursor>
      </a-camera>
      <a-sky material="side:double"></a-sky>  
    </a-scene>
    </body>
    </html>

    0 讨论(0)
  • 2021-01-25 20:47

    Sorry for answering my own question but this time was really easy:

    function sphericalCoordsToVector3(distance, latitude, longitude){
      return new THREE.Vector3(
        distance * -Math.cos(latitude) * Math.cos(longitude),
        distance * Math.sin(latitude),
        distance * -Math.cos(latitude) * Math.sin(longitude)
      );
    }
    

    Where distance is the distance from the centre (camera) where we want to put our element.

    Just for the records, the opposite function is:

    function vector3ToSphericalCoords(vector) {
      var phi = Math.acos(vector.y / Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z));
      var theta = Math.atan2(vector.x, vector.z);
      return {
        longitude: theta < 0 ? -theta : (Math.PI * 2.0) - theta,
        latitude: (Math.PI / 2.0) - phi
      };
    };
    

    The original functions can be found here: mistic100/Photo-Sphere-Viewer (there are also nice functions to get the position of the element inside the screen)

    0 讨论(0)
提交回复
热议问题