Unity VR-Compatible Mirror Effects

The following two methods are tested under environment: Unity 2019.2.14 + SteamVR SDK, Built-in RP.

Method A. Render Texture

This is a simple way to make mirros that also work in VR.

  1. Create render texture.
  2. Create camera “MirrorCamera”, set Fov=90. Set our render texture as MirrorCamera’s Target Texture.
  3. Create material to pass our render texture into, assign the render texture as its texture.
  4. Create 3D object in the scene, preferrably a plane or thin cube. Set material from step 3 as its material. This acts as the mirror model.

The 3D obejct should now replicate MirrorCamera’s camera view on its surface. Adjust MirrorCamera rotation to face the appropriate direction.

Method B. Vive Stereo Rendering Toolkit

Vive Stereo Rendering Toolkit provides VR mirror rendering methods. We need to download it from Asset Store and make a few quick changes to make it compatible with Unity 2019.2.x. Changes are made to the following files:

DeviceType.cs

// Lines 43 ~ 68
//Comment out lines 43 ~ 68;

        public static IDeviceParamFactory InitParamFactory(HmdType hmdType)
        {
// #if (VIVE_STEREO_STEAMVR)
//             if (hmdType == HmdType.SteamVR)
//             {
                return new SteamVRParamFactory();
//             }
// #endif

// #if (VIVE_STEREO_OVR)
//             if (hmdType == HmdType.OVR)
//             {
//                 return new OVRParamFactory();
//             }
// #endif

// #if (UNITY_ANDROID && VIVE_STEREO_WAVEVR)
//             if (hmdType == HmdType.WaveVR)
//             {
//                 return new WaveVRParamFactory();
//             }
// #endif

//             Debug.LogError("Cannot get suitable projection parameter for current HMD.");
//             return null;
        }

SteamVRParamFactory.cs


//========= Copyright 2016-2017, HTC Corporation. All rights reserved. ===========
using UnityEngine;
using Valve.VR; // Add using Valve.VR at the beginning.

// Comment out some lines as shown.

namespace HTC.UnityPlugin.StereoRendering
{
// #if (VIVE_STEREO_STEAMVR)
    public class SteamVRParamFactory : IDeviceParamFactory
    {
        public int GetRenderWidth()
        {
            return (int)SteamVR.instance.sceneWidth;
        }

        public int GetRenderHeight()
        {
            return (int)SteamVR.instance.sceneHeight;
        }

        public Vector3 GetEyeSeperation(int eye)
        {
            var eyePos = SteamVR.instance.eyes[eye].pos;
            eyePos.z = 0.0f;
            return eyePos;
        }

        public Quaternion GetEyeLocalRotation(int eye)
        {
            return SteamVR.instance.eyes[eye].rot;
        }

        public Matrix4x4 GetProjectionMatrix(int eye, float nearPlane, float farPlane)
        {
            return HMDMatrix4x4ToMatrix4x4(SteamVR.instance.hmd.GetProjectionMatrix((Valve.VR.EVREye)eye, nearPlane, farPlane));
        }

        // transform a SteamVR matrix format to Unity matrix format
        private Matrix4x4 HMDMatrix4x4ToMatrix4x4(Valve.VR.HmdMatrix44_t hmdMatrix)
        {
            Matrix4x4 m = Matrix4x4.identity;

            m[0, 0] = hmdMatrix.m0;
            m[0, 1] = hmdMatrix.m1;
            m[0, 2] = hmdMatrix.m2;
            m[0, 3] = hmdMatrix.m3;

            m[1, 0] = hmdMatrix.m4;
            m[1, 1] = hmdMatrix.m5;
            m[1, 2] = hmdMatrix.m6;
            m[1, 3] = hmdMatrix.m7;

            m[2, 0] = hmdMatrix.m8;
            m[2, 1] = hmdMatrix.m9;
            m[2, 2] = hmdMatrix.m10;
            m[2, 3] = hmdMatrix.m11;

            m[3, 0] = hmdMatrix.m12;
            m[3, 1] = hmdMatrix.m13;
            m[3, 2] = hmdMatrix.m14;
            m[3, 3] = hmdMatrix.m15;

            return m;
        }
    }
// #endif
}

With these quick changes in place, there should be no error messages anymore. Create a 3D object Quad to act as our mirror model. Add a Stereo Renderer component to it. Within this Stereo Renderer component, enable IsMirror, set Canvas Origin World Rot and Anchor World Rot = 270.


References:

三日月 ふゆの. (2019, December 2). Unity 2019.2.x で VR 空間内に鏡を置きたい. Retrieved April 1, 2020, from https://mikazuki.hatenablog.jp/entry/2019/12/02/005945

    Sesleria. (2019, July 13). Vive Stereo Rendering Toolkit(MirrorExample)をUnity2019 SteamVR Plugin2.3.2で動かすまで. Retrieved April 1, 2020, from https://qiita.com/Sesleria/items/350092968d6e9b893053

    Ytomi. (2019, October 13). 【Unity】モーションキャプチャーを使ったオリジナルアニメーション作成. Retrieved April 1, 2020, from https://styly.cc/ja/tips/motioncapture-animation_ytomi/

No Comments

Send Comment Edit Comment

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Source: Genshin Impact
Source: Genshin Impact
Source: Genshin Impact
Source: Genshin Impact
颜文字
Emoji
小恐龙
花!
蒙德
璃月
稻妻
须弥
Previous
Next