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 /
  • Help Room /
avatar image
0
Question by adredengaming · Sep 19, 2019 at 11:50 AM · shader programmingcgdiffuse

How do I fix this diffuse shading flip

I have a frag shader and after a bit of rotation the diffuse light shift from one side to another. My guess is that some value is switching a value from positive to negative in one octrant (I think that is what its called) . I have tried to normalize a number of things but as I am newish to this I a missing something. Perhaps a key search term or a bit of knowledge about a function. I keep researching and learning but so far am unable to figure this out.

I made the shader a bit more extreme to show the difference in the seeming flip of the light direction. Any help or pointers would be greatly appreciated. Thanks

alt text

alt text

 Shader "Unlit/diffuseShader"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _Gloss("Gloss", Range(0,100)) = 1
         _MainTex("Texture", 2D) = "white" {}
     }
         SubShader
     {
         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
     {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag Standard alpha:fade
         #pragma target 3.0
 
         // make fog work
         #pragma multi_compile_fog
 
         #include "UnityCG.cginc"
         #include "Lighting.cginc"
         #include "AutoLight.cginc"
 
         // Mesh data: vertex position, vertex normal, UVs, tangents, vertex colors
         struct VertexInput {
         float4 vertex : POSITION;
         float3 normal : NORMAL;
         float2 uv0 : TEXCOORD0;
     };
 
     struct VertexOutput {
         float4 clipSpacePos : SV_POSITION;
         float2 uv0 : TEXCOORD0;
         float3 normal : TEXCOOD1;
         float3 worldPos : TEXCOORDS2;
     };
 
 
     float4 _Color;
     float _Gloss;
     uniform sampler2D _MainTex;
 
     // Vertex Shader
     VertexOutput vert(VertexInput v) {
         VertexOutput o;
         o.uv0 = v.uv0;
         o.normal = v.normal;
         o.worldPos = mul(unity_ObjectToWorld, v.vertex);
         o.clipSpacePos = UnityObjectToClipPos(v.vertex);
         return o;
     }
 
     float4 frag(VertexOutput o) : SV_Target{
 
         //return _Gloss;
         float2 uv = o.uv0;
         float3 normal = normalize(o.normal);//Interpolated
 
         //direct Diffuse light
         float3 lightDir = _WorldSpaceLightPos0.xyz;
         float3 lightColor = _LightColor0.rgb;
         float lightFalloff = max(0, dot(lightDir, normal)) ;
         float directDiffuseLight = lightColor * lightFalloff;
 
         // Composite
         //float3 diffuseLight = ambientLight + directDiffuseLight;
         float3 diffuseLight = directDiffuseLight;
         float3 finalSurfaceColor = normalize(diffuseLight * _Color.rgb );
 
         return float4(finalSurfaceColor, 1);
     }
         ENDCG
     }
     }
 }
 

capture1.png (52.4 kB)
capture2.png (56.7 kB)
Comment
Add comment · Show 1
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 adredengaming · Sep 19, 2019 at 02:48 AM 0
Share

seems to be something with this

float3 lightDir = _WorldSpaceLightPos0.xyz;

return float4(_WorldSpaceLightPos0.xyz,1); There is an area that cause it to flip

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by adredengaming · Sep 20, 2019 at 12:52 AM

so adding an abs() can work

float3 lightDir = abs(_WorldSpaceLightPos0.xyz);

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

184 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 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 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 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

disabling terrain diffuse lighting in surface shader 0 Answers

Mask Shader Problem in Unity 0 Answers

Shaders are horrible. Why does the shader of one object affect the shader of the other object??? 0 Answers

Does adding more material properties slow down a shader? 0 Answers

Fog not working in my own Vertext Shader 2 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