I am trying to use some shaders that allow for border/glow effects around the outside of sprites. They work fine for single sprites, because it’s easy enough to add additional transparent space around the sprite to account for the additional border that is added through the shader.
I am wondering if there is any way to add transparent space around the individual sprites in a sprite sheet generated by the PSD Importer? Similarly, is there a way to generate meshes in the skinning editor that are expanded, so that they include transparent space? I can edit the individual mesh vertices, but having to drag them all further away from the sprite perimeter would be a hassle.
Also, I’ve tried changing the extrude value of the imported PSB prefab, but that doesn’t seem to affect anything.
Currently, this is not supported in the PSD Importer package. We are, however, looking at adding the ability to specify a “Sprite padding” value on Sprite generation in a future version of the PSD Importer.
Seconding this request with another usecase:
Sometimes you can save trianglecount by placing the vertex slightly away from the actual outline and that’s not possible when there is the hard border of the sprite in the way.
For future users, I created a script that at runtime will add a transparent padding around textures. This allows my outline shaders to function properly.
public class OutlineTexture : MonoBehaviour
{
SpriteRenderer _spriteRenderer;
[SerializeField] int _expandAmt = 40;
void Awake()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
}
// Start is called before the first frame update
void Start()
{
Texture2D texture = _spriteRenderer.sprite.texture;
var pixels = texture.GetPixels32();
int newWidth = texture.width + _expandAmt * 2;
int newHeight = texture.height + _expandAmt * 2;
Color32[] expandedPixels = new Color32[newWidth * newHeight];
Color32 color = Color.clear;
for (int i = 0; i < expandedPixels.Length; i++)
{
expandedPixels[i] = color;
}
Texture2D newTexture = new Texture2D(newWidth, newHeight);
newTexture.SetPixels32(expandedPixels);
newTexture.SetPixels32(_expandAmt,_expandAmt, texture.width,texture.height, pixels);
newTexture.Apply();
Sprite newSprite = Sprite.Create(newTexture,
new Rect(Vector2.zero, new Vector2(newWidth, newHeight)),
new Vector2(.5f,0.5f),
100,0, SpriteMeshType.FullRect); // full rect is required.
_spriteRenderer.sprite = newSprite;
_spriteRenderer.drawMode = SpriteDrawMode.Sliced; // not sure why this is needed but it is.
]
}
}
I’m sure it has a performance hit. But at least in my app it’s not measurably worse for about 5 outlined sprites in my scene. It does increase the initial load time slightly so there’s a frame of stutter when they turn on where the fps drops to about 50. Not sure if this script is the cause though since for now that’s acceptable. Might try to optimize that a bit in the future.