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 GiyomuGames · Jun 29, 2015 at 01:02 AM · shadershaderslights

Possibility to check all lights in one pass of a shader?

Hi everyone,

tl;dr: is there a way to check several lights during 1 pass of a shader? I would like to modify the effect of some specific lights depending on whether the pixel is lit by other lights or not.

The long explanation: I am trying to create a surface shader for my 2D game. My shader already works very nicely with my sprites and normal maps. Additionally the game is in the night and I wanted the moonlight to lit the sprites without displaying their color (so everything is grey under the moonlight). I have put a 0 alpha on the moonlight and 255 on the other lights to be able differentiate them in the shader and it works very well. The colors appears only if a light which is not the moon is close to a sprite, otherwise it is lit only with grey/white.

Now my problem is the following: when a light (say a flame) is close to my sprite, I would like to hide the lighting coming from the moon. Right now if my sprite is between a flame and the moonlight, I have 1 side of the sprite with the colors visible and the other side lit by the moonlight so grayish. In real life the luminosity of the flame would prevent our eyes to see the moonlight reflection and this side would be black. This is the effect I want to recreate.

I could implement this effect easily if I could check whether the sprite is near a light which is not the moon in the shader. Then depending on how bright this light is I would attenuate the moonlight reflection. Unfortunately I can't find a way to check the other lights while I am handling the moonlight.

Any ideas?

See my code below. The key part is around line 65.

 Shader "Sprites/Sprite normal map"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _BumpMap ("Normalmap", 2D) = "bump" {}
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
         _Cutoff ("Alpha Cutoff", Range (0,1)) = 0.5
 
     }
 
     SubShader
     {
         Tags
         { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="TransparentCutOut" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
         LOD 300
 
         // Render front faces only
         Cull Back
         Lighting On
         ZWrite Off
         Fog { Mode Off }
         
         CGPROGRAM
         #pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff 
         #pragma multi_compile DUMMY PIXELSNAP_ON 
 
         sampler2D _MainTex;
         sampler2D _BumpMap;
         fixed4 _Color;
         float _ScaleX;
 
         struct Input
         {
             float2 uv_MainTex;
             float2 uv_BumpMap;
             fixed4 color : COLOR;
         };
         
         void vert (inout appdata_full v, out Input o)
         {
             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
             v.vertex = UnityPixelSnap (v.vertex);
             #endif
             float3 normal = v.normal;
             
             v.normal = float3(0,0,1);
             v.tangent =  float4(1, 0, 0, 1);
             
             UNITY_INITIALIZE_OUTPUT(Input, o);
         }
 
         void surf (Input IN, inout SurfaceOutput o)
         {
             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
             if (_LightColor0.a < 0.1f)
             {
                 // Here I would like to test the other lights to know if I should attenuate _LightColor0
                 o.Albedo = _LightColor0;    
             }
             else
             {
                 o.Albedo = c.rgb;
             }
             
             o.Alpha = c.a;
             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
             o.Normal.z *= -1;
         }
         ENDCG
     }
 
 Fallback "Transparent/Cutout/Diffuse"
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Creepgin · Jul 29, 2015 at 09:36 PM

Using forward rendering, this can be done through blending.

http://docs.unity3d.com/Manual/SL-Blend.html

Comment
Add comment · Show 8 · 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 GiyomuGames · Jul 30, 2015 at 10:46 AM 0
Share

Thanks for your comment Creepgin. Correct me if I am wrong but I believe the blending will only control how the result of my shader calculation will blend with what's already drawn. What I am trying to do is change what the shader computes depending on whether there is a light nearby which is not the moon. Just out of curiosity I tried several Blend combinations and also BlendOp but as I suspected it never had the effect I was hoping for.

$$anonymous$$y understanding of shaders is limited so please correct me if needed.

avatar image Creepgin · Jul 31, 2015 at 10:41 PM 0
Share

I recently had roughly the same need as you do. That's why I searched for the same topic and came across your question.

Assu$$anonymous$$g your moonlight is a directional light, it will be rendered in the ForwardBase pass. Your flame light (a point light) will be rendered in the ForwardAdd pass. If you do a multiplicatively blend on the ForwardAdd pass with Blend Zero SrcColor, you should have the flamelight to mask out the moonlight. If you do a Blend One Zero, your flamelight will completely replace the moonlight.

avatar image GiyomuGames · Aug 01, 2015 at 12:27 AM 0
Share

Thanks that's actually very interesting and helps me understand how blending works a bit more. Unfortunately my moonlight is a point light too and I don't think I want to change it to a directional light. The moon is often on a screen and I want the elements on its right to have a rim effect on their left side, and the elements on its left to have a rim effect on their right side (which works well now and looks very nice).

avatar image Creepgin · Aug 01, 2015 at 01:43 AM 0
Share

I see. I'm researching on more control over Blending (per light vs per pass). But so far I've got nothing since all my questions are left unanswered here or on the forum.

For your case, maybe a Level or Color Lookup image effect can achieve your desired effect.

avatar image Creepgin · Aug 01, 2015 at 10:48 PM 0
Share

Actually you can still keep the moonlight as a point light and still separate it out into a separate pass. Set the flamelight's render mode to important and the moonlight to not important. This will force the moonlight to be a vertex light (or a SH light). Or you can do it the other way around, as long as those 2 types of lights are in separate passes.

Show more comments

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

22 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

Related Questions

Unlit Transparent With Color Support 1 Answer

create a texture with shader code? ( without using a texture ) 1 Answer

vertex displacement shader and ssao 0 Answers

Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers

How to make a 2d shader with edge waves 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