Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created April 18, 2018 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/a27684c532a8d2c8f8a1eca808fdb12a to your computer and use it in GitHub Desktop.
Save tsubaki/a27684c532a8d2c8f8a1eca808fdb12a to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Rendering;
public class ShowSprite : MonoBehaviour
{
[SerializeField] Material mat;
[SerializeField] Sprite sprite;
private CommandBuffer buffer;
private Matrix4x4[] matrices;
private static int idMainTex = Shader.PropertyToID("_MainTex");
void Start()
{
matrices = new Matrix4x4[] {
Matrix4x4.TRS(new Vector3(-2, 0, 0), Quaternion.identity, Vector3.one),
Matrix4x4.TRS(new Vector3(-1, 0, 0), Quaternion.identity, Vector3.one),
Matrix4x4.TRS(new Vector3( 1, 0, 0), Quaternion.identity, Vector3.one),
};
var mesh = SpriteToMesh(sprite);
var propertyBlock = new MaterialPropertyBlock();
propertyBlock.SetTexture(idMainTex, sprite.texture);
buffer = new CommandBuffer();
foreach( var matrix in matrices)
{
buffer.DrawMesh(mesh, matrix, mat, 0, 0, propertyBlock);
}
Camera.main.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, buffer);
}
private Mesh SpriteToMesh(Sprite sprite)
{
var mesh = new Mesh();
mesh.SetVertices(Array.ConvertAll(sprite.vertices, c => (Vector3)c).ToList());
mesh.SetUVs(0, sprite.uv.ToList());
mesh.SetTriangles(Array.ConvertAll(sprite.triangles, c => (int)c), 0);
return mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment