• 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 ExplosiveBarrels · Feb 14, 2017 at 06:34 AM · shadercgfragment

How to get color of a fragment in CG after it's lit?

What I'm trying to do is make my lit meshes look more like sprites by reducing the colors that a given material can draw down to half a dozen or so. For example, a cold grey stone block that normally has a flat blueish gray color for its material, once lit, will be drawn with only 6 or so shades of that blueish gray, varying from black on up to a slightly blueish white, with no dithering.

What I think I need for that is a shader that takes the color of a fragment AFTER it's been lit, and compare it to half a dozen colors specified in the shader, and set the fragment color to the closest matching color from that half dozen.

In order to do that, I need to know what the color is of the fragment after it's been affected by lighting, and that's where I'm stuck. After that, I need to figure out the most optimized way of finding the closest matching color from the ones given.

In combination with a low resolution (384x224) and no anti-aliasing, I'm hoping this reduction in allowed colors per material will make my simple one-color meshes look more like a sprite.

My shader experience is just a step up from absolute beginner. I've written a simple flat color shader, and listened through a few tutorials that explained what's going on inside the CG, but so far had no luck in tracking down how to go about my particular problem.

Also open to alternative approaches, if you think this one is not going to work or is not the best method.

Comment
Add comment · Show 2
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 hexagonius · Feb 14, 2017 at 12:57 PM 1
Share

I'm no shader expert, but as far as I know, that would need a second pass. But I also think, that a better idea is to just write the shader in one go, with light and color adjustment in one go. Looking at this manual page under the section "Calculating Lighting", you should be able to modify it so the final color is calculated along with the necessary light information.

avatar image ExplosiveBarrels2 · Feb 14, 2017 at 03:37 PM 0
Share

Thanks! And now this is making a lot more sense, and what I was asking for was pretty simple.

What I need is to take a standard lit shader - the Unity one will do - and add in my comparison and color conversion just before the return statement in the fragment function, since the color the function would've been returning at that point is the lit color I needed.

I'll leave this question open for a bit longer in case anyone has recommendations on how to do the color comparison. $$anonymous$$y initial thought is to treat the color as a vector and do a dot product on each of the allowed colors, and go with the one that has the highest value from the dot product. But not sure how optimized that is, and this kind of shader will be run on most of the environment meshes in a scene.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Projected Texture Shader Issues 1 Answer

Stop shader values from being clamped 1 Answer

What is the proper way of calculating attentuation in Unity3D non-surface shaders? 0 Answers

How to force the compilation of a shader in Unity? 5 Answers

_LightMatrix0 and Directional Light 1 Answer


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