From f479420743532c84652f3e9b9809fe5259731926 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Sat, 16 Jul 2022 01:40:18 +0200 Subject: [PATCH] Deathly afraid of loosing this code --- Effects/CalamityShaders.cs | 4 ++ Effects/Compiler/SwingEffect.fx | 73 ++++++++++++++++++++++++++++++ Effects/Compiler/SwingEffect.xnb | Bin 0 -> 3308 bytes ExtraTextures/TestTexture.png | Bin 0 -> 525 bytes ExtraTextures/TestTextureTall.png | Bin 0 -> 409 bytes 5 files changed, 77 insertions(+) create mode 100644 Effects/Compiler/SwingEffect.fx create mode 100644 Effects/Compiler/SwingEffect.xnb create mode 100644 ExtraTextures/TestTexture.png create mode 100644 ExtraTextures/TestTextureTall.png diff --git a/Effects/CalamityShaders.cs b/Effects/CalamityShaders.cs index 5a4b803fae..96e6a97a3e 100644 --- a/Effects/CalamityShaders.cs +++ b/Effects/CalamityShaders.cs @@ -143,6 +143,10 @@ namespace CalamityMod.Effects screenRef = new Ref(CalamityMod.Instance.Assets.Request("Effects/RoverDriveShield", AssetRequestMode.ImmediateLoad).Value); Filters.Scene["RoverDriveShield"] = new Filter(new ScreenShaderData(screenRef, "ShieldPass"), EffectPriority.High); Filters.Scene["RoverDriveShield"].Load(); + + screenRef = new Ref(CalamityMod.Instance.Assets.Request("Effects/Compiler/SwingEffect", AssetRequestMode.ImmediateLoad).Value); + Filters.Scene["SwingRotationEffect"] = new Filter(new ScreenShaderData(screenRef, "SwingPass"), EffectPriority.High); + Filters.Scene["SwingRotationEffect"].Load(); } } } diff --git a/Effects/Compiler/SwingEffect.fx b/Effects/Compiler/SwingEffect.fx new file mode 100644 index 0000000000..994c747342 --- /dev/null +++ b/Effects/Compiler/SwingEffect.fx @@ -0,0 +1,73 @@ +float rotation; // The rotation of the sword sprite. +//float2 spriteDimensions; //The size of the sprite (necessary as the shader only gives us a map between 0 and 1). +float pommelToOriginPercent; //I have no idea for a better variable name. This is the perccentage of the full lenght between the rotation origin and the sword's tip that is an empty gap between it and the bottom left of the sword. + +texture sampleTexture; +sampler2D Texture1Sampler = sampler_state { texture = ; magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR; AddressU = wrap; AddressV = wrap; }; + +float realCos(float value) +{ + return sin(value + 1.57079); +} + +//This only works on square sprites. Commented out code can be found for attempts to make it work on non square sprites but my god is it a headache to work with transform matrices +//In a context like that. Gee. +//At this point i barely understand why some of it works but yea it just happens. +float4 main(float2 uv : TEXCOORD) : COLOR +{ + if (uv.x < 0.005 || uv.x > 1 - 0.005 || uv.y < 0.005 || uv.y > 1 - 0.005) + return float4(1, 0, 0, 1); + if (length(uv - float2(0.5, 0.5)) < 0.01) + return float4(1, 0, 0, 1); + + //Transformation matrices + //float2x2 squareify = float2x2(1, 0, 0, 1); + //float2x2 unsquareify = float2x2(1, 0, 0, 1); + + + float2x2 rotate = float2x2(realCos(rotation), -sin(rotation), sin(rotation), realCos(rotation)); + float spriteDiagonal = 1 / (sqrt(2) / 4); + float spriteDiagonalReal = spriteDiagonal * (1 / (1 - pommelToOriginPercent)); + + float2x2 downscale = float2x2(spriteDiagonalReal, 0, 0, spriteDiagonalReal); + float displaceFromOrigin = (1 / (1 - pommelToOriginPercent)) * (1 - 0.5 * (1 - pommelToOriginPercent)); //It's complicated as fuck to explain, but this gets the distance of the displacement. + + /* + //Since the rotation effect stretches and squishes sprites that arent square, we have to turn it into a square before the rotation (and then unsquareify it after its done) + if (spriteDimensions.x != spriteDimensions.y) + { + float aspectRatio = max(spriteDimensions.x, spriteDimensions.y) / min(spriteDimensions.x, spriteDimensions.y); + //Rescale matrix + squareify = float2x2(1, 0, 0, aspectRatio); + unsquareify = float2x2(1 / aspectRatio, 0, 0, 1 / aspectRatio); + + if (spriteDimensions.x > spriteDimensions.y) + { + squareify = float2x2(aspectRatio, 0, 0, 1); + unsquareify = float2x2(1 / aspectRatio, 0, 0, 1 / aspectRatio); + } + } + */ + + uv += float2(-0.5, -0.5); //remap the uv to (-0.5, -0.5) - (0.5, 0.5) for trig to work. + //uv = mul(uv, squareify); + uv = mul(uv, rotate); + uv = mul(uv, downscale); + //uv = mul(uv, unsquareify); + uv += displaceFromOrigin; + uv += float2(0.5, 0.5); //remap the uv properly + + //Crop (Attempting to sample a texture with coordinates that arent between 0 to 1 wraps it around + if (uv.x < 0 || uv.x >= 1 || uv.y < 0 || uv.y >= 1) + return float4(0, 0, 0, 0); + + return tex2D(Texture1Sampler, uv); +} + +technique Technique1 +{ + pass SwingPass + { + PixelShader = compile ps_2_0 main(); + } +} diff --git a/Effects/Compiler/SwingEffect.xnb b/Effects/Compiler/SwingEffect.xnb new file mode 100644 index 0000000000000000000000000000000000000000..196fd3a58262a72f348f5fdb73b1332f0356b611 GIT binary patch literal 3308 zcmbtWPiUKE7=ORCNz<+_J?R9&)a@{#hP0;AAtklx>V{FXCe9h?YY0~%6`IHgxE(nrgq zSiO3y&;k}Qxmz;+Ic?`+`#%b~QLk3rO0NE@SH4`XWn8a_0YD!F4}(7h8)EEnaI;Wt zRNS0L#?_|(YNwycaa8>M>>&hw%|JMQk7Wn^ zotF2o-U4rF`7YMqz}s3LM@$&}jh3gdKMnp?%W3S-f%$y(^ro|NmY#gJ5$HK{o5mny zH_NrlnL@KEc1y<~=iFkcR(@;4ofakfBc+Vzo*V@mN??;Q0@eoj155rK`w|mq3hTjB z^{3Wz`9#~h5bl7fqbpaNdLSdGc_0J5-a+rANabEwd;;R@G(yKX%AhFm#lbwU)GvTZ zgEJZyyT zWAd-$`?r3s#@B8pRjND56VEK={+@h%L9L@dDgAzO@A>!EzvzS(luRtYisgJd^%xY6 zn3yonotSyfNh4~3eW7FeGCG#$|NI zu4?&l*|}_7Dwn@5^N{SkjOVi%#7I99^Y9%_qVG7^_BAu2oxZ%Hq{H6*c$HQ8+P&-E z9%znsF22V|tgCa~bLQ-UbcnI-tE;Ta&U22we0Fp8S%+kwmBD$0m5WvD=w~uGyZiNE z&rW0TVbo=S302+>Zi{P1p)`VjO`{5W>|vs@P!}8_=;z7nVBaWWEuJC5-r98#Mtz?TN+gL;bqMxh3l=`w--A8#9#H0T;4h zo=#q3r@VzaTOHQ2`h(Awjv@9{D@OHZVpN_0Fbm{i|0YghVXX1N{+jVI^&QxaZc)xR zBKsHNuCC8Ks}ko?c~qYYlc^ODqwp7~rTS8H56L>r;fRQ|bH<##%1d2O)*cb6Hoo}u z+X)IaFUo^`I)eDb^`PjOVegz5 W?g9Ghwt3(9zm@&1hW*I@$NvFrcb5bJ literal 0 HcmV?d00001 diff --git a/ExtraTextures/TestTexture.png b/ExtraTextures/TestTexture.png new file mode 100644 index 0000000000000000000000000000000000000000..2e1de03467be9afb612c8cab27bfbf27bb64b50c GIT binary patch literal 525 zcmeAS@N?(olHy`uVBq!ia0vp^Eg;On1|$PrnwJA9#^NA%Cx&(BWL^T<$30yfLn`LH zy}8jV%}~VcV)4=M^-u3Bb#7?h+O_WLoyMPCnr6oj+N_^>{lK?xm;W==P5=J6{rRu% z>#tuQ{%G_4(m($<-_NxwusCuw{rF>lwj`K|DX0Za;HFx0*9kO6Hw0Px$fb7 zt^Ysob_w4M)?5X^Hi1#*du}1X&b0oItwsCBJ2F->(PbIttt~ zdUL)YErvr6WGY9K0Lw$&!*y@JU57Y80I2X#blYFab3NP#U18SmNxiZD?!R3yE{h}3 zV*A503l(>O3;^4-o#*$~+wtNsF4*F93u8SNaftiDZrfk>`?V9;J5Y=N@uVMXeV_z! zD%fq;^Xg+^+$IH}#q%GY*&z@EaVo@Zx8APj2D=<)vGE*9@uqGv1Ler`e62*FBR(-c XYxpIw+0fPr7{3gju6{1-oD!MBu#S=*M7^$f9j~@cZtL_Eb&FVg3UNz-6e^$Q6 RB4AiEc)I$ztaD0e0s!rzqP_qC literal 0 HcmV?d00001