我認爲我不太理解Unity渲染引擎。爲什麼我需要depthBuffer才能使用RenderTexture?
我使用渲染紋理生成的截圖(我需要在以後對其進行管理):
screenshotRenderTexture = new RenderTexture(screenshot.width, screenshot.height, depthBufferBits, RenderTextureFormat.Default);
screenshotRenderTexture.Create();
RenderTexture currentRenderTexture = RenderTexture.active;
RenderTexture.active = screenshotRenderTexture;
Camera[] cams = Camera.allCameras;
System.Array.Sort(
cams,
delegate(Camera cam1, Camera cam2)
{
// It's easier than write float to int conversion that won't floor
// depth deltas under 1 to zero and will correctly work with NaNs
if (cam1.depth < cam2.depth)
return -1;
else if (cam1.depth > cam2.depth)
return 1;
else return 0;
}
);
foreach(Camera cam in cams)
{
cam.targetTexture = screenshotRenderTexture;
cam.Render();
cam.targetTexture = null;
}
screenshot.ReadPixels(new Rect(0, 0, textureWidth, textureHeight), 0, 0);
screenshot.Apply();
RenderTexture.active = currentRenderTexture;
但是,如果depthBufferBits爲0,渲染與所有類型的Z緩存的bug(中呈現的東西結果錯誤的順序)。
我明白了什麼是一般意義上的深度緩衝。但是,我不明白的是 - 如果使用RenderTexture來合併單個相機的渲染結果,爲什麼需要深度緩衝區?這個抽象如何工作,確切地說 - 相機是否自己創建圖像,然後將其提供給RenderTexture,或者相機是否使用RenderTexture的深度緩衝區?它似乎是後者,因爲我遇到的錯誤(錯誤順序的東西都是用同一個相機渲染的,所以問題是在一個相機內部排序,而不是在不同相機之間排序),但是在相同的情況下這與C#層次上這些抽象結構的常識是矛盾的。
而且,最後 - 我可以以某種方式使用用於正常渲染就這一個默認的深度緩衝?因爲移動設備上的每像素16位非常痛苦。
更新:
這就是我試圖做的事:
screenshotRenderTexture = new RenderTexture(
screenshot.width,
screenshot.height,
0,
RenderTextureFormat.Default
);
screenshotRenderTexture.Create();
RenderBuffer currentColorBuffer = Graphics.activeColorBuffer;
Graphics.SetRenderTarget(screenshotRenderTexture.colorBuffer, Graphics.activeDepthBuffer);
yield return new WaitForEndOfFrame();
Graphics.SetRenderTarget(currentColorBuffer, Graphics.activeDepthBuffer);
這裏就是我的了:
SetRenderTarget can only mix color & depth buffers from RenderTextures. You're trying to set depth buffer from the screen.
UnityEngine.Graphics:SetRenderTarget(RenderBuffer, RenderBuffer)
<ScreenshotTaking>c__Iterator21:MoveNext() (at Assets/Scripts/Managers/ScreenshotManager.cs:126)
爲什麼不能從屏幕混合深度緩衝和來自RenderTexture的顏色緩衝區?