【unity的VR開發(fā)】GoogleVR SDK如何添加Image Effects特效

環(huán)境:

Unity 5.3.4f1 (64-bit)

GoogleVR SDK(version 2.0)

小米5 Android6.0

前景提要

今天想要在Unitycardboard VR項目中添加Bloom特效。
于是直接將這個特效像其他項目一樣掛在了攝像機下,在未開啟VR模式的情況下在PC上調試,并對Bloom屬性數值進行了調整。發(fā)布到移動平臺后發(fā)現效果消失了,仔細檢查后才想起,cardboard VR 實現是在場景里創(chuàng)建兩個攝像機分別對應左眼右眼,掛在主攝像機下的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后,分別代表左右眼的兩個子攝像機就會出現在場景中?,F在就可以輕松方便地為他們添加特效或者腳本了。***

需要注意的一個問題是:
如果新添加的腳本順序在StereoRenderEffect腳本的順序下的話,攝像頭畫面會扭曲

作為一個Unity入門學習者,從中學到了很多教訓。
在使用源碼時,仔細閱讀備注是很重要的!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容