Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by domportera · Apr 19, 2017 at 01:21 PM · shaderlightingshadersshadowscg

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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"
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image domportera · Apr 20, 2017 at 01:52 AM 0
Share

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

avatar image Namey5 domportera · Apr 20, 2017 at 02:38 AM 0
Share

I've updated the answer with the fixed shader (for some reason it wasn't letting me post it here, now it is).

avatar image domportera Namey5 · Apr 20, 2017 at 03:45 PM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges