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 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): 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): Shader "Custom/Per-Pixel Lens Flare" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #pragma target 3.0 #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform float4 _uLensColor; uniform float _uDispersal; uniform float _uHaloWidth; uniform float _uDistortion; uniform int _uSamples; uniform float _HorizontalSize; //--------------Distortion Function---------------------------------------*/ float4 texDistorted(in sampler2D tex, in float2 texcoord, in float2 direction, in float3 distortion) { return float4(tex2D(tex, texcoord + direction * distortion.r).r, tex2D(tex, texcoord + direction * distortion.g).g, tex2D(tex, texcoord + direction * distortion.b).b, 1.0); } //----------------------------------------------------------------------------*/ float4 frag(v2f_img i) : COLOR { float2 texcoord = -i.uv + float2(1.0, 1.0); //Flip the texcoordinates float2 texelSize = 1.0 / _HorizontalSize; float2 ghostVec = (float2(0.5, 0.5) - texcoord) * _uDispersal; float2 haloVec = normalize(ghostVec) * _uHaloWidth; float3 distortion = float3(-texelSize.x * _uDistortion, 0.0, texelSize.x * _uDistortion); //sample ghost //unroll (8)] float4 result = float4(0, 0, 0, 0); for (int i = 0; i < 8; i++) { float2 offset = frac(texcoord + ghostVec * float(i)); float weight = length(float2(0.5, 0.5) - offset) / length(float2(0.5, 0.5)); weight = pow(1.0 - weight, 10.0); result += texDistorted(_MainTex, offset, normalize(ghostVec), distortion) * weight; } float2 thistex = length(float2(0.5, 0.5) - texcoord) / length(float2(0.5, 0.5)); result *= _uLensColor; //sample halo half thislength = length(float2(0.5, 0.5) - frac(texcoord + haloVec)); float weight = thislength / length(float2(0.5, 0.5)); weight = pow(1.0 - weight, 10.0); result += texDistorted(_MainTex, frac(texcoord + haloVec), normalize(ghostVec), distortion) * weight; return result; } ENDCG } } } Bright Pass Filter Code (csharp): Shader "Custom/BrightPassFilter" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _uScale ("", Range(0.0,1.0)) = 1.0 _uBias ("", Range(0.0,1.0)) = 1.0 } SubShader { Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #pragma target 4.0 #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform float4 _uScale; uniform float4 _uBias; //struct v2p //{ // noperspective float2 vTexcoord : TEXCOORD0; //}; //struct result //{ // float4 fResult : COLOR; //}; //void main(in v2p IN, out result OUT) //{ //OUT.fResult = max(float4(0,0,0,0), tex2D(_MainTex, IN.vTexcoord) + _uBias) * _uScale; //} float4 frag(v2f_img i) : COLOR { float4 result = max(float4(0,0,0,0), tex2D(_MainTex, i.uv * _uScale) + _uBias); return result; } ENDCG } } } Current C# Code: Code (csharp): using UnityEngine; using System.Collections; [ExecuteInEditMode] public class ScreenSpaceLensFlare : MonoBehaviour { #region Variables public Shader BrightPassFilterShader; public Shader LensFlareShader; public float Threshold = 0.0f; public int Downsampling = 1; public Color LensColor; public float HaloWidth = 1.0f; public float Dispersal = 1.0f; public float Distortion = 1.0f; private Material BrightPassFilterMaterial; private Material LensFlareMaterial; #endregion #region Properties Material ThresholdMaterial { get { if(BrightPassFilterMaterial == null) { BrightPassFilterMaterial = new Material(BrightPassFilterShader); BrightPassFilterMaterial.hideFlags = HideFlags.HideAndDontSave; } return BrightPassFilterMaterial; } } Material PerPixelFlareMaterial { get { if(LensFlareMaterial == null) { LensFlareMaterial = new Material(LensFlareShader); LensFlareMaterial.hideFlags = HideFlags.HideAndDontSave; } return LensFlareMaterial; } } #endregion // Use this for initialization void Start () { if(!SystemInfo.supportsImageEffects) { enabled = false; return; } } void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture) { if(BrightPassFilterShader LensFlareShader != null) { //Step #1: Downsample screen, extract bright parts RenderTexture Extract = RenderTexture.GetTemporary(Screen.width, Screen.height, 0); ThresholdMaterial.SetVector("_uScale", new Vector4 (Downsampling, Downsampling, Downsampling, Downsampling)); ThresholdMaterial.SetVector("_uBias", new Vector4 (Threshold, Threshold, Threshold, Threshold)); Graphics.Blit(sourceTexture, Extract, ThresholdMaterial); //Step #2: Render Lens flare on Screen PerPixelFlareMaterial.SetFloat("_HorizontalSize", Screen.width); PerPixelFlareMaterial.SetFloat("_uHaloWidth", HaloWidth); PerPixelFlareMaterial.SetFloat("_uDistortion", Distortion); PerPixelFlareMaterial.SetFloat("_uDispersal", Dispersal); //PerPixelFlareMaterial.SetColor("_uLensColor", LensColor); Graphics.Blit(Extract, destTexture, PerPixelFlareMaterial); RenderTexture.ReleaseTemporary(Extract); } } // Update is called once per frame void Update () { Threshold = Mathf.Clamp(Threshold, -1.0f, 0.0f); } void OnDisable () { if(BrightPassFilterMaterial LensFlareMaterial) { DestroyImmediate(BrightPassFilterMaterial); } } } Its doing something at least
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