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
2
Question by dreadofmondays · Apr 19, 2016 at 06:51 PM · shadershader programminguv mappingnormal mapworld space

Incorrect lighting projecting normal map downwards in world space

Hey everyone, I'm new to writing shaders but I've been trying to create a raindrops shader for the roads and surfaces in my game. It uses a couple of textures to animate the display of a bump texture that is meant to affect the normals of the road. Because it's rain, I want to project the normal map downwards in world space so that it is always even and disregards the scale and orientation of the UVs on the object it hits.

The core of my issue: I simply want to project a normal map downwards in world space and still have it reflect light correctly. I've looked at this question but I think my situation is slightly different.

This is the current response of the pure normal map: http://puu.sh/oog7h/9228a864cf.gif The map currently disregards orientation of the object's UVs, which is good, but as you can see the light response is incorrect.

The surf function code is here (and the entire shader code here (with some recent changes)):

 void surf (Input IN, inout SurfaceOutputStandard o) {
     half2 uv = IN.uv_MainTex;
     uv.x = clamp(uv.x,0,1);fixed4 c = tex2D (_MainTex, uv);
     fixed4 m1 = tex2D (_MetallicDry, IN.uv_MainTex);
     fixed4 m2 = tex2D (_MetallicWet, IN.uv_MainTex);
     fixed4 n2 = tex2D (_Ripple, IN.worldPos.xz); 
     //using worldPos.xz to project the ripple normal map downwards
     fixed4 no = tex2D (_RippleOffset, IN.worldPos.xz);
     
     o.Albedo = c.rgb;
     o.Metallic = 0;
     o.Smoothness = lerp(_SmoothnessDry,_SmoothnessWet,_Wetness);
     o.Normal = UnpackNormal (n);
     //water stuff
     float fade = 10 - distance(_WorldSpaceCameraPos, IN.worldPos)/10; //distance culling of the effect
     if (_Rain > 0 && fade > 0) {
         float3 ripple = UnpackNormal(n2); //normal map for the raindrops, this is animated below
         //make animation value
         //the +0.01 just clips away the background of the channel
         float repeat = frac(-_Time.y+no.rgb)+0.01; 
         //this applies an offset to the second layer of the ripple
         float repeat2 = frac(-_Time.y+no.rgb+0.2)+0.01; 
         //first step is the lower bound, second step is the upper bound. *_Rain effects the intensity. 
         //There are two equations ()+() because there are two ripples
         fixed l = ((step(repeat,n2.b)*(1-step(repeat*1.3,n2.b))) + (step(repeat2,n2.b)*(1-step(repeat2*2,n2.b)))) * _Rain * n2.b * fade;
         //so now L will be 1 where there is part of a water droplet and 0 otherwise
         //"refract" on the droplets by pushing the uvs of the surface around where they are covered
         c = tex2D (_MainTex, lerp(IN.uv_MainTex,IN.uv_MainTex + (IN.viewDir.xy * -.03),l)) * _Color;
         o.Normal = lerp(o.Normal,worldToTangentMatrix(IN.vnormal, IN.vtangent, ripple),1); 
         //display road bump map where L = 0 and raindrop bumpmap where L is 1. Constant 1 here instead of L for testing purposes
     }
 }

I'm certain the maths behind the worldToTangent transform are correct, because I used them in another shader. However i'm not sure if that transform is appropriate here (I'm new to shaders). It looks like I'm almost there but i'm just not sure how to correct the response of the map. Thanks for any help !

Comment
Add comment · Show 2
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 dreadofmondays · Apr 27, 2016 at 02:47 PM 0
Share

Bump. I haven't been able to make any headway on this. Any help is appreciated!

avatar image Caronte3D · May 24, 2017 at 08:45 AM 0
Share

Hi @dreadofmondays Have you found a solution for this issue?

0 Replies

· Add your reply
  • Sort: 

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

Why is my UV'd texture displaying wrapped with the wrong scale? 0 Answers

Overlaying a Darkening Texture over Certain UVs with a Shader 0 Answers

Layering multiple Normal maps into one without blending in a Surface shader script 0 Answers

UnityObjectToClipPos is inverted? 0 Answers

How to add metallic and smoothness to a frag 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