Пример #1
0
        public void AddEdge(int a, int b, int usage, bool checkContains = true)
        {
            TriangulatorEdge edge = new TriangulatorEdge(a, b, _edges.Count);

            if (checkContains && _edges.Contains(edge))
            {
                return;
            }

            _edges.Add(edge);
            _edgesUsage.Add(usage);

            curAddEdge = edge;
            DDARasterization.DrawLine(
                (nodes[a].x - chunkPos.x),
                (nodes[a].z - chunkPos.y),
                (nodes[b].x - chunkPos.x),
                (nodes[b].z - chunkPos.y),
                edgeMapPiselSize,
                AddEdgeDelegate);

            //Debuger_K.AddLine(nodes[a].positionV3, nodes[b].positionV3, Color.blue, 0.1f);
        }
Пример #2
0
        /// <summary>
        /// return used edges,
        /// </summary>
        private void MakeCell(TriangulatorEdge target, bool aFirst, List <TriangulatorEdge>[] edgesDictionary, TriangulatorNode[] nodes, out List <int> cellNodes, out List <TriangulatorEdge> cellEdges)
        {
            cellNodes = new List <int>();
            cellEdges = new List <TriangulatorEdge>();
            cellEdges.Add(target);
            int startNode;

            if (aFirst)  //a are origin
            {
                startNode = target.a;
                cellNodes.Add(target.a);
                cellNodes.Add(target.b);
            }
            else
            {
                startNode = target.b;
                cellNodes.Add(target.b);
                cellNodes.Add(target.a);
            }

            int limit = 0;

            harhar++;

            while (true)
            {
                limit++;
                if (limit > 50)
                {
#if UNITY_EDITOR
                    if (Debuger_K.doDebug && Debuger_K.debugOnlyNavMesh == false)
                    {
                        for (int i = 0; i < cellNodes.Count - 1; i++)
                        {
                            Vector3 a1 = nodes[cellNodes[i]].positionV3 + (Vector3.up * 0.02f * i);
                            Vector3 a2 = nodes[cellNodes[i + 1]].positionV3 + (Vector3.up * 0.02f * i);

                            Debuger_K.AddTriangulatorDebugLine(pos.x, pos.z, properties, a1, a2, Color.red);
                            //Debuger_K.AddTriangulatorDebugLabel(chunk, properties, SomeMath.MidPoint(a1, a2), i);
                        }
                    }
#endif
                    Debug.LogError("error while making cells " + harhar);
                    break;
                }

                int nodeMinus   = cellNodes[cellNodes.Count - 2];
                int nodeCurrent = cellNodes[cellNodes.Count - 1];
                int?nodePlus    = null;
                TriangulatorEdge?connectionPlus = null;

                Vector2 directionToMinus = (nodes[nodeMinus].positionV2 - nodes[nodeCurrent].positionV2).normalized;
                float   lowestAngle      = 180f;
                List <TriangulatorEdge> searchConnections = edgesDictionary[nodeCurrent];

                foreach (var connection in searchConnections)
                {
                    int potentialNodePlus = connection.GetOtherNode(nodeCurrent);

                    if (nodeMinus == potentialNodePlus)
                    {
                        continue;
                    }

                    Vector2 directionToPotentialPlus = (nodes[potentialNodePlus].positionV2 - nodes[nodeCurrent].positionV2).normalized;
                    float   cross        = SomeMath.V2Cross(directionToMinus, directionToPotentialPlus);
                    float   currentAngle = Vector2.Angle(directionToMinus, directionToPotentialPlus);

                    if (cross > 0f & currentAngle != 180)
                    {
                        continue;
                    }

                    if (currentAngle > lowestAngle)
                    {
                        continue;
                    }

                    connectionPlus = connection;
                    nodePlus       = potentialNodePlus;
                    lowestAngle    = currentAngle;
                }

                if (nodePlus == null)
                {
                    #region error
#if UNITY_EDITOR
                    if (Debuger_K.doDebug)
                    {
                        Debuger_K.AddTriangulatorDebugLine(pos.x, pos.z, properties, nodes[cellNodes[0]].positionV3, nodes[cellNodes[0]].positionV3 + SmallV3(0.3f), Color.green);

                        for (int i = 0; i < cellNodes.Count - 1; i++)
                        {
                            Debuger_K.AddTriangulatorDebugLine(pos.x, pos.z, properties, nodes[cellNodes[i]].positionV3 + SmallV3(0.1f), nodes[cellNodes[i + 1]].positionV3 + SmallV3(0.1f), Color.red);
                        }
                        for (int i = 0; i < cellNodes.Count - 1; i++)
                        {
                            Vector3 a1 = nodes[cellNodes[i]].positionV3 + SmallV3(0.02f * i);
                            Vector3 a2 = nodes[cellNodes[i + 1]].positionV3 + SmallV3(0.02f * i);
                            Debuger_K.AddTriangulatorDebugLine(pos.x, pos.z, properties, a1, a2, Color.red);
                        }

                        foreach (var connection in searchConnections)
                        {
                            int potentialNodePlus = connection.GetOtherNode(nodeCurrent);

                            if (nodeMinus == potentialNodePlus)
                            {
                                continue;
                            }

                            Vector2 directionToPotentialPlus = (nodes[potentialNodePlus].positionV2 - nodes[nodeCurrent].positionV2).normalized;

                            float cross = SomeMath.V2Cross(directionToMinus, directionToPotentialPlus);

                            Debuger_K.AddLabel(nodes[potentialNodePlus].positionV3, cross);
                            Debuger_K.AddLabel(nodes[nodeCurrent].positionV3, Vector2.Angle(directionToMinus, directionToPotentialPlus));
                            //Debuger_K.AddTriangulatorDebugLabel(chunk, properties, nodes[potentialNodePlus].positionV3, cross);
                            //Debuger_K.AddTriangulatorDebugLabel(chunk, properties, nodes[nodeCurrent].positionV3, Vector2.Angle(directionToMinus, directionToPotentialPlus));
                        }
                    }
#endif
                    #endregion
                    Debug.LogError("nodePlus == null");
                    break;
                }

                cellEdges.Add(connectionPlus.Value);

                if (nodePlus == startNode)
                {
                    break;
                }

                cellNodes.Add(nodePlus.Value);
            }
        }
Пример #3
0
        private void GenerateCells()
        {
            foreach (var dataSet in data)
            {
                TriangulatorNode[]      nodes    = dataSet.nodes;
                TriangulatorNodeData[]  nodeData = dataSet.data;
                List <TriangulatorEdge> edges    = dataSet.edges;
                List <int> edgesUsage            = dataSet.edgesUsage;

                List <TriangulatorEdge>[] edgesDictionary = new List <TriangulatorEdge> [nodes.Length];//index are node index to know
                for (int i = 0; i < nodes.Length; i++)
                {
                    edgesDictionary[i] = new List <TriangulatorEdge>();
                }

                foreach (var item in edges)
                {
                    edgesDictionary[item.a].Add(item);
                    edgesDictionary[item.b].Add(item);
                }

                //first iteration. we know if edge used once then it's directional and node "a" is left
                for (int i = 0; i < edgesUsage.Count; i++)
                {
                    if (edgesUsage[i] != 0)
                    {
                        continue;
                    }

                    TriangulatorEdge curentEdge = edges[i];

                    List <int> cellNodes;
                    List <TriangulatorEdge> cellEdges;

                    MakeCell(curentEdge, true, edgesDictionary, nodes, out cellNodes, out cellEdges);
                    foreach (var cellEdge in cellEdges)
                    {
                        edgesUsage[cellEdge.id]++;
                    }

                    List <Vector3> cellNodePositions = new List <Vector3>();
                    foreach (var item in cellNodes)
                    {
                        cellNodePositions.Add(nodes[item].positionV3);
                    }
                    combiner.AddCell(cellNodePositions, dataSet.area, dataSet.passability, dataSet.layer);
                }

                //yet again remain only with 1 usage edges, border edges are no more. next cell we dont know if it a left or right so we try bouth
                for (int i = 0; i < edgesUsage.Count; i++)
                {
                    if (edgesUsage[i] != 1)
                    {
                        continue;
                    }

                    TriangulatorEdge curentEdge = edges[i];

                    List <int> cellNodes;
                    List <TriangulatorEdge> cellEdges;

                    MakeCell(curentEdge, true, edgesDictionary, nodes, out cellNodes, out cellEdges);
                    foreach (var cellEdge in cellEdges)
                    {
                        if (edgesUsage[cellEdge.id] > 1)                                                       //mean this cell already was created
                        {
                            MakeCell(curentEdge, false, edgesDictionary, nodes, out cellNodes, out cellEdges); //key change here is false. mean left is node "b"
                            break;
                        }
                    }

                    foreach (var cellEdge in cellEdges)
                    {
                        edgesUsage[cellEdge.id]++;
                    }

                    List <Vector3> cellNodePositions = new List <Vector3>();
                    foreach (var item in cellNodes)
                    {
                        cellNodePositions.Add(nodes[item].positionV3);
                    }
                    combiner.AddCell(cellNodePositions, dataSet.area, dataSet.passability, dataSet.layer);
                }
            }
        }