Last active
June 19, 2020 07:06
-
-
Save thatcosmonaut/e5867b13ca521d1b1df6a4a92d7f8a23 to your computer and use it in GitHub Desktop.
FNA shader effect example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework.Graphics; | |
using MyGame.IO; | |
namespace MyGame.Containers | |
{ | |
public sealed class EffectContainer | |
{ | |
public Effect SoftLightEffect { get; } | |
public EffectContainer(GraphicsDevice graphicsDevice) | |
{ | |
SoftLightEffect = EffectLoader.LoadEffect(graphicsDevice, "Effects/SoftLightEffect.fxb"); | |
} | |
public void Unload() | |
{ | |
SoftLightEffect.Dispose(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework.Graphics; | |
namespace MyGame.IO | |
{ | |
public static class EffectLoader | |
{ | |
public static Effect LoadEffect(GraphicsDevice graphicsDevice, string path) | |
{ | |
return new Effect(graphicsDevice, ContentIO.ReadAllBytes(path)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Soft light blend mode using Pegtop's formula. | |
sampler SourceTexture : register(s0); | |
sampler BlendTexture : register(s1); | |
float blendAlpha; | |
float3 Blend(float3 base, float3 blend) | |
{ | |
return (1.0 - 2.0 * blend) * base * base + 2.0 * base * blend; | |
} | |
float3 Blend(float3 base, float3 blend, float opacity) | |
{ | |
return (Blend(base, blend) * opacity + base * (1.0 - opacity)); | |
} | |
float4 PixelShaderFunction(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0 | |
{ | |
float4 sourceColor = tex2D(SourceTexture, texCoord); | |
float4 blendColor = tex2D(BlendTexture, texCoord); | |
return float4(Blend(sourceColor.rgb, blendColor.rgb, blendAlpha), 1.0); | |
} | |
technique Technique1 | |
{ | |
pass Pass1 | |
{ | |
PixelShader = compile ps_3_0 PixelShaderFunction(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment