void BuildBack(CardMesh card, Dictionary <Texture2D, SubMesh> table, CardShape paper) { CardShape back = Atlas.FindById(Stock.Back); if (back != null) { Vector3[] v = RectPoints(Stock.Size * Stock.Scale, Stock.BackBorder * Stock.Scale); SubMesh core = GetMesh(card, table, back); // middle if (Stock.HalfBack) { Square4(core, back, v[1], v[0], v[12], v[13], Color.white, false, false); Square4(core, back, v[13], v[12], v[3], v[2], Color.white, true, true); } else { Square4(core, back, v[1], v[0], v[3], v[2], Color.white, false, false); } // top Square4(core, paper, v[5], v[4], v[0], v[1], Color.white, false, false); // back-left Square5(core, paper, v[2], v[7], v[6], v[1], v[13], Color.white, false); // bottom Square4(core, paper, v[2], v[3], v[9], v[8], Color.white, false, false); // back-right Square5(core, paper, v[0], v[11], v[10], v[3], v[12], Color.white, false); BuildCorner3(core, paper, v[1], v[6], v[5]); BuildCorner3(core, paper, v[2], v[8], v[7]); BuildCorner3(core, paper, v[3], v[10], v[9]); BuildCorner3(core, paper, v[0], v[4], v[11]); } }
public void Rebuild() { if (!IsValid()) { Debug.LogError("The card definition is not valid."); return; } Stock.Validate(); Dictionary <Texture2D, SubMesh> table = new Dictionary <Texture2D, SubMesh>(); CardMesh card = new CardMesh(); CardShape paper = Definition.Atlas.FindById(Definition.Stock.Paper); if (paper == null) { Debug.LogError("Paper does not exist in atlas = " + Definition.Atlas.name + "::" + Definition.Stock.Paper); return; } SubMesh data = GetMesh(card, table, paper); float x = Stock.Size.x / 2; float y = Stock.Size.y / 2; float cx = x - Stock.Border.x; float cy = y - Stock.Border.y; Vector3 v0 = new Vector2(-cx, +cy); // middle Vector3 v1 = new Vector2(+cx, +cy); Vector3 v2 = new Vector2(+cx, -cy); Vector3 v3 = new Vector2(-cx, -cy); Vector3 v4 = new Vector2(-cx, +y); // top edge Vector3 v5 = new Vector2(+cx, +y); Vector3 v6 = new Vector2(+x, +cy); // right edge Vector3 v7 = new Vector2(+x, -cy); Vector3 v8 = new Vector2(+cx, -y); // bot edge Vector3 v9 = new Vector2(-cx, -y); Vector3 vA = new Vector2(-x, -cy); // left edge Vector3 vB = new Vector2(-x, +cy); // 4 5 // B 0 1 6 // C D // A 3 2 7 // 9 8 Vector3 vC = new Vector2(-cx, 0); // mid Vector3 vD = new Vector2(+cx, 0); if (Stock.Smooth > 0) { // middle Square4(data, paper, v0, v1, v2, v3, Color.white, false, false); // top Square4(data, paper, v4, v5, v1, v0, Color.white, false, false); // right Square4(data, paper, v1, v6, v7, v2, Color.white, false, false); // bottom Square4(data, paper, v3, v2, v8, v9, Color.white, false, false); // left Square4(data, paper, vB, v0, v3, vA, Color.white, false, false); BuildCorner3(data, paper, v0, vB, v4); BuildCorner3(data, paper, v1, v5, v6); BuildCorner3(data, paper, v2, v7, v8); BuildCorner3(data, paper, v3, v9, vA); } else // simple rectangle { Vector3 p1 = new Vector3(-x, +y, 0); Vector3 p2 = new Vector3(+x, +y, 0); Vector3 p3 = new Vector3(+x, -y, 0); Vector3 p4 = new Vector3(-x, -y, 0); Square4(data, paper, p1, p2, p3, p4, Color.white, false, false); } Vector2 textSize = new Vector2(0.175f, 0.175f); Vector2 symSize = new Vector2(0.25f, 0.25f); float symW = symSize.x * 0.5f; float symH = symSize.y * 0.5f; float rimX = Mathf.Max(textSize.x, symW); float rimY = Mathf.Max(textSize.y, symH); CardShape symbol = Atlas.FindById(Definition.Symbol); if (symbol == null && !string.IsNullOrEmpty(Definition.Symbol)) { Debug.LogError(string.Format("Symbol shape '{0}' is not defined in atlas.", Definition.Symbol)); } CardShape fullImage = Definition.FullImage ? Atlas.FindById(Definition.Image) : null; CardShape halfImage = !Definition.FullImage ? Atlas.FindById(Definition.Image) : null; if (fullImage != null) { SubMesh core = GetMesh(card, table, fullImage); Square4(core, fullImage, v0, v1, v2, v3, Color.white, false, false); } else if (halfImage != null) { SubMesh core = GetMesh(card, table, halfImage); Vector3 lift = new Vector3(0, 0, -0.01f); Square4(core, halfImage, v0 + lift, v1 + lift, vD + lift, vC + lift, Color.white, false, false); Square4(core, halfImage, vC + lift, vD + lift, v2 + lift, v3 + lift, Color.white, true, true); } else if (Definition.Pattern != 0 && symbol != null) { if (Definition.Pattern >= 1 && Definition.Pattern < patternBits.Length) { SubMesh core = GetMesh(card, table, symbol); Vector2 ssize = symSize; int bits = patternBits[Definition.Pattern]; float x0 = -x + Stock.Border.x; float x1 = +x - Stock.Border.x; float y0 = +y - Stock.Border.y; float y1 = -y + Stock.Border.y; for (int b = 0; b < 17; b++) { if ((bits & (1 << b)) != 0) { float px = Mathf.Lerp(x0, x1, patternPos[b].x); float py = Mathf.Lerp(y0, y1, patternPos[b].y); float scale = (Definition.Pattern == 1) ? 2.5f:1; Square(core, symbol, new Vector3(px, py, -0.01f), scale * ssize, Color.white); } } } else { Debug.LogError(string.Format("Pattern value '{0}' is not valid.", Definition.Pattern)); } } CardShape text = Atlas.FindById(Definition.Text); if (text == null && !string.IsNullOrEmpty(Definition.Text)) { Debug.LogError(string.Format("Text shape '{0}' is not defined in atlas.", Definition.Text)); } if (text != null) { SubMesh sub = GetMesh(card, table, text); float x0 = -x + (Stock.Border.x + rimX) * 0.5f; float x1 = +x - (Stock.Border.x + rimX) * 0.5f; float y0 = +y - Stock.Border.y; float y1 = -y + Stock.Border.y; Color color = GetSymbolColor(Definition.Symbol); Square(sub, text, new Vector3(x0, y0, -0.01f), textSize, color); Square(sub, text, new Vector3(x1, y1, -0.01f), textSize, color); } if (symbol != null) { SubMesh sub = GetMesh(card, table, symbol); Vector2 ssize = symSize * 0.5f; float gapY = ssize.y / 3; float x0 = -x + (Stock.Border.x + rimX) * 0.5f; float x1 = +x - (Stock.Border.x + rimX) * 0.5f; float y0 = +y - Stock.Border.y - textSize.y - gapY - ssize.y; float y1 = -y + Stock.Border.y + textSize.y + gapY + ssize.y; Color color = GetSymbolColor(Definition.Symbol); Square(sub, symbol, new Vector3(x0, y0, -0.01f), ssize, color); Square(sub, symbol, new Vector3(x1, y1, -0.01f), ssize, color); } if (Stock.TwoSided) { CardShape back = Atlas.FindById(Stock.Back); if (back != null) { SubMesh core = GetMesh(card, table, back); // middle Square4(core, back, v1, v0, vC, vD, Color.white, false, false); Square4(core, back, vD, vC, v3, v2, Color.white, true, true); // top Square4(core, paper, v5, v4, v0, v1, Color.white, false, false); // back-left Square5(core, paper, v2, v7, v6, v1, vD, Color.white, false); // bottom Square4(core, paper, v2, v3, v9, v8, Color.white, false, false); // back-right Square5(core, paper, v0, vB, vA, v3, vC, Color.white, false); BuildCorner3(core, paper, v1, v6, v5); BuildCorner3(core, paper, v2, v8, v7); BuildCorner3(core, paper, v3, vA, v9); BuildCorner3(core, paper, v0, v4, vB); } } Mesh mesh = SetupMesh(); mesh.vertices = card.GetCombinedVertices().ToArray(); mesh.triangles = data.IndexList.ToArray(); mesh.uv = card.GetCombinedTexCoords().ToArray(); mesh.colors = card.GetCombinedColors().ToArray(); if (card.MeshList.Count > 1) { mesh.subMeshCount = card.MeshList.Count; int vbase = 0; for (int i = 1; i < card.MeshList.Count; ++i) { SubMesh sub = card.MeshList[i]; int [] tris = sub.IndexList.ToArray(); vbase += card.MeshList[i - 1].VertexList.Count; for (int t = 0; t < tris.Length; ++t) { tris[t] += vbase; } mesh.SetTriangles(tris, i); } } mesh.RecalculateBounds(); ; mesh.RecalculateNormals(); this.GetComponent <Renderer>().sharedMaterials = card.Materials.ToArray(); }
public void Rebuild() { if (!IsValid()) { Debug.LogError("The card definition is not valid. " + this.name); return; } Stock.Validate(); Dictionary <Texture2D, SubMesh> table = new Dictionary <Texture2D, SubMesh>(); CardMesh card = new CardMesh(); CardShape paper = Definition.Atlas.FindById(Definition.Stock.Paper); if (paper == null) { Debug.LogError("Paper does not exist in atlas = " + Definition.Atlas.name + "::" + Definition.Stock.Paper); return; } SubMesh data = GetMesh(card, table, paper); float x = Stock.Size.x * Stock.Scale / 2; float y = Stock.Size.y * Stock.Scale / 2; float bx = Mathf.Max(Stock.CornerSize, Stock.Border.x) * Stock.Scale; float by = Mathf.Max(Stock.CornerSize, Stock.Border.y) * Stock.Scale; //float cx = x-bx; //float cy = y-by; // 4 5 // B 0 1 6 // C D // A 3 2 7 // 9 8 float crad = Stock.CornerSize; Vector2 cxy = new Vector2(crad, crad); Vector3[] v = RectPoints(Stock.Size * Stock.Scale, Stock.Scale * cxy); Vector3 c0 = new Vector3(-x + bx, +y - by, 0); Vector3 c1 = new Vector3(+x - bx, +y - by, 0); Vector3 c2 = new Vector3(+x - bx, -y + by, 0); Vector3 c3 = new Vector3(-x + bx, -y + by, 0); if (Stock.Smooth > 0) { // middle Square4(data, paper, v[0], v[1], v[2], v[3], Color.white, false, false); // top Square4(data, paper, v[4], v[5], v[1], v[0], Color.white, false, false); // right Square4(data, paper, v[1], v[6], v[7], v[2], Color.white, false, false); // bottom Square4(data, paper, v[3], v[2], v[8], v[9], Color.white, false, false); // left Square4(data, paper, v[11], v[0], v[3], v[10], Color.white, false, false); BuildCorner3(data, paper, v[0], v[11], v[4]); BuildCorner3(data, paper, v[1], v[5], v[6]); BuildCorner3(data, paper, v[2], v[7], v[8]); BuildCorner3(data, paper, v[3], v[9], v[10]); } else // simple rectangle { Vector3 p1 = new Vector3(-x, +y, 0); Vector3 p2 = new Vector3(+x, +y, 0); Vector3 p3 = new Vector3(+x, -y, 0); Vector3 p4 = new Vector3(-x, -y, 0); Square4(data, paper, p1, p2, p3, p4, Color.white, false, false); } Vector2 textSize = Stock.TextSize * Stock.Scale; float s1 = Stock.BigSymbolSize * Stock.Scale; float s2 = Stock.SymbolSize * Stock.Scale; float s3 = Stock.CornerSymbolSize * Stock.Scale; //float symW = s2; //float symH = s2; //float rimX = Mathf.Max(textSize.x,symW); //float rimY = Mathf.Max(textSize.y,symH); CardShape symbol = Atlas.FindById(Definition.Symbol); if (symbol == null && !string.IsNullOrEmpty(Definition.Symbol)) { Debug.LogError(string.Format("Symbol shape '{0}' is not defined in atlas.", Definition.Symbol)); } // Full Image is used by cards like ACE/JOKER that might want a fully unique face image // Half Image is used by cards like JQK where half image is flipped and reflected for the top/bottom of the face image // Pattern is used to form standard layout of suit symbols for 2-10 cards CardShape fullImage = Definition.FullImage ? Atlas.FindById(Definition.Image) : null; CardShape halfImage = !Definition.FullImage ? Atlas.FindById(Definition.Image) : null; if (fullImage != null) { SubMesh core = GetMesh(card, table, fullImage); //Square4 (core,fullImage,v[0],v[1],v[2],v[3],Color.white,false,false); Square4(core, fullImage, c0, c1, c2, c3, Color.white, false, false); } else if (halfImage != null) { SubMesh core = GetMesh(card, table, halfImage); Vector3 lift = new Vector3(0, 0, -0.01f); Square4(core, halfImage, v[0] + lift, v[1] + lift, v[13] + lift, v[12] + lift, Color.white, false, false); Square4(core, halfImage, v[12] + lift, v[13] + lift, v[2] + lift, v[3] + lift, Color.white, true, true); } else if (Definition.Pattern != 0 && symbol != null) { if (Definition.Pattern >= 1 && Definition.Pattern < patternBits.Length) { SubMesh core = GetMesh(card, table, symbol); int bits = patternBits[Definition.Pattern]; float x0 = -x + bx; float x1 = +x - bx; float y0 = +y - by; float y1 = -y + by; for (int b = 0; b < 17; b++) { if ((bits & (1 << b)) != 0) { float px = Mathf.Lerp(x0, x1, patternPos[b].x); float py = Mathf.Lerp(y0, y1, patternPos[b].y); Vector2 symSize = (Definition.Pattern == 1) ? new Vector2(s1, s1) : new Vector2(s2, s2); if (Stock.AlwaysUpright || patternUpper[b]) { Square(core, symbol, new Vector3(px, py, -0.01f), symSize, Color.white); } else { SquareFlip(core, symbol, new Vector3(px, py, -0.01f), symSize, Color.white, true, true); } } } } else { Debug.LogError(string.Format("Pattern value '{0}' is not valid.", Definition.Pattern)); } } // Text is used for number/letter in corner of card (ex. A for Ace) CardShape text = Atlas.FindById(Definition.Text); if (text == null && !string.IsNullOrEmpty(Definition.Text)) { Debug.LogError(string.Format("Text shape '{0}' is not defined in atlas.", Definition.Text)); } if (text != null) { float tx = Stock.TextOffset.x * Stock.Scale; float ty = Stock.TextOffset.y * Stock.Scale; SubMesh sub = GetMesh(card, table, text); float x0 = -x + tx; // + (bx+rimX)*0.5f; float x1 = +x - tx; // - (bx+rimX)*0.5f; float y0 = +y - ty; //by; float y1 = -y + ty; //by; Color color = GetSymbolColor(Definition.Symbol); Square(sub, text, new Vector3(x0, y0, -0.01f), textSize, color); if (Stock.AlwaysUpright) { Square(sub, text, new Vector3(x1, y1, -0.01f), textSize, color); } else { SquareFlip(sub, text, new Vector3(x1, y1, -0.01f), textSize, color, true, true); } } // Symbol is used for corner suit icons (heart,diamond,spade,club) if (symbol != null) { float sx = Stock.SymbolOffset.x * Stock.Scale; float sy = Stock.SymbolOffset.y * Stock.Scale; SubMesh sub = GetMesh(card, table, symbol); //float gapY = s3/3; float x0 = -x + sx; //(bx+rimX)*0.5f; float x1 = +x - sx; //(bx+rimX)*0.5f; float y0 = +y - sy; //by - textSize.y - gapY - rad; float y1 = -y + sy; //by + textSize.y + gapY + rad; Color color = GetSymbolColor(Definition.Symbol); Vector2 halfSym = new Vector2(s3, s3); // Top-left corner symbol Square(sub, symbol, new Vector3(x0, y0, -0.01f), halfSym, color); // Bottom-right symbol can be up-right or upside-down depending on preferences if (Stock.AlwaysUpright) { Square(sub, symbol, new Vector3(x1, y1, -0.01f), halfSym, color); } else { SquareFlip(sub, symbol, new Vector3(x1, y1, -0.01f), halfSym, color, true, true); } } if (Stock.TwoSided) { BuildBack(card, table, paper); /* * CardShape back = Atlas.FindById(Stock.Back); * if (back != null) * { * Vector3 [] v = RectPoints(Stock.Size*Stock.Scale,Stock.BackBorder*Stock.Scale); * * SubMesh core = GetMesh(card,table,back); * // middle * if (Stock.HalfBack) * { * Square4(core,back,v[1],v[0],v[12],v[13],Color.white,false,false); * Square4(core,back,v[13],v[12],v[3],v[2],Color.white,true,true); * } * else * { * Square4(core,back,v[1],v[0],v[3],v[2],Color.white,false,false); * } * // top * Square4(core,paper,v[5],v[4],v[0],v[1],Color.white,false,false); * // back-left * Square5(core,paper,v[2],v[7],v[6],v[1],v[13],Color.white,false); * // bottom * Square4(core,paper,v[2],v[3],v[9],v[8],Color.white,false,false); * // back-right * Square5(core,paper,v[0],v[11],v[10],v[3],v[12],Color.white,false); * * BuildCorner3(core, paper, v[1],v[6],v[5]); * BuildCorner3(core, paper, v[2],v[8],v[7]); * BuildCorner3(core, paper, v[3],v[10],v[9]); * BuildCorner3(core, paper, v[0],v[4],v[11]); * } */ } Mesh mesh = SetupMesh(); mesh.vertices = card.GetCombinedVertices().ToArray(); mesh.triangles = data.IndexList.ToArray(); mesh.uv = card.GetCombinedTexCoords().ToArray(); mesh.colors = card.GetCombinedColors().ToArray(); if (card.MeshList.Count > 1) { mesh.subMeshCount = card.MeshList.Count; int vbase = 0; for (int i = 1; i < card.MeshList.Count; ++i) { SubMesh sub = card.MeshList[i]; int[] tris = sub.IndexList.ToArray(); vbase += card.MeshList[i - 1].VertexList.Count; for (int t = 0; t < tris.Length; ++t) { tris[t] += vbase; } mesh.SetTriangles(tris, i); } } mesh.RecalculateBounds(); mesh.Optimize(); mesh.RecalculateNormals(); this.GetComponent <Renderer>().sharedMaterials = card.Materials.ToArray(); // Setup Collider this.gameObject.AddComponent <BoxCollider2D>(); // Mark as not clicked this.Clicked = false; }