From f2fa8537be3843bcfb72114640d6f835e2b62717 Mon Sep 17 00:00:00 2001 From: fryzahh Date: Mon, 19 May 2025 03:47:54 -0400 Subject: [PATCH] Underwater distortion and rays effect - Gave the ModMenu background a new underwater distortion effect and added underwater rays at the top of the screen --- Effects/CalamityShaders.cs | 6 ++++ Effects/UnderwaterRaysShader.fx | 48 +++++++++++++++++++++++++ Effects/UnderwaterRaysShader.xnb | Bin 0 -> 2764 bytes MainMenu/CalamityMainMenu_Sunken.cs | 52 ++++++++++++++++++++++++---- 4 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 Effects/UnderwaterRaysShader.fx create mode 100644 Effects/UnderwaterRaysShader.xnb diff --git a/Effects/CalamityShaders.cs b/Effects/CalamityShaders.cs index 1b283d531c..480806437e 100644 --- a/Effects/CalamityShaders.cs +++ b/Effects/CalamityShaders.cs @@ -216,6 +216,9 @@ namespace CalamityMod.Effects #region fryzahh's Shaders // A simple shader which distorts an image using a provided texture of choice. internal static Effect BasicTextureDistortionShader; + + // The underwater rays seen at the top of the Sunken Sea Mod Menu. + internal static Effect UnderwaterRaysShader; #endregion public override void PostSetupContent() @@ -429,6 +432,9 @@ namespace CalamityMod.Effects #region Loading fryzahh's Shaders BasicTextureDistortionShader = LoadShader("BasicTextureDistortionShader"); RegisterMiscShader(BasicTextureDistortionShader, "DistortionPass", "BasicTextureDistortion"); + + UnderwaterRaysShader = LoadShader("UnderwaterRaysShader"); + RegisterMiscShader(UnderwaterRaysShader, "UnderwaterRayPass", "UnderwaterRays"); #endregion } diff --git a/Effects/UnderwaterRaysShader.fx b/Effects/UnderwaterRaysShader.fx new file mode 100644 index 0000000000..652afbcca4 --- /dev/null +++ b/Effects/UnderwaterRaysShader.fx @@ -0,0 +1,48 @@ +sampler baseTexture : register(s0); + +float time; +float fadeOutMargin; +float overallOpacity; +float pixelationAmount; +float scrollSpeedX; +float scrollSpeedY; +float2 noiseScale; +float3 rayColor; + +float4 PixelShaderFunction(float4 sampleColor : COLOR0, float2 coords : TEXCOORD0) : COLOR0 +{ + // Pixelation. + coords = round(coords * pixelationAmount) / pixelationAmount; + + // Distort the coords into a sine wave. + coords.y += sin(coords.x + time * 0.25) * 0.05; + + // Get the correct scale for our two noise maps. + float2 scaledCoords = coords * noiseScale; + + // Calculate the offsets for each noise map. + float2 noiseOffset1 = float2(time * scrollSpeedX, time * scrollSpeedY); + float2 noiseOffset2 = float2(time * -scrollSpeedX, time * -scrollSpeedY); + + // Calculate the colors of the two noise maps. + float4 noiseTexture1 = tex2D(baseTexture, scaledCoords + noiseOffset1); + float4 noiseTexture2 = tex2D(baseTexture, scaledCoords + noiseOffset2); + + // Add the noise maps together to layer them over each other. + // Ensure to add them at 50% intensity to avoid making a super bright effect. + float combinedNoise = (noiseTexture1.r * 0.5) + (noiseTexture2.r * 0.5); + + // Fade out at a certain margin. + float opacity = (1 - (coords.y * 2 - fadeOutMargin)); + + return (float4(rayColor, 1) * combinedNoise * opacity) * overallOpacity; + +} + +technique Technique1 +{ + pass UnderwaterRayPass + { + PixelShader = compile ps_2_0 PixelShaderFunction(); + } +} diff --git a/Effects/UnderwaterRaysShader.xnb b/Effects/UnderwaterRaysShader.xnb new file mode 100644 index 0000000000000000000000000000000000000000..9faf1094f530ae9c40f39d69e8e546d19608f616 GIT binary patch literal 2764 zcmc&$&2Jk;6rbJ1jT5^#Ko60qgoQvVm1U(NL5WaE8>=A^E)8*v+(T71&V;PG*){7m zNhA(i!J$YTis&D(+>p&77vK`1T#Bh!Ry`%H`cjZ z$e-r@j%zeF&3dzt|A4zq)3IkKi{tcH$d_7{*K+x+&0C&pScUv@Yt1t2@ABPM=Qg)z zrza=s8`Eq2(mJ0SpPG7u5oxbtcE4sG?T-poerUsCFRM!#W;O^uFV*BT3u(=0@>RY= zRoOsfDJLFC4w;xJ1@_D>PIBzKLrO%d?{mO5(1+_S@0#I$WZKZ5T{M1@!p*OLcmQ`tRzFuRg zR1#BuwwN0*oxu>@f&4VE`_N_IbrwJ-+5_w8422wkkHE)Zoec@>_Xv9| zuvDL420=PI(}$fGSXy8gAX7f|&JD6GZ1g9o|ITzeG(*)Mxg~>gt~#3V)stNRxJyr6 zljLl5I#DdulGJG4nz%L2k~b`WVdeGwV!2YzS2pQ+%a@$3hG{{VDNejuoSB*^PE1cu zvR?R*Ew5ayXnJj-7nYMf_zP(XOWq-k<>|awV45-2^}+YR0azEP+z-MI1SaQ4JtXW{ zVDe1=7GX~<5^MXn9`?NhC-^J&8|(zTcoR>;cm7NNvgluB3Cl@Hz3=hABSYZpbaw!c5jg61S>xthZii_8#vE_hpqIe{kVn#^-kuULSz7$8b zh{#XYJvlG=X2}smzVf{s?VDqqi=G)GwQh_NuFjWoQC^Mp=Zb5^anG8IzPlcQav*y7 z-p5z-Fv|si=RW)MmA4<{e!l!5w{$@G2=HGH%d@auX3Q7+LU;zacJ}CdBlE+Lo%Y4I zzPNjLhVU%#PP@Gi|DPiNxtHZ~eB7hGNWRC?h!=R;N1p<$5&r}5>F(j34Hygk7ll0w z>?I%lz*8&iq~D=fjin*Sv4bT08scCJQholaPaE~B0o9cGDX#6y`cfWdZP>%$HOxt4 zy0B+hH^_pepN<+;AHRzNd~;Gg%teP@jqH?%@G9l~M$VnE_MH*rmi`0&P?S%P)}^to zKakXrYb8tvHjnjaEQcIjpQWX)Lx)Z1a=jY$Dd{Km2epLGFp`hPhJb~B8aZVQ("CalamityMod/MainMenu/SunkenMenuBackground").Value; + Texture2D backgroundTexture = ModContent.Request("CalamityMod/MainMenu/SunkenMenuBackground").Value; // Calculate the draw position offset and scale in the event that someone is using a non-16:9 monitor Vector2 drawOffset = Vector2.Zero; - float xScale = (float)Main.screenWidth / texture.Width; - float yScale = (float)Main.screenHeight / texture.Height; + float xScale = (float)Main.screenWidth / backgroundTexture.Width; + float yScale = (float)Main.screenHeight / backgroundTexture.Height; float scale = xScale; // if someone's monitor isn't in wacky dimensions, no calculations need to be performed at all @@ -71,15 +72,54 @@ namespace CalamityMod.MainMenu if (yScale > xScale) { scale = yScale; - drawOffset.X -= (texture.Width * scale - Main.screenWidth) * 0.5f; + drawOffset.X -= (backgroundTexture.Width * scale - Main.screenWidth) * 0.5f; } else // The opposite is true if someone's monitor is widescreen - drawOffset.Y -= (texture.Height * scale - Main.screenHeight) * 0.5f; + drawOffset.Y -= (backgroundTexture.Height * scale - Main.screenHeight) * 0.5f; } + + // Distort the background slightly to make it appear as if it's properly underwater. + MiscShaderData distortionShader = GameShaders.Misc["CalamityMod:BasicTextureDistortion"]; + Asset distortionTexture = ModContent.Request("CalamityMod/ExtraTextures/GreyscaleGradients/Swirls"); + spriteBatch.End(); // BLESS THIS. + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.UIScaleMatrix); + + distortionShader.Shader.Parameters["time"].SetValue(Main.GlobalTimeWrappedHourly); + distortionShader.Shader.Parameters["distortionXSpeed"].SetValue(-0.012f); + distortionShader.Shader.Parameters["distortionYSpeed"].SetValue(0.015f); + distortionShader.Shader.Parameters["distortionStrength"].SetValue(0.02f); + distortionShader.Shader.Parameters["noiseScale"].SetValue(0.75f); + distortionShader.SetShaderTexture(distortionTexture); + distortionShader.Apply(); + + spriteBatch.Draw(backgroundTexture, drawOffset, null, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f); + + spriteBatch.End(); + spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.UIScaleMatrix); + + // Draw rays shining down from the top of the screen. + MiscShaderData underwaterRaysShader = GameShaders.Misc["CalamityMod:UnderwaterRays"]; + Asset underwaterRayTexture = ModContent.Request("CalamityMod/ExtraTextures/GreyscaleGradients/Pebbles"); + + spriteBatch.End(); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.None, Main.Rasterizer, null, Main.UIScaleMatrix); + + underwaterRaysShader.Shader.Parameters["time"].SetValue(Main.GlobalTimeWrappedHourly); + underwaterRaysShader.Shader.Parameters["fadeOutMargin"].SetValue(0.58f); + underwaterRaysShader.Shader.Parameters["overallOpacity"].SetValue(1f); + underwaterRaysShader.Shader.Parameters["pixelationAmount"].SetValue(Main.screenWidth * 0.5f); + underwaterRaysShader.Shader.Parameters["scrollSpeedX"].SetValue(0.013f); + underwaterRaysShader.Shader.Parameters["scrollSpeedY"].SetValue(0.006f); + underwaterRaysShader.Shader.Parameters["noiseScale"].SetValue(new Vector2(1.25f, 0.25f)); + underwaterRaysShader.Shader.Parameters["rayColor"].SetValue(Color.LightSkyBlue.ToVector3()); + underwaterRaysShader.Apply(); + + spriteBatch.Draw(underwaterRayTexture.Value, new Rectangle(0, 0, Main.screenWidth, Main.screenHeight), Color.White); + + spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, Main.Rasterizer, null, Main.UIScaleMatrix); - spriteBatch.Draw(texture, drawOffset, null, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f); static Color selectBubbleColor() {