3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
6 using System.Collections.Generic;
11 static class WikiImage
13 public static Rectangle CalculateBoundingBox(Character character)
17 void addPointsToBBox(
float extentX,
float extentY, Vector2 worldPos, Vector2 origin,
float rotation)
19 float sinRotation = (float)Math.Sin((
double)rotation);
20 float cosRotation = (float)Math.Cos((
double)rotation);
23 origin.X * cosRotation + origin.Y * sinRotation,
24 origin.X * sinRotation - origin.Y * cosRotation);
25 var limbPos = worldPos.ToPoint();
26 boundingBox.AddPoint(limbPos);
27 Vector2 xExtend =
new Vector2((extentX * cosRotation), (extentX * sinRotation));
28 Vector2 yExtend =
new Vector2((extentY * sinRotation), (-extentY * cosRotation));
29 boundingBox.AddPoint(limbPos + (xExtend + yExtend - origin).ToPoint());
30 boundingBox.AddPoint(limbPos + (xExtend - yExtend - origin).ToPoint());
31 boundingBox.AddPoint(limbPos + (-xExtend - yExtend - origin).ToPoint());
32 boundingBox.AddPoint(limbPos + (-xExtend + yExtend - origin).ToPoint());
35 foreach (Limb limb
in character.AnimController.Limbs)
37 if (limb.ActiveSprite ==
null) {
continue; }
38 float extentX = (float)limb.ActiveSprite.size.X * limb.Scale * limb.TextureScale * 0.5f;
40 float extentY = (
float)limb.ActiveSprite.size.Y * limb.Scale * limb.TextureScale * 0.5f;
43 Vector2 origin = (limb.ActiveSprite.Origin - (limb.ActiveSprite.SourceRect.Size.ToVector2() * 0.5f)) * limb.Scale * limb.TextureScale;
44 addPointsToBBox(extentX, extentY, limb.WorldPosition, origin, limb.body.Rotation);
48 if (character.Inventory !=
null)
50 foreach (var item
in character.Inventory.AllItems)
52 if (item?.Sprite !=
null && item?.body !=
null)
54 float extentX = (float)item.Sprite.size.X * item.Scale * 0.5f;
56 float extentY = (
float)item.Sprite.size.Y * item.Scale * 0.5f;
59 Vector2 origin = (item.Sprite.Origin - (item.Sprite.SourceRect.Size.ToVector2() * 0.5f)) * item.Scale;
60 addPointsToBBox(extentX, extentY, item.WorldPosition, origin, item.body.Rotation);
65 boundingBox.X -= 25; boundingBox.Y -= 25;
66 boundingBox.Width += 50; boundingBox.Height += 50;
71 public static void Create(Character character)
73 Rectangle boundingBox = CalculateBoundingBox(character);
75 int texWidth = Math.Clamp((
int)(boundingBox.Width * 2.5f), 512, 4096);
76 float zoom = (float)texWidth / (
float)boundingBox.Width;
77 int texHeight = (int)(zoom * boundingBox.Height);
79 Camera cam =
new Camera();
80 cam.SetResolution(
new Point(texWidth, texHeight));
82 cam.MinZoom = zoom * 0.5f;
84 cam.Position = boundingBox.Center.ToVector2();
85 cam.UpdateTransform(
false);
87 using (RenderTarget2D rt =
new RenderTarget2D(
88 GameMain.Instance.GraphicsDevice,
89 texWidth, texHeight,
false, SurfaceFormat.Color, DepthFormat.None))
91 using (SpriteBatch spriteBatch =
new SpriteBatch(GameMain.Instance.GraphicsDevice))
93 Viewport prevViewport = GameMain.Instance.GraphicsDevice.Viewport;
94 GameMain.Instance.GraphicsDevice.Viewport =
new Viewport(0, 0, texWidth, texHeight);
95 GameMain.Instance.GraphicsDevice.SetRenderTarget(rt);
96 GameMain.Instance.GraphicsDevice.Clear(Color.Transparent);
97 spriteBatch.Begin(SpriteSortMode.BackToFront, transformMatrix: cam.Transform);
98 character.Draw(spriteBatch, cam);
99 if (character.Inventory !=
null)
101 foreach (var item
in character.Inventory.AllItems)
105 item.Draw(spriteBatch,
false,
false);
106 item.Draw(spriteBatch,
false,
true);
111 GameMain.Instance.GraphicsDevice.SetRenderTarget(
null);
112 GameMain.Instance.GraphicsDevice.Viewport = prevViewport;
113 using (
FileStream fs = File.Open(
"wikiimage.png", System.IO.FileMode.Create))
115 rt.SaveAsPng(fs, texWidth, texHeight);
121 public static void Create(Submarine sub)
123 int width = 4096;
int height = 4096;
125 Rectangle subDimensions = sub.CalculateDimensions(
false);
126 Vector2 viewPos = subDimensions.Center.ToVector2();
127 float scale = Math.Min(width / (
float)subDimensions.Width, height / (
float)subDimensions.Height);
129 var viewMatrix = Matrix.CreateTranslation(
new Vector3(width / 2.0f, height / 2.0f, 0));
130 var transform = Matrix.CreateTranslation(
131 new Vector3(-viewPos.X, viewPos.Y, 0)) *
132 Matrix.CreateScale(
new Vector3(scale, scale, 1)) *
135 using (RenderTarget2D rt =
new RenderTarget2D(
136 GameMain.Instance.GraphicsDevice,
137 width, height,
false, SurfaceFormat.Color, DepthFormat.None))
138 using (SpriteBatch spriteBatch =
new SpriteBatch(GameMain.Instance.GraphicsDevice))
140 Viewport prevViewport = GameMain.Instance.GraphicsDevice.Viewport;
141 GameMain.Instance.GraphicsDevice.Viewport =
new Viewport(0, 0, width, height);
142 GameMain.Instance.GraphicsDevice.SetRenderTarget(rt);
143 GameMain.Instance.GraphicsDevice.Clear(Color.Transparent);
145 DrawBatch(() =>
Submarine.DrawBack(spriteBatch, editing:
false, e => e is Structure s && (e.SpriteDepth >= 0.9f || s.Prefab.BackgroundSprite !=
null)));
146 DrawBatch(() =>
Submarine.DrawBack(spriteBatch, editing:
false, e => (e is not Structure || e.SpriteDepth < 0.9f)));
147 DrawBatch(() =>
Submarine.DrawDamageable(spriteBatch,
null, editing:
false));
148 DrawBatch(() =>
Submarine.DrawFront(spriteBatch, editing:
false));
150 void DrawBatch(Action drawAction)
152 spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied,
null,
null,
null,
null, transform);
157 GameMain.Instance.GraphicsDevice.SetRenderTarget(
null);
158 GameMain.Instance.GraphicsDevice.Viewport = prevViewport;
159 using (
FileStream fs = File.Open(
"wikiimage.png", System.IO.FileMode.Create))
161 rt.SaveAsPng(fs, width, height);