Skip to content

Instantly share code, notes, and snippets.

@Teqqles
Created August 26, 2016 09:11
Show Gist options
  • Save Teqqles/3e46a6906e13dcb3ee08cd4cb21e66fc to your computer and use it in GitHub Desktop.
Save Teqqles/3e46a6906e13dcb3ee08cd4cb21e66fc to your computer and use it in GitHub Desktop.
Borg Cube Glow
using UnityEngine;
using System.Collections;
public class GlowCycle : MonoBehaviour {
private Material _objMaterial;
private int _shaderPropertyId;
IEnumerator IntensityCycle(float start, float end) {
for (float i = 0f; i <= 1.0f; i += 0.1f) {
_objMaterial.SetFloat (_shaderPropertyId, Mathf.Lerp(start, end, i));
yield return new WaitForSeconds (0.05f);
}
yield return StartCoroutine(IntensityCycle(end,start));
}
void Start () {
_objMaterial = gameObject.GetComponent<Renderer> ().material;
_shaderPropertyId = Shader.PropertyToID ("_Intensity");
StartCoroutine(IntensityCycle(1.0f, 0.8f));
}
}
Shader "Custom/Glow"
{
//this shader is a little long and unoptimized.
Properties
{
_MainTex ("Base (RGB) Glow (A)", 2D) = "white" {}
_Intensity("Glow brightness", Float) = 1
_Color ("Diffuse Material Color", Color) = (1,1,1,1)
}
SubShader {
Tags {"Queue"="Geometry" "RenderMode"="Opaque"}
//standard diffuse lighting pass
Pass {
Name "Diffuse"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
uniform float4 _LightColor0;
uniform float4 _Color;
struct v2f {
float2 uv: TEXCOORD0;
float4 pos : SV_POSITION;
float4 col: COLOR;
};
v2f vert (appdata_base v)
{
v2f o;
float4x4 modelMatrix = unity_ObjectToWorld;
float4x4 modelMatrixInverse = unity_WorldToObject;
float3 normalDirection = normalize(
mul(float4(v.normal, 0.0), modelMatrixInverse).xyz);
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuseReflection = _LightColor0.rgb * _Color.rgb
* max(0.0, dot(normalDirection, lightDirection));
o.col = float4(diffuseReflection, 1.0);
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = mul(UNITY_MATRIX_TEXTURE0, v.texcoord);
return o;
}
half4 frag (v2f i) : COLOR
{
fixed4 text = tex2D(_MainTex, i.uv)*i.col;
text.a = 1;
return text;
}
ENDCG
}
//secondary pass for "glowing" lights
Pass
{
Lighting Off
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
half _Intensity;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
float4 _MainTex_ST;
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
}
half4 frag (v2f input) : COLOR
{
//grab our fragment colour
fixed4 texcol = tex2D(_MainTex, float2(input.uv.x, input.uv.y));
// only calculate glow if we have high opacity on the alpha channel
if ( texcol.a<0.5 ) {
//reset so we can colour the glow
texcol = fixed4(0,0,0,0);
fixed coef=1.0;
fixed neighbourPixelDistance=0;
//modify neighbouring fragments so we can extend our glow beyond the info provided
for (int j = 0; j < 2; j++) {
neighbourPixelDistance++;
coef*=0.32;
texcol += tex2D(_MainTex, float2(input.uv.x, input.uv.y - neighbourPixelDistance * texcol.a)) * coef;
texcol += tex2D(_MainTex, float2(input.uv.x - neighbourPixelDistance * texcol.a, input.uv.y)) * coef;
texcol += tex2D(_MainTex, float2(input.uv.x + neighbourPixelDistance * texcol.a, input.uv.y)) * coef;
texcol += tex2D(_MainTex, float2(input.uv.x, input.uv.y + neighbourPixelDistance * texcol.a)) * coef;
}
}
return texcol*_Intensity;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment