Beispiel #1
0
    public GameObject InsertPointAt(Transform edge, Vector3 position)
    {
        _Block originBlock = edge.GetComponent <_Block>();
        int    index       = blocks.IndexOf(originBlock);

        _Vertex point = vertexPrefab.Spawn();

        points.Insert(index, point);
        point.transform.parent        = transform;
        point.transform.localRotation = Quaternion.identity;
        point.transform.position      = position;   //ition;

        Vector2 localPosition = point.transform.localPosition;

        originBlock.SetEndsPosition(localPosition, _vertices[index]);

        _Block block = edgePrefab.Spawn();

        blocks.Insert(index, block);
        block.transform.parent = transform;
        block.SetEndsPosition(_vertices[PreIndex(index)], localPosition);


        _vertices.Insert(index, localPosition);

        return(point.gameObject);
    }
    public void InitialPoints()
    {
        foreach (var pos in InitialVertices)
        {
            _Vertex vertex = vertexPrefab.Spawn();
            vertex.transform.SetParent(transform);
            vertex.transform.localPosition = pos;
            vertex.Border = this;

            vertexs.Add(vertex);
        }

        Face2D face = Shape.AddPoints(vertexs.Select(v => v.Point).ToArray());

        face.Name = "BorderFace";

        foreach (var e in face.BasicEdges)
        {
            _Block block = blockPrefab.Spawn();
            block.transform.SetParent(transform);
            block.Border = this;

            block.Edge = e;
            blocks.Add(block);
        }

        _lazyAllVertices.isValeChanged = true;
    }
Beispiel #3
0
    private void SpawnPoint(int index)
    {
        Vector2 position = _vertices[index];

//		GameObject pointObj = Instantiate( vertexPrefab) as GameObject;
//		pointObj.transform.parent = transform;
//		pointObj.transform.localRotation = Quaternion.identity;
//		pointObj.transform.localPosition = position;
//
//		_Point point = pointObj.GetComponent<_Point>();
//		points.Add (point);

        _Vertex point = vertexPrefab.Spawn(transform.position, transform.rotation);

        points.Add(point);

        point.transform.parent        = transform;
        point.transform.localRotation = Quaternion.identity;
        point.transform.localPosition = position;

//		GameObject blockObj = Instantiate( edgePrefab) as GameObject;
//		blockObj.transform.parent = transform;
//
//		_Block block = blockObj.GetComponent<_Block>();
//		blocks.Add (block);
        _Block block = edgePrefab.Spawn(transform.position, transform.rotation);

        blocks.Add(block);

        block.transform.parent = transform;

        block.SetEndsPosition(_vertices[PreIndex(index)], position);
    }
 public void TransferPoint(_Vertex vertex)
 {
     if (!_lazyAllVertices.isValeChanged)
     {
         _lazyAllVertices.isValeChanged = true;
     }
 }
Beispiel #5
0
    private void checkReachable()
    {
        _Vertex[] _vertices = new _Vertex[_waypoints.Length];
        //Debug.Log(gameObject.name + " # of waypoints " + _waypoints.Length);

        foreach (GameObject wp in _waypoints)
        {
            WaypointNode WaypointNodeScript = wp.GetComponent <WaypointNode>();
            WaypointNodeScript.isConnected   = false;
            _vertices[WaypointNodeScript.ID] = new _Vertex(wp);
        }

        _Vertex         _currentVertex = _vertices[0];
        Queue <_Vertex> verticesLeft   = new Queue <_Vertex>();

        foreach (GameObject wp in _currentVertex.waypointData.GetComponent <WaypointNode>().nearbyWaypoints)
        {
            WaypointNode WaypointNodeScript = wp.GetComponent <WaypointNode>();
            verticesLeft.Enqueue(_vertices[WaypointNodeScript.ID]);
        }

        _currentVertex.isReachable = true;
        _currentVertex.wasVisited  = true;

        while (verticesLeft.Count > 0)
        {
            _currentVertex = verticesLeft.Dequeue();

            if (_currentVertex.wasVisited)
            {
                continue;
            }
            else
            {
                _currentVertex.isReachable = true;
                _currentVertex.wasVisited  = true;

                foreach (GameObject wp in _currentVertex.waypointData.GetComponent <WaypointNode>().nearbyWaypoints)
                {
                    WaypointNode WaypointNodeScript = wp.GetComponent <WaypointNode>();
                    verticesLeft.Enqueue(_vertices[WaypointNodeScript.ID]);
                }
            }
        }

        foreach (GameObject wp in _waypoints)
        {
            WaypointNode WaypointNodeScript = wp.GetComponent <WaypointNode>();
            WaypointNodeScript.isConnected = _vertices[WaypointNodeScript.ID].isReachable;
        }
    }
Beispiel #6
0
    public void TransferPoint(Transform point, Vector3 position)
    {
        point.position = position;
        Vector2 localPosition = point.localPosition;

        _Vertex _point = point.GetComponent <_Vertex>();

        int index = points.IndexOf(_point);

        _vertices[index] = localPosition;

        blocks[index].SetEndsPosition(_vertices[PreIndex(index)], localPosition);
        blocks[NextIndex(index)].SetEndsPosition(localPosition, _vertices[NextIndex(index)]);
    }
Beispiel #7
0
    void InitializeCPUBuffers()
    {
        m_verticesPosition = m_mesh.vertices;

        m_verticesUV = m_mesh.uv;

        m_vertexBufferCPU = new _Vertex[m_verticesPosition.Length];

        for (int i = 0; i < m_vertexBufferCPU.Length; i++)
        {
            _Vertex v = new _Vertex(m_verticesPosition[i], m_verticesUV[i]);
            m_vertexBufferCPU[i] = v;
        }


        m_indexBuffer = m_mesh.triangles;


        Debug.Log(string.Format("Initialized the cpu buffers with {0} vertices, for the compute shader", m_verticesPosition.Length));
    }
Beispiel #8
0
    public void RemovePointAt(Transform point)
    {
        _Vertex originPoint = point.GetComponent <_Vertex>();
        int     index       = points.IndexOf(originPoint);
        _Block  originBlock = blocks[index];

        blocks[NextIndex(index)].SetEndsPosition(_vertices[PreIndex(index)], _vertices[NextIndex(index)]);

        points.Remove(originPoint);
        blocks.Remove(originBlock);
        _vertices.RemoveAt(index);

        originBlock.Recycle();
        originPoint.Recycle();

        if (_vertices.Count < 1)
        {
            RecyclePoints();
        }
    }
    public void InsertPointAt(_Block block, Vector3 position)
    {
        //_Block block = blockObj.GetComponent<_Block>();
        Edge2D edge = block.Edge;

        block.Recycle();
        blocks.Remove(block);

        _Vertex vertex = vertexPrefab.Spawn();

        vertex.transform.SetParent(transform);
        vertex.transform.position = position;
        vertex.Border             = this;

        edge.MiddlePoint = vertex.Point;

        Edge2D e0 = new Edge2D(edge.Points[0], vertex.Point);
        Edge2D e1 = new Edge2D(vertex.Point, edge.Points[1]);

        _Block b0 = blockPrefab.Spawn();
        _Block b1 = blockPrefab.Spawn();

        b0.transform.SetParent(transform);
        b1.transform.SetParent(transform);
        b0.Border = this;
        b1.Border = this;
        b0.Edge   = e0;
        b1.Edge   = e1;

        blocks.Add(b0);
        blocks.Add(b1);

        vertexs.Add(vertex);

        _lazyAllVertices.isValeChanged = true;

        //Debug.Log("Border Vertices Count : " + Vertices.Count);
    }
Beispiel #10
0
    public _Vertex NextPoint(_Vertex point)
    {
        int index = points.IndexOf(point);

        return(NextPoint(index));
    }
Beispiel #11
0
    public GameObject[] GenPath(GameObject start, GameObject target)
    {
        _Vertex[] _vertices = new _Vertex[_waypoints.Length];
        int[,] adjMatrix = new int[_waypoints.Length, _waypoints.Length];
        int            adjVertex;
        int            verticesUsed = 0;
        List <_Vertex> adjList      = new List <_Vertex>();

        foreach (GameObject wp in _waypoints)
        {
            WaypointNode WaypointNodeScript = wp.GetComponent <WaypointNode>();
            _vertices[WaypointNodeScript.ID] = new _Vertex(wp);

            foreach (GameObject wp2 in WaypointNodeScript.nearbyWaypoints)
            {
                WaypointNode WaypointNodeScript2 = wp2.GetComponent <WaypointNode>();
                adjMatrix[WaypointNodeScript.ID, WaypointNodeScript2.ID] = 1;
            }
        }

        _Vertex workingV = _vertices[start.GetComponent <WaypointNode>().ID];

        // Mark source
        workingV.isPerm         = true;
        workingV.distFromSource = 0;

        while (true)
        {
            // Check for adjacent vertices
            while (GetAdjUnvisitedVertex(workingV.index, adjMatrix, _vertices) > -1)
            {
                // Get adjacent vertex
                adjVertex = GetAdjUnvisitedVertex(workingV.index, adjMatrix, _vertices);
                _vertices[adjVertex].wasVisited = true;

                // Add cost from from current node to distance to adjacent node
                // Check if less than previous calculations
                if (_vertices[adjVertex].distFromSource > workingV.distFromSource + Vector3.Distance(workingV.waypoint.transform.position, _vertices[adjVertex].waypoint.transform.position))
                {
                    _vertices[adjVertex].distFromSource     = workingV.distFromSource + Vector3.Distance(workingV.waypoint.transform.position, _vertices[adjVertex].waypoint.transform.position);
                    _vertices[adjVertex].closestVertexIndex = workingV.index;
                }

                if (!_vertices[adjVertex].isPerm)
                {
                    adjList.Add(_vertices[adjVertex]);
                }
            }

            _Vertex smallestCost = null;
            // Loop through all adjacent vertices and find the one with the lowest cost
            foreach (_Vertex v in adjList)
            {
                v.wasVisited = false;

                if (smallestCost == null)
                {
                    smallestCost = v;
                }
                else if (smallestCost.distFromSource > v.distFromSource)
                {
                    smallestCost = v;
                }
            }

            smallestCost.isPerm = true;
            workingV            = smallestCost;
            adjList.Remove(workingV);

            if (AllPerm(_vertices))
            {
                break;
            }
        }

        // Add the nodes to the path in reverse order
        Stack <_Vertex> path = new Stack <_Vertex>();

        workingV = _vertices[target.GetComponent <WaypointNode>().ID];
        path.Push(workingV);

        while (workingV.closestVertexIndex != -1)
        {
            // Add the closest neighbours in reverse order
            workingV = _vertices[workingV.closestVertexIndex];
            path.Push(workingV);
        }

        // Pop into array and flip
        GameObject[] nodePath = new GameObject[path.Count];
        int          temp     = path.Count;

        for (int i = 0; i < temp; i++)
        {
            nodePath[i] = path.Pop().waypointData;
        }

        Array.Reverse(nodePath);
        return(nodePath);
    }