public void OnSceneGUI()
    {
        AttachedPathScript pathScript = (AttachedPathScript)target as AttachedPathScript;

        Event currentEvent = Event.current;



        if (pathScript.addNodeMode == true)
        {
            // If P is pressed, than create a node at selected point (window has to have focus)
            if (currentEvent.isKey && currentEvent.character == 'p')
            {
                Vector3 pathNode = GetTerrainCollisionInEditor(currentEvent);
                Debug.Log(currentEvent);
                TerrainPathCell pathNodeCell = new TerrainPathCell();
                Debug.Log(pathNode);
                pathNodeCell.position.x   = pathNode.x;
                pathNodeCell.position.y   = pathNode.z;
                pathNodeCell.heightAtCell = pathNode.y;

                pathScript.CreatePathNode(pathNodeCell);
                pathScript.addNodeMode = false;
            }
        }

        if (pathScript.nodeObjects != null && pathScript.nodeObjects.Length != 0)
        {
            int n = pathScript.nodeObjects.Length;
            for (int i = 0; i < n; i++)
            {
                PathNodeObjects node = pathScript.nodeObjects[i];
                node.position = Handles.PositionHandle(node.position, Quaternion.identity);
            }
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(pathScript);

            if ((pathScript.pathFlat || pathScript.isRoad) && (pathScript.isFinalized == false))
            {
                pathScript.CreatePath(pathScript.pathSmooth, true, false);
            }

            else if (!pathScript.pathFlat && !pathScript.isRoad)
            {
                pathScript.CreatePath(pathScript.pathSmooth, false, false);
            }

            else if (pathScript.isFinalized)
            {
                pathScript.CreatePath(pathScript.pathSmooth, true, true);
            }
        }
    }
Beispiel #2
0
    public void visualizePath()
    {
        GameObject pathMesh = new GameObject();

        pathMesh.name = "Path";
        //pathMesh.tag = "Road";
        pathMesh.AddComponent(typeof(MeshFilter));
        pathMesh.AddComponent(typeof(MeshRenderer));
        pathMesh.AddComponent("AttachedPathScript");


        AttachedPathScript APS = (AttachedPathScript)pathMesh.GetComponent("AttachedPathScript");

        APS.pathMesh      = pathMesh;
        APS.parentTerrain = gameObject;
        //APS.NewPath();
        APS.NewPath();
        APS.pathWidth = 3;
        //APS.pathTexture = 1;
        APS.isRoad     = true;
        APS.pathSmooth = 5;
        //APS.pathUniform = true;
        //APS.pathWear = 0.5f;
        bool check = false;

//        foreach(Node node in path) {
        for (int i = 0; i < path.Count; i++)
        {
            if (i % 3 == 0 || i == path.Count - 1)
            {
                TerrainPathCell pathNodeCell = new TerrainPathCell();
                pathNodeCell.position.x = Mathf.RoundToInt((float)((((Node)path[i]).getPosition().x / Terrain.activeTerrain.terrainData.size.x) * Terrain.activeTerrain.terrainData.heightmapResolution));
                pathNodeCell.position.y = Mathf.RoundToInt((float)((((Node)path[i]).getPosition().z / Terrain.activeTerrain.terrainData.size.z) * Terrain.activeTerrain.terrainData.heightmapResolution));

                pathNodeCell.heightAtCell = (Terrain.activeTerrain.SampleHeight(new Vector3(pathNodeCell.position.x, pathNodeCell.position.y))) / Terrain.activeTerrain.terrainData.size.y;
                //Debug.Log(pathNodeCell.heightAtCell);
                //Debug.Log("path node " + pathNodeCell.position);

                if (!APS.CreatePathNode(pathNodeCell))
                {
                    check = true;
                    break;
                }



                if (check)
                {
                    DestroyImmediate(pathMesh);
                    continue;
                }
            }
        }
        APS.terrainCells = new TerrainPathCell[APS.terData.heightmapResolution * APS.terData.heightmapResolution];
        APS.terrainCells = terrainCells;
        APS.FinalizePath();
        APS.pathMesh.renderer.enabled              = true;
        APS.pathMesh.renderer.sharedMaterial       = new Material(Shader.Find("Diffuse"));
        APS.pathMesh.renderer.sharedMaterial.color = Color.grey;
        //pathMesh.GetComponent<MeshCollider>().convex = true;
        pathMesh.AddComponent <Rigidbody>() /*.inertiaTensor = Vector3.one*/;
        pathMesh.GetComponent <Rigidbody>().useGravity = false;
        pathMesh.transform.Translate(new Vector3(0, 0.2f, 0));
        pathMesh.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
        pathMesh.AddComponent <CollisionMover>();
        for (int i = 0; i < APS.nodeObjects.Length; i++)
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere.renderer.sharedMaterial       = new Material(Shader.Find("Diffuse"));
            sphere.renderer.sharedMaterial.color = Color.blue;
            sphere.transform.localScale          = new Vector3(5, 5, 5);
            sphere.transform.position            = APS.nodeObjects[i].position;
            sphere.AddComponent <DestroyOnLoad>();
            DestroyImmediate(sphere.GetComponent <SphereCollider>());
            sphere.transform.parent = pathMesh.transform;
        }
        pathMesh.GetComponent <CollisionMover>().check();
    }