Search Unity

Pseudo Lens Flare Cg porting help

Discussion in 'Shaders' started by WGermany, Jul 17, 2013.

  1. WGermany

    WGermany

    Joined:
    Jun 27, 2013
    Posts:
    78
    Hello everyone, I'm back again after finishing the bloom, I'd thought I'd go after Kawase’s Light Streak Filter but I decided to implement something else while I continue figuring it out. John chapman has posted this great tutorial and I'm stuck on the first part :p

    http://john-chapman-graphics.blogspot.co.uk/2013/02/pseudo-lens-flare.html

    Problem #1
    [FIXED] Problem was C# code, I forgot when using a float4 it must be material.SetVector new Vector4

    He uses the "noperspective" command which i'm not very familiar with to downscale and threshold the main image. I don't really know if i'm doing this right I'm still a beginner. Here is what I have so far.. I will most likely need help until the shader is finished, I hope i'm not asking for too much.

    Problem #2
    uScale doesn't scale the image still. Multiplying it by "i.uv" works but I'm not sure if thats what I should be multiplying it by or the result that i'm looking for.

    Problem#3
    Calculating screen texture coordinates in CG/HLSL similar problem here:
    http://stackoverflow.com/questions/14204364/calculating-screen-texture-coordinates-in-cg-hlsl

    Code (csharp):
    1. float2 texelSize = 1.0 / float2(textureSize(_MainTex, 0);
    Problem#4
    Screen Aligned Quad in Unity for a post processing framework? Still a beginner trying to take on something more advanced but thats how you learn right?

    Current CG Code:
    Lens Flare
    Code (csharp):
    1. Shader "Custom/Per-Pixel Lens Flare" {
    2.     Properties
    3.     {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader
    7.     {
    8.         Pass
    9.         {
    10.        
    11.         CGPROGRAM
    12.         #pragma vertex vert_img
    13.         #pragma fragment frag
    14.         #pragma fragmentoption ARB_precision_hint_fastest
    15.         #pragma target 3.0
    16.         #include "UnityCG.cginc"
    17.        
    18.  
    19.         uniform sampler2D _MainTex;
    20.         uniform float4 _uLensColor;
    21.         uniform float _uDispersal;
    22.         uniform float _uHaloWidth;
    23.         uniform float _uDistortion;
    24.         uniform int _uSamples;
    25.         uniform float _HorizontalSize;
    26.  
    27. //--------------Distortion Function---------------------------------------*/
    28.         float4 texDistorted(in sampler2D tex, in float2 texcoord, in float2 direction, in float3 distortion)
    29.          {
    30.             return float4(tex2D(tex, texcoord + direction * distortion.r).r,
    31.                         tex2D(tex, texcoord + direction * distortion.g).g,
    32.                         tex2D(tex, texcoord + direction * distortion.b).b, 1.0);
    33.         }
    34.        
    35. //----------------------------------------------------------------------------*/
    36.        
    37.         float4 frag(v2f_img i) : COLOR
    38.         {
    39.             float2 texcoord = -i.uv + float2(1.0, 1.0); //Flip the texcoordinates
    40.             float2 texelSize = 1.0 / _HorizontalSize;
    41.            
    42.             float2 ghostVec = (float2(0.5, 0.5) - texcoord) * _uDispersal;
    43.             float2 haloVec = normalize(ghostVec) * _uHaloWidth;
    44.            
    45.             float3 distortion = float3(-texelSize.x * _uDistortion, 0.0, texelSize.x * _uDistortion);
    46.            
    47.             //sample ghost
    48.             //unroll (8)]
    49.             float4 result = float4(0, 0, 0, 0);
    50.             for (int i = 0; i < 8; i++)
    51.             {
    52.                 float2 offset = frac(texcoord + ghostVec * float(i));
    53.                
    54.                 float weight = length(float2(0.5, 0.5) - offset) / length(float2(0.5, 0.5));
    55.                 weight = pow(1.0 - weight, 10.0);
    56.                
    57.                 result += texDistorted(_MainTex, offset, normalize(ghostVec), distortion) * weight;
    58.             }
    59.                 float2 thistex = length(float2(0.5, 0.5) - texcoord) / length(float2(0.5, 0.5));
    60.            
    61.                 result *= _uLensColor;
    62.                
    63.             //sample halo
    64.             half thislength = length(float2(0.5, 0.5) - frac(texcoord + haloVec));
    65.             float weight = thislength / length(float2(0.5, 0.5));
    66.             weight = pow(1.0 - weight, 10.0);
    67.             result += texDistorted(_MainTex, frac(texcoord + haloVec), normalize(ghostVec), distortion) * weight;
    68.            
    69.             return result;
    70.            
    71.             }
    72.        
    73.  
    74.         ENDCG
    75.        
    76.      }
    77.   }
    78. }
    Bright Pass Filter
    Code (csharp):
    1. Shader "Custom/BrightPassFilter" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _uScale ("", Range(0.0,1.0)) = 1.0
    5.         _uBias ("", Range(0.0,1.0)) = 1.0
    6.     }
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.        
    12.         CGPROGRAM
    13.         #pragma vertex vert_img
    14.         #pragma fragment frag
    15.         #pragma fragmentoption ARB_precision_hint_fastest
    16.         #pragma target 4.0
    17.         #include "UnityCG.cginc"
    18.  
    19.         uniform sampler2D _MainTex;
    20.         uniform float4 _uScale;
    21.         uniform float4 _uBias;
    22.        
    23.         //struct v2p
    24.         //{
    25.         //  noperspective float2 vTexcoord : TEXCOORD0;
    26.         //};
    27.        
    28.         //struct result
    29.         //{
    30.         //  float4 fResult : COLOR;
    31.         //};
    32.        
    33.         //void main(in v2p IN, out result OUT)
    34.         //{
    35.             //OUT.fResult = max(float4(0,0,0,0), tex2D(_MainTex, IN.vTexcoord) + _uBias) * _uScale;
    36.         //}
    37.        
    38.         float4 frag(v2f_img i) : COLOR
    39.         {
    40.             float4 result = max(float4(0,0,0,0), tex2D(_MainTex, i.uv * _uScale) + _uBias);
    41.             return result;
    42.        
    43.         }
    44.  
    45.         ENDCG
    46.         }
    47.     }
    48. }
    Current C# Code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class ScreenSpaceLensFlare : MonoBehaviour {
    6.  
    7. #region Variables
    8.     public Shader BrightPassFilterShader;
    9.     public Shader LensFlareShader;
    10.     public float Threshold = 0.0f;
    11.     public int Downsampling = 1;
    12.     public Color LensColor;
    13.     public float HaloWidth = 1.0f;
    14.     public float Dispersal = 1.0f;
    15.     public float Distortion = 1.0f;
    16.     private Material BrightPassFilterMaterial;
    17.     private Material LensFlareMaterial;
    18.     #endregion
    19.    
    20.     #region Properties
    21.     Material ThresholdMaterial
    22.     {
    23.         get
    24.         {
    25.             if(BrightPassFilterMaterial == null)
    26.             {
    27.                 BrightPassFilterMaterial = new Material(BrightPassFilterShader);
    28.                 BrightPassFilterMaterial.hideFlags = HideFlags.HideAndDontSave;
    29.             }
    30.             return BrightPassFilterMaterial;
    31.         }
    32.     }
    33.     Material PerPixelFlareMaterial
    34.     {
    35.         get
    36.         {
    37.             if(LensFlareMaterial == null)
    38.             {
    39.                 LensFlareMaterial = new Material(LensFlareShader);
    40.                 LensFlareMaterial.hideFlags = HideFlags.HideAndDontSave;   
    41.             }
    42.             return LensFlareMaterial;
    43.         }
    44.     }
    45.     #endregion
    46.     // Use this for initialization
    47.     void Start ()
    48.     {
    49.         if(!SystemInfo.supportsImageEffects)
    50.         {
    51.             enabled = false;
    52.             return;
    53.         }
    54.     }
    55.    
    56.     void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
    57.     {
    58.         if(BrightPassFilterShader  LensFlareShader != null)
    59.         {
    60.             //Step #1: Downsample screen, extract bright parts
    61.             RenderTexture Extract = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
    62.             ThresholdMaterial.SetVector("_uScale", new Vector4 (Downsampling, Downsampling, Downsampling, Downsampling));
    63.             ThresholdMaterial.SetVector("_uBias", new Vector4 (Threshold, Threshold, Threshold, Threshold));
    64.             Graphics.Blit(sourceTexture, Extract, ThresholdMaterial);
    65.            
    66.             //Step #2: Render Lens flare on Screen
    67.             PerPixelFlareMaterial.SetFloat("_HorizontalSize", Screen.width);
    68.             PerPixelFlareMaterial.SetFloat("_uHaloWidth", HaloWidth);
    69.             PerPixelFlareMaterial.SetFloat("_uDistortion", Distortion);
    70.             PerPixelFlareMaterial.SetFloat("_uDispersal", Dispersal);
    71.             //PerPixelFlareMaterial.SetColor("_uLensColor", LensColor);
    72.             Graphics.Blit(Extract, destTexture, PerPixelFlareMaterial);
    73.            
    74.            
    75.             RenderTexture.ReleaseTemporary(Extract);
    76.            
    77.         }
    78.  
    79.        
    80.        
    81.     }
    82.    
    83.     // Update is called once per frame
    84.     void Update ()
    85.     {
    86.         Threshold = Mathf.Clamp(Threshold, -1.0f, 0.0f);
    87.     }
    88.    
    89.     void OnDisable ()
    90.     {
    91.         if(BrightPassFilterMaterial  LensFlareMaterial)
    92.         {
    93.             DestroyImmediate(BrightPassFilterMaterial);
    94.         }
    95.        
    96.     }
    97.    
    98.    
    99. }
    Its doing something at least
    $Untitled-3.jpg
    $Untitled-6.jpg
     
    Last edited: Jul 19, 2013
  2. WGermany

    WGermany

    Joined:
    Jun 27, 2013
    Posts:
    78
    Even though I've made little progress on my own I think I'm pretty stuck on this.. :confused:
     
  3. BrainMedicine

    BrainMedicine

    Joined:
    Sep 3, 2013
    Posts:
    3
    I saw glarefx, but it's from john-chapman-graphics perfectly
    I think that you can do it:cool:
     
  4. LoopSRL

    LoopSRL

    Joined:
    Mar 1, 2013
    Posts:
    3
    Hi WGermany, have you ended up with a solution? Cause I'm having the same issue and I don't know where to look.
    Any help would be appreciated :)