• 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 ungalyant2 · Feb 20, 2014 at 02:02 PM · shaderlighting

Creating a lighting shader

I'm trying to develop a lighting system to allow a situation where, when moving inside the outdoors appears brigher, and when moving outdoors makes the inside appear darker. The system is specifically for a 2D game.

What I have in mind currently is creating a shader that uses a float to track whether the player is indoors or outdoors, this would be adjusted by trigger colliders, and would fade from 0 when outdoors, to 1 when indoors.

I would then create a mask of a grey scale texture over the entire playable area, the textures for these would largely just be large squares of white or black, but blending in the middle from white to black for where there is a transition between indoors and outdoors, whilst the indoors and outdoors would be single quads with a 1 pixel texture, these transition sections would need to be large bespoke textures.

The greyscale texture would be present in the level over the rest of the assets, but would not technically be visible. It would use the texture as data, by using both the texture colour and the float value to track whether the player is inside or out. The shader would simply multiply the existing pixels behind the greyscale texture, by the greyscale colour + the in out value.

The logic behind this being, when the player is inside, the float InOut value is at 1, and the texture in front of him is black, therefore (0,0,0) + (1,1,1) = existing colour (1, 1, 1) so the level indoors appears at normal lighting. The outdoors however has the white texture over it, so (1,1,1) + (1,1,1) = existing colour (2, 2, 2) so the outdoors appears twice as bright. When outdoors the float value is 0, so the black pixles in front of the indoors equate to (0,0,0) + (0,0,0) = existing colour * (0,0,0) meaning darkness.

I'm not sure if my logic even works, but the shader I have written looks like this:

Everything is a fairly standard shader, other than my Blend operation is:

Blend DstColor Zero

and my fragment function is:

 half4 fragmentFunction(v2f_vct i) : COLOR
 {
     half4 col = tex2D(_MainTex, i.texcoord) * i.color;
 
     col = col + half4(_InOut, _InOut, _InOut, 0);
 
     return col;
 }

I've made the colour return a half4 rather than a fixed 4, because I assume that's the only way I'll be able to have the colour store 2,2,2 rather than capping at 1,1,1.

This doesn't appear to work. I'd like to know if anyone can see anything wrong with my logic or the code itself.

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

· 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

18 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

Related Questions

Combining a custom shader with a normal one 0 Answers

Jelly Defense like Planet Lighting/Shading? 1 Answer

Why are Cubemaps and Specular highlights not equally blurry at the same Roughness / Glossyness value with standard pbr shader? 0 Answers

Shader: get nearest point light's color, direction and general custom lighting questions 0 Answers

Adding a Pass to the Standard Shader 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