コード例 #1
0
    public UgMatrix2D Concat(UgMatrix2D other)
    {
        var a  = this.a * other.a;
        var b  = 0.0f;
        var c  = 0.0f;
        var d  = this.d * other.d;
        var tx = this.tx * other.a + other.tx;
        var ty = this.ty * other.d + other.ty;

        if (this.b != 0.0f || this.c != 0.0f || other.b != 0.0f || other.c != 0.0f)
        {
            a  += this.b * other.c;
            d  += this.c * other.b;
            b  += this.a * other.b + this.b * other.d;
            c  += this.c * other.a + this.d * other.c;
            tx += this.ty * other.c;
            ty += this.tx * other.b;
        }

        this.a  = a;
        this.b  = b;
        this.c  = c;
        this.d  = d;
        this.tx = tx;
        this.ty = ty;
        return(this);
    }
コード例 #2
0
    public UgMatrix2D Invert(UgMatrix2D target)
    {
        var a  = this.a;
        var b  = this.b;
        var c  = this.c;
        var d  = this.d;
        var tx = this.tx;
        var ty = this.ty;

        if (b == 0 && c == 0)
        {
            target.b = target.c = 0;
            if (a == 0 || d == 0)
            {
                target.a = target.d = target.tx = target.ty = 0;
            }
            else
            {
                a         = target.a = 1 / a;
                d         = target.d = 1 / d;
                target.tx = -a * tx;
                target.ty = -d * ty;
            }
            return(this);
        }
        var determinant = a * d - b * c;

        if (determinant == 0)
        {
            target.Identity();
            return(this);
        }
        determinant = 1 / determinant;
        var k = target.a = d * determinant;

        b         = target.b = -b * determinant;
        c         = target.c = -c * determinant;
        d         = target.d = a * determinant;
        target.tx = -(k * tx + c * ty);
        target.ty = -(b * tx + d * ty);
        return(this);
    }
コード例 #3
0
    void LateUpdate()
    {
        sharedMesh = new Mesh();
        int k           = 0;
        var displayData = data.armatures[0].displayDatas;
        var vertexDatas = data.armatures[0].vertexDatas;
        //Vector3[] vertices = new Vector3[count];
        //Vector2[] uvs = new Vector2[count];
        List <Vector3> vertices        = new List <Vector3>();
        List <Vector2> uvs             = new List <Vector2>();
        List <int>     triangles       = new List <int>();
        var            textureDatas    = data.armatures[0].textureDatas;
        var            sourceWidth     = textureDatas.width;
        var            sourceHeight    = textureDatas.height;
        var            subTextureDatas = textureDatas.datas;

        //foreach (UgBone bone in currentArmature.bonesList)
        //{
        foreach (UgSlot slot in currentArmature.slotList)
        {
            UgSprite d_data = slot.children[0] as UgSprite;
            //foreach (UgSprite d_data in slot.sprites)
            //{

            if (d_data.offset >= 0)
            {
                float targetWidth  = 0;
                float targetHeight = 0;
                float targetX      = 0;
                float targetY      = 0;
                int   weightoffset = d_data.weightOffset;
                for (int j = 0; j < subTextureDatas.Length; j++)
                {
                    if (subTextureDatas[j].name == d_data.name)
                    {
                        targetWidth  = subTextureDatas[j].width;
                        targetHeight = subTextureDatas[j].height;
                        targetX      = subTextureDatas[j].x;
                        targetY      = subTextureDatas[j].y;
                        break;
                    }
                }
                var count = vertices.Count;
                for (int j = 0; j < d_data.count; j++)
                {
                    Vector2 vertex = Vector2.zero;
                    Vector2 uv     = new Vector2();


                    Vector2    pos       = new Vector2(vertexDatas.vertices[(d_data.offset + j) * 2], vertexDatas.vertices[(d_data.offset + j) * 2 + 1]);
                    Vector2    result    = Vector2.zero;
                    int        boneCount = (int)vertexDatas.weights[weightoffset++];
                    UgMatrix2D matrix    = new UgMatrix2D();
                    Vector2    scale     = Vector2.one;
                    float      rotation  = 0f;
                    Vector2    trans     = Vector3.zero;
                    for (int c = 0; c < boneCount; c++)
                    {
                        UgBone bone = currentArmature.bonesList[(int)vertexDatas.weights[weightoffset]];
                        weightoffset++;
                        if (bone != null)
                        {
                            //Debug.Log(bone.deltaMatrix.ToString());
                            //Debug.Log(bone.matrix.tx - bone.startMatrix.tx);
                            //matrix.a +=  (bone.matrix.a - bone.startMatrix.a) * vertexDatas.weights[weightoffset];
                            //matrix.b +=  (bone.matrix.b - bone.startMatrix.b) * vertexDatas.weights[weightoffset];
                            //matrix.c +=  (bone.matrix.c - bone.startMatrix.c) * vertexDatas.weights[weightoffset];
                            //matrix.d +=  (bone.matrix.d - bone.startMatrix.d) * vertexDatas.weights[weightoffset];
                            //matrix.tx += (bone.matrix.tx - bone.startMatrix.tx) * vertexDatas.weights[weightoffset];
                            //matrix.ty += (bone.matrix.ty - bone.startMatrix.ty) * vertexDatas.weights[weightoffset];
                            //matrix.a += (bone.deltaMatrix.a) * vertexDatas.weights[weightoffset];
                            //matrix.b += (bone.deltaMatrix.b) * vertexDatas.weights[weightoffset];
                            //matrix.c += (bone.deltaMatrix.c) * vertexDatas.weights[weightoffset];
                            //matrix.d += (bone.deltaMatrix.d) * vertexDatas.weights[weightoffset];
                            //matrix.tx += (bone.deltaMatrix.tx) * vertexDatas.weights[weightoffset];
                            //matrix.ty += (bone.deltaMatrix.ty) * vertexDatas.weights[weightoffset];
                            //scale += (bone.scale - bone.startScale) * vertexDatas.weights[weightoffset];
                            //rotation += (bone.rotation - bone.startRotation) * vertexDatas.weights[weightoffset];
                            //trans += (bone.pos - bone.startPos) * vertexDatas.weights[weightoffset];
                            scale    += new Vector2(bone.matrix.GetScaleX() - bone.startMatrix.GetScaleX(), bone.matrix.GetScaleY() - bone.startMatrix.GetScaleY()) * vertexDatas.weights[weightoffset];
                            rotation += (bone.matrix.GetAngle() - bone.startMatrix.GetAngle()) * vertexDatas.weights[weightoffset];
                            trans    += new Vector2(bone.matrix.tx - bone.startMatrix.tx, bone.matrix.ty - bone.startMatrix.ty) * vertexDatas.weights[weightoffset];
                            //vertex += bone.deltaMatrix.TransformPoint(pos.x, pos.y) * vertexDatas.weights[weightoffset];
                        }
                        weightoffset++;
                    }
                    matrix.CreateBox(scale.x, scale.y, rotation, trans.x, trans.y);
                    //matrix.Concat(d_data.parent.matrix);
                    vertex = matrix.TransformPoint(pos.x, pos.y);
                    //pos = new Vector2(pos.x * scale.x, pos.y * scale.y);
                    //var r = -2 * Mathf.PI * rotation / 360;
                    //pos = new Vector2(pos.x * Mathf.Cos(r) - pos.y * Mathf.Sin(r), pos.x * Mathf.Sin(r) + pos.y * Mathf.Cos(r));
                    //pos = pos + trans;
                    //vertex = pos;
                    //Vector2 scale = Vector2.one;
                    //float rotation = 0f;
                    //Vector2 trans = Vector3.zero;
                    //for (int c = 0; c < boneCount; c++)
                    //{
                    //    UgBone bone = currentArmature.bonesList[(int)vertexDatas.weights[weightoffset]];
                    //    weightoffset++;
                    //    if (bone != null)
                    //    {
                    //        scale += (bone.scale-bone.startScale) * vertexDatas.weights[weightoffset];
                    //        rotation += (bone.rotation-bone.startRotation) * vertexDatas.weights[weightoffset];
                    //        trans += (bone.pos-bone.startPos) * vertexDatas.weights[weightoffset];
                    //    }
                    //    weightoffset++;
                    //}
                    //pos = new Vector2(pos.x * scale.x, pos.y * scale.y);
                    //var r = 2 * Mathf.PI * rotation / 360;
                    //pos = new Vector2(pos.x * Mathf.Cos(r) - pos.y * Mathf.Sin(r), pos.x * Mathf.Sin(r) + pos.y * Mathf.Cos(r));
                    //pos = pos + trans;
                    //vertex = pos;
                    uv.x = (vertexDatas.uvs[(d_data.offset + j) * 2] * targetWidth + targetX) / sourceWidth;
                    uv.y = 1 - (vertexDatas.uvs[(d_data.offset + j) * 2 + 1] * targetHeight + targetY) / sourceHeight;
                    //vertices[d_data.offset + j] = vertex;
                    //uvs[d_data.offset + j] = uv;
                    vertices.Add(vertex);
                    uvs.Add(uv);
                }

                for (int j = 0; j < d_data.triangleCount;)
                {
                    triangles.Add(count + vertexDatas.triangles[d_data.triangleOffset + j++]);
                    triangles.Add(count + vertexDatas.triangles[d_data.triangleOffset + j++]);
                    triangles.Add(count + vertexDatas.triangles[d_data.triangleOffset + j++]);
                }
            }
            //图片
            else
            {
                if (k > 5)
                {
                    continue;
                }
                k++;
                float targetWidth  = 0;
                float targetHeight = 0;
                float targetX      = 0;
                float targetY      = 0;
                float pivotX       = 0;
                float pivotY       = 0;
                for (int j = 0; j < subTextureDatas.Length; j++)
                {
                    if (subTextureDatas[j].name == d_data.name)
                    {
                        targetWidth  = subTextureDatas[j].width;
                        targetHeight = subTextureDatas[j].height;
                        targetX      = subTextureDatas[j].x;
                        targetY      = subTextureDatas[j].y;
                        break;
                    }
                }
                pivotX = d_data.pivot.x;
                pivotY = d_data.pivot.y;
                var   count = vertices.Count;
                float x = 0, y = 0;
                for (int j = 0; j < 4; j++)
                {
                    int u = 0, v = 0;
                    if (0 == j)
                    {
                        u = 0;
                        v = 0;
                        x = -targetWidth * pivotX;
                        y = -targetHeight * pivotY;
                    }
                    else if (1 == j)
                    {
                        u = 1;
                        v = 0;
                        x = targetWidth * (1 - pivotX);
                        y = -targetHeight * pivotY;
                    }
                    else if (2 == j)
                    {
                        u = 1;
                        v = 1;
                        x = targetWidth * (1 - pivotX);
                        y = targetHeight * (1 - pivotY);
                    }
                    else if (3 == j)
                    {
                        u = 0;
                        v = 1;
                        x = -targetWidth * pivotX;
                        y = targetHeight * (1 - pivotY);
                    }

                    Vector3 vertex = new Vector3();
                    Vector2 uv     = new Vector2();
                    Vector2 pos    = d_data.matrix.TransformPoint(x, y);
                    //Vector2 pos = new Vector2(x * (d_data.scale).x, y * (d_data.scale).y);
                    //var r = -2 * Mathf.PI * (d_data.rotation) / 360;
                    //pos = new Vector2(pos.x * Mathf.Cos(r) - pos.y * Mathf.Sin(r), pos.x * Mathf.Sin(r) + pos.y * Mathf.Cos(r));
                    //pos = pos + d_data.pos;
                    //var bone = d_data.parent;
                    //while (bone != null)
                    //{
                    //    pos = new Vector2(pos.x * (bone.scale).x, pos.y * (bone.scale).y);
                    //    r = -2 * Mathf.PI * (bone.rotation) / 360;
                    //    pos = new Vector2(pos.x * Mathf.Cos(r) - pos.y * Mathf.Sin(r), pos.x * Mathf.Sin(r) + pos.y * Mathf.Cos(r));
                    //    pos = pos + (bone.pos);
                    //    bone = bone.parent;
                    //}
                    vertex.x = pos.x;
                    vertex.y = pos.y;
                    //Debug.Log(vertex.x + "," + vertex.y);
                    //Debug.Log("-----------");
                    uv.x = (u * targetWidth + targetX) / sourceWidth;
                    uv.y = 1 - (v * targetHeight + targetY) / sourceHeight;
                    //vertices[d_data.offset + j] = vertex;
                    //uvs[d_data.offset + j] = uv;
                    vertices.Add(vertex);
                    uvs.Add(uv);
                }

                triangles.Add(count + 0);
                triangles.Add(count + 3);
                triangles.Add(count + 2);
                triangles.Add(count + 2);
                triangles.Add(count + 1);
                triangles.Add(count + 0);
                //}
                //continue;
            }
        }
        //}
        sharedMesh.vertices = vertices.ToArray();
        sharedMesh.uv       = uvs.ToArray();

        sharedMesh.triangles  = triangles.ToArray();
        meshFilter.sharedMesh = sharedMesh;
    }
コード例 #4
0
 public UgMatrix2D Copy(UgMatrix2D matrix)
 {
     return(this.SetTo(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty));
 }
コード例 #5
0
    public void updateMatrix()
    {
        if (null == this.matrix)
        {
            //Debug.Log("updateMatrix");
            this.matrix      = new UgMatrix2D();
            this.deltaMatrix = new UgMatrix2D();
            this.startMatrix = new UgMatrix2D();
        }
        if (this.rotation == 0.0f)
        {
            this.matrix.a = 1.0f;
            this.matrix.b = 0.0f;
        }
        else
        {
            this.matrix.a = (float)Math.Cos(-Math.PI * this.rotation / 180);
            this.matrix.b = (float)Math.Sin(-Math.PI * this.rotation / 180);
        }

        //if (this.skew == 0.0f)
        //{
        this.matrix.c = -this.matrix.b;
        this.matrix.d = this.matrix.a;
        //}
        //else
        //{
        //    matrix.c = -(float)Math.Sin(this.skew + this.rotation);
        //    matrix.d = (float)Math.Cos(this.skew + this.rotation);
        //}

        if (scale.x != 1.0f)
        {
            this.matrix.a *= this.scale.x;
            this.matrix.b *= this.scale.x;
        }

        if (scale.y != 1.0f)
        {
            this.matrix.c *= this.scale.y;
            this.matrix.d *= this.scale.y;
        }

        this.matrix.tx = this.pos.x;
        this.matrix.ty = this.pos.y;
        matrix.CreateBox(scale.x, scale.y, -rotation, pos.x, pos.y);
        if (this.parent != null)
        {
            this.matrix.Concat(this.parent.matrix);
            startMatrix.CreateBox(this.startScale.x, this.startScale.y, -this.startRotation, this.startPos.x, this.startPos.y);
            startMatrix.Concat(this.parent.startMatrix);
            //deltaMatrix.SetTo(matrix.a-startMatrix.a,matrix.b-startMatrix.b,matrix.c-startMatrix.c,matrix.d-startMatrix.d,matrix.tx-startMatrix.tx,matrix.ty-startMatrix.ty);
        }

        //foreach (UgTrans bone in children)
        //{
        //    Debug.Log(bone.name + " --- " + "updateMatrix");
        //}

        foreach (UgTrans bone in this.children)
        {
            bone.updateMatrix();
        }
    }