Created
April 18, 2018 13:37
-
-
Save tsubaki/a27684c532a8d2c8f8a1eca808fdb12a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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