- Home /
Get "on screen" color of a texture to use in a legend
Hello all,
as you can see in the below, I'm trying to set up a legend for my generated map.
Problem is that the colors of my sprites in the legend are not impacted by the scene lighting and therefore do not match the actual in-game colors. Eg. the mountain is much darker in the legend than in the lighted scene.
I could copy the colors and manually modify the sprite but my colors come from a generator and, to help accessibility, I would like to give the user the possibility to change them... e.g. make mountains Black in the settings, rivers - magenta. Or to change the lighting intensity.
Any suggestion on how to get the actual game colors and map them to my legend sprites - in run time / after the map generation?
I was thinking of taking a printscreen - after generation - and trying to read colors from that but I'm wondering if there is an easier way.
Is this the way to go, or should I approach this in a different way?
Thanks a lot,
Answer by tuinal · Jun 18, 2021 at 11:25 PM
Use an unlit shader for the map? As I understand it I don't think you're trying to light the map, it's more that the fact it's lit is undesirable?
There is a default unlit shader in the default and URP pipelines. Just changing the material of your map tiles to this shader would fix the problem if my above understanding is correct.
mmm - I was thinking the give the users the possibility to adjust the lighting... but your comment gets me thinking that they could achieve the same results by selecting the colors in the menu. I'm - like often - overcomplicating. I'm guessing also that an unlit shader will help performance. In fact I already use it on some elements, just not the map tiles. I may find a reason why I need the lighted textures but nothing pop ups for the time being. THANKS for helping me keep it simple.
Answer by Sinterklaas · Jun 18, 2021 at 08:07 PM
If I understand correctly, you have a map texture on a quad in the scene, that receives light. You want to be able to change the colors in the source sprite before lighting is applied?
If that's what you're trying to do, I think there are two ways to do it.
Make a copy of the source sprite in your game object's Awake function, and whenever the player changes one of the target colors, find all pixels with the current color and replace them. You'll want to look into the Texture2D.GetPixels and Texture2D.SetPixels functions for this. Don't forget to call Texture2D.Apply after calling SetPixels or the changes won't be visible. This might be very slow however, depending on how often you expect these colors to change, and you'll have to keep the texture in memory twice (I don't think you can modify the source texture directly at run-time, but I might be wrong there, definitely look into that if you go this route).
Use a shader, where all your colors are float properties. For every fragment, figure out what the fragment represents, and replace it with the desired color. Then apply lighting. This is probably the more performant way to do this, but will likely take you longer to implement if you are unfamiliar with shaders.
Does that help you?
Hi, Thanks but that is not my issue.
Sorry if I wasn't clear - what I'm trying to do is a legend for my map. My legend is a UI and use sprites. My map tiles are planes or cubes and use textures. For example, my mountain tile is by default the color #5B504C but user will be able to override this color in the game settings. My problem is that the UI/sprite color #5B504C displayed in the legend does not look the same color as the actual tile in the game even if its texture color is #5B504C - due to the lighting. I'm trying to find a way to have these 2 visually match - meaning the color displayed in the legend for the mountain is the same as the color of the mountain on the map. See attached picture. Neither the colors nor the lighting will change once the game is launched and the map is generated.
Thanks a lot for your help and any pointers you may have,
Your answer
Follow this Question
Related Questions
Why change the light color so fast? 2 Answers
Using URP how to make sprites lit by 3D Lights again? 6 Answers
My game is too dark... 3 Answers
How to edit this shader to use diffuse lighting (2D) 0 Answers