// Updates the UVs of the specified sprite and copies the new values // into the mesh object. public void UpdateUV(Zprite sprite) { UVs[sprite.uv1] = sprite.lowerLeftUV + Vector2.up * sprite.uvDimensions.y; // Upper-left UVs[sprite.uv2] = sprite.lowerLeftUV; // Lower-left UVs[sprite.uv3] = sprite.lowerLeftUV + Vector2.right * sprite.uvDimensions.x;// Lower-right UVs[sprite.uv4] = sprite.lowerLeftUV + sprite.uvDimensions; // Upper-right uvsChanged = true; }
// Enlarges the sprite array by the specified count and also resizes // the UV and vertex arrays by the necessary corresponding amount. // Returns the index of the first newly allocated element // (ex: if the sprite array was already 10 elements long and is // enlarged by 10 elements resulting in a total length of 20, // EnlargeArrays() will return 10, indicating that element 10 is the // first of the newly allocated elements.) protected int EnlargeArrays(int count) { int firstNewElement; if (sprites == null) { InitArrays(); firstNewElement = 0; count = count - 1; // Allocate one less since InitArrays already allocated one sprite for us } else firstNewElement = sprites.Length; //Debug.Log("enlarge "+sprites.Length+" +"+count+" colors="+colors.Length+" vertices="+vertices.Length); // Resize sprite array: Zprite[] tempSprites = sprites; sprites = new Zprite[sprites.Length + count]; tempSprites.CopyTo(sprites, 0); // Vertices: Vector3[] tempVerts = vertices; vertices = new Vector3[vertices.Length + count*4]; tempVerts.CopyTo(vertices, 0); // UVs: Vector2[] tempUVs = UVs; UVs = new Vector2[UVs.Length + count*4]; tempUVs.CopyTo(UVs, 0); // Colors: Color[] tempColors = colors; colors = new Color[colors.Length + count * 4]; tempColors.CopyTo(colors, 0); // Triangle indices: int[] tempTris = triIndices; triIndices = new int[triIndices.Length + count*6]; tempTris.CopyTo(triIndices, 0); // Inform existing sprites of the new vertex and UV buffers: for (int i = 0; i < firstNewElement; ++i) { sprites[i].SetBuffers(vertices, UVs); } // Setup the newly-added sprites and Add them to the list of available // sprite blocks. Also initialize the triangle indices while we're at it: for (int i = firstNewElement; i < sprites.Length; ++i) { // Create and setup sprite: sprites[i] = new Zprite(); sprites[i].index = i; sprites[i].manager = this; sprites[i].SetBuffers(vertices, UVs); // Setup indices of the sprite's vertices in the vertex buffer: sprites[i].mv1 = i * 4 + 0; sprites[i].mv2 = i * 4 + 1; sprites[i].mv3 = i * 4 + 2; sprites[i].mv4 = i * 4 + 3; // Setup the indices of the sprite's UV entries in the UV buffer: sprites[i].uv1 = i * 4 + 0; sprites[i].uv2 = i * 4 + 1; sprites[i].uv3 = i * 4 + 2; sprites[i].uv4 = i * 4 + 3; // Setup the indices to the color values: sprites[i].cv1 = i * 4 + 0; sprites[i].cv2 = i * 4 + 1; sprites[i].cv3 = i * 4 + 2; sprites[i].cv4 = i * 4 + 3; // Setup the default color: sprites[i].SetColor(Color.white); // Add as an available sprite: availableBlocks.Add(sprites[i]); // Init triangle indices: if(winding == WINDING_ORDER.CCW) { // Counter-clockwise winding triIndices[i * 6 + 0] = i * 4 + 0; // 0_ 2 0 ___ 3 triIndices[i * 6 + 1] = i * 4 + 1; // | / Verts: | /| triIndices[i * 6 + 2] = i * 4 + 3; // 1|/ 1|/__|2 triIndices[i * 6 + 3] = i * 4 + 3; // 3 triIndices[i * 6 + 4] = i * 4 + 1; // /| triIndices[i * 6 + 5] = i * 4 + 2; // 4/_|5 } else { // Clockwise winding triIndices[i * 6 + 0] = i * 4 + 0; // 0_ 1 0 ___ 3 triIndices[i * 6 + 1] = i * 4 + 3; // | / Verts: | /| triIndices[i * 6 + 2] = i * 4 + 1; // 2|/ 1|/__|2 triIndices[i * 6 + 3] = i * 4 + 3; // 3 triIndices[i * 6 + 4] = i * 4 + 2; // /| triIndices[i * 6 + 5] = i * 4 + 1; // 5/_|4 } } vertsChanged = true; uvsChanged = true; colorsChanged = true; vertCountChanged = true; return firstNewElement; }
// Updates the vertices of a sprite such that it is oriented // more or less toward the camera public void TransformBillboarded(Zprite sprite) { Vector3 pos = sprite.clientTransform.position; Transform t = Camera.main.transform; vertices[sprite.mv1] = pos + t.TransformDirection(sprite.v1); vertices[sprite.mv2] = pos + t.TransformDirection(sprite.v2); vertices[sprite.mv3] = pos + t.TransformDirection(sprite.v3); vertices[sprite.mv4] = pos + t.TransformDirection(sprite.v4); vertsChanged = true; }
// Updates the color values of the specified sprite and copies the // new values into the mesh object. public void UpdateColors(Zprite sprite) { colors[sprite.cv1] = sprite.color; colors[sprite.cv2] = sprite.color; colors[sprite.cv3] = sprite.color; colors[sprite.cv4] = sprite.color; colorsChanged = true; }
// Updates the vertices of a sprite based on the transform // of its client GameObject public void Transform(Zprite sprite) { sprite.Transform(); vertsChanged = true; }
public void ShowSprite(Zprite sprite) { // Only show the sprite if it has a client: // if(sprite.client == null) // return; if(sprite.billboarded) activeBillboards.Add(sprite); else activeBlocks.Add(sprite); sprite.hidden = false; // Update the vertices: sprite.Transform(); vertsChanged = true; }
public void SetBillboarded(Zprite sprite) { // Make sure the sprite isn't in the active list // or else it'll get handled twice: activeBlocks.Remove(sprite); activeBillboards.Add(sprite); }
public void RemoveSprite(Zprite sprite) { sprite.SetSizeXY(0,0); sprite.v1 = Vector3.zero; sprite.v2 = Vector3.zero; sprite.v3 = Vector3.zero; sprite.v4 = Vector3.zero; vertices[sprite.mv1] = sprite.v1; vertices[sprite.mv2] = sprite.v2; vertices[sprite.mv3] = sprite.v3; vertices[sprite.mv4] = sprite.v4; sprite.client = null; availableBlocks.Add(sprite); // Remove the sprite from the billboarded list // since that list should only contain active // sprites: if (sprite.billboarded) activeBillboards.Remove(sprite); else activeBlocks.Remove(sprite); sprite.billboarded = false; vertsChanged = true; }
public void HideSprite(Zprite sprite) { vertices[sprite.mv1] = Vector3.zero; vertices[sprite.mv2] = Vector3.zero; vertices[sprite.mv3] = Vector3.zero; vertices[sprite.mv4] = Vector3.zero; // Remove the sprite from the billboarded list // since that list should only contain sprites // we intend to transform: if (sprite.billboarded) activeBillboards.Remove(sprite); else activeBlocks.Remove(sprite); sprite.hidden = true; vertsChanged = true; }