Search Unity

Help with Custom Deferred Shading

Discussion in 'Scripting' started by WGermany, Dec 22, 2014.

  1. WGermany

    WGermany

    Joined:
    Jun 27, 2013
    Posts:
    78
    Hey everyone. I decided to tackle a custom renderer, and while you might be thinking, "Unity 5 is around the corner, why on earth would you do that"? Well I plan to extend it to tiled deferred render and have a rendering pipeline that I have full control over. It seems the old, drop the built-in shaders into the resource folder and make changes doesnt work with Unity 5. I don't have control over shadow maps. And I really really want to make my own BRDF system :)

    For those of you who are not familiar with custom rendering it essentially what Jove 2.0 and Dolkar are doing here on the Unity forums. Creating a custom renderer in Unity allows for the easy use of Unity of course with the bonus of full access to how everything is rendered.

    Here is the current code I have. So far it works, kinda. I have 3 issues. The last two are minute and you don't have to worry as I am bound to figure it out. So for now my biggest concern is the issue below.

    TL;DR : When I move the camera around, it does not update what's being rendered.

    Just to show you that it does function and that I'm excited about the potential use of it, here is a super basic diffuse, ambient and specular I made in about 1.5 minutes:

    EDIT: I added some of my BRDF code to it and it works nicely, still haven't found a fix yet for the camera not updating....

    (Sorry for the messy code)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DeferredShading : MonoBehaviour
    5. {
    6.  
    7.     Camera originalCamera;
    8.     Camera renderingCamera;
    9.     public static RenderTexture[] RTs;
    10.     public Shader GBufferShader;
    11.     public Shader DirectionalLightShader;
    12.     public Light MainLight;
    13.     [Range(0, 10)]
    14.     public float LightIntensity = 1.0f;
    15.     public Color LightColor = Color.white;
    16.     private RenderBuffer[] colorBuffers;
    17.     private RenderBuffer depthBuffer;
    18.     private Material DirectionalLightMaterial;
    19.     private Material GBufferMat;
    20.  
    21.     void Awake()
    22.     {
    23.         originalCamera = camera;
    24.     }
    25.  
    26.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    27.     {
    28.         if(!GBufferMat)
    29.         {
    30.             GBufferMat = new Material(GBufferShader);
    31.             GBufferMat.SetTexture("_MainTex", RTs[0]);
    32.             GBufferMat.SetTexture("_NormalTexture", RTs[1]);
    33.             GBufferMat.SetTexture("_DepthTexture", RTs[1]);
    34.             GBufferMat.SetTexture("_SpecColor", RTs[2]);
    35.  
    36.             source = RTs[0];
    37.        
    38.             Graphics.Blit(RTs[0], destination, GBufferMat);
    39.             Graphics.Blit(RTs[1], destination, GBufferMat);
    40.             Graphics.Blit(RTs[2], destination, GBufferMat);
    41.        
    42.         }
    43.    
    44.         if(!DirectionalLightMaterial)
    45.         {
    46.             DirectionalLightMaterial = new Material(DirectionalLightShader);
    47.             DirectionalLighting(source, destination);
    48.         }
    49.     }
    50.  
    51.     void OnEnable()
    52.     {
    53.         CreateCamera();
    54.     }
    55.  
    56.     void CreateCamera()
    57.     {
    58.         ReformCameras();
    59.         CreateBuffers();
    60.     }
    61.  
    62.     void OnPostRender()
    63.     {
    64.         renderingCamera.SetTargetBuffers(colorBuffers, depthBuffer);
    65.         renderingCamera.RenderWithShader(GBufferShader, "");
    66.     }
    67.  
    68.     void OnGUI()
    69.     {
    70.         //GUI.DrawTexture(new Rect(0, 0, RTs[0].width, RTs[0].height), RTs[0], ScaleMode.StretchToFill, false);
    71.     }
    72.  
    73.     public void DirectionalLighting (RenderTexture Input, RenderTexture Output)
    74.     {
    75.         DirectionalLightMaterial.SetFloat("_LightIntensity", LightIntensity);
    76.         DirectionalLightMaterial.SetColor("_LightColor", LightColor);
    77.         DirectionalLightMaterial.SetVector("_LightDirection", MainLight.transform.forward * 1.0f);
    78.         DirectionalLightMaterial.SetTexture("_MainTex", RTs[0]);
    79.         DirectionalLightMaterial.SetTexture("_NormalTexture", RTs[1]);
    80.         Graphics.Blit(Input, Output, DirectionalLightMaterial);
    81.     }
    82.  
    83.     void ReformCameras()
    84.     {
    85.         renderingCamera = new GameObject("RenderingCamera").AddComponent<Camera>();
    86.    
    87.         renderingCamera.enabled = false;
    88.    
    89.         renderingCamera.transform.parent = transform;
    90.         renderingCamera.transform.localPosition = Vector3.zero;
    91.         renderingCamera.transform.localRotation = Quaternion.identity;
    92.    
    93.         originalCamera.renderingPath = RenderingPath.VertexLit;
    94.         originalCamera.cullingMask = 0;
    95.         originalCamera.clearFlags = CameraClearFlags.Depth;
    96.         originalCamera.backgroundColor = Color.black;
    97.    
    98.         renderingCamera.renderingPath = RenderingPath.VertexLit;
    99.         renderingCamera.clearFlags = CameraClearFlags.SolidColor;
    100.         renderingCamera.farClipPlane = originalCamera.farClipPlane;
    101.         renderingCamera.fieldOfView = originalCamera.fieldOfView;
    102.     }
    103.  
    104.     void CreateBuffers ()
    105.     {
    106.         RTs = new RenderTexture[] {
    107.             RenderTexture.GetTemporary(Screen.width, Screen.height, 32, RenderTextureFormat.Default),
    108.             RenderTexture.GetTemporary(Screen.width, Screen.height, 32, RenderTextureFormat.Default),
    109.             RenderTexture.GetTemporary(Screen.width, Screen.height, 32, RenderTextureFormat.Default) };
    110.    
    111.         colorBuffers = new RenderBuffer[] { RTs[0].colorBuffer, RTs[1].colorBuffer, RTs[2].colorBuffer };
    112.         depthBuffer = RTs[1].depthBuffer;
    113.     }
    114.  
    115.     void OnDisable()
    116.     {
    117.         Destroy(renderingCamera.gameObject);
    118.     }
    119. }
    120.  
     
    Last edited: Dec 24, 2014
  2. WGermany

    WGermany

    Joined:
    Jun 27, 2013
    Posts:
    78
    Bump. Should I be posting this in the ShaderLab section so that @Dolkar and @Aieth can see this? ;)