環境:
Unity 5.3.4f1 (64-bit)
GoogleVR SDK(version 2.0)
小米5 Android6.0
前景提要
今天想要在Unity的cardboard 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 theCamera
component.
*** 只要爲攝像機添加UpdateStereoCameras後,分別代表左右眼的兩個子攝像機就會出現在場景中。現在就可以輕鬆方便地爲他們添加特效或者腳本了。***
需要注意的一個問題是:
如果新添加的腳本順序在StereoRenderEffect腳本的順序下的話,攝像頭畫面會扭曲。
作爲一個Unity入門學習者,從中學到了很多教訓。
在使用源碼時,仔細閱讀備註是很重要的!