Programming in VRChat

VRChat でのプログラミングについて調べたことの書き溜め

Locomotion system を自作するためのガイド(草稿)

locomotion system を作成する基礎である player base tracking の解説が書かれました。 http://vrchat.wikidot.com/worlds:guides:player-tracking (まだブラッシュアップ中) いま書いてある範囲は読み解けたので、 自分の備忘録を兼ねて周辺の情報と関連付けて簡単な説明を書きました。

この文書だけでは作れないので、 これを参考に上記ページを読んでいただければと思います。


  • locomotion system は、元から用意されているプレイヤーの移動操作を上書きするもの

    • そのように界隈の人が勝手に呼んでいる。
    • climing mechanics by CyanLaser (壁上り機構)はその実現の一つ
  • 任意の locomotion を実現するには 基本的には teleport player すればよいだけ

    • 頻繁に(≒毎フレーム) teleport player する事で実現できる
    • teleport position は移動後のプレイヤーの足元の位置を指定する。
      • アバターの足ではなくてアバターオブジェクトが置かれている場所
      • この場所を player base と呼んでいる
  • 問題は teleport position の算出

    • locomotion メカニズムによる移動前の、前フレームでの player base を正確に知る必要がある
    • これを実現するのが player base tracking という仕組み
      • この tracking は「プレイヤーなどオブジェクトの移動を把握しつづけること」という意味
      • base tracking と略記されることもあり
    • player base が分かったら、それに今のフレームで移動させたい量を加算した場所に TeleportPlayer すればよい
  • player base tracking の基礎部品として player camera tracking を使う

    • (なので前提としてそれを知っている必要がある)
    • これはプレイヤーの視界を提供するカメラを tracking するもの
  • camera tracking の基礎は "Screen Space - Camera" 設定の canvas

    • 位置を正確に取るための調整が必要
      • Toybox あるいは CyanLaser のチュートリアルビデオを参照
      • この部分は wiki ページが加筆予定で整備されていない
  • "VRChat Vector Math"

  • player base 算出原理

    1. 現在の(=前フレームの) player camera の位置を保存する
    2. 一時的に player を既知の位置に移動させる
    3. その時の player camera の位置を調べる
    4. その camera 位置と一時的な player base 位置(これは前述の既知の位置)の両方から、その差分ベクトルを求められる
    5. 保存しておいた camera 位置に、この差分を加算すると、前フレームでの player base を算出できる
    6. 算出した場所に player を移動させて元に戻す
  • locomotion mechanics への発展

    • 上記手順でプレイヤーを元に戻す際に、別途作り出したベクトルを加えた場所に移動させることで、任意の locomotion が作れる。
  • 作成作業にあたって必要なもの

    • Animation, Animator component, Animation Event の知識
    • uGUI, UI Event の知識 (Button だけで OK)
    • Standard Assets
    • Toybox v2 by HardLight
      • PlayerTracking プレハブで使用している範囲
        • camera tracking の scale 調整部分
        • それに含まれる毎フレーム動かすイベント機構
      • もちろん同等のものを自作してもよい
    • EasyEventsEditor by Merlin
      • UI Event の編集補助エディタ拡張
      • private method の指定が可能
      • 同一オブジェクトの同種の複数コンポーネントの使い分けが可能
      • 無くてもデバッグモードなインスペクタでもできるのだが不毛

VRChat Vector Mathematics

Ways to calculate vector with GameObject transform and VRC_SceneResetPosition.

Object transform and VRC_SceneResetPosition are vector math library in VRChat!

Notation

  • For any vectors : A, B
  • For local space origin : O
  • For rotation matrix : r
  • For scalar : k
  • For indexing local space :
    • _ps: in problem space
    • _ss: in solution space

Object hierarchy :

P        // local root
 - Q     // child object
    - R  // grand child object

Move vector

Move a vector to another local space. A_ps => A_ss

implementation:

  1. Move P to O_ps
  2. Move Q to A_ps
  3. Move P to O_ss
  4. Then, Q is at A_ss

Add vectors

Add two vectors in problem space and get result in solution space. ( A_ps, B_ps ) => (A + B)_ss

f:id:naqtn:20190702170229p:plain

implementation:

  1. Move P and Q to O_ps
  2. Move R to A_ps
  3. Move Q to B_ps
  4. Move P to O_ss (skippable if O_ps = O_ss)
  5. Then, R is at (A + B)_ss

Subtract vectors

Subtract two vectors in problem space and get result in solution space. ( A_ps, B_ps ) => (A - B)_ss

f:id:naqtn:20190702170309p:plain

implementation:

  1. Move P to O_ps
  2. Move Q to B_ps
  3. Move R to A_ps (R's local transform is subtract result)
  4. Move Q to O_ss
  5. Then, R is at (A - B)_ss

Rotate and scalar multiplication

Rotate a vectors in problem space and get result in solution space. Scalar multiplication solution is identical. So both could be done at once.

  • ( r_ps, A_ps ) => (r * A)_ss
  • ( k, A_ps ) => (k * A)_ss
  • ( k, r_ps, A_ps ) => (k * r * A)_ss

implementation:

  1. Move P and Q to O_ps
  2. Move R to A_ps
  3. Move Q to r_ps
  4. Move P to O_ss
  5. Then, R is at (r * A)_ss

Acknowledgments

I lean this technique from http://vrchat.wikidot.com/worlds:guides:player-tracking by CyanLaser. Thank you for sharing your wonderful work.