• 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 sarahnorthway · Jul 19, 2019 at 02:54 AM · shaderlightingalpha

Simple surface shader causing ghostly glow

Edit: NOT premultiplied alpha related as I initially thought. Also happens with straight alpha textures. Might be some other aspect of the sprite renderer those textures use (Spine 2D).

When I apply basic surface shader lighting to my Spine 2D sprites they get this ghostly effect where the individual sprites overlap:

alt text


My full shader:

 Shader "Northway/SpritesDiffuseWave" {
     Properties {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _ColorTint ("Tint", Color) = (1,1,1,1)
         [Toggle(_PREMULTIPLIED_ALPHA)] _StraightAlphaInput("Premultiplied Alpha", Int) = 0
     }
 
     SubShader {
         Tags {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Blend One OneMinusSrcAlpha
 
         CGPROGRAM
             #pragma shader_feature _ _PREMULTIPLIED_ALPHA
             #pragma surface surf SimpleLambert vertex:vert nolightmap nodynlightmap keepalpha noinstancing
             #include "UnitySprites.cginc"
 
             struct Input {
                 float2 uv_MainTex;
                 fixed4 color;
             };
             
             fixed4 _ColorTint;
             
             half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) {
                 half4 c;
                 
                         c.rgb = s.Albedo * _LightColor0.rgb * atten;
                 c.a = s.Alpha;
                 
                 return c;
             }
      
             void vert (inout appdata_full v, out Input o) {
                 UNITY_INITIALIZE_OUTPUT(Input, o);
                 o.color = float4(1, 1, 1, 1);
                 
                 // ... unrelated custom shader code
             }
 
             void surf (Input IN, inout SurfaceOutput o) {
                 fixed4 color = tex2D(_MainTex, IN.uv_MainTex);
                 o.Albedo = color.rgb * _ColorTint;
                 
                 #if !defined(_PREMULTIPLIED_ALPHA)
                     o.Albedo *= color.a;
                 #endif
                 
                 o.Alpha = color.a;
             }
 
         ENDCG
     }
 }
 


premultiplied-ghost.png (411.1 kB)
Comment
Add comment · Show 9
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 19, 2019 at 08:53 AM 0
Share

In what sense is the lighting effecting the opacity of the object? If my understanding of premul textures is correct, you would want to inverse the premul before lighting and then re-multiply the alpha, i.e.

 c.rgb = (s.Albedo / s.Alpha) *  _LightColor0.rgb * atten * s.Alpha;
 c.a = s.Alpha;

I think I'm losing it a bit here because that calculation looks mathematically redundant, but in theory if you apply lighting to what would be a normal straight alpha colour and then re-multiply the alpha it should be no different to a regular alpha blended shader.

Show more comments
avatar image sarahnorthway · Jul 22, 2019 at 11:39 PM 0
Share

I also reported this to the Spine forums where it seems to be a known issue with no current solution: http://esotericsoftware.com/forum/Simple-surface-shader-lighting-in-Unity-12307

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

201 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 avatar image avatar image avatar image avatar image avatar image 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

Transparent Vertex Shader Error 1 Answer

Toon Lighted Alpha 1 Answer

cast shadow - export lightmap from 3ds max to unity 2 Answers

Unity 5 Light problem 2 Answers

Point Light Behave different from editor and IOS device 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