- Home /
Rendering a G-Buffer for a cubemap
Hi,
I'd like to render a small G-Buffer (Depth+Normals, and eventually Color) for a cubemap in EditorMode but the resulting RenderTextures look like garbage. Performance is not an issue so I'd like to stick to the built-in shaders as much as possible, i.e. I don't really mind the 2 render passes below but if someone could make a 1 pass version that'd be great too. :-)
So without further ado, the code snippet that I use to generate those buffers:
 using System;
 using UnityEngine;
 
 namespace MyNamespace
 {
     class CubemapFaceGBuffer
     {
         public RenderTexture NormalsLinearDepth;
         public RenderTexture Color;
         public CubemapFaceGBuffer(int Size)
         {
             NormalsLinearDepth = new RenderTexture(Size, Size, 16, RenderTextureFormat.ARGB32);
             NormalsLinearDepth.isPowerOfTwo = true;
             NormalsLinearDepth.wrapMode = TextureWrapMode.Clamp;
             NormalsLinearDepth.generateMips = false;
 
             Color = new RenderTexture(Size, Size, 16, RenderTextureFormat.ARGB32);
             Color.isPowerOfTwo = true;
             Color.wrapMode = TextureWrapMode.Clamp;
             Color.generateMips = false;
         }
     }
 
     class CubemapGBuffer
     {
         public CubemapGBuffer(int FaceSize, float FarClip)
         {
             // Create the camera game object and set it up for the cubemap
             _CameraGameObj = new GameObject();
             //_CameraGameObj.hideFlags = HideFlags.HideAndDontSave;
 
             _Camera = _CameraGameObj.AddComponent<Camera>();
             _Camera.enabled = false;
             _Camera.backgroundColor = Color.black;
             _Camera.clearFlags = CameraClearFlags.SolidColor;
             _Camera.nearClipPlane = 0.01f;
             _Camera.farClipPlane = FarClip;
             _Camera.fieldOfView = 90.0f;
 
             // Create the 6 faces
             Faces = new CubemapFaceGBuffer[6];
             for (int i = 0; i < 6; ++i)
             {
                 Faces[i] = new CubemapFaceGBuffer(FaceSize);
             }
 
             // Load the shader
             _CubemapGBufferShader = Shader.Find("Hidden/Camera-DepthNormalTexture");
             if (_CubemapGBufferShader == null)
             {
                 throw new Exception("Could not find built-in shaders.");
             }
         }
         
         public void Render(Vector3 NewPosition)
         {
             // Face render order: Top, Right, Front, Bottom, Left, Back
             Quaternion[] rotations =
             {
                 Quaternion.Euler(-90, 0, 0),
                 Quaternion.Euler(0, 90, 0),
                 Quaternion.Euler(0, 0, 0),
                 Quaternion.Euler(90, 0, 0),
                 Quaternion.Euler(0, -90, 0),
                 Quaternion.Euler(0, 180, 0)
             };
 
             _CameraGameObj.transform.position = NewPosition;
 
             // Render each face separately
             for (int i = 0; i < 6; ++i)
             {
                 _CameraGameObj.transform.rotation = rotations[i];
 
                 var face = Faces[i];
                 _Camera.targetTexture = face.NormalsLinearDepth;
                 _Camera.depthTextureMode = DepthTextureMode.DepthNormals;
                 _Camera.RenderWithShader(_CubemapGBufferShader, null);
 
                 _Camera.targetTexture = face.Color;
                 _Camera.depthTextureMode = DepthTextureMode.Depth; 
                 _Camera.Render();
 
                 _Camera.targetTexture = null;
             }
         }
         private Camera _Camera;
         private GameObject _CameraGameObj;
         private Shader _CubemapGBufferShader;
         public CubemapFaceGBuffer[] Faces;
     }
 }
 
Any help is greatly appreciated!
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
How to clean on RenderTexture to transparent with brush 0 Answers
Manipulating pixels in a texture 0 Answers
Cube map performance on Android 0 Answers
Render material to full screen quad 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                