Unity HDRP下VRTK传送、穿墙 时画面淡入淡出、视觉遮挡没办法正确显示问题解决
Unity HDRP下VRTK传送、穿墙 时画面淡入淡出、视觉遮挡无法正确显示问题解决
Unity HDRP的渲染方式和普通Unity有所不同,而SteamVR插件中负责VR画面淡入、淡出的脚本“SteamVR_Fade”是通过在相机渲染一帧后执行的方法OnPostRender调用GL.QUADS去执行绘制一个片,然后使用Shader控制这个片的颜色、渐变等来实现画面淡入、淡出遮当视线的功能;
然而在HDRP中在OnPostRender下的GL绘制不会正常显示,要把“SteamVR_Fade”脚本原来普通方式放在 OnRenderObject 方法中调用GL来绘制的部分放到
UnityEngine.Rendering.RenderPipelineManager.endFrameRendering 这个方法回调中。
protected void OnCameraRender(ScriptableRenderContext context, Camera[] cameraList)
{
foreach (var camera in cameraList)
{
DrawLines();
}
}
void DrawLines()
{
if (currentColor != targetColor)
{
if (Mathf.Abs(currentColor.a - targetColor.a) < Mathf.Abs(deltaColor.a) * Time.deltaTime)
{
currentColor = targetColor;
deltaColor = new Color(0, 0, 0, 0);
}
else
{
currentColor = deltaColor * Time.deltaTime;
}
if (fadeOverlay)
{
var overlay = SteamVR_Overlay.instance;
if (overlay != null)
{
overlay.alpha = 1.0f - currentColor.a;
}
}
}
if (currentColor.a > 0 && fadeMaterial)
{
fadeMaterial.SetColor(fadeMaterialColorID, currentColor);
fadeMaterial.SetPass(0);
GL.Begin(GL.QUADS);
GL.Vertex3(-1, -1, 0);
GL.Vertex3( 1, -1, 0);
GL.Vertex3(1, 1, 0);
GL.Vertex3(-1, 1, 0);
GL.End();
}
}
}
至此Game视窗中已经可以实现画面淡入、淡出和遮挡了,但VR头盔内画面依然没有变化;
我们还需要调用OpenVR中的Compositor.FadeToColor方法对VR头盔内的画面进行操作;
最终代码
//#define TEST_FADE_VIEW
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: CameraFade script adapted to work with SteamVR.
//
// Usage: Add to your top level SteamVR_Camera (the one with ApplyDistoration
// checked) and drag a reference to this component into SteamVR_Camera
// RenderComponents list. Then call the static helper function
// SteamVR_Fade.Start with the desired color and duration.
// Use a duration of zero to set the start color.
//
// Example: Fade down from black over one second.
// SteamVR_Fade.Start(Color.black, 0);
// SteamVR_Fade.Start(Color.clear, 1);
//
// Note: This component is provided to fade out a single camera layer's
// scene view. If instead you want to fade the entire view, use:
// SteamVR_Fade.View(Color.black, 1);
// (Does not affect the game view, however.)
//
//=============================================================================
using UnityEngine;
using UnityEngine.Rendering;
using Valve.VR;
public class SteamVR_Fade : MonoBehaviour
{
private Color currentColor = new Color(0, 0, 0, 0); // default starting color: black and fully transparent
private Color targetColor = new Color(0, 0, 0, 0); // default target color: black and fully transparent
private Color deltaColor = new Color(0, 0, 0, 0); // the delta-color is basically the "speed / second" at which the current color should change
private bool fadeOverlay = false;
static public void Start(Color newColor, float duration, bool fadeOverlay = false)
{
SteamVR_Events.Fade.Send(newColor, duration, fadeOverlay);
}
static public void View(Color newColor, float duration)
{
var compositor = OpenVR.Compositor;
if (compositor != null)
compositor.FadeToColor(duration, newColor.r, newColor.g, newColor.b, newColor.a, false);
}
#if TEST_FADE_VIEW
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
SteamVR_Fade.View(Color.black, 0);
SteamVR_Fade.View(Color.clear, 1);
}
}
#endif
public void OnStartFade(Color newColor, float duration, bool fadeOverlay)
{
if (duration > 0.0f)
{
targetColor = newColor;
deltaColor = (targetColor - currentColor) / duration;
}
else
{
currentColor = newColor;
}
}
static Material fadeMaterial = null;
static int fadeMaterialColorID = -1;
void OnEnable()
{
RenderPipelineManager.endFrameRendering = OnCameraRender;
if (fadeMaterial == null)
{
fadeMaterial = new Material(Shader.Find("Custom/SteamVR_Fade"));
fadeMaterialColorID = Shader.PropertyToID("fadeColor");
}
SteamVR_Events.Fade.Listen(OnStartFade);
SteamVR_Events.FadeReady.Send();
}
void OnDisable()
{
RenderPipelineManager.endFrameRendering -= OnCameraRender;
SteamVR_Events.Fade.Remove(OnStartFade);
}
protected void OnCameraRender(ScriptableRenderContext context, Camera[] cameraList)
{
foreach (var camera in cameraList)
{
DrawLines();
}
}
void DrawLines()
{
if (currentColor != targetColor)
{
// if the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
if (Mathf.Abs(currentColor.a - targetColor.a) < Mathf.Abs(deltaColor.a) * Time.deltaTime)
{
currentColor = targetColor;
deltaColor = new Color(0, 0, 0, 0);
}
else
{
currentColor = deltaColor * Time.deltaTime;
}
if (fadeOverlay)
{
var overlay = SteamVR_Overlay.instance;
if (overlay != null)
{
overlay.alpha = 1.0f - currentColor.a;
}
}
}
if (currentColor.a > 0 && fadeMaterial)
{
SteamVR_Fade.View(currentColor, 0);
fadeMaterial.SetColor(fadeMaterialColorID, currentColor);
fadeMaterial.SetPass(0);
GL.Begin(GL.QUADS);
GL.Vertex3(-1, -1, 0);
GL.Vertex3( 1, -1, 0);
GL.Vertex3(1, 1, 0);
GL.Vertex3(-1, 1, 0);
GL.End();
}
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhficcfb
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01