- Home /
How to add shadows to surface shader?
This is a question I've seen around a lot but the shader I'm using seems to resist any solutions I've tried. It's a sort of echolocation shader that behaves like this, made by jinglebiscuits that I modified and would like to add shadows to.
Here's my shader code. Please note I'm a total beginner when it comes to shaders so I don't understand much yet:
Shader "My/Normal/AlphaPulseShader" {
Properties {
_MainTex ("Base (RGB) Trans (A)", 2D) = "blue" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_EmissionMap ("Emission Map", 2D) = "black" {}
_Color ("Color", Color) = (1, 0, 0, 1)
_PDistance("PulseDistance", Float) = 0
_PFadeDistance("FadeDistance", Float) = 10
_PEdgeSoftness("EdgeSoftness", Float) = 5
_Origin("PulseOrigin", Vector) = (0, 0, 0, 0)
_RimColor("Rim Color", Color) = (1,1,1,1)
_RimPower("Rim Power", Range(0.5, 8.0)) = 1.059702
_RimOn("Rim On", Range(0, 1)) = 0.0
_EmissionMultiplier("Emission Multiplier",Range(-10,10)) = 1.0
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 200
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
//LIGHTING
#include "AutoLight.cginc"
// define finalcolor and vertex programs:
#pragma surface surf Lambert finalcolor:mycolor vertex:myvert// alpha
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv_EmissionMap;
float3 worldPos;
float3 viewDir;
half alpha;
};
fixed4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _EmissionMap;
half _PDistance;
half _PFadeDistance;
half _PEdgeSoftness;
float4 _Origin;
float4 _RimColor;
float _RimPower;
float _RimOn;
float _EmissionMultiplier;
void myvert (inout appdata_full v, out Input data) {
UNITY_INITIALIZE_OUTPUT(Input, data);
}
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {
// set the vertex color alpha to the value calculated:
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
color.a = c.a;
half dis;
half interval;
half origin;
dis = sqrt(pow((IN.worldPos.x - _Origin.x),2) + pow((IN.worldPos.y - _Origin.y), 2) + pow((IN.worldPos.z - _Origin.z), 2));
if(dis >= _PDistance)
{
color.rgb = color.rgb * (1 - saturate(abs(_PDistance - dis)/_PEdgeSoftness));
}
else
{
color.rgb = color.rgb * (1 - saturate(abs(_PDistance - dis)/_PFadeDistance));
}
}
void surf (Input IN, inout SurfaceOutput o) {
// simply copy the corresponding texture element color:
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// o.Alpha = tex2D(_MainTex,IN.uv_MainTex).a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
// half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
if(_RimOn > 0)
{
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow(rim, _RimPower) + _EmissionMultiplier*tex2D(_EmissionMap,IN.uv_EmissionMap);
}
}
ENDCG
}
// Fallback "Diffuse"
Fallback "VertexLit"
}
Anyone have any idea how I could do this?
Answer by Namey5 · Apr 20, 2017 at 12:45 AM
Simple answer: you can't. Transparent objects cannot receive shadows in Unity, that's just how things are. There used to be ways of faking it/getting around this, but they had issues of their own and no longer work. From the looks of it, your shader doesn't have to rely on transparency, so you could simply change the colour of the areas that aren't visible rather than having them transparent.
Here is the shader with support for shadows (and cleaned up a bit);
Shader "My/Normal/AlphaPulseShader" {
Properties{
_MainTex ("Base (RGB) Trans (A)", 2D) = "blue" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_EmissionMap ("Emission Map", 2D) = "black" {}
_Color ("Color", Color) = (1,0,0,1)
_PDistance ("PulseDistance", Float) = 0
_PFadeDistance ("FadeDistance", Float) = 10
_PEdgeSoftness ("EdgeSoftness", Float) = 5
_Origin ("PulseOrigin", Vector) = (0, 0, 0, 0)
_RimColor ("Rim Color", Color) = (1,1,1,1)
_RimPower ("Rim Power", Range(0.5, 8.0)) = 1.059702
[Toggle(USE_RIM)]_RimOn ("Rim On", Int) = 0.0
_EmissionMultiplier ("Emission Multiplier", Range(-10,10)) = 1.0
}
SubShader{
Tags{ "RenderType"="Opaque" }
LOD 200
CGPROGRAM
//LIGHTING
#include "AutoLight.cginc"
// define finalcolor and vertex programs:
#pragma surface surf Lambert finalcolor:mycolor// alpha
#pragma shader_feature USE_RIM
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv_EmissionMap;
float3 worldPos;
float3 viewDir;
half alpha;
};
fixed4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _EmissionMap;
half _PDistance;
half _PFadeDistance;
half _PEdgeSoftness;
float4 _Origin;
float4 _RimColor;
float _RimPower;
float _RimOn;
float _EmissionMultiplier;
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {
// set the vertex color alpha to the value calculated:
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
color.a = c.a;
half dis;
half interval;
half origin;
dis = sqrt (pow ((IN.worldPos.x - _Origin.x),2) + pow ((IN.worldPos.y - _Origin.y), 2) + pow ((IN.worldPos.z - _Origin.z), 2));
if (dis >= _PDistance)
{
color.rgb = color.rgb * (1 - saturate (abs (_PDistance - dis) / _PEdgeSoftness));
}
else
{
color.rgb = color.rgb * (1 - saturate (abs (_PDistance - dis) / _PFadeDistance));
}
}
void surf (Input IN, inout SurfaceOutput o) {
// simply copy the corresponding texture element color:
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// o.Alpha = tex2D(_MainTex,IN.uv_MainTex).a;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
// half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
#ifdef USE_RIM
half rim = 1.0 - saturate (dot (normalize (IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower) + _EmissionMultiplier * tex2D (_EmissionMap, IN.uv_EmissionMap);
#endif
}
ENDCG
}
// Fallback "Diffuse"
Fallback "VertexLit"
}
You're right, my shader doesn't need transparency. Excuse me for being a total novice, but how would I make it so that it's colored black ins$$anonymous$$d of transparent? And after this, how could shadows be enabled for a directional light?
Sorry if I'm asking too much
I've updated the answer with the fixed shader (for some reason it wasn't letting me post it here, now it is).
Amazing!! I can't thank you enough, you've been an enormous help. Do you have any idea why I might be losing emission with this?
Your answer
Follow this Question
Related Questions
Any way to get rid of squares (artifacts) in a semi transparent shadow of a fade shader? 0 Answers
_LightMatrix0 and Directional Light 1 Answer
Shadow sprite rotation regardless of the light source 0 Answers
Spherical Hamonics - ShadeSH9 and Surface Shader issue 0 Answers
Toon shader light culling shadow issue 0 Answers