コード例 #1
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
 public void AddTriangle(SkeletonTri newTriangle)
 {
     OffsetShapeLog.AddLine("Add Triangle: " + newTriangle[0].id + " " + newTriangle[1].id + " " + newTriangle[2].id);
     newTriangle.id = _triIndex;
     _triIndex++;
     _tris.Add(newTriangle);
 }
コード例 #2
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
        public void Clean(SkeletonData data)
        {
            int triCount = _tris.Count;

            for (int t = 0; t < triCount; t++)
            {
                SkeletonTri tri    = _tris[t];
                bool        remove = false;
                if (!data.isStaticNode(tri[0]))
                {
                    remove = true;
                }
                if (!data.isStaticNode(tri[1]))
                {
                    remove = true;
                }
                if (!data.isStaticNode(tri[2]))
                {
                    remove = true;
                }
                if (remove)
                {
                    _tris.RemoveAt(t);
                    triCount--;
                    t--;
                }
            }
        }
コード例 #3
0
        private void CalculateUVs(SkeletonTri tri)
        {
            int baseIndex = FindBaseIndex(tri);

            OffsetShapeLog.DrawLine(tri.centre, tri.positions[baseIndex], Color.red);
            Vector2 baseUV = Vector2.zero;

            OffsetShapeLog.AddLine("============");
            OffsetShapeLog.AddLine("triangle uv node find! ", tri.id, " base node id ", tri[baseIndex].id, " tangent ", tri.tangentV3);
            OffsetShapeLog.AddLine("============");
            foreach (UVNode uvnode in _uvnodes)
            {
                OffsetShapeLog.AddLine("uvnode! ", uvnode.node.id, uvnode.tangent);
                if (uvnode.node != tri[baseIndex])
                {
                    continue;
                }
                if (!uvnode.TangentCheck(tri.tangentV3))
                {
                    continue;
                }
                baseUV = uvnode.uv;
            }
            OffsetShapeLog.AddLine("!!!!!!!!!!!!");
            OffsetShapeLog.DrawLabel(tri.centre, baseUV.ToString());

            int indexB = (baseIndex + 1) % 3;
            int indexC = (baseIndex + 2) % 3;

            Vector3 p0 = tri.positions[baseIndex];
            Vector3 p1 = tri.positions[indexB];
            Vector3 p2 = tri.positions[indexC];

            Vector3 vA = p1 - p0;
            Vector3 vB = p2 - p0;

            Vector3 right   = tri.tangentV3;
            Vector3 up      = Vector3.Cross(right, tri.normal);
            Vector3 upVA    = Vector3.Project(vA, up);
            Vector3 rightVA = Vector3.Project(vA, right);
            Vector3 upVB    = Vector3.Project(vB, up);
            Vector3 rightVB = Vector3.Project(vB, right);

            float   apexUVAX = rightVA.magnitude * Mathf.Sign(Vector3.Dot(right, rightVA));
            float   apexUVAY = upVA.magnitude * Mathf.Sign(Vector3.Dot(up, upVA));
            Vector2 apexUVA  = baseUV + new Vector2(apexUVAX, apexUVAY);
            float   apexUVBX = rightVB.magnitude * Mathf.Sign(Vector3.Dot(right, rightVB));
            float   apexUVBY = upVB.magnitude * Mathf.Sign(Vector3.Dot(up, upVB));
            Vector2 apexUVB  = baseUV + new Vector2(apexUVBX, apexUVBY);

            _uvnodes.Add(new UVNode(tri[indexB], apexUVA, right));
            _uvnodes.Add(new UVNode(tri[indexC], apexUVB, right));

            Vector2[] uvs = new Vector2[3];
            uvs[baseIndex] = baseUV;
            uvs[indexB]    = apexUVA;
            uvs[indexC]    = apexUVB;
            tri.uvs        = uvs;
        }
コード例 #4
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
 public SkeletonTri[] Convert()
 {
     SkeletonTri[] output;
     if (size == 3)
     {
         output = new[] { new SkeletonTri(_nodes[0], _nodes[1], _nodes[2], _tangent) };
     }
     else
     {
         output    = new SkeletonTri[2];
         output[0] = new SkeletonTri(_nodes[0], _nodes[1], _nodes[2], _tangent);
         output[1] = new SkeletonTri(_nodes[2], _nodes[3], _nodes[1], _tangent);
     }
     return(output);
 }
コード例 #5
0
        private int FindBaseIndex(SkeletonTri tri)
        {
            System.Collections.Generic.List <int> startNodeIndices = new System.Collections.Generic.List <int>();
            for (int i = 0; i < 3; i++)
            {
                if (tri[i].startNode)
                {
                    startNodeIndices.Add(i);
                }
            }
            int startNodeCount = startNodeIndices.Count;

            if (startNodeCount == 1)
            {
                return(startNodeIndices[0]);
            }

            if (startNodeCount > 1)
            {
                float lowestDot = 1;
                int   output    = 0;
                for (int i = 0; i < startNodeCount; i++)
                {
                    Vector3 dir = (tri.positions[startNodeIndices[i]] - tri.centre).normalized;
                    float   dot = Vector3.Dot(dir, tri.tangentV3);
                    if (dot < lowestDot)
                    {
                        lowestDot = dot;
                        output    = startNodeIndices[i];
                    }
                }
                return(output);
            }

            float lowestY     = Mathf.Infinity;
            int   lowestIndex = 0;

            for (int i = 0; i < 3; i++)
            {
                if (tri.positions[i].y < lowestY)
                {
                    lowestY     = tri.positions[i].y;
                    lowestIndex = i;
                }
            }
            return(lowestIndex);
        }
コード例 #6
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
        public SkeletonTri RetireNode(Node node)
        {
            if (!_nodes.Contains(node))
            {
                return(null);
            }

            SkeletonTri output = new SkeletonTri(_nodes[0], _nodes[1], node, _tangent);

            if (_nodes[2] == node)
            {
                _nodes.Remove(_nodes[0]);
                return(output);
            }
            if (_nodes[3] == node)
            {
                _nodes.Remove(_nodes[1]);
                return(output);
            }
            return(null);
        }
コード例 #7
0
        public void Build(SkeletonData data)
        {
            System.Collections.Generic.List <SkeletonTri> allTris   = new System.Collections.Generic.List <SkeletonTri>(data.mesh.GetTriangles());
            System.Collections.Generic.List <SkeletonTri> startTris = new System.Collections.Generic.List <SkeletonTri>();
            int triCount = allTris.Count;

            for (int t = 0; t < triCount; t++)
            {
                SkeletonTri tri = allTris[t];
                if (tri.hasStartEdge)
                {
                    allTris.RemoveAt(t);
                    triCount--;
                    t--;
                    startTris.Add(tri);
                }
            }

            while (startTris.Count > 0)
            {
                SkeletonTri tri = startTris[0];
                startTris.RemoveAt(0);
                CalculateUVs(tri);
            }

            while (allTris.Count > 0)
            {
                SkeletonTri tri = allTris[0];
                allTris.RemoveAt(0);
                CalculateUVs(tri);
            }

            foreach (UVNode uvnode in _uvnodes)
            {
                OffsetShapeLog.DrawLabel(uvnode.node.position, "\n\n" + uvnode.uv);
            }
        }
コード例 #8
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
        public void SplitEdge(SplitEvent e)
        {
            Edge         oldLiveEdge  = e.edge;
            Vector3      splitTangent = Utils.ToV3(oldLiveEdge.direction);
            SkeletonFace oldFace      = _nodeDic[oldLiveEdge];
            Node         staticNode   = e.newStaticNode;

            Edge newLiveEdgeA = e.newLiveEdgeA;
            Edge newLiveEdgeB = e.newLiveEdgeB;

            Node oldNode      = e.node;
            Node newLiveNodeA = e.newLiveNodeA;
            Node newLiveNodeB = e.newLiveNodeB;

            //centre tri
            SkeletonTri newTriangle = new SkeletonTri(oldFace[0], oldFace[1], staticNode, splitTangent);

            AddTriangle(newTriangle);

            if (!_nodeDic.ContainsKey(e.nodeEdgeA))
            {
                return;
            }
            SkeletonFace faceA = _nodeDic[e.nodeEdgeA];

            faceA.ReplaceNode(oldNode, newLiveNodeA);
            //left tri
            SkeletonTri newTriangleA = new SkeletonTri(staticNode, faceA[0], newLiveNodeA, splitTangent);

            AddTriangle(newTriangleA);

            if (!_nodeDic.ContainsKey(e.nodeEdgeB))
            {
                return;
            }
            SkeletonFace faceB = _nodeDic[e.nodeEdgeB];

            faceB.ReplaceNode(oldNode, newLiveNodeB);
            //right tri
            SkeletonTri newTriangleB = new SkeletonTri(faceB[1], staticNode, newLiveNodeB, splitTangent);

            AddTriangle(newTriangleB);

            RemoveFace(oldFace);
            AddFace(newLiveEdgeA, oldFace[0], staticNode);
            AddFace(newLiveEdgeB, staticNode, oldFace[1]);

            int oldNodeReferenceCount = 0;

            if (_edgeDic.ContainsKey(oldNode))
            {
                oldNodeReferenceCount = _edgeDic[oldNode].Count;
            }
            for (int r = 0; r < oldNodeReferenceCount; r++)
            {
                SkeletonFace face     = _edgeDic[oldNode][r];
                Edge         liveEdge = face.liveEdge;

                if (liveEdge.Contains(newLiveNodeA))
                {
                    if (!_edgeDic.ContainsKey(newLiveNodeA))
                    {
                        _edgeDic.Add(newLiveNodeA, new List <SkeletonFace>());
                    }
                    _edgeDic[newLiveNodeA].Add(face);
                }
                if (liveEdge.Contains(newLiveNodeB))
                {
                    if (!_edgeDic.ContainsKey(newLiveNodeB))
                    {
                        _edgeDic.Add(newLiveNodeB, new List <SkeletonFace>());
                    }
                    _edgeDic[newLiveNodeB].Add(face);
                }
            }
        }
コード例 #9
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
        public void CollapseEdge(Edge liveEdge, Node newLiveNode, Node newStaticNode)
        {
            Edge edgeA = null, edgeB = null;

            if (!_edgeDic.ContainsKey(newLiveNode))
            {
                _edgeDic.Add(newLiveNode, new List <SkeletonFace>());
            }
            foreach (SkeletonFace face in _faces)
            {
                Edge liveFaceEdge = face.liveEdge;

                if (liveEdge == liveFaceEdge)
                {
                    continue;
                }

                if (liveFaceEdge.Contains(liveEdge.nodeA))
                {
                    edgeA = liveFaceEdge;
                    _edgeDic[newLiveNode].Add(face);
                }
                if (liveFaceEdge.Contains(liveEdge.nodeB))
                {
                    edgeB = liveFaceEdge;
                    _edgeDic[newLiveNode].Add(face);
                }
            }

            if (edgeA != null)
            {
                SkeletonFace faceA = _nodeDic[edgeA];
                Node         n0    = faceA[0];
                Node         n1    = faceA[1];
                Node         n2    = newStaticNode;
                if (n0 != n2 && n1 != n2)
                {
                    SkeletonTri newtriangle = new SkeletonTri(n0, n1, n2, faceA.tangent);
                    AddTriangle(newtriangle);//remove static triangle from face - add to triangle stack
                    faceA.ReplaceNode(faceA[1], newStaticNode);
                    faceA.ReplaceNode(faceA[3], newLiveNode);
                }
            }
            if (edgeB != null)
            {
                SkeletonFace faceB = _nodeDic[edgeB];
                Node         n0    = faceB[0];
                Node         n1    = faceB[1];
                Node         n2    = newStaticNode;
                if (n0 != n2 && n1 != n2)
                {
                    SkeletonTri newtriangle = new SkeletonTri(n0, n1, n2, faceB.tangent);
                    AddTriangle(newtriangle);
                    faceB.ReplaceNode(faceB[0], newStaticNode);
                    faceB.ReplaceNode(faceB[2], newLiveNode);
                }
            }

            if (_nodeDic.ContainsKey(liveEdge))
            {
                SkeletonFace liveFace = _nodeDic[liveEdge];
                Node         lfn0     = liveFace[0];
                Node         lfn1     = liveFace[1];
                Node         lfn2     = newStaticNode;
                if (lfn0 != lfn2 && lfn1 != lfn2)
                {
                    SkeletonTri newFaceTriangle = new SkeletonTri(lfn0, lfn1, lfn2, liveFace.tangent);
                    AddTriangle(newFaceTriangle);
                }
                _nodeDic.Remove(liveEdge);
                RemoveFace(liveFace);
            }
        }
コード例 #10
0
ファイル: SkeletonMesh.cs プロジェクト: ishui/unity3DScripts
        public void AddTriangle(Node a, Node b, Node c, Vector3 tangent)
        {
            SkeletonTri newTriangle = new SkeletonTri(a, b, c, tangent);

            AddTriangle(newTriangle);
        }