Created
August 8, 2016 09:05
-
-
Save elringus/30ef5f164b537b7bb1e06c92fc12aae4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
namespace UnityStandardAssets.ImageEffects | |
{ | |
[ExecuteInEditMode] | |
[RequireComponent (typeof(Camera))] | |
[AddComponentMenu ("Image Effects/Other/Screen Overlay")] | |
public class ScreenOverlay : PostEffectsBase | |
{ | |
public enum OverlayBlendMode | |
{ | |
Additive = 0, | |
ScreenBlend = 1, | |
Multiply = 2, | |
Overlay = 3, | |
AlphaBlend = 4, | |
} | |
public OverlayBlendMode blendMode = OverlayBlendMode.Overlay; | |
public float intensity = 1.0f; | |
public Texture2D texture = null; | |
public Shader overlayShader = null; | |
private Material overlayMaterial = null; | |
public override bool CheckResources () | |
{ | |
CheckSupport (false); | |
overlayMaterial = CheckShaderAndCreateMaterial (overlayShader, overlayMaterial); | |
if (!isSupported) | |
ReportAutoDisable (); | |
return isSupported; | |
} | |
void OnRenderImage (RenderTexture source, RenderTexture destination) | |
{ | |
if (CheckResources() == false) | |
{ | |
Graphics.Blit (source, destination); | |
return; | |
} | |
Vector4 UV_Transform = new Vector4(1, 0, 0, 1); | |
#if UNITY_WP8 | |
// WP8 has no OS support for rotating screen with device orientation, | |
// so we do those transformations ourselves. | |
if (Screen.orientation == ScreenOrientation.LandscapeLeft) { | |
UV_Transform = new Vector4(0, -1, 1, 0); | |
} | |
if (Screen.orientation == ScreenOrientation.LandscapeRight) { | |
UV_Transform = new Vector4(0, 1, -1, 0); | |
} | |
if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) { | |
UV_Transform = new Vector4(-1, 0, 0, -1); | |
} | |
#endif | |
overlayMaterial.SetVector("_UV_Transform", UV_Transform); | |
overlayMaterial.SetFloat ("_Intensity", intensity); | |
overlayMaterial.SetTexture ("_Overlay", texture); | |
Graphics.Blit (source, destination, overlayMaterial, (int) blendMode); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Hidden/BlendModesOverlay" { | |
Properties { | |
_MainTex ("Screen Blended", 2D) = "" {} | |
_Overlay ("Color", 2D) = "grey" {} | |
} | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
struct v2f { | |
float4 pos : SV_POSITION; | |
float2 uv[2] : TEXCOORD0; | |
}; | |
sampler2D _Overlay; | |
sampler2D _MainTex; | |
half _Intensity; | |
half4 _MainTex_TexelSize; | |
half4 _UV_Transform = half4(1, 0, 0, 1); | |
v2f vert( appdata_img v ) { | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.uv[0] = float2( | |
dot(v.texcoord.xy, _UV_Transform.xy), | |
dot(v.texcoord.xy, _UV_Transform.zw) | |
); | |
#if UNITY_UV_STARTS_AT_TOP | |
if(_MainTex_TexelSize.y<0.0) | |
o.uv[0].y = 1.0-o.uv[0].y; | |
#endif | |
o.uv[1] = v.texcoord.xy; | |
return o; | |
} | |
half4 fragAddSub (v2f i) : SV_Target { | |
half4 toAdd = tex2D(_Overlay, i.uv[0]) * _Intensity; | |
return tex2D(_MainTex, i.uv[1]) + toAdd; | |
} | |
half4 fragMultiply (v2f i) : SV_Target { | |
half4 toBlend = tex2D(_Overlay, i.uv[0]) * _Intensity; | |
return tex2D(_MainTex, i.uv[1]) * toBlend; | |
} | |
half4 fragScreen (v2f i) : SV_Target { | |
half4 toBlend = (tex2D(_Overlay, i.uv[0]) * _Intensity); | |
return 1-(1-toBlend)*(1-(tex2D(_MainTex, i.uv[1]))); | |
} | |
half4 fragOverlay (v2f i) : SV_Target { | |
half4 m = (tex2D(_Overlay, i.uv[0]));// * 255.0; | |
half4 color = (tex2D(_MainTex, i.uv[1]));//* 255.0; | |
// overlay blend mode | |
//color.rgb = (color.rgb/255.0) * (color.rgb + ((2*m.rgb)/( 255.0 )) * (255.0-color.rgb)); | |
//color.rgb /= 255.0; | |
/* | |
if (Target > ½) R = 1 - (1-2x(Target-½)) x (1-Blend) | |
if (Target <= ½) R = (2xTarget) x Blend | |
*/ | |
float3 check = step(half3(0.5,0.5,0.5), color.rgb); | |
float3 result = 0; | |
result = check * (half3(1,1,1) - ( (half3(1,1,1) - 2*(color.rgb-0.5)) * (1-m.rgb))); | |
result += (1-check) * (2*color.rgb) * m.rgb; | |
return half4(lerp(color.rgb, result.rgb, (_Intensity)), color.a); | |
} | |
half4 fragAlphaBlend (v2f i) : SV_Target { | |
half4 toAdd = tex2D(_Overlay, i.uv[0]) ; | |
return lerp(tex2D(_MainTex, i.uv[1]), toAdd, toAdd.a * _Intensity); | |
} | |
ENDCG | |
Subshader { | |
ZTest Always Cull Off ZWrite Off | |
ColorMask RGB | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment fragAddSub | |
ENDCG | |
} | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment fragScreen | |
ENDCG | |
} | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment fragMultiply | |
ENDCG | |
} | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment fragOverlay | |
ENDCG | |
} | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment fragAlphaBlend | |
ENDCG | |
} | |
} | |
Fallback off | |
} // shader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment