我正在为我的metaverse应用程序使用AFRAME.我正在try 为我的头像获取第三方透视图(TPP).我在我的模型中使用了带摄像头的旋转组件,以便在旋转come时化身会旋转.我把相机放在我的头像后面一点,以产生TPP效果.为摄像头和头像提供wasd控制.

我能够完美地同步运动,但问题是相机的旋转.相机围绕原点旋转,即以位置="0 0 0"为中心,而不是以化身位置为中心.

这是我正在使用的寄存器组件.

AFRAME.registerComponent("rotate-with-camera", {
tick: (function () {
// create once
const tmpq = new THREE.Quaternion();
const tmpe = new THREE.Euler();

return function (t, dt) {
  if (!this.el.sceneEl.camera) return; // ignore when there is no camera
  const cam = this.el.sceneEl.camera.el; // get the camera entity
  cam.object3D.getWorldQuaternion(tmpq); // get the world rotation
  tmpe.setFromQuaternion(tmpq, "YXZ");
  // set attribute is necesarry for wasd-controls
  this.el.setAttribute("rotation", {
    x: 0,
    y: (tmpe.y * 180) / Math.PI,
    z: 0,
  });
};
})(),
});

这是我在代码中使用的come和avatar.

<!--Camera -->
  <a-entity look-at=".avatar"  look-controls="pointerLockEnabled: false" wasd-controls="acceleration:12;">
    <a-entity camera="near:0.01;" position="4 1.72 177.5"></a-entity>
  </a-entity>

  <!-- Avatar -->
  <a-entity class="avatar" >
    <a-gltf-model
    id="player"
    src="#boyBlue"
    position="4 0.12 175"
    animation-mixer="clip:Idle"
    wasd-controls="acceleration: 12"
    rotate-with-camera
    >
    </a-gltf-model>
  </a-entity>

我希望我的相机以我的头像位置为轴旋转.我如何继续?

推荐答案

这是因为相机相对于父实体具有x偏移,因此:

enter image description here


一个简单的解决方案是将偏移移动到父实体:

<a-entity look-controls wasd-controls="acceleration:12;" position="4 0 0">
<a-entity camera="near:0.01;" position="0 1.72 177.5"></a-entity>
</a-entity>

glitch here


但最好按照找到组件的answer,使用javascript控制器将摄像头移动到盒子后面,而不是同步位置,两者都有wasd controls.

控制器很简单:

AFRAME.registerComponent("follow-target", {
  schema: {
    target: {type: "selector"}
  },
  tick: (function() {
    // create once
    const tmpv = new THREE.Vector3();

    return function(t, dt) {
      if (!this.data.target) return; // ignore when there is no target
      const target = this.data.target.object3D; // get the mesh
      // track the position
      const position = target.getWorldPosition(tmpv); // get the world position
      this.el.object3D.position.lerp(tmpv, 0.5) // linear interpolation towards the world position
    }
  })()
})

glitch here

Javascript相关问答推荐

强制执行useStatego struct 化变量[foo,setFoo]的命名约定?

容器如何更改默认插槽中子项的显示?

禁用从vue.js 2中的循环创建的表的最后td的按钮

react 路由加载程序行为

使用续集和下拉栏显示模型属性

微软Edge编辑和重新发送未显示""

切换时排序对象数组,切换不起作用

为什么按钮会随浮动属性一起移动?

使用JQuery单击元素从新弹出窗口获取值

JSDoc创建并从另一个文件导入类型

在forEach循环中获取目标而不是父对象的属性

如何使用JavaScript拆分带空格的单词

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

VSCode中出现随机行

为什么在函数中添加粒子的速率大于删除粒子的速率?

如何为仅有数据可用的点显示X轴标签?

在ChartJS中使用spanGaps时,是否获取空值的坐标?

如何使用Cypress在IFRAME中打字

正则表达式以确定给定文本是否不只包含邮箱字符串

如何检测当前是否没有按下键盘上的键?