Skip to content

Commit

Permalink
Allow shaders to omit parameters they don't use. No longer throw exce…
Browse files Browse the repository at this point in the history
…ption.
  • Loading branch information
JavidPack committed May 8, 2024
1 parent c226ff6 commit 30b2b9b
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--- src/TerrariaNetCore/Terraria/GameContent/Dyes/ReflectiveArmorShaderData.cs
+++ src/tModLoader/Terraria/GameContent/Dyes/ReflectiveArmorShaderData.cs
@@ -15,7 +_,7 @@
public override void Apply(Entity entity, DrawData? drawData)
{
if (entity == null) {
- base.Shader.Parameters["uLightSource"].SetValue(Vector3.Zero);
+ base.Shader.Parameters["uLightSource"]?.SetValue(Vector3.Zero);
}
else {
float num = 0f;
@@ -53,7 +_,7 @@
value.Y *= 2f;
value.Normalize();
value.Z *= 0.6f;
- base.Shader.Parameters["uLightSource"].SetValue(value);
+ base.Shader.Parameters["uLightSource"]?.SetValue(value);
}

base.Apply(entity, drawData);
19 changes: 19 additions & 0 deletions patches/tModLoader/Terraria/GameContent/TilePaintSystemV2.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@

namespace Terraria.GameContent;

@@ -63,12 +_,12 @@
protected void PrepareShader(int paintColor, TreePaintingSettings settings)
{
Effect tileShader = Main.tileShader;
- tileShader.Parameters["leafHueTestOffset"].SetValue(settings.HueTestOffset);
+ tileShader.Parameters["leafHueTestOffset"]?.SetValue(settings.HueTestOffset);
- tileShader.Parameters["leafMinHue"].SetValue(settings.SpecialGroupMinimalHueValue);
+ tileShader.Parameters["leafMinHue"]?.SetValue(settings.SpecialGroupMinimalHueValue);
- tileShader.Parameters["leafMaxHue"].SetValue(settings.SpecialGroupMaximumHueValue);
+ tileShader.Parameters["leafMaxHue"]?.SetValue(settings.SpecialGroupMaximumHueValue);
- tileShader.Parameters["leafMinSat"].SetValue(settings.SpecialGroupMinimumSaturationValue);
+ tileShader.Parameters["leafMinSat"]?.SetValue(settings.SpecialGroupMinimumSaturationValue);
- tileShader.Parameters["leafMaxSat"].SetValue(settings.SpecialGroupMaximumSaturationValue);
+ tileShader.Parameters["leafMaxSat"]?.SetValue(settings.SpecialGroupMaximumSaturationValue);
- tileShader.Parameters["invertSpecialGroupResult"].SetValue(settings.InvertSpecialGroupResult);
+ tileShader.Parameters["invertSpecialGroupResult"]?.SetValue(settings.InvertSpecialGroupResult);
int index = Main.ConvertPaintIdToTileShaderIndex(paintColor, settings.UseSpecialGroups, settings.UseWallShaderHacks);
tileShader.CurrentTechnique.Passes[index].Apply();
}
@@ -80,7 +_,38 @@

public override void Prepare()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- src/TerrariaNetCore/Terraria/GameContent/TreePaintingSettings.cs
+++ src/tModLoader/Terraria/GameContent/TreePaintingSettings.cs
@@ -15,12 +_,12 @@

public void ApplyShader(int paintColor, Effect shader)
{
- shader.Parameters["leafHueTestOffset"].SetValue(HueTestOffset);
+ shader.Parameters["leafHueTestOffset"]?.SetValue(HueTestOffset);
- shader.Parameters["leafMinHue"].SetValue(SpecialGroupMinimalHueValue);
+ shader.Parameters["leafMinHue"]?.SetValue(SpecialGroupMinimalHueValue);
- shader.Parameters["leafMaxHue"].SetValue(SpecialGroupMaximumHueValue);
+ shader.Parameters["leafMaxHue"]?.SetValue(SpecialGroupMaximumHueValue);
- shader.Parameters["leafMinSat"].SetValue(SpecialGroupMinimumSaturationValue);
+ shader.Parameters["leafMinSat"]?.SetValue(SpecialGroupMinimumSaturationValue);
- shader.Parameters["leafMaxSat"].SetValue(SpecialGroupMaximumSaturationValue);
+ shader.Parameters["leafMaxSat"]?.SetValue(SpecialGroupMaximumSaturationValue);
- shader.Parameters["invertSpecialGroupResult"].SetValue(InvertSpecialGroupResult);
+ shader.Parameters["invertSpecialGroupResult"]?.SetValue(InvertSpecialGroupResult);
int index = Main.ConvertPaintIdToTileShaderIndex(paintColor, UseSpecialGroups, UseWallShaderHacks);
shader.CurrentTechnique.Passes[index].Apply();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
@@ -14,10 +_,15 @@
@@ -14,49 +_,54 @@
private Asset<Texture2D> _uImage;
private Vector2 _uTargetPosition = Vector2.One;

Expand All @@ -21,6 +21,65 @@

public virtual void Apply(Entity entity, DrawData? drawData = null)
{
- base.Shader.Parameters["uColor"].SetValue(_uColor);
+ base.Shader.Parameters["uColor"]?.SetValue(_uColor);
- base.Shader.Parameters["uSaturation"].SetValue(_uSaturation);
+ base.Shader.Parameters["uSaturation"]?.SetValue(_uSaturation);
- base.Shader.Parameters["uSecondaryColor"].SetValue(_uSecondaryColor);
+ base.Shader.Parameters["uSecondaryColor"]?.SetValue(_uSecondaryColor);
- base.Shader.Parameters["uTime"].SetValue(Main.GlobalTimeWrappedHourly);
+ base.Shader.Parameters["uTime"]?.SetValue(Main.GlobalTimeWrappedHourly);
- base.Shader.Parameters["uOpacity"].SetValue(_uOpacity);
+ base.Shader.Parameters["uOpacity"]?.SetValue(_uOpacity);
- base.Shader.Parameters["uTargetPosition"].SetValue(_uTargetPosition);
+ base.Shader.Parameters["uTargetPosition"]?.SetValue(_uTargetPosition);
if (drawData.HasValue) {
DrawData value = drawData.Value;
Vector4 value2 = ((!value.sourceRect.HasValue) ? new Vector4(0f, 0f, value.texture.Width, value.texture.Height) : new Vector4(value.sourceRect.Value.X, value.sourceRect.Value.Y, value.sourceRect.Value.Width, value.sourceRect.Value.Height));
- base.Shader.Parameters["uSourceRect"].SetValue(value2);
+ base.Shader.Parameters["uSourceRect"]?.SetValue(value2);
- base.Shader.Parameters["uLegacyArmorSourceRect"].SetValue(value2);
+ base.Shader.Parameters["uLegacyArmorSourceRect"]?.SetValue(value2);
- base.Shader.Parameters["uWorldPosition"].SetValue(Main.screenPosition + value.position);
+ base.Shader.Parameters["uWorldPosition"]?.SetValue(Main.screenPosition + value.position);
- base.Shader.Parameters["uImageSize0"].SetValue(new Vector2(value.texture.Width, value.texture.Height));
+ base.Shader.Parameters["uImageSize0"]?.SetValue(new Vector2(value.texture.Width, value.texture.Height));
- base.Shader.Parameters["uLegacyArmorSheetSize"].SetValue(new Vector2(value.texture.Width, value.texture.Height));
+ base.Shader.Parameters["uLegacyArmorSheetSize"]?.SetValue(new Vector2(value.texture.Width, value.texture.Height));
- base.Shader.Parameters["uRotation"].SetValue(value.rotation * (value.effect.HasFlag(SpriteEffects.FlipHorizontally) ? (-1f) : 1f));
+ base.Shader.Parameters["uRotation"]?.SetValue(value.rotation * (value.effect.HasFlag(SpriteEffects.FlipHorizontally) ? (-1f) : 1f));
- base.Shader.Parameters["uDirection"].SetValue((!value.effect.HasFlag(SpriteEffects.FlipHorizontally)) ? 1 : (-1));
+ base.Shader.Parameters["uDirection"]?.SetValue((!value.effect.HasFlag(SpriteEffects.FlipHorizontally)) ? 1 : (-1));
}
else {
Vector4 value3 = new Vector4(0f, 0f, 4f, 4f);
- base.Shader.Parameters["uSourceRect"].SetValue(value3);
+ base.Shader.Parameters["uSourceRect"]?.SetValue(value3);
- base.Shader.Parameters["uLegacyArmorSourceRect"].SetValue(value3);
+ base.Shader.Parameters["uLegacyArmorSourceRect"]?.SetValue(value3);
- base.Shader.Parameters["uRotation"].SetValue(0f);
+ base.Shader.Parameters["uRotation"]?.SetValue(0f);
}

if (_uImage != null) {
Main.graphics.GraphicsDevice.Textures[1] = _uImage.Value;
- base.Shader.Parameters["uImageSize1"].SetValue(new Vector2(_uImage.Width(), _uImage.Height()));
+ base.Shader.Parameters["uImageSize1"]?.SetValue(new Vector2(_uImage.Width(), _uImage.Height()));
}

if (entity != null)
- base.Shader.Parameters["uDirection"].SetValue((float)entity.direction);
+ base.Shader.Parameters["uDirection"]?.SetValue((float)entity.direction);

if (entity is Player player) {
Rectangle bodyFrame = player.bodyFrame;
- base.Shader.Parameters["uLegacyArmorSourceRect"].SetValue(new Vector4(bodyFrame.X, bodyFrame.Y, bodyFrame.Width, bodyFrame.Height));
+ base.Shader.Parameters["uLegacyArmorSourceRect"]?.SetValue(new Vector4(bodyFrame.X, bodyFrame.Y, bodyFrame.Width, bodyFrame.Height));
- base.Shader.Parameters["uLegacyArmorSheetSize"].SetValue(new Vector2(40f, 1120f));
+ base.Shader.Parameters["uLegacyArmorSheetSize"]?.SetValue(new Vector2(40f, 1120f));
}

Apply();
@@ -74,6 +_,12 @@
public ArmorShaderData UseImage(string path)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
@@ -17,10 +_,15 @@
@@ -17,38 +_,43 @@

public bool ShaderDisabled => _shaderDisabled;

Expand All @@ -21,6 +21,46 @@

public virtual void Apply(Player player, DrawData? drawData = null)
{
if (!_shaderDisabled) {
- base.Shader.Parameters["uColor"].SetValue(_uColor);
+ base.Shader.Parameters["uColor"]?.SetValue(_uColor);
- base.Shader.Parameters["uSaturation"].SetValue(_uSaturation);
+ base.Shader.Parameters["uSaturation"]?.SetValue(_uSaturation);
- base.Shader.Parameters["uSecondaryColor"].SetValue(_uSecondaryColor);
+ base.Shader.Parameters["uSecondaryColor"]?.SetValue(_uSecondaryColor);
- base.Shader.Parameters["uTime"].SetValue(Main.GlobalTimeWrappedHourly);
+ base.Shader.Parameters["uTime"]?.SetValue(Main.GlobalTimeWrappedHourly);
- base.Shader.Parameters["uOpacity"].SetValue(_uOpacity);
+ base.Shader.Parameters["uOpacity"]?.SetValue(_uOpacity);
- base.Shader.Parameters["uTargetPosition"].SetValue(_uTargetPosition);
+ base.Shader.Parameters["uTargetPosition"]?.SetValue(_uTargetPosition);
if (drawData.HasValue) {
DrawData value = drawData.Value;
Vector4 value2 = new Vector4(value.sourceRect.Value.X, value.sourceRect.Value.Y, value.sourceRect.Value.Width, value.sourceRect.Value.Height);
- base.Shader.Parameters["uSourceRect"].SetValue(value2);
+ base.Shader.Parameters["uSourceRect"]?.SetValue(value2);
- base.Shader.Parameters["uWorldPosition"].SetValue(Main.screenPosition + value.position);
+ base.Shader.Parameters["uWorldPosition"]?.SetValue(Main.screenPosition + value.position);
- base.Shader.Parameters["uImageSize0"].SetValue(new Vector2(value.texture.Width, value.texture.Height));
+ base.Shader.Parameters["uImageSize0"]?.SetValue(new Vector2(value.texture.Width, value.texture.Height));
}
else {
- base.Shader.Parameters["uSourceRect"].SetValue(new Vector4(0f, 0f, 4f, 4f));
+ base.Shader.Parameters["uSourceRect"]?.SetValue(new Vector4(0f, 0f, 4f, 4f));
}

if (_uImage != null) {
Main.graphics.GraphicsDevice.Textures[1] = _uImage.Value;
- base.Shader.Parameters["uImageSize1"].SetValue(new Vector2(_uImage.Width(), _uImage.Height()));
+ base.Shader.Parameters["uImageSize1"]?.SetValue(new Vector2(_uImage.Width(), _uImage.Height()));
}

if (player != null)
- base.Shader.Parameters["uDirection"].SetValue((float)player.direction);
+ base.Shader.Parameters["uDirection"]?.SetValue((float)player.direction);

Apply();
}
@@ -67,6 +_,12 @@
public HairShaderData UseImage(string path)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- src/TerrariaNetCore/Terraria/Graphics/Shaders/MiscShaderData.cs
+++ src/tModLoader/Terraria/Graphics/Shaders/MiscShaderData.cs
@@ -19,11 +_,17 @@
@@ -19,31 +_,37 @@
private Vector4 _shaderSpecificData = Vector4.Zero;
private SamplerState _customSamplerState;

Expand All @@ -17,7 +17,60 @@
+
public virtual void Apply(DrawData? drawData = null)
{
base.Shader.Parameters["uColor"].SetValue(_uColor);
- base.Shader.Parameters["uColor"].SetValue(_uColor);
+ base.Shader.Parameters["uColor"]?.SetValue(_uColor);
- base.Shader.Parameters["uSaturation"].SetValue(_uSaturation);
+ base.Shader.Parameters["uSaturation"]?.SetValue(_uSaturation);
- base.Shader.Parameters["uSecondaryColor"].SetValue(_uSecondaryColor);
+ base.Shader.Parameters["uSecondaryColor"]?.SetValue(_uSecondaryColor);
- base.Shader.Parameters["uTime"].SetValue(Main.GlobalTimeWrappedHourly);
+ base.Shader.Parameters["uTime"]?.SetValue(Main.GlobalTimeWrappedHourly);
- base.Shader.Parameters["uOpacity"].SetValue(_uOpacity);
+ base.Shader.Parameters["uOpacity"]?.SetValue(_uOpacity);
- base.Shader.Parameters["uShaderSpecificData"].SetValue(_shaderSpecificData);
+ base.Shader.Parameters["uShaderSpecificData"]?.SetValue(_shaderSpecificData);
if (drawData.HasValue) {
DrawData value = drawData.Value;
Vector4 value2 = Vector4.Zero;
if (drawData.Value.sourceRect.HasValue)
value2 = new Vector4(value.sourceRect.Value.X, value.sourceRect.Value.Y, value.sourceRect.Value.Width, value.sourceRect.Value.Height);

- base.Shader.Parameters["uSourceRect"].SetValue(value2);
+ base.Shader.Parameters["uSourceRect"]?.SetValue(value2);
- base.Shader.Parameters["uWorldPosition"].SetValue(Main.screenPosition + value.position);
+ base.Shader.Parameters["uWorldPosition"]?.SetValue(Main.screenPosition + value.position);
- base.Shader.Parameters["uImageSize0"].SetValue(new Vector2(value.texture.Width, value.texture.Height));
+ base.Shader.Parameters["uImageSize0"]?.SetValue(new Vector2(value.texture.Width, value.texture.Height));
}
else {
- base.Shader.Parameters["uSourceRect"].SetValue(new Vector4(0f, 0f, 4f, 4f));
+ base.Shader.Parameters["uSourceRect"]?.SetValue(new Vector4(0f, 0f, 4f, 4f));
}

SamplerState value3 = SamplerState.LinearWrap;
@@ -53,19 +_,19 @@
if (_uImage0 != null) {
Main.graphics.GraphicsDevice.Textures[0] = _uImage0.Value;
Main.graphics.GraphicsDevice.SamplerStates[0] = value3;
- base.Shader.Parameters["uImageSize0"].SetValue(new Vector2(_uImage0.Width(), _uImage0.Height()));
+ base.Shader.Parameters["uImageSize0"]?.SetValue(new Vector2(_uImage0.Width(), _uImage0.Height()));
}

if (_uImage1 != null) {
Main.graphics.GraphicsDevice.Textures[1] = _uImage1.Value;
Main.graphics.GraphicsDevice.SamplerStates[1] = value3;
- base.Shader.Parameters["uImageSize1"].SetValue(new Vector2(_uImage1.Width(), _uImage1.Height()));
+ base.Shader.Parameters["uImageSize1"]?.SetValue(new Vector2(_uImage1.Width(), _uImage1.Height()));
}

if (_uImage2 != null) {
Main.graphics.GraphicsDevice.Textures[2] = _uImage2.Value;
Main.graphics.GraphicsDevice.SamplerStates[2] = value3;
- base.Shader.Parameters["uImageSize2"].SetValue(new Vector2(_uImage2.Width(), _uImage2.Height()));
+ base.Shader.Parameters["uImageSize2"]?.SetValue(new Vector2(_uImage2.Width(), _uImage2.Height()));
}

_ = _useProjectionMatrix;
@@ -102,6 +_,24 @@
public MiscShaderData UseImage2(string path)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@
public virtual void Update(GameTime gameTime)
{
}
@@ -48,18 +_,18 @@
Vector2 value = new Vector2(Main.screenWidth, Main.screenHeight) / Main.GameViewMatrix.Zoom;
Vector2 vector2 = new Vector2(Main.screenWidth, Main.screenHeight) * 0.5f;
Vector2 vector3 = Main.screenPosition + vector2 * (Vector2.One - Vector2.One / Main.GameViewMatrix.Zoom);
- base.Shader.Parameters["uColor"].SetValue(_uColor);
+ base.Shader.Parameters["uColor"]?.SetValue(_uColor);
- base.Shader.Parameters["uOpacity"].SetValue(CombinedOpacity);
+ base.Shader.Parameters["uOpacity"]?.SetValue(CombinedOpacity);
- base.Shader.Parameters["uSecondaryColor"].SetValue(_uSecondaryColor);
+ base.Shader.Parameters["uSecondaryColor"]?.SetValue(_uSecondaryColor);
- base.Shader.Parameters["uTime"].SetValue(Main.GlobalTimeWrappedHourly);
+ base.Shader.Parameters["uTime"]?.SetValue(Main.GlobalTimeWrappedHourly);
- base.Shader.Parameters["uScreenResolution"].SetValue(value);
+ base.Shader.Parameters["uScreenResolution"]?.SetValue(value);
- base.Shader.Parameters["uScreenPosition"].SetValue(vector3 - vector);
+ base.Shader.Parameters["uScreenPosition"]?.SetValue(vector3 - vector);
- base.Shader.Parameters["uTargetPosition"].SetValue(_uTargetPosition - vector);
+ base.Shader.Parameters["uTargetPosition"]?.SetValue(_uTargetPosition - vector);
- base.Shader.Parameters["uImageOffset"].SetValue(_uImageOffset);
+ base.Shader.Parameters["uImageOffset"]?.SetValue(_uImageOffset);
- base.Shader.Parameters["uIntensity"].SetValue(_uIntensity);
+ base.Shader.Parameters["uIntensity"]?.SetValue(_uIntensity);
- base.Shader.Parameters["uProgress"].SetValue(_uProgress);
+ base.Shader.Parameters["uProgress"]?.SetValue(_uProgress);
- base.Shader.Parameters["uDirection"].SetValue(_uDirection);
+ base.Shader.Parameters["uDirection"]?.SetValue(_uDirection);
- base.Shader.Parameters["uZoom"].SetValue(Main.GameViewMatrix.Zoom);
+ base.Shader.Parameters["uZoom"]?.SetValue(Main.GameViewMatrix.Zoom);
for (int i = 0; i < _uAssetImages.Length; i++) {
Texture2D texture2D = _uCustomImages[i];
if (_uAssetImages[i] != null && _uAssetImages[i].IsLoaded)
@@ -76,7 +_,7 @@
else
Main.graphics.GraphicsDevice.SamplerStates[i + 1] = SamplerState.AnisotropicClamp;

- base.Shader.Parameters["uImageSize" + (i + 1)].SetValue(new Vector2(width, height) * _imageScales[i]);
+ base.Shader.Parameters["uImageSize" + (i + 1)]?.SetValue(new Vector2(width, height) * _imageScales[i]);
}
}

@@ -100,6 +_,14 @@
public ScreenShaderData UseProgress(float progress)
{
Expand Down

0 comments on commit 30b2b9b

Please sign in to comment.