Created
February 5, 2020 16:05
-
-
Save andykorth/9c32c97a876a3a95f0e9ddd877d4d23e 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
{ | |
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