Skip to content

Instantly share code, notes, and snippets.

@andykorth
Created February 5, 2020 16:05
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 andykorth/9c32c97a876a3a95f0e9ddd877d4d23e to your computer and use it in GitHub Desktop.
Save andykorth/9c32c97a876a3a95f0e9ddd877d4d23e to your computer and use it in GitHub Desktop.
{
var batch = batches.First();
var sprite = batch.Key;
var mat = new Material(batch.First().material);
mat.mainTexture = sprite.texture;
var count = batch.Count();
var indexes = new int[6*count];
var vertexes = new Vector3[4*count];
var uvs = new Vector2[4*count];
var iidx = 0;
var vidx = 0;
foreach(var clump in batch){
indexes[iidx + 0] = vidx + 0;
indexes[iidx + 1] = vidx + 1;
indexes[iidx + 2] = vidx + 2;
indexes[iidx + 3] = vidx + 1;
indexes[iidx + 4] = vidx + 2;
indexes[iidx + 5] = vidx + 3;
iidx += 6;
var bounds = sprite.bounds;
var m = clump.transform.localToWorldMatrix;
vertexes[vidx + 0] = m.MultiplyPoint(new Vector3(bounds.min.x, bounds.min.y, 0)); uvs[vidx + 0] = new Vector2(0, 0);
vertexes[vidx + 1] = m.MultiplyPoint(new Vector3(bounds.max.x, bounds.min.y, 0)); uvs[vidx + 1] = new Vector2(1, 0);
vertexes[vidx + 2] = m.MultiplyPoint(new Vector3(bounds.min.x, bounds.max.y, 0)); uvs[vidx + 2] = new Vector2(0, 1);
vertexes[vidx + 3] = m.MultiplyPoint(new Vector3(bounds.max.x, bounds.max.y, 0)); uvs[vidx + 3] = new Vector2(1, 1);
vidx += 4;
}
var mesh = new Mesh();
mesh.vertices = vertexes;
mesh.uv = uvs;
mesh.triangles = indexes;
var go = new GameObject("ClumpMesh");
var mf = go.AddComponent<MeshFilter>();
mf.mesh = mesh;
var mr = go.AddComponent<MeshRenderer>();
mr.sharedMaterial = mat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment