/** Build a new mesh for a sprite */ public SpriteMesh CreateMesh(Sprite sprite, bool exists) { SpriteMesh rtn = null; if (!exists) { var shader = Shader.Find("Custom/nPropShader"); rtn = new SpriteMesh() { Material = new Material(shader), Mesh = new Mesh(), Position = new Vector3(), Rotation = new Quaternion() }; _cache[sprite.Id] = rtn; } else rtn = _cache[sprite.Id]; UpdateMesh(rtn, sprite); return rtn; }
/** Rebuild parts of a mesh without creating a new one */ public void UpdateMesh(SpriteMesh sm, Sprite sprite) { nLog.Debug("Update!"); /* mesh, uvs, etc */ var verticies = new Vector3[4] { new Vector3(sprite.Points[0], sprite.Points[1], 0f), new Vector3(sprite.Points[2], sprite.Points[3], 0f), new Vector3(sprite.Points[4], sprite.Points[5], 1f), new Vector3(sprite.Points[6], sprite.Points[7], 1f) }; var uvs = new Vector2[4] { new Vector2(sprite.UV[0], sprite.UV[1]), new Vector2(sprite.UV[2], sprite.UV[3]), new Vector2(sprite.UV[4], sprite.UV[5]), new Vector2(sprite.UV[6], sprite.UV[7]) }; var triangles = new int[6] { 0, 1, 2, 2, 1, 3, }; sm.Mesh.vertices = verticies; sm.Mesh.uv = uvs; sm.Mesh.triangles = triangles; sm.Mesh.RecalculateNormals(); /* material */ sm.Material.color = new Color(sprite.Color[0], sprite.Color[1], sprite.Color[2], sprite.Color[3]); sm.Material.mainTexture = sprite.Texture; /* position */ sm.Position[0] = sprite.Position[0]; sm.Position[1] = sprite.Position[1]; sm.Position[2] = sprite.Depth; /* rotation */ sm.Rotation = Quaternion.AngleAxis(sprite.Rotation, new Vector3(sprite.Origin[0], sprite.Origin[1], 1.0f)); /* cache result */ sprite.Invalid = false; }