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 Runewake · Feb 16, 2017 at 11:32 PM · shaderlighting buggeometry shader

_WorldSpaceLightPos0 Giving Inconsistent Results in Different Rendering Paths

I am developing a low poly shader to explore geometry shaders. Since geometry shaders are not currently supported in Surface Shaders I have had to write my own lighting. I began with diffuse lighting using _WorldSpaceLightPos0 to retrieve the current direction of my light (I'm using a directional light). I am getting inconsistent results. In forward and deferred rendering the result is similar (and incorrect). The _WorldSpaceLightPos0 variable switches between two different values depending on the position of my Main Camera. However, if I switch to the Legacy Vertex Lit render path my shader renders correctly.

How do I get consistent behavior using the _WorldSpaceLightPos0 variable? According to the Unity documentation this will contain the direction of the light (there is only a single, directional light in my scene). Why does this work perfectly in the Legacy Rendering path but fails in the up to date Forward and Deferred renderers?

When I modify my shader to draw the _WorldSpaceLightPos0 this is the result I get. Note how it switches between two colors as the camera moves in and out, this is the world space direction of the light changing. It only ever switches between these two values. Green is the correct light position, in legacy vertex lit rendering I only get that result:

Incorrect Light Direction in Shader

All of my lighting calculations are being done in world space inside the geometry shader using the following snippet from the shader.

 float3 lightPosition = _WorldSpaceLightPos0;
 
 float3 v0 = IN[0].pos.xyz;
 float3 v1 = IN[1].pos.xyz;
 float3 v2 = IN[2].pos.xyz;
 
 float3 normal = normalize(cross(v0 - v1, v1 - v2));
 float4 worldNormal = normalize(mul(unity_ObjectToWorld, normal));

 float lightStrength = max(dot(normalize(lightPosition), worldNormal), 0);

Below is the complete shader for reference and reproduction of the issue. I've replaced the texture rendering with drawing the value of the lightPosition instead for debugging. This is the same code as I used to generate the attached gif. I am using Unity 5.5.1f1.

 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 
 // Low Poly Shader developed as part of World of Zero, uses geometry shaders to build unique triangles.
 // Based upon the example at: http://www.battlemaze.com/?p=153
 
 Shader "World of Zero/Low Poly" {
     Properties{
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
         _Glossiness("Smoothness", Range(0,1)) = 0.5
         _Metallic("Metallic", Range(0,1)) = 0.0
     }
         SubShader{
             Tags { "RenderType" = "Opaque" }
             LOD 200
 
             Pass
         {
 
             CGPROGRAM
             #include "UnityCG.cginc" 
             #pragma vertex vert
             #pragma fragment frag
             #pragma geometry geom
 
             // Use shader model 4.0 target, we need geometry shader support
             #pragma target 4.0
 
             sampler2D _MainTex;
 
             struct v2g
             {
                 float4 pos : SV_POSITION;
                 float3 norm : NORMAL;
                 float2 uv : TEXCOORD0;
                 float3 color : TEXCOORD1;
             };
 
             struct g2f
             {
                 float4 pos : SV_POSITION;
                 float3 norm : NORMAL;
                 //float2 uv : TEXCOORD0;
                 float3 diffuseColor : TEXCOORD1;
                 //float3 specularColor : TEXCOORD2;
             };
 
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;
 
             v2g vert(appdata_full v)
             {
                 float3 v0 = v.vertex.xyz;
 
                 v2g OUT;
                 OUT.pos = v.vertex;
                 OUT.norm = v.normal;
                 OUT.uv = v.texcoord;
                 OUT.color = tex2Dlod(_MainTex, v.texcoord).rgb;
                 return OUT;
             }
 
             [maxvertexcount(3)]
             void geom(triangle v2g IN[3], inout TriangleStream<g2f> triStream)
             {
                 float3 lightPosition = _WorldSpaceLightPos0;
 
                 float3 v0 = IN[0].pos.xyz;
                 float3 v1 = IN[1].pos.xyz;
                 float3 v2 = IN[2].pos.xyz;
 
                 float3 normal = normalize(cross(v0 - v1, v1 - v2));
                 float4 worldNormal = normalize(mul(unity_ObjectToWorld, normal));
 
                 float3 color = (IN[0].color + IN[1].color + IN[2].color) / 3;
                 float lightStrength = max(dot(normalize(lightPosition), worldNormal), 0);
 
                 g2f OUT;
                 OUT.pos = mul(UNITY_MATRIX_MVP, IN[0].pos);
                 OUT.norm = normal;
                 OUT.diffuseColor = _WorldSpaceLightPos0;// color * lightStrength;
                 triStream.Append(OUT);
 
                 OUT.pos = mul(UNITY_MATRIX_MVP, IN[1].pos);
                 OUT.norm = normal;
                 OUT.diffuseColor = _WorldSpaceLightPos0;//color * lightStrength;
                 triStream.Append(OUT);
 
                 OUT.pos = mul(UNITY_MATRIX_MVP, IN[2].pos);
                 OUT.norm = normal;
                 OUT.diffuseColor = _WorldSpaceLightPos0;//color * lightStrength;
                 triStream.Append(OUT);
             }
 
             half4 frag(g2f IN) : COLOR
             {
                 return float4(IN.diffuseColor.rgb, 1.0);
             }
             ENDCG
 
         }
         }
 }
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 Runewake · Feb 20, 2017 at 04:02 AM 0
Share

When the effect occurs depends on the objects world space position, but the light is constant over the entire object regardless. This makes zero sense.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by NotAdvisable · Feb 25, 2018 at 12:18 AM

A bit too late for you but i had the same issue and some other person might find this useful:

You need this tag in your pass: Tags{ "LightMode" = "ForwardBase" }

It's important that it is placed in the Pass and not the SubShader. I placed it in the SubShader section and spent like an hour debugging because i thought it wouldn't make a difference. It does.

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 dkdhunnoo · Aug 18, 2021 at 10:02 AM 0
Share

Oh my god, you life saver! I've literally spent an hour trying to find out what was wrong!

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

121 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

Related Questions

Problem rendering textured quads with transparent areas 0 Answers

Specular problems with shader graph materials on iOS/Android 1 Answer

Extrude Geometry shader 0 Answers

[URP] Lighting issues when using Light Attenuation. 1 Answer

Shader Clip with Stencil 0 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