Author Topic: The Glow Shader  (Read 5391 times)

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 8229
    • View Profile
    • Kronnect
The Glow Shader
« on: November 16, 2015, 07:16:16 PM »
In World Political Map Globe Edition we created a custom shader for the Glow effect.
You can get an idea of what we wanted to achieve in this video: https://youtu.be/dR5ed6BKmUI

There're several reasons which motivated us to create a new glow shader (and not use the standard light halo):

- Avoid the distortion of the standard halo effect under sharp camera angles or changes in scale.
- Better contrast, gradient size and light falloff control
- Improve the halo effect when zooming in/out
- Align the halo with the day/light effect of the Earth (that's another custom shader)

Another good feature of this shader is that all logic is implemented in the vertex shader, leaving a basic fragment shader, which makes it quite performant.

Below is the glow shader included in the asset for your perusal. You can use this code in your projects under BSD license*.
To add this glow effect, create a new material, assign this shader and the material created to a different sphere game object (which usually is about 1.2-1.5x the size of the planet sphere).

If you're interested in knowing what else is included in World Political Map Globe Edition, including other shaders, check out the product documentation (PDF) available in this board or browse demos and videos here: http://kronnect.me/taptapgo/index.php?board=6.0


Code
Shader "World Political Map/Unlit Earth Glow" {

Properties {
_GlowColor("Glow Color", Color) = (0.4, 0.3, 0.9, 1)
_GlowIntensity("Glow Intensity", Range(0, 5)) = 2
_GlowGrow("Glow Grow", Range(0, 2)) = 1
_GlowStep("Glow Brightness Step", Range(0,1)) = 1
_GlowFallOff("Glow FallOff", Range(0, 1)) = 1
_GlowDistanceFallOff("Distance Fall Off", Range(0, 1)) = 0.5
_SunLightDirection("Sun Light Direction", Vector) = (0,0,1)
}

Subshader {
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
Lighting Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Front
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest

#include "UnityCG.cginc"

fixed3 _GlowColor;
fixed _GlowGrow, _GlowStep, _GlowIntensity, _GlowDistanceFallOff, _GlowFallOff;
float3 _SunLightDirection;

struct v2f {
float4 pos  : SV_POSITION;
fixed4 color: COLOR0;
};
       
v2f vert (appdata_tan v) {
TANGENT_SPACE_ROTATION;
v2f o;

const float epsilon = 0.00001;
v.vertex    *= _GlowGrow + epsilon;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

float3 n  = mul( _Object2World, float4( v.normal, 0.0 ) ).xyz;
float atten         = saturate (dot(normalize(_SunLightDirection), n));

float3 worldPos = mul( _Object2World, float4(0,0,0,1)).xyz;
float d2 = distance(_WorldSpaceCameraPos, worldPos);
float dist = 1+_GlowDistanceFallOff/d2;

float3 viewDir = normalize(mul(rotation, ObjSpaceViewDir(v.vertex*atten)));
float3 normal = normalize(mul(rotation, -v.normal));
float d1 = 1-dot(viewDir, normal) / (_GlowFallOff * dist);
d1    /= dist;

float glowStrength  = _GlowStep -saturate(d1*_GlowStep);
glowStrength    /= d2;
fixed3 color = lerp(_GlowColor, 1, _GlowStep - d1);
o.color     = saturate (fixed4(color, glowStrength) * (_GlowIntensity + glowStrength));
return o;
}

fixed4 frag (v2f i) : COLOR {
return i.color;
}

ENDCG
}
}
}


Copyright Kronnect Games. All Rights Reserved.

Redistribution and use in source and binary forms of shader above, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY KRONNECT GAMES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
« Last Edit: November 24, 2015, 07:22:15 AM by Kronnect »

Azaphrael

  • Customers
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: The Glow Shader
« Reply #1 on: November 17, 2015, 09:16:50 AM »
Just wanted to tell you that I love it.

The only struggle I have is trying to place in a way so that it seems directly centered behind the globe.
Ah well. One of these days I'll get it right.  ;D

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 8229
    • View Profile
    • Kronnect
Re: The Glow Shader
« Reply #2 on: November 17, 2015, 09:24:27 AM »
Thank you very much.

You should be able to do that setting the Sun Light Direction to either (0,0,1) or (0,0,-1) and tweaking the other params.

john.connor

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: The Glow Shader
« Reply #3 on: November 24, 2015, 11:27:12 AM »
Updated and *Wow*. I did purchase Globe Edition and I'm love with it. Latest update is gorgeous!