public bool AllocReal(int w, int h, out TexRect output) { for (int i=0;i<m_free.Count;i++) { TexRect c = m_free[i]; int fw = c.u1 - c.u0; int fh = c.v1 - c.v0; if (fw >= w && fh >= h) { output = new TexRect(c.u0, c.v0, c.u0 + w, c.v0 + h); output._u0 = (float)output.u0 / (float)m_texture.width; output._v0 = 1.0f - (float)output.v0 / (float)m_texture.height; output._u1 = (float)output.u1 / (float)m_texture.width; output._v1 = 1.0f - (float)output.v1 / (float)m_texture.height; TexRect rhs = new TexRect(c.u0 + w, c.v0, c.u1, c.v1); TexRect bottom = new TexRect(c.u0, c.v0 + h, c.u0 + w, c.v1); m_free.RemoveAt(i); m_free.Add(rhs); m_free.Add(bottom); return true; } } output = new TexRect(0,0,0,0); return false; }
void Start() { meshFilter = GetComponent <MeshFilter>(); mesh = new Mesh(); mesh.MarkDynamic(); mesh.vertices = verts; mesh.triangles = tris; mesh.uv = uvs; mesh.bounds = new Bounds(Vector3.zero, new Vector3(float.MaxValue, float.MaxValue, float.MaxValue)); meshFilter.mesh = mesh; camera = Camera.main.transform; int width = Mathf.Max(blusterTex.width, rocketTex.width); texAtlas = new Texture2D(width, blusterTex.height + rocketTex.height); texAtlas.SetPixels(0, 0, width, blusterTex.height, blusterTex.GetPixels()); texAtlas.SetPixels(0, blusterTex.height, width, rocketTex.height, rocketTex.GetPixels()); texAtlas.Apply(); atlasMap = new TexRect[2]; atlasMap[0] = new TexRect(); atlasMap[1] = new TexRect(); atlasMap[0].leftTop = Vector2.zero; atlasMap[0].leftBot = new Vector2(0, ((float)blusterTex.height) / texAtlas.height); atlasMap[0].rightTop = new Vector2(((float)blusterTex.width) / texAtlas.width, 0); atlasMap[0].rightBot = new Vector2(((float)blusterTex.width) / texAtlas.width, ((float)blusterTex.height) / texAtlas.height); atlasMap[1].leftTop = new Vector2(0, ((float)blusterTex.height) / texAtlas.height); atlasMap[1].leftBot = new Vector2(0, ((float)(blusterTex.height + rocketTex.height)) / texAtlas.height); atlasMap[1].rightTop = new Vector2(((float)rocketTex.width) / texAtlas.width, ((float)blusterTex.height) / texAtlas.height); atlasMap[1].rightBot = new Vector2(((float)rocketTex.width) / texAtlas.width, ((float)(blusterTex.height + rocketTex.height)) / texAtlas.height); GetComponent <MeshRenderer>().material.mainTexture = texAtlas; }
public bool Alloc(int w, int h, out TexRect output) { int mod = 4; int widthRounded = mod * ((w + mod - 1) / mod); int padw = widthRounded - w; if (AllocReal(w + padw + 2, h + 2, out output)) { float adjw = (1.0f) / (float)m_texture.width; float adjh = 1.0f / (float)m_texture.height; output._u0 += adjw; output._u1 -= adjw + padw / (float)m_texture.width; output._v0 -= adjh; output._v1 += adjh; output.u0 += 1; output.v0 += 1; output.u1 -= 1 + padw; output.v1 -= 1; return true; } return false; }
public void SetPixels(TexRect where, Color[] array) { m_texture.SetPixels(where.u0, where.v0, where.u1 - where.u0, where.v1 - where.v0, array, 0); m_texture.Apply(); }
public void Clear() { TexRect f = new TexRect(0, 0, m_texture.width, m_texture.height); m_free.Add(f); }
public override void SetTextures(TextureAtlas atlas) { uvRect = atlas.GetRect(tex); }
public override void SetTextures(TextureAtlas atlas) { uvRocket = atlas.GetRect(rocketTexture); uvTrail = atlas.GetRect(trailTexture); }