Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Scarzzurs · Nov 21, 2014 at 12:22 PM · shadershadowssurfacegradient

Error in shadow received by vertex displacement shader

I am currently working on small game project with a friend but seem to have run into a problem I am unable to fix regarding one of our shaders and shadows in Unity. The shader itself is quite simply and basically performs vertex displacement to simulate wind and alphatest in order to simulate cut outs in surfaces. The problem is this:

1) The shadows casted by the displaced vertices are not affected by the displacement by default.

2) I have solved this by adding the addshadows keyword to the surface shader (end of #pragma line).

3) Adding the addshadows keyword causes a "gradient-blinds" like error in shadows received.

4) Switching focus between programs randomly seem to fix/reintroduce the issue for all objects - even in builds!

Notes: We are currently using Forward rendering only and all shadows are realtime.

The inconsistent behaviour in 4) above is puzzling to me but hints at it being ordering issue of some kind - a order that is changed every time the unity program receives/loses focus.

Same view with no code changes. Unity is paused. Focus is switching between programs only. Left: Error. Right: No Error

Shader source:

 Shader "Custom/WindIllumCutout" {
     Properties {
         _Color ("Color", Color ) = ( 1,1,1,1 )
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _Direction ("Direction", Vector ) = ( 0,0,0,0 )
         _Speed ("Speed", Float ) = 1.0
         _WaveLength ("Wave Length", Float ) = 1.0
         
         _Illum ("Illumin (A)", 2D) = "white" {}
         _Emission   ("Emission", Range(0,1)) = 0
         _EmissionLM ("Emission (Lightmapper)", Float) = 0
         
         _WindLike ("Wind Like", Range(0,1)) = 1.0
         
         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
     }
     SubShader {
         Tags {
             "Queue"="AlphaTest" 
             "IgnoreProjector"="True" 
             "RenderType"="TransparentCutout"
         }
         Blend One OneMinusSrcAlpha
         //AlphaTest Greater .01
         
         Lod 200
         
         CGPROGRAM
         #pragma surface surf Lambert vertex:vert alphatest:_Cutoff addshadow
         #include "TerrainEngine.cginc"
         
         sampler2D _MainTex;
         float4 _Color, _Direction;
         float _Speed, _WaveLength;
         sampler2D _Illum;
         float _Emission;
         float _WindIntensity = 1;
         float _WindLike;
         
         struct Input {
             float2 uv_MainTex;
             float2 uv_Illum;
             float4 color : COLOR;
         };
         
         void vert( inout appdata_full v ) 
         {
             float intensity = lerp( 1, _WindIntensity, _WindLike );
             float3 wpos = mul(_Object2World, v.vertex ).xyz * _WaveLength;
             float4 t = _Time.xxxx * _Speed * float4( 1.0f, 0.333f, 0.111f, 0.07333f );
             float4 p = SmoothTriangleWave( t + wpos.xyzz );
             float4 o = mul(_World2Object, _Direction * p * v.color.r );
             v.vertex.xyz += o.xyz * intensity;
         }
                 
         void surf( Input IN, inout SurfaceOutput o ) 
         {
             half4 mainColor = tex2D(_MainTex, IN.uv_MainTex );
             half illuminationAlpha = tex2D(_Illum, IN.uv_Illum).a;
             
             half4 c = mainColor * _Color;
             
             o.Emission = c.rgb * illuminationAlpha * _Emission;
             o.Alpha = c.a * IN.color.a;
             o.Albedo = c.rgb * o.Alpha;
         }
         ENDCG
     } 
     FallBack "Transparent/Cutout/Diffuse"
 }

I am currently stuck on this, so all comments and feedback is much appreciated!

shadowcomparison.png (173.3 kB)
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 Scarzzurs · Dec 08, 2014 at 02:42 PM

Yah! Problem solved!

An old colleague of mine suggested I tried to implement the shadow caster and collector passes on my own. When this gave exactly the same results as using addshadows I decided to remove the displacement altogether. This still caused the problem and thus I found that it had to be something really simple that was causing it.

Ultimately I found that the problem was caused by the line:

  Blend One OneMinusSrcAlpha

So basically, adding a blend rule caused the problem and thus removing it solved the problem!

Comment
Add comment · 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

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

Why the shader responds differently to Tri-color ambient light vs. Skybox lighting? 0 Answers

World/screen space gradient overlay in surface shader 1 Answer

Adding Shadows to a Shader 1 Answer

Is there a way to have a transparent vertex shader recieve and cast Shadows? 0 Answers

Vertex program max instructions limit 2 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