This is a decoupled shader control system I wrote for WolfenDoom: Blade of Agony, and some examples. It enables shaders when the player has 2 or more of a shader control item, and calls the SetUniforms method to set the shader uniforms.
When the player is on a map where he is given these shader control items, the shader gets enabled and controlled by the shader controller, as expected. However, if I start a new map, or I take away all of my shader control items, the shader "freezes" in place when it should be disabled altogether. This is what I've come up with so far, but it's giving me readonly errors on lines 53 and 54. I don't know what to do.
Code: Select all
/*
Shader controller base class
This allows mappers to control shaders using the GiveInventory/TakeInventory
ACS functions.
Do not use this directly! Instead, make an actor class that inherits from
ShaderControl. Set the ShaderControl.Shader property to the name of the
shader to control. The shader must be defined in GLDEFS.
To enable a shader, give 2 of the ShaderControl subclass item to the player.
For example:
GiveInventory("ShakeShaderControl", 2);
To disable a shader, take 1 of the shader control item away so that the
player only has 1 of said item. For example:
TakeInventory("ShakeShaderControl", 1);
or
SetInventory("ShakeShaderControl", 1);
Note that the latter requires you to define the SetInventory function in
your ACS code.
*/
class ShaderControl : Inventory {
string ShaderToControl;
bool known;
property Shader: ShaderToControl;
Default {
Inventory.MaxAmount 0x7fffffff;
}
override void BeginPlay() {
known = false;
}
virtual ui void SetUniforms(PlayerInfo p, RenderEvent e) {}
}
class CustomShaderHandler : StaticEventHandler
{
Array<String> shaders;
override void RenderOverlay(RenderEvent e)
{
PlayerInfo p = players[consoleplayer];
ShaderControl shaderControl = ShaderControl(p.mo.FindInventory("ShaderControl", true));
if (shaderControl != null)
{
if (!shaderControl.known) {
shaders.Push(shaderControl.ShaderToControl);
shaderControl.known = true;
}
//Console.Printf("Shader: %s", shaderControl.ShaderToControl);
if (shaderControl.amount >= 2)
{
Shader.SetUniform1f(p, shaderControl.ShaderToControl, "timer", gametic + e.FracTic);
shaderControl.SetUniforms(p, e);
Shader.SetEnabled(p, shaderControl.ShaderToControl, true);
}
else
{
Shader.SetEnabled(p, shaderControl.ShaderToControl, false);
}
} else {
for (int i = 0; i < shaders.Size(); i++) {
Shader.SetEnabled(p, shaders[i], false);
}
}
}
}