当前位置:网站首页>Solved the problem that when VRTK transmission under Unity HDRP, the screen fades in and out, and the visual occlusion cannot be displayed correctly when passing through the wall
Solved the problem that when VRTK transmission under Unity HDRP, the screen fades in and out, and the visual occlusion cannot be displayed correctly when passing through the wall
2022-08-08 07:07:00 【LCF_CSharp】
Unity HDRP下VRTK传送、穿墙 When the screen fades in and out、Visual occlusion does not display correctly problem solved
Unity HDRPThe rendering method and normalUnity有所不同,而SteamVRresponsible for the pluginVRFade in、Fade out script“SteamVR_Fade”is via a method that is executed after the camera has rendered a frameOnPostRender调用GL.QUADSTo perform drawing a slice,然后使用ShaderControls the color of this slice、Gradient, etc. to achieve the screen fade in、Fade out the function of obscuring the line of sight;
然而在HDRP中在OnPostRender下的GLDrawing does not display properly,要把“SteamVR_Fade”The script was originally placed in the normal way OnRenderObject 方法中调用GLThe part to be drawn is placed
UnityEngine.Rendering.RenderPipelineManager.endFrameRendering This method is called back.
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();
}
}
}
至此GameThe window has been able to achieve the screen fade in、Fade out and occlude,但VRThe screen inside the helmet remains unchanged;
我们还需要调用OpenVR中的Compositor.FadeToColor方法对VRThe screen inside the helmet is operated;
最终代码
//#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();
}
}
}
边栏推荐
猜你喜欢
Unity HDRP下VRTK传送、穿墙 时画面淡入淡出、视觉遮挡无法正确显示问题解决
NVIDIA CUDA 高度并行处理器编程(六):并行模式:卷积
神经网络和多元线性回归,神经网络多元线性回归
Dropout、剪枝、正则化有什么关系?什么是Dropout?
【图形学】 06 四元数(一)
类与对象之动静态方法,继承,名字的查找顺序,经典类和新式类,派生方法
win11+MX250+CUDA Tookit 10.1 update 2
ExecutionEngineException: String conversion error: Illegal byte sequence encounted in the input.
C语言实现冒泡排序及对冒泡排序的优化处理
tcpdump进行ARP抓包
随机推荐
线程P01——进程 并发 并行 线程的使用
Stack queue OJ question sharing and explanation
PURE(A Frustratingly Easy Approach for Entity and Relation Extraction)
Unity_条形图(柱状图)+ UI动画
神经网络的图像识别技术,神经网络的层数怎么看
Double week leetcode 84th game
C语言实现链表的增删查改以及OJ题讲解
Lettcode链表OJ题分享以及讲解
Unity_组件自动绑定
Prompt for Extraction? PAIE:Prompting Arguement Interaction for Event Argument Extraction
神经网络训练是什么意思,神经网络训练准确率
OSPF动态配置网络环境
Unity HDRP下VRTK传送、穿墙 时画面淡入淡出、视觉遮挡无法正确显示问题解决
神经网络对数据的要求,神经网络分析相关性
深度神经网络的训练过程,深度神经网络训练方法
神经网络预测值几乎一样,神经网络为什么能预测
Accelerate CNNs from Three Dimensions: A Comprehensive Pruning Framework详解
背包问题小结
状态机控制移位寄存器multisim仿真过程中出现的状态变量和状态转移条件不匹配的问题
什么是类与对象?