Aframe 如何检测摄像机和球体';A帧中的s距离

Aframe 如何检测摄像机和球体';A帧中的s距离,aframe,webvr,Aframe,Webvr,我试图在相机靠近球体时显示一些文本。这个想法是当用户看到球体移动得更近时,文本就会显示出来,比如说,“你好”。但现在我只知道如何使用a实体添加具有固定位置的文本,我不知道如何检测相机和球体之间的距离,并在用户可以看到球体靠近时显示文本。 下面是我的代码: <html> <head> <script src="https://aframe.io/releases/0.7.0/aframe.min.js"></script> </hea

我试图在相机靠近球体时显示一些文本。这个想法是当用户看到球体移动得更近时,文本就会显示出来,比如说,“你好”。但现在我只知道如何使用a实体添加具有固定位置的文本,我不知道如何检测相机和球体之间的距离,并在用户可以看到球体靠近时显示文本。 下面是我的代码:

<html>
<head>
    <script src="https://aframe.io/releases/0.7.0/aframe.min.js"></script> 
</head>
<body>
    <a-scene>
        <a-sphere position="0 0 0" perspective="true" radius="1.5" color="#aaa" id="sphere1"></a-sphere>
        <a-entity position="4.5 2 0" text="width: 10; color: white; value: Hello"></a-entity>
        <a-sky color="#000"></a-sky>
        <a-entity class="camwrap" position="0 0 0">
            <a-camera look-controls wasd-controls="fly:true acceleration:1" near="1" position="0 0 20" user-height="0" fov="60">
            </a-camera>
        </a-entity>
    </a-scene>
    <script>
        const cam = document.querySelector("a-camera");
        setTimeout(function() {
            cam.components["wasd-controls"].keys["KeyW"] = true;
        }, 1000);
    </script>
</body>

const-cam=document.querySelector(“a-camera”);
setTimeout(函数(){
cam.components[“wasd控制”]。键[“KeyW”]=true;
}, 1000);


有什么想法吗?

如果你知道摄像机的位置和球体的位置,你可以计算距离:
1)
sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)

2) 或者使用三个.js
a.distanceTo(b)

将计算结果放入自定义组件中,并在
勾选功能中检查距离:

init: function() {
  this.cam = document.querySelector("[camera]")
  this.sphere = document.querySelector("a-sphere")
},
tick: function() {
  let camPos = this.cam.object3D.position
  let spherePos = this.sphere.object3D.position
  let distance = camPos.distanceTo(spherePos)
  if (distance < 5) {
      // camera closer than 5m, do something
  }
}
init:function(){
this.cam=document.querySelector(“[camera]”)
this.sphere=document.querySelector(“a-sphere”)
},
勾选:函数(){
让camPos=this.cam.object3D.position
设spherePos=this.sphere.object3D.position
let distance=camPos.distanceTo(spherePos)
如果(距离<5){
//摄像机距离小于5米,做点什么
}
}

在小提琴中查看。

您可以交替在主球体周围制作一个不可见球体, 并使用碰撞检测使文本显示。
碰撞检测内置于aframe.extras.js中

很高兴我能提供帮助