【unity的VR开发】GoogleVR SDK如何添加Image Effects特效

环境:

Unity 5.3.4f1 (64-bit)

GoogleVR SDK(version 2.0)

小米5 Android6.0

前景提要

今天想要在Unitycardboard VR项目中添加Bloom特效。
于是直接将这个特效像其他项目一样挂在了摄像机下,在未开启VR模式的情况下在PC上调试,并对Bloom属性数值进行了调整。发布到移动平台后发现效果消失了,仔细检查后才想起,cardboard VR 实现是在场景里创建两个摄像机分别对应左眼右眼,挂在主摄像机下的Bloom没有起作用。

通过谷歌Google百度Baidu尝试搜索解决方案,得知可以通过将StereoController下的directRender设为false解决问题,但是这个脚本只会在运行时自动挂载到摄像机下,再次前往脚本StereoController.cs直接更改。

C#
 public bool directRender = false;

但是这个方法并没有解决问题。

于是开始自己思考解决方案。

第一个想法是通过脚本Find新添加的两个摄像机,为他们添加脚本。
然后又想到直接在GoogleVR的SDK中修改GvrViewer.cs,直接挂载脚本。

C#
void AddPrePostRenderStages() {
    var preRender = UnityEngine.Object.FindObjectOfType<GvrPreRender>();
    if (preRender == null) {
      var go = new GameObject("PreRender", typeof(GvrPreRender));
      go.SendMessage("Reset");
      go.transform.parent = transform;
    }
    var postRender = UnityEngine.Object.FindObjectOfType<GvrPostRender>();
    if (postRender == null) {
            var go = new GameObject("PostRender", typeof(GvrPostRender));
            go.SendMessage("Reset");
            go.transform.parent = transform;
            //go.AddComponent<Antialiasing>();

            /*
            //modify camera
            go.GetComponent<Camera>().hdr = true;

            //add script Bloom
            go.AddComponent<Bloom>();
            Bloom bloom = go.GetComponent<Bloom>();
            bloom.tweakMode = Bloom.TweakMode.Complex;
            bloom.bloomIntensity = 1f;
            bloom.bloomThreshold = 0.16f;
            bloom.bloomThresholdColor = new Color(244f/255f, 203f/255f, 143f/255f);
            bloom.bloomBlurIterations = 3;
            bloom.sepBlurSpread = 5f;

            //add Tonemapping
            go.AddComponent<Tonemapping>();
            Tonemapping tonemapping = go.GetComponent<Tonemapping>();
            tonemapping.type = Tonemapping.TonemapperType.AdaptiveReinhardAutoWhite;
            tonemapping.middleGrey = 0.1f;
            tonemapping.adaptionSpeed = 100f;
            tonemapping.adaptiveTextureSize = Tonemapping.AdaptiveTexSize.Square512;
            

            //add ScreenSpaceAmbientOcclusion
            go.AddComponent<ScreenSpaceAmbientObscurance>();
            ScreenSpaceAmbientObscurance screenSpaceAmbientObscurance = go.GetComponent<ScreenSpaceAmbientObscurance>();
            screenSpaceAmbientObscurance.intensity = 0.5f;
            screenSpaceAmbientObscurance.radius = 0.2f;
            screenSpaceAmbientObscurance.blurIterations = 1;
            screenSpaceAmbientObscurance.blurFilterDistance = 1.25f;
            screenSpaceAmbientObscurance.downsample = 0;

            //add Antialiasing
            go.AddComponent<Antialiasing>();
            Antialiasing antialiasing = go.GetComponent<Antialiasing>();

            ////DepthOfFieldDeprecated
            //go.AddComponent<DepthOfFieldDeprecated>();
            //DepthOfFieldDeprecated depthOfFieldDeprecated = go.GetComponent<DepthOfFieldDeprecated>();
            */
    }
  }

用这个方法依然遇到了问题。出现的问题我就不赘述了。

多次遇阻决定仔细查看备注。

终于找到了简洁的解决方案。

解决方案

StereoController.cs
/// The Inspector panel for this script includes a button Update Stereo Cameras.
/// This performs the same action as described above for startup, but in the Editor.
/// Use this to generate the rig if you intend to customize it. This action is also
/// available via Component -> GVR -> Update Stereo Cameras in the Editor’s
/// main menu, and in the context menu for the Camera component.

*** 只要为摄像机添加UpdateStereoCameras后,分别代表左右眼的两个子摄像机就会出现在场景中。现在就可以轻松方便地为他们添加特效或者脚本了。***

需要注意的一个问题是:
如果新添加的脚本顺序在StereoRenderEffect脚本的顺序下的话,摄像头画面会扭曲

作为一个Unity入门学习者,从中学到了很多教训。
在使用源码时,仔细阅读备注是很重要的!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章