Sprint 4: Darkness - UQdeco2800/2021-studio-6 GitHub Wiki
The darkness is a core element in the story, and there are two polishing features that were implemented.
- Adjusting lighting engine to match look and feel of story, and enhance gameplay
- A method to see if an object is within the light or not
Lighting Shaders
The original lighting engine uses the box2d lights library for its library. As this was imported as a precompiled library, no modifications could be added to it. Therefore, in Sprint 4, the Box2D library from the lighting engine was refactored to /source/core/src/main/com/badlogic/box2dlights to allow for custom modifications to the engine. As result of this, there may be a large majority of code smells within this refractured copy, but the majority of these will not be addressed.
Under the hood, the lighting engine works in a couple of steps (simplified).
- For each light on the screen, a specified amount of rays is fired from the light origin and intercepts to a specified physics layer(s) or the edge of the screen.
- Between the light origin and intercept a triangle is drawn on a lighting or diffuse layer.
- The area where the rays miss, triangles are drawn on the shadow layer.
- The triangles are drawn using the shadow, lighting and diffuse shader.
- The resulting triangles are rendered onto the scene according a specific blend type.
The diffuse blend type is GL_DST_COLOR
where the colour is mixed into the below layer. The shadow blend type is GL_ONE_MINUS_SRC_ALPHA
where the shadow is layered on top using one minus the transparency. The lighting blend type is GL_SRC_ALPHA
where the lights are layered on top using transparency.
Without extensive research into how OpenGL shaders work, there are some limitations to what adjustments that is possible to make within Sprint 4. However, it was possible to get a desirable outcome by changing some values.
- The shadow frag rgb output was changed to the texture.rgb + ambient.rgb instead of texture.rgb * texture.rgb + ambient.rgb. This allows for the shadow to retain its original colour, instead of darkening it further.
- The light shader frag rgb output was lifted by 20% and the alpha output was changed to be the vertex alpha lifted by 70%. This overall lifting of values creates a visible not not sharp definition between where the shadow and light is. This is important for gameplay, as it is important for the player where the light is.
DarknessDetectionComponent
The darkness detection component uses prebuilt functions within the lighting engine to detect if it is in a light or not. To use this component, add it to an entity and listen for the events inLight
and inShadow
to adjust your logic. It is also possible to directly query for its status using isInLight()
.
Within the implementation of the component, some leeway was given to the entity, by querying the lighting engine multiple times at points around the entity. Further iterations of the lighting engine may consider the revamp of how light is detected.