- Home /
Cell-based Lighting?
Say I wanted to have a 3D grid of cells where each cell is affected by only its content and its immediate neighbors. In such a scenario, the light in one cell causes each of it's neighbors to have a slightly lesser light value in a neighboring cell. For instance, imagine this is a 2D map of one layer of the game world:
+---+---+---+---+---+---+
| 0 | 0 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+---+
| 0 | 1 | 2 | 1 | 0 | 0 |
+---+---+---+---+---+---+
| 1 | 2 | 3 | 2 | 1 | 0 |
+---+---+---+---+---+---+
| 0 | 1 | 2 | 1 | 0 | 0 |
+---+---+---+---+---+---+
| 0 | 0 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+
The cell with the "3" contains a light of intensity 3. This causes the surrounding cells to have a light of intensity 2, and the cells surrounding that intensity 1, etc. The 0's should be completely dark (black color for the material). Within a cell, the lighting should be constant, and in fact can be as simple as adjusting the color brightness for any textures that touch that cell. For example, you could imagine in the map above that each cell has a square floor. Each floor square would have a texture multiplied by the light intensity for that cell. If there is any obstruction (wall, block, etc.), it would prevent the light propagation to that cell.
Any idea how to get something like this working? Can it be done in shaders alone, or will each light level need its own submesh?
Answer by Eric5h5 · Sep 09, 2010 at 09:40 PM
I did something like this for Realmaze3D. The lighting is calculated for each cell, and stored in the vertex colors for the meshes. I used this shader for most of the objects, but if you want real-time lighting on top of that, you'd need a different shader that includes lighting. Using this method, the cell lighting can be updated by recomputing the lighting and updating the vertex colors for the meshes.
Edit: oops, slightly too late. ;)
Answer by Dave 5 · Sep 09, 2010 at 09:39 PM
Well I was overthinking this one. All that I needed to do was set the proper vertex colors for each cell and it works. Voila. :)
Your answer

Follow this Question
Related Questions
Diffuse 2-sided shader only accepts light on one side? 2 Answers
Customize Standard Shader Light Rig - Help 1 Answer
Trying to create 2D pixel art lighting 1 Answer
2D Sprite Shader Acting like Light 0 Answers
Custom shader behaves differently on WebGL deployment (correct) and editor view (incorrect) 1 Answer