Skip to content

Commit

Permalink
Update various shader effects, clean up shader files
Browse files Browse the repository at this point in the history
Multiple changes have been made to shader effects including the addition of new effects like Predator, ScanLines, FishEye, Sobel, and Bloom. Alongside these additions, existing effects like CrossHatching and CrossStitching now have updated attributes and changes to their shader and scripting files. This commit also includes numerous code clean-up and renaming tasks, rendering the shaders more efficient and easy to read.
  • Loading branch information
MrScautHD committed Jul 7, 2024
1 parent 8ddaa14 commit 8fb0e4a
Show file tree
Hide file tree
Showing 17 changed files with 423 additions and 100 deletions.
71 changes: 71 additions & 0 deletions src/Sparkle/CSharp/Effects/Types/BloomEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Numerics;
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;

namespace Sparkle.CSharp.Effects.Types;

public class BloomEffect : Effect {

public int ResolutionLoc { get; private set; }
public int SamplesLoc { get; private set; }
public int QualityLoc { get; private set; }

public float Samples;
public float Quality;

private float _oldSamples;
private float _oldQuality;

/// <summary>
/// Constructor for creating a BloomEffect object.
/// </summary>
/// <param name="shader">The shader to be used by the bloom effect.</param>
/// <param name="samples">The number of samples for the bloom effect.</param>
/// <param name="quality">The quality of the bloom effect.</param>
public BloomEffect(Shader shader, float samples = 5.0F, float quality = 2.5F) : base(shader) {
this.Samples = samples;
this.Quality = quality;
}

protected internal override void Init() {
base.Init();
this.SetLocations();
this.UpdateResolution();
}

public override void Apply(Material? material = default) {
base.Apply(material);

if (Window.IsResized()) {
this.UpdateResolution();
}

if (RayMath.FloatEquals(this.Samples, this._oldSamples) != 1) {
this.Shader.SetValue(this.SamplesLoc, this.Samples, ShaderUniformDataType.Float);
this._oldSamples = this.Samples;
}

if (RayMath.FloatEquals(this.Quality, this._oldQuality) != 1) {
this.Shader.SetValue(this.QualityLoc, this.Quality, ShaderUniformDataType.Float);
this._oldQuality = this.Quality;
}
}

/// <summary>
/// Sets the locations of shader parameters.
/// </summary>
private void SetLocations() {
this.ResolutionLoc = this.Shader.GetLocation("resolution");
this.SamplesLoc = this.Shader.GetLocation("samples");
this.QualityLoc = this.Shader.GetLocation("quality");
}

/// <summary>
/// Updates the resolution of the shader parameter.
/// </summary>
private void UpdateResolution() {
this.Shader.SetValue(this.ResolutionLoc, new Vector2(Window.GetRenderWidth(), Window.GetRenderHeight()), ShaderUniformDataType.Vec2);
}
}
88 changes: 88 additions & 0 deletions src/Sparkle/CSharp/Effects/Types/CrossHatchingEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;

namespace Sparkle.CSharp.Effects.Types;

public class CrossHatchingEffect : Effect {

public int HatchOffsetYLoc { get; private set; }
public int LumThreshold01Loc { get; private set; }
public int LumThreshold02Loc { get; private set; }
public int LumThreshold03Loc { get; private set; }
public int LumThreshold04Loc { get; private set; }

public float HatchOffsetY;
public float LumThreshold01;
public float LumThreshold02;
public float LumThreshold03;
public float LumThreshold04;

private float _oldHatchOffsetY;
private float _oldLumThreshold01;
private float _oldLumThreshold02;
private float _oldLumThreshold03;
private float _oldLumThreshold04;

/// <summary>
/// Constructor for creating a CrossHatchingEffect object.
/// </summary>
/// <param name="shader">The shader to be used by the cross hatching effect.</param>
/// <param name="hatchOffsetY">The vertical offset for the hatching lines.</param>
/// <param name="lumThreshold01">The first luminance threshold for hatching.</param>
/// <param name="lumThreshold02">The second luminance threshold for hatching.</param>
/// <param name="lumThreshold03">The third luminance threshold for hatching.</param>
/// <param name="lumThreshold04">The fourth luminance threshold for hatching.</param>
public CrossHatchingEffect(Shader shader, float hatchOffsetY = 5.0F, float lumThreshold01 = 0.9F, float lumThreshold02 = 0.7F, float lumThreshold03 = 0.5F, float lumThreshold04 = 0.3F) : base(shader) {
this.HatchOffsetY = hatchOffsetY;
this.LumThreshold01 = lumThreshold01;
this.LumThreshold02 = lumThreshold02;
this.LumThreshold03 = lumThreshold03;
this.LumThreshold04 = lumThreshold04;
}

protected internal override void Init() {
base.Init();
this.SetLocations();
}

public override void Apply(Material? material = default) {
base.Apply(material);

if (RayMath.FloatEquals(this.HatchOffsetY, this._oldHatchOffsetY) != 1) {
this.Shader.SetValue(this.HatchOffsetYLoc, this.HatchOffsetY, ShaderUniformDataType.Float);
this._oldHatchOffsetY = this.HatchOffsetY;
}

if (RayMath.FloatEquals(this.LumThreshold01, this._oldLumThreshold01) != 1) {
this.Shader.SetValue(this.LumThreshold01Loc, this.LumThreshold01, ShaderUniformDataType.Float);
this._oldLumThreshold01 = this.LumThreshold01;
}

if (RayMath.FloatEquals(this.LumThreshold02, this._oldLumThreshold02) != 1) {
this.Shader.SetValue(this.LumThreshold02Loc, this.LumThreshold02, ShaderUniformDataType.Float);
this._oldLumThreshold02 = this.LumThreshold02;
}

if (RayMath.FloatEquals(this.LumThreshold03, this._oldLumThreshold03) != 1) {
this.Shader.SetValue(this.LumThreshold03Loc, this.LumThreshold03, ShaderUniformDataType.Float);
this._oldLumThreshold03 = this.LumThreshold03;
}

if (RayMath.FloatEquals(this.LumThreshold04, this._oldLumThreshold04) != 1) {
this.Shader.SetValue(this.LumThreshold04Loc, this.LumThreshold04, ShaderUniformDataType.Float);
this._oldLumThreshold04 = this.LumThreshold04;
}
}

/// <summary>
/// Sets the locations of shader parameters.
/// </summary>
private void SetLocations() {
this.HatchOffsetYLoc = this.Shader.GetLocation("hatchOffsetY");
this.LumThreshold01Loc = this.Shader.GetLocation("lumThreshold01");
this.LumThreshold02Loc = this.Shader.GetLocation("lumThreshold02");
this.LumThreshold03Loc = this.Shader.GetLocation("lumThreshold03");
this.LumThreshold04Loc = this.Shader.GetLocation("lumThreshold04");
}
}
71 changes: 71 additions & 0 deletions src/Sparkle/CSharp/Effects/Types/CrossStitching.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Numerics;
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;

namespace Sparkle.CSharp.Effects.Types;

public class CrossStitching : Effect {

public int ResolutionLoc { get; private set; }
public int StitchingSizeLoc { get; private set; }
public int InvertLoc { get; private set; }

public float StitchingSize;
public bool Invert;

private float _oldStitchingSize;
private bool _oldInvert;

/// <summary>
/// Constructor for creating a CrossStitching object.
/// </summary>
/// <param name="shader">The shader to be used by the cross stitching effect.</param>
/// <param name="stitchingSize">The size of the stitching pattern.</param>
/// <param name="invert">Indicates whether to invert the stitching effect.</param>
public CrossStitching(Shader shader, float stitchingSize = 6.0F, bool invert = false) : base(shader) {
this.StitchingSize = stitchingSize;
this.Invert = invert;
}

protected internal override void Init() {
base.Init();
this.SetLocations();
this.UpdateResolution();
}

public override void Apply(Material? material = default) {
base.Apply(material);

if (Window.IsResized()) {
this.UpdateResolution();
}

if (RayMath.FloatEquals(this.StitchingSize, this._oldStitchingSize) != 1) {
this.Shader.SetValue(this.StitchingSizeLoc, this.StitchingSize, ShaderUniformDataType.Float);
this._oldStitchingSize = this.StitchingSize;
}

if (this.Invert != this._oldInvert) {
this.Shader.SetValue(this.InvertLoc, this.Invert ? 1 : 0, ShaderUniformDataType.Float);
this._oldInvert = this.Invert;
}
}

/// <summary>
/// Sets the locations of shader parameters.
/// </summary>
private void SetLocations() {
this.ResolutionLoc = this.Shader.GetLocation("resolution");
this.StitchingSizeLoc = this.Shader.GetLocation("stitchingSize");
this.InvertLoc = this.Shader.GetLocation("invert");
}

/// <summary>
/// Updates the resolution of the shader parameter.
/// </summary>
private void UpdateResolution() {
this.Shader.SetValue(this.ResolutionLoc, new Vector2(Window.GetRenderWidth(), Window.GetRenderHeight()), ShaderUniformDataType.Vec2);
}
}
2 changes: 1 addition & 1 deletion src/Sparkle/CSharp/Effects/Types/PbrEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override void Apply(Material? material = default) {
this._oldAmbientIntensity = this.AmbientIntensity;
}

if (this._inactiveLights.Count != this._oldLightCount) {
if (this._activeLights.Count != this._oldLightCount) {
this.Shader.SetValue(this.LightCountLoc, this._activeLights.Count, ShaderUniformDataType.Int);
this._oldLightCount = this._inactiveLights.Count;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sparkle/CSharp/Effects/Types/PixelizerEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Apply(Material? material = default) {
}

if (RayMath.Vector2Equals(this.PixelSize, this._oldPixelSize) != 1) {
this.Shader.SetValue(this.PixelSizeLoc, this._oldPixelSize, ShaderUniformDataType.Vec2);
this.Shader.SetValue(this.PixelSizeLoc, this.PixelSize, ShaderUniformDataType.Vec2);
this._oldPixelSize = this.PixelSize;
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/Sparkle/CSharp/Effects/Types/ScanLinesEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Numerics;
using Raylib_CSharp;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;

namespace Sparkle.CSharp.Effects.Types;

public class ScanLinesEffect : Effect {

public int ResolutionLoc { get; private set; }
public int OffestLoc { get; private set; }

public float Offset;

private float _oldOffset;

/// <summary>
/// Constructor for creating a ScanLinesEffect object.
/// </summary>
/// <param name="shader">The shader to be used by the scan lines effect.</param>
/// <param name="offset">The offset for the scan lines.</param>
public ScanLinesEffect(Shader shader, float offset = 0) : base(shader) {
this.Offset = offset;
}

protected internal override void Init() {
base.Init();
this.SetLocations();
this.UpdateResolution();
}

public override void Apply(Material? material = default) {
base.Apply(material);

if (Window.IsResized()) {
this.UpdateResolution();
}

if (RayMath.FloatEquals(this.Offset, this._oldOffset) != 1) {
this.Shader.SetValue(this.OffestLoc, this.Offset, ShaderUniformDataType.Float);
this._oldOffset = this.Offset;
}
}

/// <summary>
/// Sets the locations of shader parameters.
/// </summary>
private void SetLocations() {
this.ResolutionLoc = this.Shader.GetLocation("resolution");
this.OffestLoc = this.Shader.GetLocation("offset");
}

/// <summary>
/// Updates the resolution of the shader parameter.
/// </summary>
private void UpdateResolution() {
this.Shader.SetValue(this.ResolutionLoc, new Vector2(Window.GetRenderWidth(), Window.GetRenderHeight()), ShaderUniformDataType.Vec2);
}
}
45 changes: 45 additions & 0 deletions src/Sparkle/CSharp/Effects/Types/SobelEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Numerics;
using Raylib_CSharp.Materials;
using Raylib_CSharp.Shaders;
using Raylib_CSharp.Windowing;

namespace Sparkle.CSharp.Effects.Types;

public class SobelEffect : Effect {

public int ResolutionLoc { get; private set; }

/// <summary>
/// Constructor for creating a SobelEffect object.
/// </summary>
/// <param name="shader">The shader to be used by the Sobel effect.</param>
public SobelEffect(Shader shader) : base(shader) { }

protected internal override void Init() {
base.Init();
this.SetLocations();
this.UpdateResolution();
}

public override void Apply(Material? material = default) {
base.Apply(material);

if (Window.IsResized()) {
this.UpdateResolution();
}
}

/// <summary>
/// Sets the locations of shader parameters.
/// </summary>
private void SetLocations() {
this.ResolutionLoc = this.Shader.GetLocation("resolution");
}

/// <summary>
/// Updates the resolution of the shader parameter.
/// </summary>
private void UpdateResolution() {
this.Shader.SetValue(this.ResolutionLoc, new Vector2(Window.GetRenderWidth(), Window.GetRenderHeight()), ShaderUniformDataType.Vec2);
}
}
Loading

0 comments on commit 8fb0e4a

Please sign in to comment.