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 /
avatar image
0
Question by ROMgame · Mar 16, 2016 at 02:44 AM · shadermeshshaderlabdistortion

Shader mesh not being effected by directional lights?

I've written a simple shader which takes two heightmaps and distorts a flat plane mesh accordingly. You can find this below:

    Shader "Extrusion" {
     Properties {
       _WaveTex ("Texture", 2D) = "white" {}
       _HeightTex ("Texture", 2D) = "white" {}
       _Amount ("Height Extrusion Amount", Range(-1,1)) = 0.5
       _WaveAmount ("Wave Extrusion Amount", Range(-1,1)) = 0.5
     }
     SubShader {
       Tags { "RenderType" = "Opaque" }
 
       CGPROGRAM
       #pragma surface surf BlinnPhong addshadow fullforwardshadows vertex:vert nolightmap
       struct Input {
           float2 uv_HeightTex;
       };
 
       sampler2D _WaveTex;
       sampler2D _HeightTex;
        sampler2D _OutputTex;
 
       float _Amount;
       float _WaveAmount;
 
       void vert (inout appdata_full v) {
          float4 waveTex = tex2Dlod (_WaveTex, float4(v.texcoord.xy,0,0));
          float4 heightTex = tex2Dlod (_HeightTex, float4(v.texcoord.xy,0,0));
 
         v.vertex.y += ((waveTex.b * _WaveAmount) + (heightTex.b * _Amount)) / 2.0;      
            }
 
       void surf (Input IN, inout SurfaceOutput o) {
           o.Albedo = 1.0;
       }
       ENDCG
     }
     Fallback "Diffuse"
   }

The weird thing is that it doesn't seem to interact correctly with directional lights. The entire distorted plane is a uniform single colour - even in areas where you'd expect the light to be much darker such as cracks and crevices. This isn't the case when a point light is placed near to the plane, then it is shaded as you'd expect.

Erroneous uniform colour (directional light): alt text

Interestingly, the plane is definitely receiving the directional light because if I rotate it around then the lighting changes - it's just always a completely uniform colour!

Any help would be greatly appreciated - I'm really tearing my hair out over it!

problem.png (68.6 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 Eno-Khaon · Mar 16, 2016 at 08:26 AM

You're modifying the vertex positions on the mesh, but you aren't making the same adjustments for the normals.

If you want to generate appropriate lighting in the vertex shader to work with the vertex positions, you'll need some more texture samples.

 // 
 //     Y
 //     |
 //     O---X
 // 

Consider the "O" to be the pixel you're currently sampling on your texture. The "X", then, is the neighboring pixel at float4(v.texcoord.x + _WaveHeight_TexelSize.x, v.texcoord.y, 0, 0). The "Y" is the same, adjusted accordingly. http://forum.unity3d.com/threads/_maintex_texelsize-whats-the-meaning.110278/

Once you've established your variations of waveTex, waveTexPlusX and waveTexPlusY (and heightTex, by association), you take the cross product of them to get a new normal.

 // Names shortened for simplicity (waveTex -> wt)
 float3 newNormal = normalize(cross(wtpY - wt, wtpX - wt));

Hope this gets you started in the right direction!

Comment
Add comment · Show 5 · 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 ROMgame · Mar 16, 2016 at 01:06 PM 0
Share

Thanks for the reply! $$anonymous$$uch appreciated, great explanation too, though I can't quite get it working. So you're saying my code block would now be (ignoring the waves for now):

          float4 heightTexPlusX = tex2Dlod( _HeightTex, float4(v.texcoord.x + _HeightTex_TexelSize.x, v.texcoord.y, 0, 0) );
          float4 heightTexPlusY = tex2Dlod( _HeightTex, float4(v.texcoord.x, v.texcoord.y + _HeightTex_TexelSize.y, 0, 0) );
 
          float3 hNormal = normalize(cross( heightTexPlusY-heightTex, heightTexPlusX-heightTex));
          v.normal = hNormal;

Because the resultant shading it generates is a little funky strangely enough!

avatar image Eno-Khaon ROMgame · Mar 16, 2016 at 05:07 PM 0
Share

Well, admittedly, my example would be a little bit off. If you intend to provide further accuracy to the normals, you'll want a texture sample from every side rather than just from two, so you'd wind up with 5 texture samples per pixel (center one for its own color, if applicable, and then cross((y+1 - y-1), (x+1 - x-1)) for the normals).

The more accuracy you want, the more expensive it becomes, since 3/6 texture lookups is plenty less expensive than 5/10 (using one or two interpolated normals from them).

avatar image ROMgame Eno-Khaon · Mar 16, 2016 at 05:43 PM 0
Share

I think - aside from the inaccuracy - there's a problem with the logic (perhaps my misunderstanding?). The shading that it generates is very wrong - so with the Unity "default-particle" used as the input for example it generates the following image (A single directional light is placed directly above): alt text

To me it looks like half of the normals face "up" and half of them face "down"? Very peculiar because I'm pretty sure I understand the logic you proposed! Is there anything obvious I've done wrong per chance? Thanks again for the support by the way!

shape.png (88.9 kB)
Show more comments
Show more comments

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

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best Way to Implement an Exaggerated Horizon Curve to 3D Endless Runner? 0 Answers

InnerGlow Shader for Polygons 0 Answers

Simulating horizon curvature 1 Answer

How to determine shader center? 0 Answers

Vertex Shader and Non-uniformly scaled meshes 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