Soft Shadows (Automatic)

--by matharoo

Marketplace Link

Usage Instructions

Use shadows_start(depth) to start the shadow system in a room. Read the Functions section below to know how to use this function.


Functions

shadows_start(depth) •

Use this function to start the shadow manager. It may go in a Create or Room Start event.

Arguments:

·        depth: This will be the depth of the shadows. Set it to something high (like 100) so that they appear below other objects.

This function returns the instance id of the shadow manager.

 

shadows_exceptions_add(exceptions) •

There might be objects/instances that shouldn’t be casting shadows. So, you can mark them as exceptions using this function.

Arguments:

·        exceptions: ids or object indexes to add to exceptions (add any amount of exceptions you want, separated by commas)

 

shadows_exception_delete(exceptions) •

Using this function, you can remove an exception.

Arguments:

·        id: exception entry to remove (formerly added using shadows_exceptions_add())

 

light_source_create(x, y) •

For Light Mode 0, it creates a light source at a position.

Arguments:

·        x: x position of the light source

·        y: y position of the light source

This function returns the id of the light source. Save it in a variable to be able to move/delete a light source.

To get the x/y position of a light source, use the id returned from the function above in the oShadowManager.lightX[] and oShadowManager.lightY[] arrays.

 

light_source_pos(id, x, y) •

Changes the position of a light source.

Arguments:

·        id: id of the light source to move, returned from light_source_create()

·        x: new x position of the light source

·        y: new y position of the light source

 

light_source_delete(id) •

Deletes a light source.

Arguments:

·        id: id of the light source to delete, returned from light_source_create()

 

cube_caster_create(x, y, width, height, z_height) •

Using this function you can create a cube caster. It basically sets a rectangular area with a certain height to cast a shadow of a cube.

Arguments:

·        x: x position for the cube

·        y: y position for the cube

·        width: 2d width of the cube (x size)

·        height: 2d height of the cube (y size)

·        z_height: the z height of the cube, that affects the shadow length

It returns the instance id of the cube caster created.

You can edit a cube caster’s variables to change its properties:

·        cubeW: 2d width of the cube (x size)

·        cubeH: 2d height of the cube (y size)

·        cubeZH: the z height of the cube

In the picture below, you can see 3 cube casters. Cube casters themselves are invisible; the blue blocks you see are separate objects that spawn them.


Customization

The customization variables are located in oShadowManager’s Create event. Here are the explanations:


Lighting Mode

lightMode

This is the lighting mode. There are two lighting modes: 0 and 1. The default is 0.

0 uses point lights, 1 uses a single light source (sun).

·        lightMode = 0

When lightMode is 0, it will use the point light method for shadows. So, the shadows will be casted by lights that are at certain coordinates.
Those coordinates are stored in the lightX and lightY arrays. You can use the light_source functions to add/manage the light sources.

In the picture below, you can see the lamps acting as light sources.



Variables:

if dynamicLen is true, then the shadow’s length will be different based on its distance from a light source. Use the baseDist variable to set at what distance the shadow’s size is the same as the sprite.

maxDist is the maximum distance till which the dynamic length has effect. If the distance to the light source is greater than this value, the shadow size won’t increase.

·        lightMode = 1

When lightMode is 1, it will use the directional method for shadows. This way, all of the shadows will be casted in the same direction.
This method can be used for simulating the sun.

Here you can see the shadows being cast by a single light source, the sun.



Variables:

shadowDir controls the direction towards which the shadows are casted.


Shadow Properties

shadowLen

This is the shadow height scale. It will be used even if dynamicLen is enabled.

 

shadowColor

This is the color of the shadows.

 

shadowAlpha

This is the alpha/opacity of the shadows.

 

circleSize

The size of the circle that is drawn with the shadows. Set to 0 to disable the circle.


Shader Constants

These constants can be found in the fragment shader of the shader shSSWhite.

blurEnable

Sets whether blur is enabled or not. Takes true/false.

 

blurPower

This is the power/softness of the blur. Minimum value: 3.

 

blurAcc

This is the accuracy of the blur shader. You may decrease this for performance if your game is of a lower resolution, or increase it if the game is of a higher resolution.


Effects

blurDist

This tells how early the blur starts in the shadows. So, if you set it to something low (around 0.2), more of the shadows would be blurred; if you set it to something high (around 1.6) then the sprite will be harder and less blurred.

 

distFading

If this is true, then the shadow’s alpha will decrease the farther an object gets from a light source.

 

baseAlpha

If distFading is enabled, then this is what defines beyond what distance the alpha starts to fade.


Shadow Position

maskCenterX

If this is true, then the shadow’s x position will be the center of the mask. If false, the x position will be the center of the sprite.j

 

bottomMask

If this is true, then the shadows will appear at the bottom of the masks. Disable it to draw the shadows at the instance’s y/origin.

 

shadowOffsetX

This is the offset of the X coordinate of the shadows. It will be added to it.

 

shadowOffsetY

This is the offset of the Y coordinate of the shadows. It will be added to it.


Important Things You Should Know

·        The shadows appear at the bottom of an object's mask. You can turn this off by disabling bottomMask, and adjust the shadow location using the offset variables (shadowOffsetX & shadowOffsetY).

 


Example Instructions

There is an example room included with the asset, so that you can test the shadows.

In it, the mouse controls a moving light source, along with having many static torches.

The blurDist can be controlled using the mouse wheel. The lamps get destroyed when you click on them.

About the code: the shadow system is set up in oSSPlayer’s Create event, and the point lights are added in oSSLight’s Create event.