private static void GeneratePackTextureImpl(CNode node, ref PackTextureAttrSet packTextureAttrSet) { if (node != null && packTextureAttrSet != null && packTextureAttrSet.packTexture) { if (node.IsFilled) { TextureVertexAttr attri = node.refTextureInfo; CalcVertexAtrribute( packTextureAttrSet.packTexWidth, packTextureAttrSet.packTexHeight, ref attri ); packTextureAttrSet.texVertexAttrList.Add(attri); Fill2PackTexture(packTextureAttrSet.packTexture, attri); } if (node.leftChild != null) { GeneratePackTextureImpl(node.leftChild, ref packTextureAttrSet); } if (node.rightChild != null) { GeneratePackTextureImpl(node.rightChild, ref packTextureAttrSet); } } }
public TextureVertexAttr GetTextureVertexAttr(string szSpriteName) { TextureVertexAttr ret = new TextureVertexAttr(); if (!string.IsNullOrEmpty(szSpriteName)) { for (int i = 0; i < texVertexAttrList.Count; ++i) { var attr = texVertexAttrList[i]; if (attr.IsVaild && attr.spriteName.Equals(szSpriteName)) { ret = attr; break; } } } return(ret); }
private static void Fill2PackTexture(Texture2D packTex, TextureVertexAttr vertexAttr) { if (packTex != null && vertexAttr.refTexture != null) { Texture2D refTex = vertexAttr.refTexture; if (vertexAttr.blockDetail.IsFilped) { //非得这样填充才是对的,我也不知道为啥 for (int i = 0; i < refTex.width; ++i) { for (int j = 0; j < refTex.height; ++j) { Color color = refTex.GetPixel(i, j); if (i < vertexAttr.blockDetail.rect.h && j < vertexAttr.blockDetail.rect.w) { packTex.SetPixel(vertexAttr.blockDetail.rect.x + j, vertexAttr.blockDetail.rect.y + i, color); } } } } else { Color[] colors = refTex.GetPixels(); packTex.SetPixels( vertexAttr.blockDetail.rect.x, vertexAttr.blockDetail.rect.y, vertexAttr.blockDetail.rect.w, vertexAttr.blockDetail.rect.h, colors ); } } }
private static void CalcVertexAtrribute( int packTexWidth, //图集大小 int packTexHeight, ref TextureVertexAttr vertexAttr ) { if (!vertexAttr.IsVaild) { return; } if (vertexAttr.refTexture == null) { return; } int x = vertexAttr.blockDetail.rect.x; int y = vertexAttr.blockDetail.rect.y; int texWidth = vertexAttr.blockDetail.rect.w; int texHeight = vertexAttr.blockDetail.rect.h; int halfPackTexWidth = packTexWidth / 2; int halfPackTexHeight = packTexHeight / 2; //输出时,纹理四个角的相对位置 Vector2 packTextureBottomLeft = new Vector2(-halfPackTexWidth, -halfPackTexHeight); Vector2 packTextureTopLeft = new Vector2(-halfPackTexWidth, halfPackTexHeight); Vector2 packTextureTopRight = new Vector2(halfPackTexWidth, halfPackTexHeight); Vector2 packTextureBottomRight = new Vector2(halfPackTexWidth, -halfPackTexHeight); //顶点位置 if (vertexAttr.blockDetail.IsFilped) { Vector3 texBottomLeft = new Vector3(x - halfPackTexWidth, y - halfPackTexHeight, 0); //填充顶点位置 vertexAttr.blockDetail.posBL = texBottomLeft; vertexAttr.blockDetail.posTL = new Vector3(texBottomLeft.x, texBottomLeft.y + texHeight); //top-left ; vertexAttr.blockDetail.posTR = new Vector3(texBottomLeft.x + texWidth, texBottomLeft.y + texHeight); vertexAttr.blockDetail.posBR = new Vector3(texBottomLeft.x + texWidth, texBottomLeft.y); Vector2 uvBottomLeft = new Vector2((float)x / (float)packTexWidth, (float)y / (float)packTexHeight); Vector2 uvTopRight = new Vector2((float)(x + texWidth) / (float)packTexWidth, (float)(y + texHeight) / (float)packTexHeight); //注意,这个是翻转了的 Vector2 ucTopLeft = new Vector2((float)uvTopRight.x, (float)uvBottomLeft.y); Vector2 uvBottomRight = new Vector2((float)uvBottomLeft.x, (float)uvTopRight.y); vertexAttr.blockDetail.uvBL = uvBottomLeft; vertexAttr.blockDetail.uvTL = ucTopLeft; vertexAttr.blockDetail.uvTR = uvTopRight; vertexAttr.blockDetail.uvBR = uvBottomRight; } else { //填充顶点位置 Vector3 texBottomLeft = new Vector3(x - halfPackTexWidth, y - halfPackTexHeight, 0); vertexAttr.blockDetail.posBL = texBottomLeft; vertexAttr.blockDetail.posTR = new Vector3(texBottomLeft.x + texWidth, texBottomLeft.y + texHeight); vertexAttr.blockDetail.posTL = new Vector3(texBottomLeft.x, texBottomLeft.y + texHeight); //top-left ; vertexAttr.blockDetail.posBR = new Vector3(texBottomLeft.x + texWidth, texBottomLeft.y); //uv使用的采样纹理 Vector2 uvBottomLeft = new Vector2((float)x / (float)packTexWidth, (float)y / (float)packTexHeight); Vector2 uvTopRight = new Vector2((float)(x + texWidth) / (float)packTexWidth, (float)(y + texHeight) / (float)packTexHeight); Vector2 ucTopLeft = new Vector2((float)uvBottomLeft.x, (float)uvTopRight.y); Vector2 uvBottomRight = new Vector2((float)uvTopRight.x, (float)uvBottomLeft.y); vertexAttr.blockDetail.uvBL = uvBottomLeft; vertexAttr.blockDetail.uvTL = ucTopLeft; vertexAttr.blockDetail.uvTR = uvTopRight; vertexAttr.blockDetail.uvBR = uvBottomRight; } }