diff --git a/.nuget/README.md b/.nuget/README.md
index 2b84ce1..bd959a7 100644
--- a/.nuget/README.md
+++ b/.nuget/README.md
@@ -169,3 +169,36 @@ Reduces apparent bit depth across all channels individually to produce a banding
## Hello World
For a Hello World example with a simple UI see [LoFiEffects.WPF.TestApp/MainWindow.xaml](https://github.com/benpollarduk/LoFiEffects.WPF/blob/main/LoFiEffects.WPF.TestApp/MainWindow.xaml)
+
+## Compiling Shaders
+Shaders can be compiled using FXC.exe. The *LoiEffects.WPF* project has a pre-build event that can be used to compile a shader effect when it is built.
+
+The *Shader* variable needs to be set to the name of the shader effect to compile:
+
+```
+set Shader=Noise
+```
+
+> Note: The path to fxc.exe may need to be changed to suit your build environment depending on the version installed.
+
+```
+REM to compile a shader specify the name of the shader, e.g:
+REM set Shader=Crt
+REM Otherwise use "", e.g:
+REM set Shader=""
+set Shader=""
+
+set Fxc=C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\fxc.exe
+set Input=$(ProjectDir)Effects\HLSL\%Shader%.fx
+set Output=$(ProjectDir)Effects\Shaders\%Shader%.ps
+
+if %Shader% == "" (
+ echo Not compiling shader as not shader set. To compile a shader edit the $(ProjectName) pre-build event.
+) else (
+ echo Compiling shader %Input%...
+ "%Fxc%" /O0 /Zi /T ps_2_0 /Fo "%Output%" "%Input%"
+ echo Shader compiled to %Output%. Don't forget to set compiled shader build action to "Resource".
+)
+```
+
+> Note: If a shader is built for the first time its *Build Action* will need to be manually set to *Resource* to be used by the project.
\ No newline at end of file
diff --git a/LoFiEffects.WPF.TestApp/MainWindow.xaml b/LoFiEffects.WPF.TestApp/MainWindow.xaml
index 1da98c3..356ba5b 100644
--- a/LoFiEffects.WPF.TestApp/MainWindow.xaml
+++ b/LoFiEffects.WPF.TestApp/MainWindow.xaml
@@ -361,7 +361,19 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/LoFiEffects.WPF.Tests/Effects/CrtEffect_Tests.cs b/LoFiEffects.WPF.Tests/Effects/CrtEffect_Tests.cs
index d615327..da70db6 100644
--- a/LoFiEffects.WPF.Tests/Effects/CrtEffect_Tests.cs
+++ b/LoFiEffects.WPF.Tests/Effects/CrtEffect_Tests.cs
@@ -75,5 +75,22 @@ public void GAdjustIncludeScanlines_ThenNoException()
Assert.Fail($"Exception occurred: {ex.Message}");
}
}
+
+ [TestMethod]
+ public void GAdjustIntensity_ThenNoException()
+ {
+ try
+ {
+ CrtEffect effect = new()
+ {
+ Intensity = 0.5
+ };
+ Assert.AreEqual(0.5, effect.Intensity);
+ }
+ catch (Exception ex)
+ {
+ Assert.Fail($"Exception occurred: {ex.Message}");
+ }
+ }
}
}
diff --git a/LoFiEffects.WPF/Effects/CrtEffect.cs b/LoFiEffects.WPF/Effects/CrtEffect.cs
index f3a5d09..2b509f2 100644
--- a/LoFiEffects.WPF/Effects/CrtEffect.cs
+++ b/LoFiEffects.WPF/Effects/CrtEffect.cs
@@ -62,6 +62,15 @@ protected double IncludeScanlinesDouble
set { SetValue(IncludeScanlinesDoubleProperty, value); }
}
+ ///
+ /// Get or set the intensity. Higher values will produce a more pronounced effect. This is a dependency property.
+ ///
+ public double Intensity
+ {
+ get { return (double)GetValue(IntensityProperty); }
+ set { SetValue(IntensityProperty, value); }
+ }
+
#endregion
#region DependencyProperties
@@ -91,6 +100,11 @@ protected double IncludeScanlinesDouble
///
public static readonly DependencyProperty IncludeScanlinesDoubleProperty = DependencyProperty.Register("IncludeScanlinesDouble", typeof(double), typeof(CrtEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2)));
+ ///
+ /// Identifies the CrtEffect.Intensity property.
+ ///
+ public static readonly DependencyProperty IntensityProperty = DependencyProperty.Register("Intensity", typeof(double), typeof(CrtEffect), new UIPropertyMetadata(1.0, PixelShaderConstantCallback(3)));
+
#endregion
#region Constructors
@@ -106,6 +120,7 @@ public CrtEffect()
UpdateShaderValue(TextureWidthProperty);
UpdateShaderValue(TextureHeightProperty);
UpdateShaderValue(IncludeScanlinesDoubleProperty);
+ UpdateShaderValue(IntensityProperty);
}
#endregion
diff --git a/LoFiEffects.WPF/Effects/HLSL/Crt.fx b/LoFiEffects.WPF/Effects/HLSL/Crt.fx
index 3f9b202..5973c78 100644
--- a/LoFiEffects.WPF/Effects/HLSL/Crt.fx
+++ b/LoFiEffects.WPF/Effects/HLSL/Crt.fx
@@ -2,6 +2,7 @@ sampler2D implicitInput;
float textureWidth : register(c0);
float textureHeight : register(c1);
float includeScanlines : register(c2);
+float intensity : register(c3);
float4 main(float2 uv : TEXCOORD0) : COLOR
{
@@ -9,26 +10,16 @@ float4 main(float2 uv : TEXCOORD0) : COLOR
int x = int(pixelCoords.x);
int y = int(pixelCoords.y);
float4 color = tex2D(implicitInput, uv);
+ float clampedIntensity = clamp(intensity, 0.0, 1.0);
- // scanline mode
- if (includeScanlines > 0)
+ if (includeScanlines > 0 && y % 2 == 1)
{
- if (y % 2 == 1)
- {
- return float4(0.0, 0.0, 0.0, color.a);
- }
-
- float redChannel = (x % 4 == 0) ? color.r : 0.0;
- float greenChannel = (x % 4 == 1) ? color.g : 0.0;
- float blueChannel = (x % 4 == 2) ? color.b : 0.0;
-
- return float4(redChannel, greenChannel, blueChannel, color.a);
+ return float4 (0.0, 0.0, 0.0, color.a);
}
- // non-scanline mode
- float redChannel = (x % 3 == 0) ? color.r : 0.0;
- float greenChannel = (x % 3 == 1) ? color.g : 0.0;
- float blueChannel = (x % 3 == 2) ? color.b : 0.0;
+ float redChannel = (x % 3 == 0) ? color.r : (1.0 - clampedIntensity) * color.r;
+ float greenChannel = (x % 3 == 1) ? color.g : (1.0 - clampedIntensity) * color.g;
+ float blueChannel = (x % 3 == 2) ? color.b : (1.0 - clampedIntensity) * color.b;
return float4(redChannel, greenChannel, blueChannel, color.a);
}
diff --git a/LoFiEffects.WPF/Effects/Shaders/Crt.ps b/LoFiEffects.WPF/Effects/Shaders/Crt.ps
index 6feb302..9632113 100644
Binary files a/LoFiEffects.WPF/Effects/Shaders/Crt.ps and b/LoFiEffects.WPF/Effects/Shaders/Crt.ps differ
diff --git a/LoFiEffects.WPF/LoFiEffects.WPF.csproj b/LoFiEffects.WPF/LoFiEffects.WPF.csproj
index b0ee4f4..d71ea49 100644
--- a/LoFiEffects.WPF/LoFiEffects.WPF.csproj
+++ b/LoFiEffects.WPF/LoFiEffects.WPF.csproj
@@ -84,7 +84,7 @@
-
+
diff --git a/README.md b/README.md
index d4923c8..663b282 100644
--- a/README.md
+++ b/README.md
@@ -84,7 +84,7 @@ An effect that aims to create the impression that the visual is being displayed
```xaml
```
@@ -93,6 +93,7 @@ An effect that aims to create the impression that the visual is being displayed
* **TextureWidth**: A double specifying the rendered width of the texture in WPF units.
* **TextureHeight**: A double specifying the rendered height of the texture in WPF units.
* **IncludeScanlines**: A boolean specifying if scan lines should be included.
+* **Intensity**: A double specifying the intensity of the effect within a normalised range of 0-1.
### Degrade
Adds overall degradation to the visual. Similar to the *Noise* effect but works in a subtractive manner.
@@ -201,3 +202,36 @@ Reduces apparent bit depth across all channels individually to produce a banding
## Hello World
For a Hello World example with a simple UI see [LoFiEffects.WPF.TestApp/MainWindow.xaml](https://github.com/benpollarduk/LoFiEffects.WPF/blob/main/LoFiEffects.WPF.TestApp/MainWindow.xaml)
+
+## Compiling Shaders
+Shaders can be compiled using FXC.exe. The *LoiEffects.WPF* project has a pre-build event that can be used to compile a shader effect when it is built.
+
+The *Shader* variable needs to be set to the name of the shader effect to compile:
+
+```
+set Shader=Noise
+```
+
+> Note: The path to fxc.exe may need to be changed to suit your build environment depending on the version installed.
+
+```
+REM to compile a shader specify the name of the shader, e.g:
+REM set Shader=Crt
+REM Otherwise use "", e.g:
+REM set Shader=""
+set Shader=""
+
+set Fxc=C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x86\fxc.exe
+set Input=$(ProjectDir)Effects\HLSL\%Shader%.fx
+set Output=$(ProjectDir)Effects\Shaders\%Shader%.ps
+
+if %Shader% == "" (
+ echo Not compiling shader as not shader set. To compile a shader edit the $(ProjectName) pre-build event.
+) else (
+ echo Compiling shader %Input%...
+ "%Fxc%" /O0 /Zi /T ps_2_0 /Fo "%Output%" "%Input%"
+ echo Shader compiled to %Output%. Don't forget to set compiled shader build action to "Resource".
+)
+```
+
+> Note: If a shader is built for the first time its *Build Action* will need to be manually set to *Resource* to be used by the project.