The following two methods are tested under environment: Unity 2019.2.14 + SteamVR SDK, Built-in RP.
方案 A. Render Texture
简单粗暴传统艺能。只要在方块中间放个朝前的相机,让方块显示相机拍到的画面,就能达到镜子的效果了。
- 新建 Render Texture 贴图,命名为「Mirror_tex」。
- 场景内新建相机,命名为「MirrorCamera」,视野90°。把「Mirror_tex」贴图绑定到相机的 “Target Texture” 栏,这样就能让相机把画面传递给「Mirror_tex」贴图。
- 新建材质,命名为「Mirror_mat」,shader 设“ Unlit→Texture ”,把「Mirror_tex」贴图绑定到这个材质的贴图栏。
- 在场景内新建一个3D物体→ Quad 作为镜子模型,把材质设为「Mirror_mat」,然后把 Transform→X Scale 设为“-1”,调整相机位置。
这样就能在模型上显示「MirrorCamera」捕捉到的画面了。
方案 B. Vive Stereo Rendering Toolket
使用 Vive Stereo Rendering Toolket 提供的镜面渲染。从 Asset Store 下载导入 Vive Stereo Rendering Toolket,修改两处代码,使其兼容 Unity 2019.2.x。
DeviceType.cs
// 行数 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; // 在开头添加 using Valve.VR.
// 注释掉以下部分
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
}
新建3D物体→ Quad 作为镜子模型,添加 Stereo Renderer 组件。启用 Is Mirror,再把 Canvas Origin World Rot 和 Anchor World Rot 设为270就行了。
参考:
三日月 ふゆの. (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/