├── Runtime ├── Unity.OcclusionLensFlare.Runtime.asmdef ├── Unity.OcclusionLensFlare.Runtime.asmdef.meta ├── OcclusionLensFlare.cs.meta └── OcclusionLensFlare.cs ├── Documentation~ └── images │ ├── sample.png │ ├── add_layer.png │ ├── flarelayer.png │ ├── rendertexture.png │ ├── sun_material.png │ └── graphics_settings.png ├── Samples~ └── Occlusion Lens Flare Sample.unitypackage ├── CHANGELOG.md.meta ├── LICENSE.md.meta ├── README.md.meta ├── package.json.meta ├── Runtime.meta ├── Shaders.meta ├── Shaders ├── OcclusionLensFlare.shader.meta ├── OcclusionLensFlare-Sun.shader.meta ├── OcclusionLensFlare-OcclusionTexture.shader.meta ├── OcclusionLensFlare-Sun.shader ├── OcclusionLensFlare-OcclusionTexture.shader └── OcclusionLensFlare.shader ├── .gitignore ├── CHANGELOG.md ├── package.json ├── LICENSE.md └── README.md /Runtime/Unity.OcclusionLensFlare.Runtime.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Unity.OcclusionLensFlare.Runtime" 3 | } 4 | -------------------------------------------------------------------------------- /Documentation~/images/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Documentation~/images/sample.png -------------------------------------------------------------------------------- /Documentation~/images/add_layer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Documentation~/images/add_layer.png -------------------------------------------------------------------------------- /Documentation~/images/flarelayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Documentation~/images/flarelayer.png -------------------------------------------------------------------------------- /Documentation~/images/rendertexture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Documentation~/images/rendertexture.png -------------------------------------------------------------------------------- /Documentation~/images/sun_material.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Documentation~/images/sun_material.png -------------------------------------------------------------------------------- /Documentation~/images/graphics_settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Documentation~/images/graphics_settings.png -------------------------------------------------------------------------------- /Samples~/Occlusion Lens Flare Sample.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pschraut/UnityOcclusionLensFlare/HEAD/Samples~/Occlusion Lens Flare Sample.unitypackage -------------------------------------------------------------------------------- /CHANGELOG.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 456ae1ff997196b4aa55a021efa58bd1 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /LICENSE.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 52e919d017732c34599ebce130d8221c 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dd1e173f9bd66b44c8852990840c9591 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 078067b748fae8a42bcb895cd1915907 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Runtime.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9b70068ef0ec8354da1b598190dbd2c9 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Shaders.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4aa68baa10ca3694ca8595fa4b3479ea 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Runtime/Unity.OcclusionLensFlare.Runtime.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b07f7b40954778446807c622908ff890 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Shaders/OcclusionLensFlare.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a8587f677c82af343b3f7daa42adc31a 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Shaders/OcclusionLensFlare-Sun.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a0d60d606e175a84bab48d39ebe0a675 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Shaders/OcclusionLensFlare-OcclusionTexture.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0fae17b6bf58d924685797236fe7ffbb 3 | ShaderImporter: 4 | externalObjects: {} 5 | defaultTextures: [] 6 | nonModifiableTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | [Aa]rtifacts/ 2 | [Bb]uild/ 3 | [Ll]ibrary/ 4 | [Oo]bj/ 5 | [Tt]emp/ 6 | [Ll]og/ 7 | [Ll]ogs/ 8 | .vs 9 | .vscode 10 | .idea 11 | .DS_Store 12 | *.aspx 13 | *.browser 14 | *.csproj 15 | *.exe 16 | *.ini 17 | *.map 18 | *.mdb 19 | *.npmrc 20 | *.pyc 21 | *.resS 22 | *.sdf 23 | *.sln 24 | *.sublime-project 25 | *.sublime-workspace 26 | *.suo 27 | *.userprefs 28 | .npmrc 29 | *.leu 30 | -------------------------------------------------------------------------------- /Runtime/OcclusionLensFlare.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9f16e5c8dd6ce0247b4f32e8dee1d3e8 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: 7 | - m_OcclusionSun: {instanceID: 0} 8 | - m_OcclusionCamera: {instanceID: 0} 9 | - m_OcclusionShader: {fileID: 4800000, guid: 0fae17b6bf58d924685797236fe7ffbb, type: 3} 10 | - m_WorldCamera: {instanceID: 0} 11 | executionOrder: 0 12 | icon: {instanceID: 0} 13 | userData: 14 | assetBundleName: 15 | assetBundleVariant: 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this package are documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [1.1.0] - 2021-03-07 8 | ### Changed 9 | - Use [Camera.RenderWithShader](https://docs.unity3d.com/ScriptReference/Camera.RenderWithShader.html) over 10 | [Camera.SetReplacementShader](https://docs.unity3d.com/ScriptReference/Camera.ResetReplacementShader.html) and [Camera.Render](https://docs.unity3d.com/ScriptReference/Camera.Render.html) calls. 11 | - Removed usage of [ColorMask RGB](https://docs.unity3d.com/Manual/SL-Pass.html) from ```OcclusionLensFlare.shader```, because according to [this Unity documentation](https://docs.unity3d.com/Manual/SL-ShaderPerformance.html) it can affect performance negatively on mobile gpu's. 12 | 13 | ## [1.0.0] - 2021-03-07 14 | - First public release 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.oddworm.occlusionlensflare", 3 | "version": "1.1.0", 4 | "displayName": "Occlusion Lens Flare", 5 | "description": "The Occlusion Lens Flare package provides the ability to use Unity's builtin Lens Flare Component without Physics.", 6 | "unity": "2019.3", 7 | "documentationUrl": "https://github.com/pschraut/UnityOcclusionLensFlare", 8 | "dependencies": { 9 | }, 10 | "keywords": [ 11 | "unity", 12 | "effect" 13 | ], 14 | "author": { 15 | "name": "Peter Schraut", 16 | "url": "http://console-dev.de" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/pschraut/UnityOcclusionLensFlare.git" 21 | }, 22 | "samples": [ 23 | { 24 | "displayName": "Occlusion Lens Flare Sample", 25 | "description": "Sample scene illustrating how to setup the Occlusion Lens Flare. The sample requires Unity's Cinemachine Package to be installed.", 26 | "path": "Samples~/Occlusion Lens Flare Sample" 27 | } 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Peter Schraut (http://www.console-dev.de) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | of the Software, and to permit persons to whom the Software is furnished to do 10 | so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Shaders/OcclusionLensFlare-Sun.shader: -------------------------------------------------------------------------------- 1 | // 2 | // Occlusion Lens Flare for Unity. Copyright (c) 2021 Peter Schraut (www.console-dev.de). See LICENSE.md 3 | // https://github.com/pschraut/UnityOcclusionLensFlare 4 | // 5 | 6 | Shader "FX/Occlusion Lens Flare - Sun" 7 | { 8 | Properties 9 | { 10 | } 11 | SubShader 12 | { 13 | Tags { "Queue" = "Transparent-1" "IgnoreProjector" = "True" "RenderType" = "OcclusionLensFlareSun" } 14 | 15 | Pass 16 | { 17 | Lighting Off 18 | ZWrite Off 19 | ZTest LEqual 20 | 21 | CGPROGRAM 22 | #pragma vertex vert 23 | #pragma fragment frag 24 | #pragma target 2.0 25 | #include "UnityCG.cginc" 26 | 27 | struct appdata 28 | { 29 | float4 vertex : POSITION; 30 | }; 31 | 32 | struct v2f 33 | { 34 | float4 vertex : SV_POSITION; 35 | }; 36 | 37 | v2f vert (appdata v) 38 | { 39 | v2f o; 40 | UNITY_INITIALIZE_OUTPUT(v2f, o); 41 | 42 | o.vertex = UnityObjectToClipPos(v.vertex); 43 | return o; 44 | } 45 | 46 | fixed4 frag(v2f i) : SV_Target 47 | { 48 | return fixed4(1,1,1,1); 49 | } 50 | ENDCG 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Shaders/OcclusionLensFlare-OcclusionTexture.shader: -------------------------------------------------------------------------------- 1 | // 2 | // Occlusion Lens Flare for Unity. Copyright (c) 2021 Peter Schraut (www.console-dev.de). See LICENSE.md 3 | // https://github.com/pschraut/UnityOcclusionLensFlare 4 | // 5 | 6 | Shader "Hidden/FX/Occlusion Lens Flare - OcclusionTexture" 7 | { 8 | CGINCLUDE 9 | #pragma target 2.0 10 | #include "UnityCG.cginc" 11 | 12 | struct appdata 13 | { 14 | float4 vertex : POSITION; 15 | float2 uv : TEXCOORD0; 16 | }; 17 | 18 | struct v2f 19 | { 20 | float4 vertex : SV_POSITION; 21 | float2 uv : TEXCOORD0; 22 | }; 23 | 24 | sampler2D _MainTex; 25 | float4 _MainTex_ST; 26 | float _Cutoff; 27 | float4 _Color; 28 | 29 | v2f vert(appdata v) 30 | { 31 | v2f o; 32 | UNITY_INITIALIZE_OUTPUT(v2f, o); 33 | 34 | o.vertex = UnityObjectToClipPos(v.vertex); 35 | o.uv = TRANSFORM_TEX(v.uv, _MainTex); 36 | return o; 37 | } 38 | 39 | fixed4 frag(v2f i) : SV_Target 40 | { 41 | return fixed4(0,0,0,0); 42 | } 43 | 44 | fixed4 frag_cutout(v2f i) : SV_Target 45 | { 46 | fixed4 col = tex2D(_MainTex, i.uv) * _Color; 47 | clip(col.a - _Cutoff); 48 | return fixed4(0,0,0,0); 49 | } 50 | 51 | fixed4 frag_sun(v2f i) : SV_Target 52 | { 53 | return fixed4(1,1,1,1); 54 | } 55 | ENDCG 56 | 57 | SubShader 58 | { 59 | Tags{ "RenderType" = "OcclusionLensFlareSun" "IgnoreProjector" = "True" } 60 | Pass 61 | { 62 | Fog { Mode Off } 63 | CGPROGRAM 64 | #pragma vertex vert 65 | #pragma fragment frag_sun 66 | ENDCG 67 | } 68 | } 69 | 70 | SubShader 71 | { 72 | Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True" } 73 | Pass 74 | { 75 | Fog { Mode Off } 76 | CGPROGRAM 77 | #pragma vertex vert 78 | #pragma fragment frag 79 | ENDCG 80 | } 81 | } 82 | 83 | SubShader 84 | { 85 | Tags{ "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" "IgnoreProjector" = "True" } 86 | Pass 87 | { 88 | Fog { Mode Off } 89 | CGPROGRAM 90 | #pragma vertex vert 91 | #pragma fragment frag_cutout 92 | ENDCG 93 | } 94 | } 95 | } 96 | 97 | -------------------------------------------------------------------------------- /Shaders/OcclusionLensFlare.shader: -------------------------------------------------------------------------------- 1 | // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) 2 | 3 | // 4 | // Occlusion Lens Flare for Unity. Copyright (c) 2021 Peter Schraut (www.console-dev.de). See LICENSE.md 5 | // https://github.com/pschraut/UnityOcclusionLensFlare 6 | // 7 | 8 | Shader "Hidden/Occlusion Lens Flare" 9 | { 10 | SubShader { 11 | 12 | Tags {"RenderType"="Overlay"} 13 | ZWrite Off ZTest Always 14 | Cull Off 15 | Blend One One 16 | //ColorMask RGB // according to unity docs, this can be bad on mobile gpu's https://docs.unity3d.com/Manual/SL-ShaderPerformance.html 17 | 18 | Pass { 19 | CGPROGRAM 20 | #pragma vertex vert 21 | #pragma fragment frag 22 | #pragma target 2.0 23 | 24 | #include "UnityCG.cginc" 25 | 26 | sampler2D _FlareTexture; 27 | float4 _FlareTexture_ST; 28 | sampler2D _FlareOcclusionTexture; 29 | 30 | struct appdata_t { 31 | float4 vertex : POSITION; 32 | fixed4 color : COLOR; 33 | float2 texcoord : TEXCOORD0; 34 | UNITY_VERTEX_INPUT_INSTANCE_ID 35 | }; 36 | 37 | struct v2f { 38 | float4 vertex : SV_POSITION; 39 | fixed4 color : COLOR; 40 | float2 texcoord : TEXCOORD0; 41 | UNITY_VERTEX_OUTPUT_STEREO 42 | }; 43 | 44 | 45 | v2f vert (appdata_t v) 46 | { 47 | v2f o; 48 | UNITY_INITIALIZE_OUTPUT(v2f, o); 49 | UNITY_SETUP_INSTANCE_ID(v); 50 | UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); 51 | 52 | o.vertex = UnityObjectToClipPos(v.vertex); 53 | o.texcoord = TRANSFORM_TEX(v.texcoord, _FlareTexture); 54 | o.color = v.color; 55 | 56 | // sample lowest mipmap from occlusion buffer 57 | o.color *= tex2Dlod(_FlareOcclusionTexture, float4(0.5, 0.5, 0, 16)).r; 58 | o.color.a = 1; 59 | return o; 60 | } 61 | 62 | fixed4 frag (v2f i) : SV_Target 63 | { 64 | return tex2D(_FlareTexture, i.texcoord) * i.color; 65 | } 66 | ENDCG 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Occlusion Lens Flare for Unity 2 | 3 | The Occlusion Lens Flare package provides the ability to use Unity's built-in [Lens Flare](https://docs.unity3d.com/Manual/class-LensFlare.html) Component without Physics raycasts. 4 | It was created to be used with Unity's built-in Render Pipeline. 5 | 6 | Please see the following video for a quick introduction, 7 | [![](http://img.youtube.com/vi/zaN5TLIMcFs/0.jpg)](http://www.youtube.com/watch?v=zaN5TLIMcFs "") 8 | 9 | Unity's built-in Lens Flare requires the Physics engine by default, 10 | because it uses a Physics raycast to determine whether the sun is blocked by another object. 11 | Here is the relevant quote from the [documentation](https://docs.unity3d.com/Manual/class-LensFlare.html): 12 | > Lens Flares are blocked by Colliders. A Collider in-between the Flare GameObject and the Camera will hide the Flare, even if the Collider does not have a Mesh Renderer. 13 | 14 | There can be reasons why you might not want to use Unity's Physics engine and in this case, the built-in Lens Flare stops working. 15 | 16 | This Occlusion Lens Flare package does not require Physics, it renders an occlusion texture instead. The occlusion texture contains the sun and any object in-between the sun and camera. The occlusion texture is then used to determine how many pixels are occluded from the sun, to control the Lens Flare opacity. 17 | 18 | This approach comes with its own benefits and drawbacks: 19 | * Benefit: The Occlusion Lens Flare knows more than on/off, it actually knows how much it's occluded and can adjust its opacity accordingly. Unity's Lens Flare only knows if a "single pixel" is occluded or not and tries to compensate the on/off flicker by introducing a fade setting. 20 | * Benefit: It uses Unity's built-in Lens Flare Component to render the Flare, thus you can use all the existing Lens Flare assets that have been created for Unity already and of course create your own assets too. 21 | * Drawback: The Occlusion Lens Flare requires to render an occlusion texture, which increases the number of draw-calls. However, the occlusion view frustum is small and it only renders objects that are inside the frustum. The occlusion texture itself is small as well, 64x64 pixels is sufficient already. Unity's Lens Flare does not add further draw-calls on the other hand. 22 | * Drawback: The Occlusion Lens Flare supports a single Lens Flare only. 23 | * Drawback: The editor Scene View integration does not work. 24 | * Benefit&Drawback: The Occlusion Lens Flare is targeted to lower-end hardware. I created it in particular for mobile, thus it's not an actual simulation of a camera lens flare effect that you'd expect from high fidelity visuals. It's the same level of fidelity as Unity's built-in Lens Flare though. 25 | 26 | # Installation 27 | 28 | As of Unity 2019.3, Unity supports to add packages from git through the Package Manager window. 29 | In Unity's Package Manager, choose "Add package from git URL" and insert one of the Package URL's you can find below. 30 | 31 | ## Package URL's 32 | 33 | | Version | Link | 34 | |----------|---------------| 35 | | 1.1.0 | https://github.com/pschraut/UnityOcclusionLensFlare.git#1.1.0 | 36 | | 1.0.0 | https://github.com/pschraut/UnityOcclusionLensFlare.git#1.0.0 | 37 | 38 | # Credits 39 | 40 | If this package is useful to you, please mention my name in your credits screen. 41 | Something like "Occlusion Lens Flare by Peter Schraut" or "Thanks to Peter Schraut" would be very much appreciated. 42 | 43 | # Honorable mentions 44 | 45 | Thanks to bgolus and KokkuHub from the Unity forums for helping me getting this working. 46 | 47 | You can read the discussion here: https://forum.unity.com/threads/how-to-create-a-lens-flare-effect-without-physics.1053590/ 48 | 49 | # Integration 50 | 51 | ## Override the built-in Lens Flare shader 52 | Open ```Edit > Project Settings...``` and select the ```Graphics``` tab. 53 | Scroll down to the ```Built-in Shader Settings``` section and change the ```Lens Flare``` setting 54 | from ```Built-in shader``` to ```Custom shader```. 55 | 56 | Drag&drop the ```Shaders/OcclusionLensFlare.shader``` asset from the Occlusion Lens Flare package folder in Lens Flare shader setting. 57 | 58 | ![alt text](Documentation~/images/graphics_settings.png "Lens Flare shader setting") 59 | 60 | 61 | # Example 62 | 63 | Once you have overridden the built-in Lens Flare shader as mentioned in the ```Integration``` section, you can either just continue with Sample or follow all further integration steps below. 64 | 65 | The Sample requires Unity's Cinemachine package, but the actual Occlusion Lens Flare does not. 66 | 67 | * Open ```Window > Package Manager``` and install ```Cinemachine``` package from the ```Unity Registry```. 68 | * Import the ```Occlusion Lens Flare Sample.unitypackage``` file found in the ```Occlusion Lens Flare/Samples~``` directory. 69 | * Open ```Occlusion Lens Flare Scene``` and press ```Play```. 70 | 71 | You should see this now: 72 | ![alt text](Documentation~/images/sample.png "Occlusion Lens Flare Sample") 73 | 74 | Hold down the right mouse button and move the mouse to rotate the camera. 75 | When the sun goes behind one of those green boxes, the Lens Flare should fade out. 76 | 77 | # Integration step by step 78 | 79 | In this section I describe how to replicate the Occlusion Lens Flare Sample. 80 | 81 | TODO 82 | 83 | 84 | # How it works 85 | 86 | TODO 87 | 88 | In this video I go into more detail how it actually works. 89 | [![](http://img.youtube.com/vi/d-0M50W1byk/0.jpg)](http://www.youtube.com/watch?v=d-0M50W1byk "") 90 | 91 | -------------------------------------------------------------------------------- /Runtime/OcclusionLensFlare.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Occlusion Lens Flare for Unity. Copyright (c) 2021 Peter Schraut (www.console-dev.de). See LICENSE.md 3 | // https://github.com/pschraut/UnityOcclusionLensFlare 4 | // 5 | using UnityEngine; 6 | 7 | namespace Oddworm.Framework 8 | { 9 | [HelpURL("https://github.com/pschraut/UnityOcclusionLensFlare")] 10 | [RequireComponent(typeof(LensFlare))] 11 | [DefaultExecutionOrder(1000)] 12 | public class OcclusionLensFlare : MonoBehaviour 13 | { 14 | [Tooltip("A reference to the renderer that represents the sun that's rendered into the occlusion texture.")] 15 | [SerializeField] Renderer m_OcclusionSun = default; 16 | 17 | [Tooltip("A reference to the camera that is used to render the occlusion texture.")] 18 | [SerializeField] Camera m_OcclusionCamera = default; 19 | 20 | [Tooltip("A reference to the render texture where the lens flare occlusion is rendered into.")] 21 | [SerializeField] RenderTexture m_OcclusionTexture = default; 22 | 23 | [Tooltip("A reference to the shader that is used to render the occlusion texture.")] 24 | [SerializeField] Shader m_OcclusionShader = default; 25 | 26 | Plane[] m_FrustumPlanes = new Plane[6]; 27 | 28 | public Renderer occlusionSun 29 | { 30 | get => m_OcclusionSun; 31 | set => m_OcclusionSun = value; 32 | } 33 | 34 | public Camera occlusionCamera 35 | { 36 | get => m_OcclusionCamera; 37 | } 38 | 39 | public RenderTexture occlusionTexture 40 | { 41 | get => m_OcclusionTexture; 42 | } 43 | 44 | public Shader occlusionShader 45 | { 46 | get => m_OcclusionShader; 47 | } 48 | 49 | void Start() 50 | { 51 | #if DEVELOPMENT_BUILD || UNITY_EDITOR 52 | if (m_OcclusionSun == null) 53 | Debug.LogError($"The occlusion sun property in '{name}' must not be 'None'.", this); 54 | 55 | if (m_OcclusionCamera == null) 56 | Debug.LogError($"The occlusion camera property in '{name}' must not be 'None'.", this); 57 | 58 | if (m_OcclusionShader == null) 59 | Debug.LogError($"The occlusion shader property in '{name}' must not be 'None'.", this); 60 | 61 | if (m_OcclusionTexture == null) 62 | Debug.LogError($"The occlusion texture property in '{name}' must not be 'None'.", this); 63 | 64 | if (m_OcclusionTexture.mipmapCount == 0) 65 | Debug.LogError($"The {nameof(RenderTexture)} '{m_OcclusionTexture.name}' must use mipmaps to propertly work with the {nameof(OcclusionLensFlare)}", this); 66 | #endif 67 | 68 | m_OcclusionCamera.enabled = false; // Don't let Unity render the camera, we do this in this Component instead 69 | m_OcclusionCamera.backgroundColor = new Color(0, 0, 0, 0); 70 | 71 | Shader.SetGlobalTexture("_FlareOcclusionTexture", m_OcclusionTexture); 72 | } 73 | 74 | void LateUpdate() 75 | { 76 | if (m_OcclusionCamera == null || m_OcclusionTexture == null || m_OcclusionSun == null || m_OcclusionShader == null) 77 | { 78 | ClearOcclusionTexture(); 79 | return; 80 | } 81 | 82 | var camera = Camera.main; 83 | if (camera == null) 84 | { 85 | ClearOcclusionTexture(); 86 | return; 87 | } 88 | 89 | //#if UNITY_EDITOR 90 | // if (!camera.TryGetComponent(out var flareLayer)) 91 | // Debug.LogError($"Camera '{camera.name}' requires the '{nameof(FlareLayer)}' Component."); 92 | //#endif 93 | 94 | var sunBounds = m_OcclusionSun.bounds; 95 | var sunDistance = (sunBounds.center - camera.transform.position).magnitude; 96 | var sunHalfSize = Mathf.Max(sunBounds.extents.x, Mathf.Max(sunBounds.extents.y, sunBounds.extents.z)); 97 | 98 | // If the sun is outside the view frustum of the actual scene, 99 | // don't even try to render the occlusion texture, because the sun is not visible. 100 | GeometryUtility.CalculateFrustumPlanes(camera, m_FrustumPlanes); 101 | if (!GeometryUtility.TestPlanesAABB(m_FrustumPlanes, sunBounds)) 102 | { 103 | ClearOcclusionTexture(); 104 | return; 105 | } 106 | 107 | // Fit sun object into camera view 108 | // https://docs.unity3d.com/Manual/FrustumSizeAtDistance.html 109 | m_OcclusionCamera.nearClipPlane = camera.nearClipPlane; 110 | m_OcclusionCamera.farClipPlane = sunDistance + sunHalfSize; 111 | m_OcclusionCamera.fieldOfView = 2.0f * Mathf.Atan(sunHalfSize / sunDistance) * Mathf.Rad2Deg; 112 | m_OcclusionCamera.transform.position = camera.transform.position; 113 | m_OcclusionCamera.transform.LookAt(m_OcclusionSun.transform, Vector3.up); 114 | 115 | // And finally render the occlusion texture 116 | m_OcclusionTexture.DiscardContents(); 117 | m_OcclusionCamera.targetTexture = m_OcclusionTexture; 118 | m_OcclusionCamera.RenderWithShader(m_OcclusionShader, "RenderType"); 119 | m_OcclusionCamera.targetTexture = null; 120 | } 121 | 122 | void ClearOcclusionTexture() 123 | { 124 | if (m_OcclusionCamera == null || m_OcclusionTexture == null) 125 | return; 126 | 127 | var activeRT = RenderTexture.active; 128 | try 129 | { 130 | m_OcclusionTexture.DiscardContents(); 131 | RenderTexture.active = m_OcclusionTexture; 132 | GL.Clear(true, true, m_OcclusionCamera.backgroundColor); 133 | } 134 | finally 135 | { 136 | RenderTexture.active = activeRT; 137 | } 138 | } 139 | } 140 | } 141 | --------------------------------------------------------------------------------