diff --git a/Effects/CalamityShaders.cs b/Effects/CalamityShaders.cs index 6c10a8a2eb..285f77211c 100644 --- a/Effects/CalamityShaders.cs +++ b/Effects/CalamityShaders.cs @@ -126,6 +126,9 @@ namespace CalamityMod.Effects // Used to render all-encompassing fog in the Floral Paradise biome. internal static Effect FogShader; + // Used to render background water features in the Floral Paradise biome. + internal static Effect WaterfallShader; + // Fusable particles. See the FusableParticleManager for comments on how this system works. // These shaders are leveraged to render the results of the fusable particle simulation to the screen. // The "Base" shader draws the particles themselves. @@ -330,6 +333,9 @@ namespace CalamityMod.Effects FogShader = LoadShader("ScreenShaders/Fog"); RegisterMiscShader(FogShader, "DyePass", "Fog"); + WaterfallShader = LoadShader("WaterfallShader"); + RegisterMiscShader(WaterfallShader, "TrailPass", "Waterfall"); + // These two shaders are often (but not always) used together. BaseFusableParticleEdgeShader = LoadShader("ParticleFusion/BaseFusableParticleEdgeShader"); RegisterMiscShader(BaseFusableParticleEdgeShader, "ParticlePass", "BaseFusableParticleEdge"); diff --git a/Effects/WaterfallShader.fx b/Effects/WaterfallShader.fx new file mode 100644 index 0000000000..de07ec82ee --- /dev/null +++ b/Effects/WaterfallShader.fx @@ -0,0 +1,66 @@ +sampler uImage0 : register(s0); +sampler uImage1 : register(s1); +sampler uImage2 : register(s2); +float3 uColor; +float3 uSecondaryColor; +float uOpacity; +float uSaturation; +float uRotation; +float uTime; +float4 uSourceRect; +float2 uWorldPosition; +float uDirection; +float3 uLightSource; +float2 uImageSize0; +float2 uImageSize1; +matrix uWorldViewProjection; + +struct VertexShaderInput +{ + float4 Position : POSITION0; + float4 Color : COLOR0; + float2 TextureCoordinates : TEXCOORD0; +}; + +struct VertexShaderOutput +{ + float4 Position : SV_POSITION; + float4 Color : COLOR0; + float2 TextureCoordinates : TEXCOORD0; +}; + +VertexShaderOutput VertexShaderFunction(in VertexShaderInput input) +{ + VertexShaderOutput output = (VertexShaderOutput)0; + float4 pos = mul(input.Position, uWorldViewProjection); + output.Position = pos; + + output.Color = input.Color; + output.TextureCoordinates = input.TextureCoordinates; + + return output; +} + +// The X coordinate is the trail completion, the Y coordinate is the same as any other. +// This is simply how the primitive TextCoord is layed out in the C# code. +// Inputted images go into uImage1 sampler, in case you have a noise map or something similar. +float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 +{ + float4 color = input.Color; + float2 coords = input.TextureCoordinates; + float2 pixellatedCoords = round(coords * 20) / 20; + float4 noiseColor = tex2D(uImage1, float2(pixellatedCoords.x - uTime * 1.3, pixellatedCoords.y) * float2(0.5, 1)); + float4 noiseColor2 = tex2D(uImage1, float2(pixellatedCoords.x - uTime * 1.1, pixellatedCoords.y + uTime * 0.44) * float2(0.5, 1)); + float4 baseColor = lerp(noiseColor, noiseColor2, sin(uTime * 5.1 + coords.x * 3.4) * 0.5 + 0.5); + baseColor = pow(baseColor, 2); + return lerp(color * 0.4, 1, baseColor.r) * uOpacity; +} + +technique Technique1 +{ + pass TrailPass + { + VertexShader = compile vs_2_0 VertexShaderFunction(); + PixelShader = compile ps_2_0 PixelShaderFunction(); + } +} diff --git a/Effects/WaterfallShader.xnb b/Effects/WaterfallShader.xnb new file mode 100644 index 0000000000..7c180ce061 Binary files /dev/null and b/Effects/WaterfallShader.xnb differ diff --git a/ExtraTextures/WorleyNoise.png b/ExtraTextures/WorleyNoise.png new file mode 100644 index 0000000000..f614e21acc Binary files /dev/null and b/ExtraTextures/WorleyNoise.png differ diff --git a/ILEditing/MechanicILChanges.cs b/ILEditing/MechanicILChanges.cs index daddb78d3d..c8848df22e 100644 --- a/ILEditing/MechanicILChanges.cs +++ b/ILEditing/MechanicILChanges.cs @@ -14,6 +14,7 @@ using CalamityMod.NPCs.Ravager; using CalamityMod.Particles; using CalamityMod.Projectiles; using CalamityMod.Systems; +using CalamityMod.Waterfalls; using CalamityMod.Waters; using CalamityMod.World; using CalamityMod.Projectiles.Typeless; @@ -706,6 +707,9 @@ namespace CalamityMod.ILEditing private static void DrawFog(float intensity) { + Main.spriteBatch.EnterShaderRegion(); + WaterfallRenderer.DrawWaterfalls(); + Texture2D fogTexture = ModContent.GetTexture("Terraria/Misc/Perlin"); Vector2 scale = new Vector2(Main.screenWidth, Main.screenHeight) / fogTexture.Size(); diff --git a/Particles/GeneralParticleHandler.cs b/Particles/GeneralParticleHandler.cs index 1c8907b360..c1e4f5c96a 100644 --- a/Particles/GeneralParticleHandler.cs +++ b/Particles/GeneralParticleHandler.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using Terraria; +using Terraria.ID; using Terraria.ModLoader; using Terraria.ModLoader.Core; @@ -15,6 +16,7 @@ namespace CalamityMod.Particles public static class GeneralParticleHandler { private static List particles; + private static Queue particlesToSpawnNextFrame; //List containing the particles to delete private static List particlesToKill; //Static list for details concerning every particle type @@ -50,6 +52,7 @@ namespace CalamityMod.Particles internal static void Load() { particles = new List(); + particlesToSpawnNextFrame = new Queue(); particlesToKill = new List(); particleTypes = new Dictionary(); particleTextures = new Dictionary(); @@ -65,6 +68,7 @@ namespace CalamityMod.Particles internal static void Unload() { particles = null; + particlesToSpawnNextFrame = null; particlesToKill = null; particleTypes = null; particleTextures = null; @@ -81,8 +85,8 @@ namespace CalamityMod.Particles { // Don't queue particles if the game is paused. // This precedent is established with how Dust instances are created. - //Don't spawn particles if on the server side either, or if the particles dict is somehow null - if (Main.gamePaused || Main.dedServ || particles == null) + // Don't spawn particles if on the server either, or if the particles dictionary is somehow null. + if (Main.gamePaused || Main.netMode == NetmodeID.Server || particles == null) return; if (particles.Count >= CalamityConfig.Instance.ParticleLimit && !particle.Important) @@ -92,8 +96,22 @@ namespace CalamityMod.Particles particle.Type = particleTypes[particle.GetType()]; } + public static void QueueParticleForNextFrame(Particle particle) + { + // Don't queue particles if the game is paused. + // This precedent is established with how Dust instances are created. + // Don't spawn particles if on the server side, or if the particles dictionary is somehow null. + if (Main.gamePaused || Main.netMode == NetmodeID.Server || particles == null) + return; + + particlesToSpawnNextFrame.Enqueue(particle); + } + public static void Update() { + while (particlesToSpawnNextFrame.Count > 0) + SpawnParticle(particlesToSpawnNextFrame.Dequeue()); + foreach (Particle particle in particles) { if (particle == null) diff --git a/Particles/WaterFoam.png b/Particles/WaterFoam.png new file mode 100644 index 0000000000..71ed6ff84b Binary files /dev/null and b/Particles/WaterFoam.png differ diff --git a/Particles/WaterFoamParticle.cs b/Particles/WaterFoamParticle.cs new file mode 100644 index 0000000000..3173719bb1 --- /dev/null +++ b/Particles/WaterFoamParticle.cs @@ -0,0 +1,62 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Particles +{ + public class WaterFoamParticle : Particle + { + public Color InitialColor; + public bool HasCreatedFoam = false; + public override bool SetLifetime => true; + public override bool UseCustomDraw => true; + public override bool UseAdditiveBlend => true; + + public override string Texture => "CalamityMod/Particles/WaterFoam"; + + public WaterFoamParticle(Vector2 relativePosition, Vector2 velocity, int lifetime, float scale, Color color) + { + Position = relativePosition; + Velocity = velocity; + Scale = scale; + Lifetime = lifetime; + Color = InitialColor = color; + Rotation = Main.rand.NextFloat(MathHelper.TwoPi); + } + + public override void Update() + { + Color = Color.Lerp(InitialColor, Color.Transparent, (float)Math.Pow(LifetimeCompletion, 1.7D)); + + if (Collision.WetCollision(Position, 1, 1)) + { + if (!HasCreatedFoam) + { + Vector2 foamVelocity = Main.rand.NextVector2Circular(2f, 0.3f); + if (foamVelocity.Y < -0.2f) + foamVelocity.Y = -0.2f; + + // Spawning the particle directly will result in the particle list collection being modified prematurely within the update loop. + // Doing it the next frame results in a single-frame buffer, but that shouldn't really be a problem and it ensures that the loop is + // not interrupted in unanticipated ways. + GeneralParticleHandler.QueueParticleForNextFrame(new MediumMistParticle(Position, foamVelocity, Color.LightCyan, Color.White, 0.2f, 255f, 0.03f)); + HasCreatedFoam = true; + } + + Velocity.Y *= 0.7f; + Time += 4; + } + else + Velocity.Y = MathHelper.Clamp(Velocity.Y + 0.12f, -8f, 12f); + } + + public override void CustomDraw(SpriteBatch spriteBatch) + { + float brightness = (float)Math.Pow(Lighting.Brightness((int)(Position.X / 16f), (int)(Position.Y / 16f)), 0.15); + Texture2D texture = ModContent.GetTexture(Texture); + spriteBatch.Draw(texture, Position - Main.screenPosition, null, Color * brightness, Rotation, texture.Size() * 0.5f, Scale, 0, 0f); + } + } +} diff --git a/Tiles/FloralParadise/WaterfallCreator.cs b/Tiles/FloralParadise/WaterfallCreator.cs new file mode 100644 index 0000000000..ed58656720 --- /dev/null +++ b/Tiles/FloralParadise/WaterfallCreator.cs @@ -0,0 +1,41 @@ +using CalamityMod.Particles; +using Microsoft.Xna.Framework; +using Terraria; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Tiles.FloralParadise +{ + public class WaterfallCreator : ModTile + { + public override void SetDefaults() + { + Main.tileSolid[Type] = false; + Main.tileBlockLight[Type] = true; + Main.tileBrick[Type] = true; + + CalamityUtils.MergeWithGeneral(Type); + CalamityUtils.MergeWithOres(Type); + CalamityUtils.MergeWithFloralParadise(Type); + + soundType = SoundID.Tink; + + AddMapEntry(new Color(33, 56, 27)); + } + + public override void NearbyEffects(int i, int j, bool closer) + { + if (Main.netMode == NetmodeID.Server || !Main.rand.NextBool() || Main.gamePaused) + return; + + for (int k = 0; k < 16; k++) + { + Vector2 waterVelocity = -Vector2.UnitY.RotatedByRandom(0.29f) * Main.rand.NextFloat(1f, 2.4f); + float waterScale = Main.rand.NextFloat(0.08f, 0.12f); + Color waterColor = Color.Lerp(Color.Cyan, Color.SkyBlue, Main.rand.NextFloat()); + Vector2 particlePosition = new Vector2(i + 0.5f, j + 0.5f) * 16f + Vector2.UnitY * Main.rand.NextFloat(24f); + GeneralParticleHandler.SpawnParticle(new WaterFoamParticle(particlePosition, waterVelocity, 150, waterScale, waterColor)); + } + } + } +} diff --git a/Tiles/FloralParadise/WaterfallCreator.png b/Tiles/FloralParadise/WaterfallCreator.png new file mode 100644 index 0000000000..622533204a Binary files /dev/null and b/Tiles/FloralParadise/WaterfallCreator.png differ diff --git a/Waterfalls/WaterfallRenderer.cs b/Waterfalls/WaterfallRenderer.cs new file mode 100644 index 0000000000..15f666b4c0 --- /dev/null +++ b/Waterfalls/WaterfallRenderer.cs @@ -0,0 +1,111 @@ +using CalamityMod.Tiles.FloralParadise; +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using Terraria; +using Terraria.Graphics.Shaders; +using Terraria.ModLoader; + +namespace CalamityMod.Waterfalls +{ + public static class WaterfallRenderer + { + private static PrimitiveTrail renderer = null; + + private static Vector2 currentWaterfallPosition; + + private static float currentWaterfallBrightness; + + private static float currentWaterfallHeight; + + public static PrimitiveTrail Renderer + { + get + { + if (renderer is null) + renderer = new PrimitiveTrail(PrimitiveWidthFunction, PrimitiveColorFunction, PrimitiveTrail.RigidPointRetreivalFunction, GameShaders.Misc["CalamityMod:Waterfall"]); + return renderer; + } + } + + internal static float PrimitiveWidthFunction(float completionRatio) + { + float baseWidth = MathHelper.Lerp(16f, 6f, Utils.InverseLerp(36f, 216f, currentWaterfallHeight, true)); + float positionOffset = currentWaterfallPosition.X * 0.17f % 300f + currentWaterfallPosition.Y * 0.33f % 300f; + float x1Offset = (float)(Math.Sin(-Main.GlobalTime * 3f + completionRatio * 9f + positionOffset) * 0.5f + 0.5f) * baseWidth * 0.2f; + float x2Offset = (float)(Math.Sin(-Main.GlobalTime * 6f + completionRatio * 6f + positionOffset) * 0.5f + 0.5f) * baseWidth * 0.25f; + + if (completionRatio < 0.2f) + return MathHelper.SmoothStep(1f, baseWidth + x2Offset - x1Offset, Utils.InverseLerp(0f, 0.2f, completionRatio, true)); + return baseWidth + x2Offset + MathHelper.SmoothStep(0f, baseWidth * 0.85f, Utils.InverseLerp(0.84f, 0.96f, completionRatio, true)) - x1Offset; + } + + internal static Color PrimitiveColorFunction(float completionRatio) + { + Color c = Color.Lerp(Color.Cyan, new Color(1f, 1f, 1f, 0f), (float)Math.Pow(Utils.InverseLerp(0.75f, 1f, completionRatio, true), 0.68)); + return c; + } + + public static IEnumerable FindNearbyWaterfallRenderers() + { + int waterfallGeneratorID = ModContent.TileType(); + Point center = Main.LocalPlayer.Center.ToTileCoordinates(); + for (int i = -Main.screenWidth / 16 - 10; i < Main.screenWidth / 16 + 10; i++) + { + for (int j = -Main.screenHeight / 16 - 10; j < Main.screenHeight / 16 + 10; j++) + { + Tile tile = CalamityUtils.ParanoidTileRetrieval(center.X + i, center.Y + j); + if (tile.type != waterfallGeneratorID || !tile.active()) + continue; + + yield return new Vector2(center.X + i, center.Y + j); + } + } + } + + public static void DrawWaterfalls() + { + foreach (Vector2 drawTilePosition in FindNearbyWaterfallRenderers()) + { + Vector2 drawPosition = drawTilePosition * 16f; + Vector2 bottom = drawPosition; + for (int i = 0; i < 16; i++) + { + if (CalamityUtils.ParanoidTileRetrieval((int)drawTilePosition.X, (int)drawTilePosition.Y + i).liquid > 0) + { + bottom.Y += i * 16f; + break; + } + } + + currentWaterfallHeight = MathHelper.Distance(bottom.Y, drawPosition.Y); + currentWaterfallBrightness = Lighting.Brightness((int)(drawPosition.X / 16f), (int)(drawPosition.Y / 16f)); + currentWaterfallPosition = drawPosition + Vector2.One * 8f; + + Vector2[] drawPoints = new Vector2[] + { + drawPosition, + Vector2.Lerp(drawPosition, bottom, 0.5f), + bottom + }; + + // Determine the position of the waterfall's ambience sound. + float distanceFromWaterfall = Main.LocalPlayer.Distance(drawPosition + Vector2.One * 8f); + float distanceFromOldWaterfall = 100000f; + if (Main.ambientWaterfallX != -1f && Main.ambientWaterfallY != -1f) + distanceFromOldWaterfall = Main.LocalPlayer.Distance(new Vector2(Main.ambientWaterfallX, Main.ambientWaterfallY)); + + if (distanceFromOldWaterfall > distanceFromWaterfall) + { + Main.ambientWaterfallX = drawPosition.X + 8f; + Main.ambientWaterfallY = drawPosition.Y + 8f; + Main.ambientWaterfallStrength = MathHelper.Max(Main.ambientWaterfallStrength, Utils.InverseLerp(740f, 300f, distanceFromWaterfall, true)); + } + + GameShaders.Misc["CalamityMod:Waterfall"].SetShaderTexture(ModContent.GetTexture("CalamityMod/ExtraTextures/WorleyNoise")); + GameShaders.Misc["CalamityMod:Waterfall"].UseOpacity((float)Math.Pow(currentWaterfallBrightness, 0.62)); + Renderer.Draw(drawPoints, Vector2.One * 8f - Main.screenPosition, 80); + } + } + } +} diff --git a/World/Minibiomes/FloralParadiseMinibiome.cs b/World/Minibiomes/FloralParadiseMinibiome.cs index 2168416d56..59b359d0b8 100644 --- a/World/Minibiomes/FloralParadiseMinibiome.cs +++ b/World/Minibiomes/FloralParadiseMinibiome.cs @@ -391,9 +391,26 @@ namespace CalamityMod.World.Minibiomes // Create a predetermined quantity of water via recursion. // This starts at the initial water spawn point and the expands outward in all four cardinal directions repeatedly until either no more locations // are valid or the water limit has been reached. - // This water will be settled to create ponds via extraneous code later in the world generation pipeline. int increment = 0; RecursivelyFillAreaWithWater(x, y, waterTileCount, ref increment); + + // Settle water and create waterfalls. + Liquid.QuickWater(3); + WorldGen.WaterCheck(); + Liquid.quickSettle = true; + Liquid.UpdateLiquid(); + WorldGen.WaterCheck(); + Liquid.quickSettle = false; + + for (int dy = 0; dy < 50; dy++) + { + if (CalamityUtils.ParanoidTileRetrieval(x, y + dy).liquid > 0) + { + Main.tile[x, y + dy - 8].active(true); + Main.tile[x, y + dy - 8].type = (ushort)ModContent.TileType(); + break; + } + } } }