Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 dantamont · Aug 08, 2015 at 06:28 PM · shadersspritesshaderlabcg

Why does this code work without any "Pass{}" expressions? And is there a way to add shadow collections?

Hey guys! I don't know much about shaders, but I found this shader on the internet. I turned culling off for both of the passes. My character consists of some sprites that are flipped, and I wanted them all to be visible and to cast shadows on the same side. I only vaguely understand the code. Just enough to get it working for my purposes. So, my question is two-fold:

  1. Why are there no Pass{} expressions? Aren't you supposed to surround each pass with one? Or is that optional?

  2. How do I add shadow collections to the shader? I tried adding this section of code, but it didn't do anything:

    // Pass to render object as a shadow collector Pass { Name "ShadowCollector" Tags { "LightMode" = "ShadowCollector" }

             Cull Off//new 
                Lighting On //new
                 Fog {Mode Off}
                 ZWrite On ZTest LEqual
          
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #pragma fragmentoption ARB_precision_hint_fastest
                 #pragma multi_compile_shadowcollector
          
                 #define SHADOW_COLLECTOR_PASS
                 #include "UnityCG.cginc"
          
                 struct appdata {
                     float4 vertex : POSITION;
                 };
          
                 struct v2f {
                     V2F_SHADOW_COLLECTOR;
                 };
          
                 v2f vert (appdata v)
                 {
                     v2f o;
                     TRANSFER_SHADOW_COLLECTOR(o)
                     return o;
                 }
          
                 fixed4 frag (v2f i) : COLOR
                 {
                     SHADOW_COLLECTOR_FRAGMENT(i)
                 }
                 ENDCG
          
             }
    
     
    
    

Shader "Sprites/Bumped Diffuse both sides with Shadows 2" { Properties { [PerRendererData] _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _Color ("Tint", Color) = (1,1,1,1) [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 _Cutoff ("Alpha Cutoff", Range (0,1)) = 0.5

      }
  
      SubShader
      {
          Tags
          { 
              "Queue"="Transparent" 
              "IgnoreProjector"="True" 
              "RenderType"="TransparentCutOut" 
              "PreviewType"="Plane"
              "CanUseSpriteAtlas"="True"
              
          }
          LOD 300
  
          // Render back faces first
          Cull Off
          Lighting On
          ZWrite Off
          Fog { Mode Off }
          
          CGPROGRAM
          #pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff 
          #pragma multi_compile DUMMY PIXELSNAP_ON 
  
          sampler2D _MainTex;
          sampler2D _BumpMap;
          fixed4 _Color;
          float _ScaleX;
  
          struct Input
          {
              float2 uv_MainTex;
              float2 uv_BumpMap;
              fixed4 color : COLOR;
          };
          
          void vert (inout appdata_full v, out Input o)
          {
              #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
              v.vertex = UnityPixelSnap (v.vertex);
              #endif
              float3 normal = v.normal;
              
              v.normal = float3(0,0,-1);
              v.tangent =  float4(1, 0, 0, 1);
              
              UNITY_INITIALIZE_OUTPUT(Input, o);
              o.color += _Color;
          }
  
          void surf (Input IN, inout SurfaceOutput o)
          {
              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
              o.Albedo = c.rgb;
              o.Alpha = c.a;
              o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
          }
          ENDCG
          
          // Now render front faces first
          Cull Off
          Lighting On
          ZWrite Off
          Fog { Mode Off }
          
          CGPROGRAM
          #pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff 
          #pragma multi_compile DUMMY PIXELSNAP_ON 
  
          sampler2D _MainTex;
          sampler2D _BumpMap;
          fixed4 _Color;
          float _ScaleX;
  
          struct Input
          {
              float2 uv_MainTex;
              float2 uv_BumpMap;
              fixed4 color: COLOR;
          };
          
          void vert (inout appdata_full v, out Input o)
          {
              #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
              v.vertex = UnityPixelSnap (v.vertex);
              #endif
              float3 normal = v.normal;
              
              v.normal = float3(0,0,1);
              v.tangent =  float4(1, 0, 0, 1);
              
              UNITY_INITIALIZE_OUTPUT(Input, o);
              o.color += _Color;
          }
  
          void surf (Input IN, inout SurfaceOutput o)
          {
              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
              o.Albedo = c.rgb;
              o.Alpha = c.a;
              o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
              o.Normal.z *= -1;
          }
          ENDCG
          
 
    
         // Pass to render object as a shadow collector
         Pass {
             Name "ShadowCollector"
             Tags { "LightMode" = "ShadowCollector" }
            
            Cull Off//new 
            Lighting On //new
             Fog {Mode Off}
             ZWrite On ZTest LEqual
      
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma multi_compile_shadowcollector
      
             #define SHADOW_COLLECTOR_PASS
             #include "UnityCG.cginc"
      
             struct appdata {
                 float4 vertex : POSITION;
             };
      
             struct v2f {
                 V2F_SHADOW_COLLECTOR;
             };
      
             v2f vert (appdata v)
             {
                 v2f o;
                 TRANSFER_SHADOW_COLLECTOR(o)
                 return o;
             }
      
             fixed4 frag (v2f i) : COLOR
             {
                 SHADOW_COLLECTOR_FRAGMENT(i)
             }
             ENDCG
      
         }
         
         
          
      }
  
  Fallback "Transparent/Cutout/Diffuse"
  }








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
0

Answer by tanoshimi · Aug 08, 2015 at 08:36 PM

You don't define explicit passes for surface shaders - Unity defines them when it compiles the shader. To add shadows, you simply include the addshadow option to the #pragma that declares the surface shader function - http://docs.unity3d.com/Manual/SL-SurfaceShaders.html.

Comment
Add comment · Show 4 · 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 dantamont · Aug 08, 2015 at 09:16 PM 0
Share

Your link isn't working. And I'm a bit confused, do you mean that "Pass{}" is optional? Also, which #pragma do you mean? The addshadow function is already included as an option in several of the #pragma lines.

avatar image Positive7 · Aug 08, 2015 at 09:21 PM 0
Share

There was a dot at the end of the link. http://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaders.html

avatar image dantamont · Aug 08, 2015 at 09:24 PM 0
Share

Thanks, I will give this a read. Does anybody have a quick answer to the questions I asked? I don't want to have to learn all about shaders to accomplish this one task.

avatar image dantamont · Aug 08, 2015 at 09:27 PM 0
Share

As in, how do I add the function to receive shadows to the shader that I posted?

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

2 People are following this question.

avatar image avatar image

Related Questions

ShaderLab builtin lighting properties are not correct? 0 Answers

Flat Shading / No Vertex Interpolation 2 Answers

Obtaining relative speed of vertex in a shader 1 Answer

Can this shader run in unity? 1 Answer

Weird shader error X5204 - read of uninitialized components 1 Answer


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