Shadow Check Module

What is it?

Shadow Check Module provides a simple way to check how much of a given model is in the shadows of another model. It also allows you to specify whether you want to consider Sun or the Moon as the source of light.

How to use it?

The module exposes a simple check function.

.check(shadowCastModel, shadowCheckModel, celestialBody)

There are three main arguments. The first two arguments can either be a table of Instances or just an Instance itself. The first argument defines the model which is casting the shadows (for example a wall). The second argument defines the model that is being checked for whether it is covered in the shadows (for example a character). The last argument can either be “Sun” or “Moon” (case does not matters) and chooses which celestial body to consider (if ignored it will be evaluated as “Sun”).

The function returns two number values. The first one is the number of exposed corners and the second one is the percentage of exposed corners (though not really percentage since the value is between 0 and 1).

Is it fast enough?

Testing with the free model pine cone tree (about 65 parts) and my R15 character (about 19) parts, the function gets its work done between the 0.003 to 0.004 seconds range.

Where to get it?

Right here


Credits

@Arbeiters for creating the module.
@WingItMan for suggesting a better input/specification method for the first two arguments

19 Likes

I think I might have a use for this, since I tried to make something like it but I failed pretty badly. Thanks for this.

1 Like

This is really cool so far.

My one suggestion would be a slight update to the getCorners method when you’re grabbing the list of parts - include a way to pass in a white-list table - for example:

To optimise performance for checking if my character is in the light, I may only want to check against the torso and the head, or just the arms.

local function getCorners(model)
--change:
	local parts = {model, unpack(model:GetDescendants())}
-- with something like this:
	local parts = {model, unpack((type(model) == 'table' and model or model:GetDescendants()))}

If this was incorporated, we could use this alongside the collection service’s tags to filter specifics lists of items and retain hierarchical organisation with models. :slight_smile:



A work-around to doing this would be calling individual casts with single parts and then adding up the percentages.
Doing this lets me check the top and bottom of my character to see if it is in the light at about 0.5% activity, rather than the 3+% from casting against my character model.

	local exposed,percentage = shadow_mod.check(workspace.Part,char.Head)
	local exposed2,percentage2 = shadow_mod.check(workspace.Part,char.LeftFoot)
	local exposed3,percentage3 = shadow_mod.check(workspace.Part,char.RightFoot)
	
	local percentage = (percentage+percentage2+percentage3)/3

Anyhow, nice module.

Thanks for your suggestions.

I’ve changed shadowCastModel and shadowCheckModel arguments to accept either a table (of Instances) or an Instance.

2 Likes