Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kketg03 · Jul 29, 2020 at 10:52 PM · lightingshaderssurface shaderpoint lightlighting-model

How to fix this point light bug in surface shader?

alt text I've been working on a shader with a custom light model. It works on most objects, but when it comes to flat meshes like planes or terrain it creates this strange effect. I haven't been able to figure out how to fix it.

Here is my code:

  Shader "CelShading/FinalCelSurf"
     {
         Properties
         {
             _MainTex("Albedo (RGB)", 2D) = "white" {}
             _Color("Tint Color", Color) = (1,1,1,1)
             _Antialiasing("Band Smoothing", Float) = 5.0
             _Glossiness("Glossiness/Shininess", Float) = 400
             _BumpMap("Normal/Bump Map", 2D) = "bump" {}
             _Fresnel("Rim", Range(0,1)) = 0.5
             _OutlineSize("Outline Size", Float) = 0.01
             _OutlineColor("Outline Color", Color) = (0,0,0,0)
             _ID("Stencil ID", Int) = 1
             _LightingRamp("LightingRamp", 2D) = "white"{}
         }
     
             SubShader
             {
                 
                 Tags { "RenderType" = "Opaque" }
     
                 
                 Stencil
                 {
                     Ref[_ID]
                     Comp always
                     Pass replace
                     Fail keep
                     ZFail keep
                 }
     
     
                 CGPROGRAM
                 #pragma surface surf Cel fullforwardshadows
     
                 #pragma target 3.0
     
                 sampler2D _MainTex;
                 fixed4 _Color;
                 float _Antialiasing;
                 float _Glossiness;
                 sampler2D _BumpMap;
                 float _Fresnel;
                 sampler2D _LightingRamp;
     
                 half4 LightingCel(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
                 {
                     float3 normal = normalize(s.Normal);
                     float diffuse = dot(normal, lightDir);
                     float delta = fwidth(diffuse) * _Antialiasing;
                     float diffuseSmooth = smoothstep(0, delta, diffuse);
                     //float3 diffuseSmooth = tex2D(_LightingRamp, float2(diffuse * 0.5 + 0.5, 0.5));
                     diffuse = diffuse > 0 ? 1 : 0;
                     float3 halfVec = normalize(lightDir + viewDir);
                     float specular = dot(normal, halfVec);
                     specular = pow(specular * diffuseSmooth, _Glossiness);
                     float specularSmooth = smoothstep(0, 0.01 * _Antialiasing, specular);
                     float rim = 1 - dot(normal, viewDir);
                     rim = rim * diffuse;
                     float fresnelSize = 1 - _Fresnel;
                     float rimSmooth = smoothstep(fresnelSize, fresnelSize * 1.1, rim);
                     half3 nDotL = dot(s.Normal, lightDir);
                     float3 lightVec = _WorldSpaceLightPos0.xyz;
                     float attenuation = 1 / (1+dot(lightVec, lightVec));
                     float3 col = s.Albedo * ((diffuseSmooth + specularSmooth + rimSmooth) * _LightColor0 * attenuation + unity_AmbientSky);
                     return float4(col, s.Alpha);
                 }
     
                 struct Input
                 {
                     float2 uv_MainTex;
                     float uv_BumpMap;
                 };
     
                 void surf(Input IN, inout SurfaceOutput o)
                 {
                     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
                     o.Albedo = c.rgb;
                     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
                 }
     
     
                 ENDCG
                 
                 Pass
                 {
                     Cull Front
                     ZWrite off
                     ZTest on
                     Stencil
                     {
                         Ref[_ID]
                         Comp notequal
                     Fail keep
                     Pass replace
                     }
     
                     CGPROGRAM
                     #pragma vertex vert
                     #pragma fragment frag
     
                     #include "UnityCG.cginc"
     
                     float _OutlineSize;
                     float4 _OutlineColor;
     
                     struct appdata
                     {
                         float4 vertex : POSITION;
                         float3 normal : NORMAL;
                     };
                     struct v2f
                     {
                         float4 vertex : SV_POSITION;
                     };
     
                     v2f vert(appdata v)
                     {
                         v2f o;
                         float3 normal = normalize(v.normal) * _OutlineSize;
                         float3 pos = v.vertex + normal;
     
                         o.vertex = UnityObjectToClipPos(pos);
     
                         return o;
                     }
     
                     float4 frag(v2f i) : SV_Target
                     {
                         return _OutlineColor;
                     }
     
     
                     ENDCG
                 }
             }
     
                 FallBack "Diffuse"
     }


lightyikes.png (20.4 kB)
Comment
Add comment · Show 7
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 Namey5 · Jul 30, 2020 at 09:28 AM 0
Share

What exactly is the purpose of;

 float3 lightVec = _WorldSpaceLightPos0.xyz;
 float attenuation = 1 / (1+dot(lightVec, lightVec));

For point and spot lights '_WorldSpaceLightPos0' is the world-space position, and as such 'dot(lightVec, lightVec)' returns the square distance of the light from the centre of the world.

avatar image kketg03 Namey5 · Jul 30, 2020 at 08:41 PM 0
Share

That was just my attempt to fix the issue with manual attenuation. Before adding those to the final color value the light was completely white over the terrain, and with it it looks more like actual light. I just never was able to figure out how to fix the weird square issue.

avatar image Namey5 kketg03 · Jul 30, 2020 at 10:05 PM 0
Share

The squares are the actual light volumes that are supposed to be masked by the attenuation. Is there a reason you can't just use the 'atten' input? That is calculated properly for every light type, and your attenuation basically just compiles to '1 / 2' for directional lights, hence why it's less intense.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Namey5 · Jul 31, 2020 at 08:33 AM

Replace it with atten and remove the ambient lighting calculation - Unity does this for you and doing it in a lighting function is incorrect;

 float3 col = s.Albedo * ((diffuseSmooth + specularSmooth + rimSmooth) * _LightColor0 * atten);
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 kketg03 · Jul 31, 2020 at 08:57 AM 0
Share

I can't believe it was that simple lmao. Thanks so much

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

182 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

Related Questions

How to edit the new particle surface shader? / Make it so particles receive light from all angles 0 Answers

Lighting works for iPad but not iPhone? 0 Answers

Lighting support in Unlit shader 0 Answers

How to write unlit surface shader? 6 Answers

Point light too bright in single-color shader within range of light 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