Minecraft-like Voxel type lighting?

It’s better to use an array of data as it’s very cheap to look up voxel data that way. The overall lighting calculations are also very quick and only need to occur when a light source has either been placed or removed. You could technically use other methods like you’ve mentioned, but I wouldn’t recommend it as it’s far less performant. Though due to these calculations being infrequent, it’s not as though it’s a terrible option. Though surface light level is calculated differently.

How would I do it using arrays? I haven’t really used arrays much.

I would highly recommend learning arrays as they’re extremely useful in a lot of ways.

You’ll use a 3D array that will appear as chunk[x][y][z] to get the particular voxel data. You’ll also be able to iterate over the array for each axis. I’d recommend writing a function to get all nearby voxels within a given distance, but it’ll need to support gathering voxel data from neighboring chunks. If you’re not using chunks, which is fine if you’re not going to save any data, then you can just have one big 3D array for everything, however then you’ll definitely need to write a function to get nearby voxels as iterating over an entire world worth of voxels will obviously take a bit of a performance hit.

Hopefully this helps you:

Okay, I’ll try this out and let you know how it goes.

I read the documentation, but how would I detect the light source of each block in the chunk and then change the light source of the surrounding ones depending on that?

Alternatively, if you’re not using chunks or saving data, a less performant but easier method would be to get all parts within a range using something like GetPartBoundsInRadius, and then applying light level that way, though you may want to do something to see if that part is even possible to be lit by the light source from wherever it is. Definitely don’t light up, or even generate, blocks that aren’t touching air as that would be wasteful.

Light level is one of the things that will be stored with each voxel. Next to their position, half a byte can be used to store the light level of 0 to 15. Though if not using an array, you can just use attributes to store a light level.

I am using chunks.

char limit sadkjhk

I’ll probably go with attributes. How would I go about changing the light level of the blocks around it though?

You’d just get the nearby blocks and set their light level to something relative to the distance from the light source. Just note that if there is more than one light source, you’d store the highest value between them for that voxel’s position. The light value from the sun doesn’t need to be saved as that’s a global value. A surface block with no obstruction will always have a light level of 15 during the day and therefore doesn’t need to be stored or calculated, it will always be the highest value until it’s not. Now if this is for purely visual stuff, I’d only worry about light sources as it would be incredibly inefficient to change the light level of every surface block whenever the day/night cycle shifts a stage. This sort of thing is normally done on the GPU rather than the CPU.

Alright, but what I’m asking is how would I CHECK for the block which is a light source, and how many blocks away the block is from that, then setting the light source depending on that.

You’ll need an event system to detect when a voxel is created or removed. When that event is triggered, if it’s a light source (which is something you’d define yourself, perhaps also with an attribute), you would then set that voxel’s light level to 15 and then search for nearby voxels and modify their light level accordingly. Either search through the array or use the less performant GetPartBoundsInRadius method that I mentioned earlier.

If using an array, you can find nearby blocks by using offset math by adding or subtracting from the starting voxel’s position, and doing that for all 3 axis up to a certain distance away. But if you want that sort of star pattern, you’ll want to exclude voxels that are beyond a certain distance away, using something like magnitude perhaps, otherwise it’ll just light up the area as a square/cube.

This is probably about as much as I can say without outright giving you code.

Alright thanks, I’ll try it!

faefea

I managed to make something out of it!
image

2 Likes

Could i ask how you did this?
Im interested in making something highly similar but cant figure it out

I barely even got it working. All I did was loop through a folder (chunk folder) to check for light sources. Then looped through all other blocks and changed their color depending on how far they were from the light source.

1 Like

It isn’t really a good solution & can lag a lot so I’m still trying to find something that works.

I was planning to change the color of the decal/part using hsv to “emulate” lighting whilst having the actual source product a light but im not sure yet

Yeah, so either you need to basically make a full on game with a chunk system or you’re stuck with the lag. Or you know, embrace 2016 Roblox lighting.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.