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 zactanpeterson · Aug 21, 2020 at 08:01 PM · efficiencyrefactor

more efficient recursive spread

I'm currently making light simulation for my 2D Terraria-like game, when you place a light source it recursively spreads out to light up the area, however, this is horribly inefficient taking 341 method calls to spread a light source with a radius of 5. I'd like to only have to call the method the same number of times as there are blocks being lit. the reference code if here:

 private void SpreadLight(LColor color, Vector2Int position, float intensity, byte radius, float distance)
 {
         LColor c = GetColor(position, color, distance, intensity, radius);
 
         if (world.Map.CheckForBlock(position))
         {
             distance--;
         }
         else
         {
             distance -= 0.5f;
         }
 
         if(world.Map.GetTile(position).GetLight().color.a < c.a)
         {
             world.Map.SetLight(c, position);
         }
 
         if (distance > 0)
         {
             Tile tempTile0 = world.Map.GetTile(position + Vector2Int.down);
             Tile tempTile1 = world.Map.GetTile(position + Vector2Int.left);
             Tile tempTile2 = world.Map.GetTile(position + Vector2Int.right);
             Tile tempTile3 = world.Map.GetTile(position + Vector2Int.up);
 
             if (tempTile0 != null)
             {
                 SpreadLight(c, tempTile0.Position, intensity, radius, distance);
             }
 
             if (tempTile1 != null)
             {
                 SpreadLight(c, tempTile1.Position, intensity, radius, distance);
             }
 
             if (tempTile2 != null)
             {
                 SpreadLight(c, tempTile2.Position, intensity, radius, distance);
             }
 
             if (tempTile3 != null)
             {
                 SpreadLight(c, tempTile3.Position, intensity, radius, distance);
             }
 
             world.Map.GetChunk(world.Map.GetChunkCoord(position)).LightUpdated = false;
           }
 }
Comment
Add comment · Show 3
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 exploringunity · Aug 21, 2020 at 09:15 PM 0
Share

Perhaps instead of recursion, you can do a 2D loop over a square of size radius * 2 by radius * 2 centered at the light's position, only updating tiles where the distance is <= radius. Hope this helps!

avatar image zactanpeterson exploringunity · Aug 21, 2020 at 11:09 PM 0
Share

The problem with that is that it won't always be a perfect square, it spreads farther where there aren't blocks blocking it.

avatar image exploringunity zactanpeterson · Aug 22, 2020 at 12:30 AM 0
Share

Idea: Keep a Set of visited tiles. Before each recursion, check if the tile has been visited (i.e. - it is in the set). If not, add it to the set and call SpreadLight, else do nothing. This will make sure every tile only gets processed once.

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.

Follow this Question

Answers Answers and Comments

131 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 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 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 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 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 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

Making floating specks for space movement 0 Answers

Having lots of Update Functions 2 Answers

How should I scale an object with collider? 1 Answer

Endless Runner Static Or Moving Player 0 Answers

In-Game-Compilation of 100kb .JS file during gameplay inefficient? 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