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 /
This question was closed Feb 09, 2017 at 09:46 PM by lyonb for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by lyonb · Feb 10, 2017 at 06:59 PM · shaderlightinglightshader programmingshader writing

Point Lights only displaying in editor on custom shader

I'm trying to write a vertex/frag shader that casts a shadow from a directional light, and then draws diffuse lighting from point lights (ideally I'd like to be able to cast shadows from those point lights as well, but I'm taking baby steps).

It was all working fine until I separated the point lights to a second pass, and that's the part where I think I'm mistaken in my understanding. The lights display in a perfect square block, which is probably a problem with my attenuation, but the bigger problem is that the point lights actually only display in the editor, and bizarrely disappear and reappear randomly while interacting with the editor's UI. Sometimes it shows in the game window only, the editor window only, or both, but never in the game window while playing. I've attached a picture of this for referencealt text

Ignore the completely invisible objects, they haven't been UV mapped yet so their transparency is completely expected

  Shader "Toon" {
     Properties{
 
         _Color("Color", Color) = (1,0,0,1)
         _MainTex("Texture", 2D) = "white" {}
         _RampText("Ramp", 2D) = "white" {}
     }
 
     SubShader {
         Tags{
             "Queue" = "Geometry"
             "RenderType" = "Transparent"
 
         }
 
         Pass {
         Tags{"LightMode" = "ForwardBase"}
 
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         CGPROGRAM
 
         #pragma vertex vert
         #pragma fragment frag
 
         #include "UnityCG.cginc"
         #include "Lighting.cginc"
 
         #pragma multi_compile_fwdbase
         #include "AutoLight.cginc"
 
         struct vertInput {
             float4 pos : POSITION;
             float4 diff: COLOR0;
             float2 uv : TEXCOORD0;
             half3 norm : NORMAL;
         };
 
         struct vertOutput {
             float2 uv : TEXCOORD0;
             fixed3 diff: COLOR0;
             float4 pos : SV_POSITION;
             LIGHTING_COORDS(1,2)
         };
 
         sampler2D _MainTex;
         float4 _MainTex_ST;
         half4 _Color;
 
         vertOutput vert(vertInput input) {
             vertOutput o;
             o.pos = UnityObjectToClipPos(input.pos);
             o.uv = input.uv;
             half3 worldNormal = UnityObjectToWorldNormal(input.norm);
             half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
             o.diff = nl * _LightColor0;
             TRANSFER_VERTEX_TO_FRAGMENT(o);
 
             return o;
         }
 
 
         half4 frag(vertOutput output) : SV_TARGET {
             half4 c = tex2D(_MainTex, output.uv) * _Color;
             fixed atten = LIGHT_ATTENUATION(output);
             fixed3 lighting = output.diff * atten +  unity_AmbientSky;
             c.rgb *= lighting;
             return c;
         }
 
         ENDCG
         }
 
         Pass {
             Tags{ "LightMode" = "ForwardAdd" }
 
             Blend One One
 
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
 
             #pragma multi_compile_fwdadd
             #include "AutoLight.cginc"
 
             struct vertInput {
                 float4 pos : POSITION;
                 half3 norm : NORMAL;
             };
 
             struct vertOutput {
                 float4 pos : SV_POSITION;
                 LIGHTING_COORDS(0,1)
                 float3 vertexLighting : TEXCOORD2;
             };
 
             half4 _Color;
 
             vertOutput vert(vertInput input) {
                 vertOutput o;
                 o.pos = UnityObjectToClipPos(input.pos);
                 half3 worldNormal = UnityObjectToWorldNormal(input.norm);
                 o.vertexLighting = float3(0.0,0.0,0.0);
             
                 for (int i = 0; i < 4; i++){
                     float4 lightPosition = float4(unity_4LightPosX0[i], unity_4LightPosY0[i], unity_4LightPosZ0[i], 1.0);
                     float3 difference = lightPosition.xyz - o.pos.xyz;
                     float3 lightDirection = normalize(difference);
                     float squaredDistance = dot(difference, difference);
                     float atten = 1.0 / (1.0 + unity_4LightAtten0[i] * squaredDistance);
                     float3 diff = unity_LightColor[i].rgb * _Color.rgb * atten * max(0.0, dot(worldNormal, lightDirection));
 
                     o.vertexLighting = o.vertexLighting + diff;
                 }
 
                 return o;
             }
 
             half4 frag(vertOutput output) : SV_TARGET {
                 return float4 (output.vertexLighting, 1.0);
             }
 
             ENDCG
         }
 
     }
 
     Fallback "Legacy Shaders/VertexLit"
 }


EDIT:

Ok, weird UI glitch aside, (it randomly stopped happening), it turn out this was a blending issue. The diffuse was being multiplied by the color, which was white, which was of course blending in additive blending.

Changed this:

 float3 diff = unity_LightColor[i].rgb * _Color.rgb * atten * max(0.0, dot(worldNormal, lightDirection));
     
 o.vertexLighting = o.vertexLighting + diff;

To this:

 float3 diff = atten * max(0.0, dot(worldNormal, lightDirection));

 o.vertexLighting = o.vertexLighting * unity_LightColor[i].rgb + diff;

weirdlight.jpg (105.7 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

0 Replies

  • Sort: 

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

Standard Surface Shader with fade too dark 0 Answers

Block/Obscure light without shadows 0 Answers

Can I modify light position data before it gets sent to the shader? 1 Answer

Receiving Shadows On a Tranparent Shader 1 Answer

Custom (Sprite-)Shader: Combine Sprite with light and original Sprite 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