Skip to content

Instantly share code, notes, and snippets.

@alexguirre
Last active August 9, 2019 13:12
Show Gist options
  • Select an option

  • Save alexguirre/e6b8dc7cdbee8b3ec5227a056a7ce535 to your computer and use it in GitHub Desktop.

Select an option

Save alexguirre/e6b8dc7cdbee8b3ec5227a056a7ce535 to your computer and use it in GitHub Desktop.
public class Sprite3D
{
public bool IsVisible { get; set; } = true;
public Color Color { get; set; }
public TextureDictionary TextureDictionary { get; set; }
public string TextureName { get; set; }
public Vector3 UpperLeft { get; set; }
public Vector3 UpperRight { get; set; }
public Vector3 BottomLeft { get; set; }
public Vector3 BottomRight { get; set; }
public RectangleF UV { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the back-face should be drawn. If <c>true</c> both front and back faces will be drawn,
/// otherwise only the face that heads to (UpperRight-UpperLeft)x(UpperLeft-BottomLeft) is drawn.
/// </summary>
/// <value>
/// <c>true</c> if the back-face should be drawn; otherwise, <c>false</c>.
/// </value>
public bool BackFace { get; set; }
public Sprite3D(TextureDictionary textureDictionary, string textureName, Vector3 upperLeft, Vector3 upperRight, Vector3 bottomLeft, Vector3 bottomRight, Color color)
{
Throw.IfNull(textureName, nameof(textureName));
TextureDictionary = textureDictionary;
TextureName = textureName;
UpperLeft = upperLeft;
UpperRight = upperRight;
BottomLeft = bottomLeft;
BottomRight = bottomRight;
Color = color;
UV = new RectangleF(0.0f, 0.0f, 1.0f, 1.0f);
BackFace = false;
}
public Sprite3D(TextureDictionary textureDictionary, string textureName, Vector3 upperLeft, Vector3 upperRight, Vector3 bottomLeft, Vector3 bottomRight)
: this(textureDictionary, textureName, upperLeft, upperRight, bottomLeft, bottomRight, Color.White)
{
}
public Sprite3D(TextureDictionary textureDictionary, string textureName)
: this(textureDictionary, textureName, Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero, Color.White)
{
}
public void SetTransform(Vector3 position, Vector2 size, Quaternion rotation)
{
SetTransform(Matrix.Scaling(new Vector3(size.X, 1.0f, size.Y)) * Matrix.RotationQuaternion(rotation) * Matrix.Translation(position));
}
public void SetTransform(Matrix transformationMatrix)
{
// TransformCoordinate from https://code.google.com/archive/p/slimmath/
Vector3 v = new Vector3(-1.0f, 0.0f, 1.0f); // upper left
Common.TransformCoordinate(ref v, ref transformationMatrix, out v);
UpperLeft = v;
v = new Vector3(1.0f, 0.0f, 1.0f); // upper right
Common.TransformCoordinate(ref v, ref transformationMatrix, out v);
UpperRight = v;
v = new Vector3(-1.0f, 0.0f, -1.0f); // bottom left
Common.TransformCoordinate(ref v, ref transformationMatrix, out v);
BottomLeft = v;
v = new Vector3(1.0f, 0.0f, -1.0f); // bottom right
Common.TransformCoordinate(ref v, ref transformationMatrix, out v);
BottomRight = v;
}
public void Draw()
{
if (!IsVisible)
return;
Draw(TextureDictionary, TextureName, UpperLeft, UpperRight, BottomLeft, BottomRight, Color, UV, BackFace);
}
public static void Draw(TextureDictionary textureDictionary, string textureName, Vector3 upperLeft, Vector3 upperRight, Vector3 bottomLeft, Vector3 bottomRight, Color color, RectangleF uv, bool backFace = false)
{
Throw.IfNull(textureName, nameof(textureName));
if (!textureDictionary.IsLoaded)
textureDictionary.Load();
Vector3 ul = upperLeft, ur = upperRight, bl = bottomLeft, br = bottomRight;
// N.DrawTriangle3D is native 0x29280002282F1928
N.DrawTriangle3D(ul, bl, ur, color.R, color.G, color.B, color.A, textureDictionary.Name, textureName, new Vector3(uv.Left, uv.Top, 1f), new Vector3(uv.Left, uv.Bottom, 1f), new Vector3(uv.Right, uv.Top, 1f));
N.DrawTriangle3D(bl, br, ur, color.R, color.G, color.B, color.A, textureDictionary.Name, textureName, new Vector3(uv.Left, uv.Bottom, 1f), new Vector3(uv.Right, uv.Bottom, 1f), new Vector3(uv.Right, uv.Top, 1f));
if (backFace)
{
N.DrawTriangle3D(ur, bl, ul, color.R, color.G, color.B, color.A, textureDictionary.Name, textureName, new Vector3(uv.Right, uv.Top, 1f), new Vector3(uv.Left, uv.Bottom, 1f), new Vector3(uv.Left, uv.Top, 1f));
N.DrawTriangle3D(ur, br, bl, color.R, color.G, color.B, color.A, textureDictionary.Name, textureName, new Vector3(uv.Right, uv.Top, 1f), new Vector3(uv.Right, uv.Bottom, 1f), new Vector3(uv.Left, uv.Bottom, 1f));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment