- Home /
Surface shader: modify the emission with lighting?
Not sure if this is possible, but what I'm trying to do is create a surface shader that uses the lighting model to modify the emissions. The reason to do this is to create a day/night planet shader with lights appearing only on the night side. The lights would be defined in a texture, and would only appear on the 'dark' side of the planet (based on light direction).
Within the surf function, I'm setting the emission as so:
o.Emission = tex2D(_Lights,IN.uv_Lights.xy);
I would then like to multiply this by the inverse of the light intensity. However, light information (lightDir) is only available in the lighting model, and changes made to the EditorSurfaceOutput there seem to have no effect (not that I'd expect them to).
Is what I'm trying to do possible? Any suggestions on how to go about it? Thanks for any help.
The full shader is below.
//////////////////////////////////////////////////////////////////////////////
Shader "TestEarthSimple"
{
Properties
{
_MainTex("_MainTex", 2D) = "black" {}
_Lights("_Lights", 2D) = "black" {}
}
SubShader
{
Tags
{
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
CGPROGRAM
#pragma surface surf BlinnPhongEditor
#pragma target 2.0
sampler2D _MainTex;
sampler2D _Lights;
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half4 Custom;
};
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;
}
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot ( lightDir, s.Normal ));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0);
half4 res;
res.rgb = _LightColor0.rgb * diff;
res.w = spec * Luminance (_LightColor0.rgb);
res *= atten * 2.0;
return LightingBlinnPhongEditor_PrePass( s, res );
}
struct Input {
float2 uv_MainTex;
float2 uv_Lights;
};
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Normal = float3(0.0,0.0,1.0);
o.Alpha = 1.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Custom = 0.0;
float4 Sampled2D2=tex2D(_MainTex,IN.uv_MainTex.xy);
float4 Sampled2D1=tex2D(_Lights,IN.uv_Lights.xy);
o.Albedo = Sampled2D2;
o.Emission = Sampled2D1;
}
ENDCG
}
Fallback "Diffuse"
}
Sorry to dredge up such an old post, but do you still have the shader you came up with? I really need something similar to will allow emissives to glow only when in on dark side of a planet. I've tried some of the planet prefads but they are way too low resolution compared to my planet so I need something I can apply to my own, high detailed model.
Yeah, actually - I posted it to the wiki all those years ago:
http://wiki.unity3d.com/index.php/Earth/Planet
Note that it's a bit behind the times at this point - it's a lot less functional than the new standard shader stuff.
Answer by ScroodgeM · Jul 14, 2012 at 01:59 PM
- don't write to o.Emission any colors you want to control by lights
write light color to some field (Custom)
replace
s.Albedo * light.rgbwith
lerp(s.Custom, s.Albedo, * light.rgb)this will make more intensity from 'Custom' instead of 'Albedo' on dark side
Thanks @Scroodge$$anonymous$$. I had actually come to the same conclusion you suggested and just didn't close my question. I'd be curious to see the shader you're using - there's some nice atmospheric effects and interesting water specularity. I posted the shader I cam up with to the wiki: http://unifycommunity.com/wiki/index.php?title=Earth/Planet
atmospheric effects is custom lighting model with special effect implementation on light and view in opposite direction and additive shader pass to draw clouds and atmosphere light over the planet surface. specular is regular specular method from simple specular shader, just used specular map where water only have it.
i can't publish sources cause it's restricted, but can point right direction how to get such effect if you need....
Answer by HannibalLicious · Nov 05, 2018 at 05:03 PM
Hey, I'm trying to make a similar shader which will make cutout textures appear in the dark (basically glow-in-the-dark tattoos), however, since I am very new to coding in general, I have no idea how to make it. Your shader seems to work in a similar way but doesn't use any additional textures which could serve as tattoos in my idea, would you mind helping me/sharing your code with me so we can figure out how it might work?