LoginSignup
2
2

More than 5 years have passed since last update.

A-Frame で Oculus Go のコントローラの向きを取得する(小ネタ)

Last updated at Posted at 2019-04-28

概要

A-FrameOculus Go 用のコンテンツを作っていて、コントローラの向きを取得したかったのですが、方法が分かりませんでした。Stack Overflow にて質問したら、A-Frame 開発者・メンテナのひとり Kevin さんからひとつのアイデアを教えていただきました。忘れないようにメモしておきます。

結論

例えば以下の方法でコントローラの向きを取得することができました。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>A-Frame で Oculus Go のコントローラの向きを取得する</title>
  <script src="https://aframe.io/releases/0.9.1/aframe.min.js"></script>
</head>
<body>
<a-scene>
  <!-- 任意に適当なオブジェクトを置いた -->
  <a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
  <a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
  <a-sky color="#ECECEC"></a-sky>
  <!-- Oculus Go のコントローラを使う laser-controls -->
  <a-entity id="ctl" laser-controls></a-entity>
  <!-- コントローラの向きを表示するための a-text -->
  <a-text id="txt" value="text" position="0 2 -2" align="center" color="#000000"></a-text>
</a-scene>

<script>

const ctl = document.getElementById("ctl");
const txt = document.getElementById("txt");
const timer = setInterval(() => {    // (任意)コントローラの向きを定期的に取得して表示
  // ここから、教えていただいた部分
  const point = new THREE.Vector3(0, 0, -1);
  ctl.object3D.localToWorld(point);
  const worldDirection = point.sub(ctl.object3D.position);
  // ここまで、教えていただいた部分
  txt.setAttribute("value", worldDirection.x.toFixed(2) + ", " + worldDirection.y.toFixed(2) + ", " + worldDirection.z.toFixed(2));
}, 100);

</script>
</body>
</html>

以下、実行画面です。

IMG_0166.PNG

以上、小ネタでした。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2