• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yoshilol · Jan 12, 2016 at 09:27 PM · shadertexturerendertexturecubemapbuffer

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting cubemap faces as rendertexture 0 Answers

How to update texture during runtime to display damage? 0 Answers

Render texture and shader issues 0 Answers

Render scene depth to a texture 4 Answers

Cubemap render faces 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges