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();
}
}
}
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article