Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Rabbito · Jun 07, 2015 at 05:21 AM · shadershadow

Shaders: Make Mesh partly visible with Shadows

Hi

I am using a Shader to cut all Meshes above a specific Y level. This is working but the Shadows that are generated are wrong. How i can correct this? The left picture shows the problem and in the right picture there is nothing cuttet. (U can Ignore the trees)

alt text

Here is my Shader:

 Shader "Test/Rim Shading Multiple Lights Texture Shadows" {
 Properties{
         _Color ("Color",Color) = (1.0,1.0,1.0,1.0)
         _MainTex("Diffuse Texture",2D) = "white"{}
         _SpecColor("SpecColor",Color) = (1.0,1.0,1.0,1.0)
         _Shininess("Shininess",Float) = 10
         _RimColor ("Rim Color", Color) = (1.0,1.0,1.0,1.0)
         _RimPower("Rim Power",Range (0.1,10.0)) = 3.0
         _LastVisibleY("LastVisibleY", Float) = 50.0
     }
     
     SubShader {
         Pass{
 
         Tags{"LightMode" = "ForwardBase"}
         
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
             #pragma multi_compile_fwdbase
             #include "AutoLight.cginc"
             
             //user defined Variable
             uniform sampler2D _MainTex;
             uniform float4 _MainTex_ST;
             uniform float4 _Color;
             uniform float4 _SpecColor;
             uniform float4 _RimColor;
             uniform float _Shininess;
             uniform float _RimPower;
             
             uniform float _LastVisibleY;
             //unity defined Variables
             uniform float4 _LightColor0;
             
             struct vertexInput{
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
                 
             };
             
             struct vertexOutput{
                 float4 pos : SV_POSITION;
                 float4 tex : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalDir : TEXCOORD2;
                 LIGHTING_COORDS(3,4)
             };
             
         
             
             
             vertexOutput vert(vertexInput v)
             {
                 vertexOutput o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.posWorld = mul(_Object2World, v.vertex);
                 o.normalDir = normalize(mul(float4(v.normal,0.0),_World2Object).xyz);
                 o.tex = v.texcoord;
                 TRANSFER_VERTEX_TO_FRAGMENT(o);
                 return o;
             }
             
             float4 frag(vertexOutput i) : COLOR
             {
             
                 if(i.posWorld.y > _LastVisibleY)
                     discard;
                 float3 normalDirection = i.normalDir;
                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                 float3 lightDirection;
                 float atten;
                 
                 if(_WorldSpaceLightPos0.w == 0.0)
                 {
                     atten = 1.0;
                     lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                 }
                 else
                 {
                     float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - i.posWorld.xyz;
                     float distance = length(fragmentToLightSource);
                     atten = 1/distance;
                     lightDirection = normalize(fragmentToLightSource);
                 }
                 
                 atten = atten * LIGHT_ATTENUATION(i);
                 //lighting
                 float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDirection, lightDirection));
                 float3 specularReflection =atten * _LightColor0.xyz * _SpecColor.rgb * saturate(dot(normalDirection, lightDirection)) *pow( saturate(dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess);
                 
                 //Rim Lighting
                 float rim = 1 - saturate(dot(normalize(viewDirection),normalDirection));
                 float3 rimLighting  = atten * _LightColor0.xyz * _RimColor * saturate(dot(normalDirection, lightDirection)) * pow(rim,_RimPower);
                 float3 lightFinal = rimLighting + diffuseReflection + specularReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;;
                 
                 //float atten2 = LIGHT_ATTENUATION(i)* atten;
                 
                 //Texture
                 float4 tex = tex2D(_MainTex,i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
                 
                 
                 return float4(tex.xyz * lightFinal * _Color,1.0);
                 
                 
             }
             
             
             ENDCG
         
         }
     
         
     }
     FallBack "VertexLit"
 }
 


shadow.png (221.9 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
0
Best Answer

Answer by Rabbito · Jun 07, 2015 at 09:47 AM

I have made it by adding the Shadowcaster Pass into the Shader:

      // Pass to render object as a shadow caster
      Pass {
          Name "ShadowCaster"
          Tags { "LightMode" = "ShadowCaster" }
          
          Fog {Mode Off}
          ZWrite On ZTest Less Cull Off
          Offset 1, 1
  
          CGPROGRAM
          #pragma vertex vert
          #pragma fragment frag
          #pragma multi_compile_shadowcaster
          #pragma fragmentoption ARB_precision_hint_fastest
          #include "UnityCG.cginc"
          uniform float _LastVisibleY;
          struct v2f { 
              V2F_SHADOW_CASTER;
              float4 posWorld : TEXCOORD1;
          };
          
          v2f vert( appdata_base v )
          {
              v2f o;
              o.posWorld = mul(_Object2World, v.vertex);
             // if(o.posWorld.y < _LastVisibleY)
             // {
              TRANSFER_SHADOW_CASTER(o)
             // }
              return o;
          }
          
          float4 frag( v2f i ) : COLOR
          {
              if(i.posWorld.y > _LastVisibleY)
              {
                  discard;
              }
              
              SHADOW_CASTER_FRAGMENT(i)
          }
          ENDCG
  
      }
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

Edge outline & 2D shadow Shader for planar mesh 0 Answers

How to make shader outline without inner shadow? 0 Answers

Make this shader receive and cast shadow? Can anyone help me thanks! 0 Answers

Combine Basic Shadow Shader and Transparency Shader in Unity 0 Answers

Render game like 'Shadow cascade' mode 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