Skip to content

Commit

Permalink
Switch over default renderer to use the ~10x faster hybrid renderer (…
Browse files Browse the repository at this point in the history
…render to an Image first and then load that as a Texture2D)
  • Loading branch information
EttienneS committed Jan 27, 2020
1 parent 2dac569 commit 0d9db54
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LPC.Spritesheet.Generator/CharacterSpriteSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CharacterSpriteSheet

public CharacterSpriteSheet(ICharacterSpriteDefinition characterSprite)
{
Texture = TextureRenderer.GetFullSheetTexture(characterSprite);
Texture = HybridRenderer.GetFullSheetTexture(characterSprite);

foreach (var renderConstant in Settings.SpriteSheetAnimationDefinition)
{
Expand Down
65 changes: 65 additions & 0 deletions LPC.Spritesheet.Generator/HybridRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using LPC.Spritesheet.Generator.Enums;
using LPC.Spritesheet.Generator.Interfaces;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;

namespace LPC.Spritesheet.Generator
{
public static class HybridRenderer
{
public static Texture2D GetFullSheetTexture(ICharacterSpriteDefinition sprite)
{
return GetSpriteSheet(sprite, new RectInt(0, 0, Settings.SheetWidth, Settings.SheetHeight));
}

public static Texture2D GetPartialSpriteSheet(ICharacterSpriteDefinition sprite, Interfaces.Animation animation, Orientation orientation)
{
var (row, _) = Settings.SpriteSheetAnimationDefinition[(animation, orientation)];
return GetSpriteSheet(sprite, new RectInt(0, row * Settings.SpriteWidth, Settings.SheetWidth, Settings.SpriteHeight));
}

public static Texture2D GetSingleSprite(ICharacterSpriteDefinition sprite, Interfaces.Animation animation, Orientation orientation, int frame)
{
var (row, frames) = Settings.SpriteSheetAnimationDefinition[(animation, orientation)];

if (frame >= frames)
{
throw new IndexOutOfRangeException($"Out of range, Cannot get more than frame count ({frames - 1})");
}
return GetSpriteSheet(sprite, new RectInt(frame * Settings.SpriteWidth, row * Settings.SpriteWidth, Settings.SpriteWidth, Settings.SpriteHeight));
}

public static Texture2D GetSpriteSheet(ICharacterSpriteDefinition sprite, RectInt rectangle)
{
var srcRectange = new RectInt(0, 0, rectangle.width, rectangle.height);
var newImage = new Texture2D(srcRectange.width, srcRectange.height, TextureFormat.RGBA32, true)
{
alphaIsTransparency = true
};

var renderedImage = ImageRenderer.GetSpriteSheet(sprite, new Rectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height)).ImageToByteArray();
newImage.LoadImage(renderedImage);

return newImage;
}

public static byte[] ImageToByteArray(this Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms, imageIn.RawFormat);
return ms.ToArray();
}
}

public static Texture2D GetTexture(byte[] spriteData, RectInt rectangle)
{
var texture = new Texture2D(rectangle.width, rectangle.height);
texture.LoadImage(spriteData);
return texture;
}
}
}
1 change: 1 addition & 0 deletions LPC.Spritesheet.Generator/LPC.Spritesheet.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="RandomHelper.cs" />
<Compile Include="Settings.cs" />
<Compile Include="SpriteSheet.cs" />
<Compile Include="HybridRenderer.cs" />
<Compile Include="TextureRenderer.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 0d9db54

Please sign in to comment.