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 stulleman · Aug 19, 2014 at 06:35 PM · 2dtilelightningvertexcolorflood

2D Tilebased Floodfill lightning

Hello Forum,

I think I have an easy question, but I can't seem to find any solution for it.

My game looks like this right now (I use Vertex Colors for lightning)

alt text

Lighting works perfect in realtime, with as many lights as I like. No performance issues.

The problem is that I want to introduce colors. Right now I'm just using a value between 0 and 255 for all R,G and B.

Okay so this is the method I use now (This is not the computation for realtime)

 private void InitializeLightData() {
         lightData = new byte[worldWidth, worldHeight];
 
         for (int i = 0; i < worldWidth; i++) {
             for (int j = 0; j < worldHeight; j++) {
 
                 if (BlockDescription.GetLightEmission(worldData[i, j, 0]) > 0) {
                     lightData[i, j] = BlockDescription.GetLightEmission(worldData[i, j, 0]);
                 } else {
                     lightData[i, j] = sunValue;
                 }
             }
         }
 
         for (int i = 0; i < worldWidth; i++) {
             for (int j = 0; j < worldHeight; j++) {
 
                 if (lightData[i, j] >= 17) {
                     PropagateLight(i, j - 1, (byte) (lightData[i, j] - 17));
                     PropagateLight(i + 1, j, (byte) (lightData[i, j] - 17));
                     PropagateLight(i, j + 1, (byte) (lightData[i, j] - 17));
                     PropagateLight(i - 1, j, (byte) (lightData[i, j] - 17));
                 }
             }
         }
     }
 
     private void PropagateLight(int x, int y, byte lightValue) {
         if (lightValue == 0) {
             return;
         }
 
         if (x < 0 || y < 0 || x >= worldWidth || y >= worldHeight) {
             return;
         }
 
         if (BlockDescription.IsSolidToLight(worldData[x, y, 0])) {
             return;
         }
 
         if (lightData[x, y] >= lightValue) {
             return;
         }
 
         lightData[x, y] = lightValue;
 
         lightValue -= 17;
 
         PropagateLight(x, y - 1, lightValue);
         PropagateLight(x + 1, y, lightValue);
         PropagateLight(x, y + 1, lightValue);
         PropagateLight(x - 1, y, lightValue);
     }

So I thought about something like this

 private void InitializeLightData() {
         lightData = new Color32[worldWidth, worldHeight];
 
         Color32 currentColor;
 
         for (int i = 0; i < worldWidth; i++) {
             for (int j = 0; j < worldHeight; j++) {
 
                 currentColor = BlockDescription.GetLightEmission(worldData[i, j, 0]);
 
                 if (currentColor.r != 0 || currentColor.g != 0 || currentColor.b != 0) {
                     lightData[i, j] = currentColor;
                 } else {
                     lightData[i, j] = sunColor;
                 }
             }
         }
 
         for (int i = 0; i < World.current.worldWidth; i++) {
             for (int j = 0; j < World.current.worldHeight; j++) {
 
                 Color32 newLight = new Color32(lightData[i, j].r, lightData[i, j].g, lightData[i, j].b, 0);
                 if (newLight.r >= 17) {
                     newLight.r -= 17;
                 }
                 if (newLight.g >= 17) {
                     newLight.g -= 17;
                 }
                 if (newLight.b >= 17) {
                     newLight.b -= 17;
                 }
 
                 PropagateLight(i, j - 1, newLight);
                 PropagateLight(i + 1, j, newLight);
                 PropagateLight(i, j + 1, newLight);
                 PropagateLight(i - 1, j, newLight);
             }
         }
     }
 
     private void PropagateLight(int x, int y, Color32 lightValue) {
         if (lightValue.r == 0 && lightValue.g == 0 && lightValue.b == 0) {
             return;
         }
 
         if (x < 0 || y < 0 || x >= worldWidth || y >= worldHeight) {
             return;
         }
 
         if (BlockDescription.IsSolidToLight(worldData[x, y, 0])) {
             return;
         }
 
         if (lightData[x, y].r >= lightValue.r && lightData[x, y].g >= lightValue.g && lightData[x, y].b >= lightValue.b) {
             return;
         }
 
         lightData[x, y].r = lightValue.r;
         lightData[x, y].g = lightValue.g;
         lightData[x, y].b = lightValue.b;
 
         if (lightValue.r >= 17) {
             lightValue.r -= 17;
         }
         if (lightValue.g >= 17) {
             lightValue.g -= 17;
         }
         if (lightValue.b >= 17) {
             lightValue.b -= 17;
         }
 
         PropagateLight(x, y - 1, lightValue);
 
         PropagateLight(x + 1, y, lightValue);
 
         PropagateLight(x, y + 1, lightValue);
 
         PropagateLight(x - 1, y, lightValue);
     }

But this doesn't work. I have changed the code above many times, this is just the last state it was in. But hopefully you get the idea and can help me out.

Really appreciate it!

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 screenname_taken · Aug 19, 2014 at 06:37 PM 0
Share

When you say that it doesn't work, what does it do exactly? Are the colors co$$anonymous$$g out as always white no matter what you do?

If yes, it's because in script colors are a float from 0 to 1. So, to use R 177, G 150, B 25 for examply, you should divide 177/255, 150/255, 25/255 and use the result.

avatar image stulleman · Aug 19, 2014 at 08:22 PM 0
Share

I have a Color32[,]-Array where all Vertex-Colors are saved in. The methods above fill this Array, so at a later point the Render-Loop can use the information to update the Vertex-Colors.

What happens with the second method is this

alt text

I don't want that anybody tries to find a bug in my code. I think it's a logical error. So if somebody maybe knows how it could be done with colors and explain the idea behind it.

EDIT: At some point I had some code that actually generated Colors, but it was horribly slow. So maybe the idea is not that wrong.

lightningfail.jpg (193.8 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Aug 19, 2014 at 06:44 PM

That's Color. The OP is using Color32, which does use component ranges of 0-255. Though I agree it's unclear how the lightData value calculated is eventually assigned to the material (apologies if that's what you meant)

http://docs.unity3d.com/ScriptReference/Color32.html

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Strange renderissues with tiled sprites 1 Answer

2D Tile Texture Size? 2 Answers

2D Tiles and sprites 1 Answer

2D tilemaps - separation of view and data, prototyping doubts 0 Answers

How do I use many different tile textures for one mesh? 2D Tile based game. 2 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