Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wheelissa · Feb 29, 2016 at 11:43 AM · shadersshader programmingshadow bugshader-replacement

Vertex and Fragment shader with vertex displacement *and* shadows

Can anyone point me to an example of a vertex and fragment shader that has vertex displacement, and has custom shadow caster and shadow collector passes to cast the shadows from the displaced geometry?

For context I am writing a shader to be used on an entire scene as a replaced shader on the main camera. So the shader needs to be able to both cast and receive shadows as a deformed object.

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
3

Answer by wheelissa · Feb 29, 2016 at 04:48 PM

In case it helps anyone, here is the solution that I created. The vertex displacement is just a translation in this code, but you could apply whatever transformation you like in both the vertex program and the shadow caster pass.

My next question: is it possible to have multiple shadow casting passes in a shader? The shader I want to write has multiple passes, each of which performs a vertex displacement (each pass creates a copy of the geometry translated to a unique location). Then I want to cast a shadow from each of these translated geometries. It seems that this requires multiple shadow casting passes, but when I try this, only one of the shadow casting passes seems to work.

 Shader "Custom/shadowTest" {
     Properties{
         _Color ("Color", Color) = (1,1,1,1)
         [NoScaleOffset] _MainTex("Texture", 2D) = "white" {}
 
     }
         SubShader{
         Tags{ "RenderType" = "Opaque" }
 
         LOD 200
         Cull Off
 
         Pass
     {
         Tags{ "LightMode" = "ForwardBase" }
         CGPROGRAM
         #pragma vertex VertexProgram addshadow
         #pragma fragment FragmentProgram
         #include "UnityCG.cginc"
 
         // shadow stuff:
         #pragma multi_compile_fwdbase
         #include "Lighting.cginc"
         #include "AutoLight.cginc"
 
 
         struct v2f {
             float4 pos : SV_POSITION;
             float4 uv : TEXCOORD0;
             LIGHTING_COORDS(1, 2)
             //SHADOW_COORDS(3)
             fixed3 diff : COLOR0;
             fixed3 ambient : COLOR1;
 
         };
 
         v2f VertexProgram(appdata_full v)
         {
             v2f o;
             
             // find the world coords of the vertex
             float3 u = mul(_Object2World, v.vertex);
             
             // transformation
             //u.y = 0.5*u.y;
             u.y += 5;
 
             // convert to view coordinates, store the position
             v.vertex.xyz = u;
             o.pos = mul(UNITY_MATRIX_VP, v.vertex);
     
             // just pass on the uv texture:
             o.uv = v.texcoord;
 
             // lighting:
             half3 worldNormal = UnityObjectToWorldNormal(v.normal);
             // take dot product between normal and light direction for standard 
             // diffuse (Lambert) lighting
             half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
             // factor in light color:
             o.diff = nl*_LightColor0.rgb;
             // add ambient lighting:
             o.ambient = ShadeSH9(half4(worldNormal, 1.0));
 
             TRANSFER_VERTEX_TO_FRAGMENT(o); 
             
 
             return o;
         }
 
         // Texture to sample, Color
         sampler2D _MainTex;
         fixed4 _Color;
 
         fixed4 FragmentProgram(v2f i) : SV_Target
         {
 
             float atten = LIGHT_ATTENUATION(i);
         
             // Return the relevant color and texture
             fixed4 u = tex2D(_MainTex, i.uv);
 
             // darken light's illumination with shadow, keep ambient intact
             fixed3 lighting = i.diff*atten + i.ambient;
             u.rgb *= lighting;
 
             u *= _Color;
             return u;
 
         }
         ENDCG
 
     }
 
         // Shadowcaster pass modified to use same vertex transformation as original shader
         Pass
         {
             Tags{ "LightMode" = "ShadowCaster" }
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_shadowcaster
             #include "UnityCG.cginc"
 
                 struct v2f {
                     V2F_SHADOW_CASTER;
                 };
 
                 v2f vert(appdata_full v)
                 {
                     v2f o;
                     TRANSFER_SHADOW_CASTER(o) // this has to be BEFORE the transformation!!!
                                     
                     //// find the world coords of the vertex
                     float4 u = mul(_Object2World, v.vertex);
 
                     ////// transformation, use same as above
                     //u.y = 0.5*u.y;
                     u.y += 5;
 
                     //// convert to view coordinates, store the position
                     v.vertex.xyz = u;
                     o.pos = mul(UNITY_MATRIX_VP, v.vertex);
                     
                     return o;
                 }
 
                 float4 frag(v2f i) : SV_Target
                 {
                     SHADOW_CASTER_FRAGMENT(i)
                 }
             ENDCG
         }
 
 
     }
         //FallBack "Diffuse"
 }
 
Comment
Add comment · Show 1 · 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 ttohin · Jan 02, 2018 at 07:11 PM 0
Share

Did you find a solution how to avoid the duplication of transformation code?

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

Nature Shader Help 2 Answers

,Help Changing a "Doom" Style Billboard Shader to a Surface Shader 0 Answers

Trying to make a masking shader and the areas that are supposed to be blank are culling the things behind it. 0 Answers

Render oject A when behind object B, but not if behind object B and object C 1 Answer

How to map letters on mesh with shader? 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