Advertisement
Guest User

Postprocess.cs

a guest
Apr 19th, 2019
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Postprocess : MonoBehaviour
  5. {
  6.  
  7.     public Material PostprocessMaterial;
  8.     public Material NormalRender;
  9.  
  10.     public enum EdgeDetectMode
  11.     {
  12.         TriangleDepthNormals = 0,
  13.         RobertsCrossDepthNormals = 1,
  14.         SobelDepth = 2,
  15.         SobelDepthThin = 3,
  16.         TriangleLuminance = 4,
  17.     }
  18.  
  19.     public EdgeDetectMode mode = EdgeDetectMode.TriangleDepthNormals;
  20.  
  21.     public float sensitivityDepth = 1.0f;
  22.     public float sensitivityNormals = 1.0f;
  23.     public float lumThreshold = 0.2f;
  24.     public float edgeExp = 1.0f;
  25.     public float sampleDist = 1.0f;
  26.     public float edgesOnly = 0.0f;
  27.     public Color edgesOnlyBgColor = Color.white;
  28.  
  29.     public RenderTexture CameraRT;
  30.     public RenderTexture Temp;
  31.  
  32.     public void Start()
  33.     {
  34.         Camera.main.depthTextureMode =  DepthTextureMode.DepthNormals;// depthnormals are needed for outline effect
  35.  
  36.         // create new render textures
  37.         CameraRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf);
  38.         Temp = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf);
  39.  
  40.         Camera.main.targetTexture = CameraRT;// set one of the textures to the main camera
  41.     }
  42.  
  43.     void OnPostRender()
  44.     {
  45.     // set edge detection variables
  46.         Vector2 sensitivity = new Vector2(sensitivityDepth, sensitivityNormals);
  47.         PostprocessMaterial.SetVector("_Sensitivity", new Vector4(sensitivity.x, sensitivity.y, 1.0f, sensitivity.y));
  48.         PostprocessMaterial.SetFloat("_BgFade", edgesOnly);
  49.         PostprocessMaterial.SetFloat("_SampleDistance", sampleDist);
  50.         PostprocessMaterial.SetVector("_BgColor", edgesOnlyBgColor);
  51.         PostprocessMaterial.SetFloat("_Exponent", edgeExp);
  52.         PostprocessMaterial.SetFloat("_Threshold", lumThreshold);
  53.  
  54.         // Set the targets for the Blit as the buffer, with the depth buffer of the main camera
  55.         Graphics.SetRenderTarget(Temp.colorBuffer, CameraRT.depthBuffer);
  56.         // render normal, clean render
  57.         Graphics.Blit(CameraRT, NormalRender);
  58.       // render effect over it
  59.         Graphics.Blit(CameraRT, PostprocessMaterial, (int)mode);
  60.  
  61.         // release rendertexture, next blit is to the screen
  62.         RenderTexture.active = null;
  63.         // render result to the screen
  64.         Graphics.Blit(Temp, NormalRender);
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement