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 supermarten · Sep 30, 2016 at 10:26 AM · shadersurfacevertex shaderdisplacement

Vertex displacement shader not updating shadows correctly

I have a vertex displacement shader that bends an object. It doesn't seem to handle light and shadows correctly though, like what it sent to the light data isn't updated correctly after the displacement. This can create invisible "ghosts" that occludes lights and shadows. How do I update it to match the actual model? alt text

Here's my current code:

 Shader "City/Vertex colored animated" {
      Properties {
          _Color ("Color", Color) = (1,1,1,1)
          _MainTex ("Albedo (RGB)", 2D) = "white" {}
          _Smoothness ("Smoothness", Range(0,1)) = 0.5
          _Stretch ("Stretch", Range(-0.5,0.5)) = 0 
          _Bend ("Bend", Range(-0.5,0.5)) = 0 
          _SubmeshOffset ("Submesh Offset", Vector) = (0,0,0,0)
 
      }
      SubShader {
          Tags { 
              "RenderType"="Opaque" 
          }
          LOD 200
          
          CGPROGRAM
          #pragma surface surf Standard vertex:vert addshadow 
          #pragma target 3.0
 
          struct Input {
              float2 uv_MainTex;
              float3 vertexColor; // Vertex color stored here by vert() method
          };
 
          float _Stretch;
          float _Bend;
          float3 _SubmeshOffset;
  
          void vert (inout appdata_full v, out Input o) {
              UNITY_INITIALIZE_OUTPUT(Input,o);
              // Save the Vertex Color in the Input for the surf() method
              o.vertexColor = v.color; 
 
              float3 wpos = v.vertex.xyz;
 
              //vertex displacing
              float y = wpos.y + _SubmeshOffset.y;
              float x = wpos.x + _SubmeshOffset.x;
 
              wpos.y += y * _Stretch - (x * y) * _Bend;
              wpos.x += (wpos.x + _SubmeshOffset.x) * -_Stretch + y * y * _Bend;
              wpos.z += (wpos.z + _SubmeshOffset.z) * -_Stretch;
 
              v.vertex.xyz = wpos.xyz;
          }
  
          sampler2D _MainTex;
          half _Smoothness;
          fixed4 _Color;
  
          void surf (Input IN, inout SurfaceOutputStandard o) {
              // Albedo comes from a texture tinted by color
              fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
               // Combine normal color with the vertex color
              o.Albedo = c.rgb * IN.vertexColor;
              o.Smoothness = _Smoothness;
              o.Alpha = c.a;
 
          }
          ENDCG
     }
 }

untitled-2.png (112.4 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 supermarten · Nov 18, 2016 at 08:59 AM

I solved this by disabling dynamic batching. Seems like a common issue that vertex displacement and dynamic batching doesn't work well together.

Comment
Add comment · Show 2 · 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 Johannski · Dec 10, 2016 at 02:34 PM 1
Share

Just as an addition, to get the shader to calculate the shadows after the vertex displacement, you have to add it to the pragma like in your example code: #pragma surface surf Standard vertex:vert addshadow Here is also a good article on that: http://diary.conewars.com/vertex-displacement-shader/

avatar image ifurkend · Feb 07, 2019 at 07:39 AM 0
Share

Sorry I have to downvote your "best answer" to notice other readers that you had a wrong assumption. Johannski's comment is correct about the "addshadow" in the pragma surface.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add new vertex attributes for a shader,Create vertex attributes 0 Answers

After displacing through shader, mesh is same when accessed through script. 2 Answers

Does dynamic batching and vertex shader work together? 1 Answer

Unity Terrain - shader for custom post-splat? 0 Answers

raycast not working after mesh deformed via shader 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