Oct
16
2012

Fake Glow Effect Shader

Interesting fake glow shader for unity indie (its based on unity builtin shaders).
(Right image has some linear fog enabled)

Features:
– Adjustable glow range, glow color, glow alpha
– Only works well on spheres..

Problems:
– Currently it only works when viewed from a certain axis direction..
– Not affected by lights yet..

Webplayer:
coming later..

Download source:
coming later..

*Earth map by NASA: http://eoimages.gsfc.nasa.gov/images/imagerecords/57000/57752/land_shallow_topo_2048.jpg

Image#2: Bigger glow (planet texture from: http://commons.wikimedia.org/wiki/File:Libya_Montes.jpg )


Shader "UnityCoder/GradientGlow2" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
//_Outline ("Outline width", Range (.002, 0.03)) = .005
_Outline ("Outline width", Range (.002, 0.5)) = .005
_MainTex ("Base (RGB)", 2D) = "white" { }
//        _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
}

CGINCLUDE
// Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members vpos)
#pragma exclude_renderers d3d11 xbox360
// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members vpos)
#pragma exclude_renderers xbox360
#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};

struct v2f {
float4 pos : POSITION;
float3 vpos;
float4 color : COLOR;
};

uniform float _Outline;
uniform float4 _OutlineColor;

v2f vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);

o.pos.xy += offset * o.pos.z * _Outline;
//        o.pos.xyz += v.normal*_Outline;
o.vpos = v.vertex.xyz;
o.color = _OutlineColor;
return o;
}
ENDCG



SubShader {
Tags { "RenderType"="Opaque" }
//Tags { "RenderType"="Transparent" }

// you can replace this pass with something else (rendering the globe)
UsePass "UnityCoder/DefaultDiffuse2/BASE"


Pass {
Name "OUTLINE"
Tags { "LightMode" = "Always" }
Cull Front
ZWrite On
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

half4 frag(v2f i) :COLOR
{
float3 gradientCenter = float3(0,0,0);
float3 pos = normalize(i.vpos.xyz-gradientCenter.xyz);
float4 c =  float4(i.color.rgb,pos.z*i.color.a);
return c;


}
ENDCG
}
}
Fallback "Toon/Basic"
}


Shader "UnityCoder/DefaultDiffuse2" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
//_ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
}


SubShader {
Tags { "RenderType"="Opaque" }
Pass {
Name "BASE"
Cull Off

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

sampler2D _MainTex;
//            samplerCUBE _ToonShade;
float4 _MainTex_ST;
float4 _Color;

struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};

struct v2f {
float4 pos : POSITION;
float2 texcoord : TEXCOORD0;
//                float3 cubenormal : TEXCOORD1;
};

v2f vert (appdata v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
//                o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
return o;
}

float4 frag (v2f i) : COLOR
{
float4 col = _Color * tex2D(_MainTex, i.texcoord);
//                float4 cube = texCUBE(_ToonShade, i.cubenormal);
//                return float4(2.0f * cube.rgb * col.rgb, col.a);
return float4(col.rgb, col.a);
}
ENDCG
}
}


Fallback "VertexLit"
}


9 Comments + Add Comment

  • Nice work!
    I would totally want to use this shader!

  • I really need this shader , i dont care if it has problems please post the source code or email it to me. Thank you i hope its not too much trouble.

  • shader sources added, but it doesn’t really work well.. (create 2 shader files for those, assign the “GradientGlow2” shader to the object)

  • Thanks for your effort. Highly appreciated. I would like to shoot couple of questions.

    1. Is this shader optimized for mobile platforms? I have created few shader codes but it seems to act weird in mobile devices.
    2. Am trying to create a glow effects in my space ship model. It is for a mobile platform.

    • 1. Not optimized for anything.. its based on toon shaders, so the glow is actually outline..
      2. Is that 2D or full 3D?

  • Hey there,
    I get wo errors:
    Shader error in ‘UnityCoder/GradientGlow2’: ‘vert’: function return value missing semantics at line 32
    Shader error in ‘UnityCoder/GradientGlow2’: ‘frag’: input parameter ‘i’ missing semantics at line 75

    • Disable dx11 mode or add something like UNITY_INITIALIZE_OUTPUT(Input,o); to the vert()

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.