- Home /
Interacting a facing glow with light
I'm trying to get a object to have an inner glow but only where there is light hitting it. I found a shader that can create an inner glow with a surface that doesn't interact with light like this...
and I also found a shader from @Namey5 that has an inner glow and reacts with light but doesn't allow for a surface to be underneath it. I've tried placing it over another sphere and adjusting the cull settings but could not get it working properly.
I'd like to have a shader that combines the option to have an underlying surface of the first one with the ability to interact with light from the second one. It should be similar to the facing layer weight blender has.
I've attached the code for each shader below. Thanks in advance!!
First shader
Shader "Custom/ItemGlow" {
Properties {
_ColorTint("Color Tint", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap("Normal Map", 2D) = "bump" {}
_RimColor("Rim Color", Color) = (1,1,1,1)
_RimPower("Rim Power", Range(1.0, 20.0)) = 3.0
}
SubShader {
Tags { "RenderType"="Opaque" }
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
struct Input {
float4 color : Color;
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
float4 _ColorTint;
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o) {
IN.color = _ColorTint;
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * IN.color;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow(rim, _RimPower);
}
ENDCG
}
FallBack "Diffuse"
}
Second shader
Shader "Custom/Atmosphere" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Size ("Atmosphere Size Multiplier", Range(0,16)) = 4
_Rim ("Fade Power", Range(0,8)) = 4
_Light ("Lighting Power", Range(0,10)) = 1.4
_Ambient ("Ambient Power", Range (0,6)) = 0.8
}
SubShader {
Tags { "RenderType"="Transparent" }
LOD 200
Cull Front
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf NegativeLambert fullforwardshadows alpha:fade
#pragma vertex vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
struct Input {
float3 viewDir;
};
half _Size;
half _Rim;
half _Light;
half _Ambient;
fixed4 _Color;
void vert (inout appdata_full v) {
v.vertex.xyz += v.vertex.xyz * _Size / 10;
v.normal *= -1;
}
half4 LightingNegativeLambert (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
s.Normal = normalize (s.Normal);
half diff = max (0, dot (-s.Normal, lightDir)) * _Light + _Ambient;
half4 c;
c.rgb = (s.Albedo * _LightColor0 * diff) * atten;
c.a = s.Alpha;
return c;
}
void surf (Input IN, inout SurfaceOutput o) {
half rim = 1.0 - saturate (dot (normalize (IN.viewDir), o.Normal));
// Albedo comes from a texture tinted by color
fixed4 c = _Color;
o.Albedo = c.rgb;
o.Alpha = lerp (0, 1, pow (rim, _Rim));
}
ENDCG
}
FallBack "Diffuse"
}
Answer by Namey5 · Oct 05, 2017 at 08:10 AM
My shader was originally made for a different scenario (although I don't remember the specifics). If you want it to look like the first one, you can make a simple modification to make the rim lighting interact with the diffuse lighting.
Shader "Custom/ItemGlow" {
Properties {
_ColorTint("Color Tint", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap("Normal Map", 2D) = "bump" {}
_RimColor("Rim Color", Color) = (1,1,1,1)
_RimPower("Rim Power", Range(1.0, 20.0)) = 3.0
}
SubShader {
Tags { "RenderType"="Opaque" }
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
struct Input {
float4 color : Color;
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
float4 _ColorTint;
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o) {
IN.color = _ColorTint;
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * IN.color;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Albedo += _RimColor.rgb * pow(rim, _RimPower); //This is the line that has changed
}
ENDCG
}
FallBack "Diffuse"
}
If this isn't what you need, I'm happy to rewrite the lighting of mine into a new shader.