Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ShaderEntity.cs
Go to the documentation of this file.
2 using Loot.Api.ModContent;
3 using Loot.Ext;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
6 using Terraria;
7 using Terraria.Graphics.Shaders;
8 
9 namespace Loot.Api.Graphics.Shader
10 {
16  public class ShaderEntity : GraphicsEntity<ShaderGraphicsProperties>
17  {
18  public int ShaderId { get; set; }
19 
20  public Texture2D SubjectTexture { get; protected set; }
21  public Texture2D ShaderTexture { get; protected set; }
22 
23  public ShaderEntity(object subjectIdentity, int shaderId,
24  Color? shaderDrawColor = null, short order = 0, ShaderGraphicsProperties props = null)
25  : base(subjectIdentity)
26  {
27  ShaderId = shaderId;
28  DrawColor = shaderDrawColor ?? Color.White;
29  Order = order;
30  Properties = props ?? ShaderGraphicsProperties.Builder.Build();
31  }
32 
33  protected override void LoadAssets(Item item)
34  {
35  var graphicsContent = Loot.ModContentManager.GetContent<GraphicsModContent>();
36  graphicsContent?.Prepare(item);
37 
38  if (SubjectTexture == null)
39  {
40  SubjectTexture = Main.itemTexture[item.type];
41  }
42 
43  if (ShaderTexture == null)
44  {
45  ShaderTexture = graphicsContent?.GetPreparedShader(item.type.ToString()) ?? SubjectTexture;
46  }
47  }
48 
52  public void DoDrawLayeredEntity(SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float scale, float rotation, GlowmaskEntity glowmaskEntity = null,
53  Texture2D suppliedShaderTexture = null, Texture2D suppliedSubjectTexture = null, Texture2D suppliedGlowmaskTexture = null)
54  {
55  if (Properties.SkipDrawing) return;
56 
57  TryGettingDrawData(rotation, scale);
58  if (Entity is Item item) LoadAssets(item);
59  else Loot.Logger.Warn("Could not identify shader entity identity as item");
60 
61  var useSubjectTexture = suppliedSubjectTexture ?? SubjectTexture;
62  var useShaderTexture = suppliedShaderTexture ?? ShaderTexture;
63  var useGlowmaskTexture = suppliedGlowmaskTexture;
64 
65  // Assets present
66  if (useSubjectTexture != null && useShaderTexture != null)
67  {
68  // Draw the subject based on the drawlayer
69  if (Properties.DrawLayer == ShaderDrawLayer.Back)
70  {
71  DoDrawShader(useShaderTexture, spriteBatch, lightColor);
72  DoDrawSubject(useSubjectTexture, spriteBatch, lightColor);
73  DoDrawGlowmask(glowmaskEntity, spriteBatch, lightColor, alphaColor, rotation, scale, Entity.whoAmI, useGlowmaskTexture);
74  }
75  else if (Properties.DrawLayer == ShaderDrawLayer.Middle)
76  {
77  DoDrawSubject(useSubjectTexture, spriteBatch, lightColor);
78  DoDrawShader(useShaderTexture, spriteBatch, lightColor);
79  DoDrawGlowmask(glowmaskEntity, spriteBatch, lightColor, alphaColor, rotation, scale, Entity.whoAmI, useGlowmaskTexture);
80  }
81  else if (Properties.DrawLayer == ShaderDrawLayer.Front)
82  {
83  DoDrawSubject(useSubjectTexture, spriteBatch, lightColor);
84  DoDrawGlowmask(glowmaskEntity, spriteBatch, lightColor, alphaColor, rotation, scale, Entity.whoAmI, useGlowmaskTexture);
85  DoDrawShader(useShaderTexture, spriteBatch, lightColor);
86  }
87  }
88 
89  Properties.SkipUpdatingDrawData = false;
90  }
91 
95  public void DoDrawGlowmask(GlowmaskEntity glowmaskEntity, SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float rotation, float scale, int whoAmI, Texture2D suppliedGlowmask = null)
96  {
97  if (glowmaskEntity == null) return;
98  glowmaskEntity.Properties.SkipDrawingGlowmask = Properties.SkipDrawingGlowmask;
99  glowmaskEntity.DoDrawGlowmask(spriteBatch, lightColor, alphaColor, rotation, scale, whoAmI, suppliedGlowmask);
100  }
101 
105  public void DoDrawShader(Texture2D shaderTexture, SpriteBatch spriteBatch, Color lightColor)
106  {
107  if (Properties.SkipDrawingShader) return;
108 
109  var drawDataTexture = DrawData.texture;
110  var drawDataColor = DrawData.color;
111 
112  DrawData.texture = shaderTexture;
113  DrawData.color = lightColor;
114  TryUpdatingDrawData(shaderTexture);
115 
116  spriteBatch.BeginShaderBatch();
117  GameShaders.Armor.Apply(ShaderId, Entity, DrawData);
118 
119  var centerPos = DrawData.position;
120  Properties.ShaderDrawStyle.DrawShader(spriteBatch, this);
121 
122  DrawData.position = centerPos;
123  DrawData.texture = drawDataTexture;
124  DrawData.color = drawDataColor;
125  spriteBatch.ResetBatch();
126  }
127 
131  public void DoDrawSubject(Texture2D subjectTexture, SpriteBatch spriteBatch, Color lightColor)
132  {
133  if (Properties.SkipDrawingSubject) return;
134 
135  var drawDataColor = DrawData.color;
136  var drawDataTexture = DrawData.texture;
137 
138  DrawData.color = lightColor;
139  DrawData.texture = subjectTexture;
140  TryUpdatingDrawData(subjectTexture);
141  DrawEntity(spriteBatch);
142 
143  DrawData.color = drawDataColor;
144  DrawData.texture = drawDataTexture;
145  }
146  }
147 }
void DoDrawGlowmask(SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float rotation, float scale, int whoAmI, Texture2D suppliedGlowmask=null)
Will draw the glowmask texture
static new ShaderGraphicsPropertiesBuilder Builder
void DoDrawSubject(Texture2D subjectTexture, SpriteBatch spriteBatch, Color lightColor)
Will draw the entity&#39;s regular sprite
Defines the GraphicsProperties for ShaderEntitys which derives from GlowmaskGraphicsProperties ...
override void LoadAssets(Item item)
Definition: ShaderEntity.cs:33
void DoDrawGlowmask(GlowmaskEntity glowmaskEntity, SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float rotation, float scale, int whoAmI, Texture2D suppliedGlowmask=null)
Will draw the glowmask
Definition: ShaderEntity.cs:95
Defines a Shader entity, part of GraphicsEntity<T> The entity defines a particular &#39;shader&#39; "of" the ...
Definition: ShaderEntity.cs:16
void DoDrawShader(Texture2D shaderTexture, SpriteBatch spriteBatch, Color lightColor)
Will draw the shader effect
Defines the base abstract of a GraphicsEntity
ShaderEntity(object subjectIdentity, int shaderId, Color?shaderDrawColor=null, short order=0, ShaderGraphicsProperties props=null)
Definition: ShaderEntity.cs:23
Defines a Glowmask entity, part of GraphicsEntity The entity defines a particular &#39;glowmask&#39; "of" the...
This class stores glowmask and shader textures that can be looked up during drawing ...
void DoDrawLayeredEntity(SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float scale, float rotation, GlowmaskEntity glowmaskEntity=null, Texture2D suppliedShaderTexture=null, Texture2D suppliedSubjectTexture=null, Texture2D suppliedGlowmaskTexture=null)
Will draw all the layers (subject, glowmask, shader) in the proper order
Definition: ShaderEntity.cs:52