public Vector3 GetTerrainCollisionInEditor(Event currentEvent)
    {
        Vector3 returnCollision = new Vector3();

        AttachedPathScript pathScript = (AttachedPathScript)target as AttachedPathScript;

        Camera SceneCameraReceptor = new Camera();

        GameObject  terrain      = GameObject.Find(pathScript.parentTerrain);
        Terrain     terComponent = (Terrain)terrain.GetComponent(typeof(Terrain));
        TerrainData terData      = terComponent.terrainData;

        if (Camera.current != null)
        {
            SceneCameraReceptor = Camera.current;

            RaycastHit raycastHit = new RaycastHit();

            Vector2 newMousePosition = new Vector2(currentEvent.mousePosition.x, Screen.height - (currentEvent.mousePosition.y + 25));

            Ray terrainRay = SceneCameraReceptor.ScreenPointToRay(newMousePosition);

            if (Physics.Raycast(terrainRay, out raycastHit, 100000))
            {
                returnCollision = raycastHit.point;

                returnCollision.x = Mathf.RoundToInt((returnCollision.x / terData.size.x) * terData.heightmapResolution);
                returnCollision.y = returnCollision.y / terData.size.y;
                returnCollision.z = Mathf.RoundToInt((returnCollision.z / terData.size.z) * terData.heightmapResolution);
            }
        }

        return(returnCollision);
    }
Пример #2
0
    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);
            }
        }
    }
Пример #3
0
    public void OnSceneGUI()
    {
        Event currentEvent = Event.current;

        AttachedPathScript aps = (AttachedPathScript)target as AttachedPathScript;

        ray = Camera.current.ScreenPointToRay(new Vector2(currentEvent.mousePosition.x, Screen.height - (currentEvent.mousePosition.y + 25)));
        if (currentEvent.type == EventType.KeyUp)
        {
            if (Physics.Raycast(ray, out hit) && currentEvent.keyCode == KeyCode.U)
            {
                Debug.Log("inside");
                //AttachedPathScript aps = Selection.activeGameObject.GetComponent<AttachedPathScript>();

                if (!endSelect)
                {
                    aps.click_coordinates = new List <PathNodeObjects>();
                    startIndex            = aps.nearestNode(hit.point);
                    //Debug.Log("startIndex: " + index);


                    //    startNode.getGameObject().renderer.material.color = Color.green;
                    // Debug.Log("START NODE COORDINATES: " + aps.nodeObjects[index - 1].position + " "  + aps.nodeObjects[index].position);
                    endSelect = true;
                }
                else
                {
                    //   Debug.Log("asdsad ");
                    endIndex = aps.nearestNode(hit.point);
                    if (endIndex > startIndex)
                    {
                        aps.click_coordinates.Add(aps.nodeObjects[startIndex - 1]);
                        aps.click_coordinates.Add(aps.nodeObjects[startIndex]);
                        aps.click_coordinates.Add(aps.nodeObjects[endIndex]);
                        aps.click_coordinates.Add(aps.nodeObjects[endIndex + 1]);
                    }
                    else
                    {
                        aps.click_coordinates.Add(aps.nodeObjects[endIndex - 1]);
                        aps.click_coordinates.Add(aps.nodeObjects[endIndex]);
                        aps.click_coordinates.Add(aps.nodeObjects[startIndex]);
                        aps.click_coordinates.Add(aps.nodeObjects[startIndex + 1]);
                    }
                    //  Debug.Log("endIndex: " + index);
                    //    endNode.getGameObject().renderer.material.color = Color.green;
                    //  Debug.Log("GOAL NODE COORDINATES: " + aps.nodeObjects[index].position + " " + aps.nodeObjects[index+1].position);
                    aps.smoothPath();

                    endSelect = false;
                }
            }
        }
    }
Пример #4
0
    public void NewPath()
    {
        GameObject pathMesh = new GameObject();

        pathMesh.name = "Path";
        pathMesh.AddComponent(typeof(MeshFilter));
        pathMesh.AddComponent(typeof(MeshRenderer));
        pathMesh.AddComponent <AttachedPathScript>();

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

        APS.pathMesh      = pathMesh;
        APS.parentTerrain = gameObject.name;
        APS.NewPath();
    }
Пример #5
0
    public Vector3 GetTerrainCollisionInEditor(Event currentEvent)
    {
        Vector3 returnCollision = new Vector3();

        AttachedPathScript pathScript = (AttachedPathScript)target as AttachedPathScript;

        Camera SceneCameraReceptor = new Camera();

        GameObject      terrain      = pathScript.parentTerrain;
        Terrain         terComponent = (Terrain)terrain.GetComponent(typeof(Terrain));
        TerrainCollider terCollider  = (TerrainCollider)terrain.GetComponent(typeof(TerrainCollider));
        TerrainData     terData      = terComponent.terrainData;

        if (Camera.current != null)
        {
            SceneCameraReceptor = Camera.current;

            RaycastHit raycastHit = new RaycastHit();

            Vector2 newMousePosition = new Vector2(currentEvent.mousePosition.x, Screen.height - (currentEvent.mousePosition.y + 25));

            Ray terrainRay = SceneCameraReceptor.ScreenPointToRay(newMousePosition);

            if (terCollider.Raycast(terrainRay, out raycastHit, Mathf.Infinity))
            {
                returnCollision = raycastHit.point;
                Debug.Log("collision x " + returnCollision.x + "collision y " + returnCollision.y);
                returnCollision.x = Mathf.RoundToInt((returnCollision.x / terData.size.x) * terData.heightmapResolution);
                returnCollision.y = returnCollision.y / terData.size.y;
                returnCollision.z = Mathf.RoundToInt((returnCollision.z / terData.size.z) * terData.heightmapResolution);
            }

            else
            {
                Debug.LogError("Error: No collision with terrain to create node");
            }
        }

        return(returnCollision);
    }
Пример #6
0
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls();

        AttachedPathScript aps = (AttachedPathScript)target as AttachedPathScript;



        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.Separator();
        EditorGUILayout.Separator();
        EditorGUILayout.PrefixLabel("Reads Road");
        aps.number_of_iterations = EditorGUILayout.IntSlider(aps.number_of_iterations, 1, 10);

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();



        if (GUI.changed)
        {
            EditorUtility.SetDirty(aps);
        }
    }
Пример #7
0
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeControls();

        AttachedPathScript pathScript = (AttachedPathScript)target as AttachedPathScript;

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.Separator();
        EditorGUILayout.Separator();
        EditorGUILayout.PrefixLabel("Show handles");
        pathScript.showHandles = EditorGUILayout.Toggle(pathScript.showHandles);

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();

        if (!pathScript.isFinalized)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
            EditorGUILayout.PrefixLabel("Road");
            pathScript.isRoad = EditorGUILayout.Toggle(pathScript.isRoad);

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();

            EditorGUILayout.Separator();
            EditorGUILayout.BeginHorizontal();
            pathScript.pathWidth = (int)EditorGUILayout.IntSlider("Path Width", pathScript.pathWidth, 3, 20);
            EditorGUILayout.EndHorizontal();

            if (!pathScript.isRoad)
            {
                EditorGUILayout.Separator();
                EditorGUILayout.BeginHorizontal();
                pathScript.pathTexture = (int)EditorGUILayout.IntSlider("Texture Prototype", pathScript.pathTexture, 0, 30);
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Separator();
                EditorGUILayout.BeginHorizontal();
                pathScript.pathUniform = (bool)EditorGUILayout.Toggle("Uniform texture?", pathScript.pathUniform);
                EditorGUILayout.EndHorizontal();

                if (!pathScript.pathUniform)
                {
                    EditorGUILayout.Separator();
                    EditorGUILayout.BeginHorizontal();
                    pathScript.pathWear = (float)EditorGUILayout.Slider("Wear", pathScript.pathWear, 0.5f, 1.0f);
                    EditorGUILayout.EndHorizontal();
                }

                EditorGUILayout.Separator();
                EditorGUILayout.BeginHorizontal();
                pathScript.pathFlat = (bool)EditorGUILayout.Toggle("Flat?", pathScript.pathFlat);
                EditorGUILayout.EndHorizontal();
            }
        }

        EditorGUILayout.Separator();
        EditorGUILayout.BeginHorizontal();
        pathScript.pathSmooth = (int)EditorGUILayout.IntSlider("Mesh Smoothing", pathScript.pathSmooth, 5, 60);
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Separator();
        EditorGUILayout.Separator();


        if (!pathScript.isFinalized)
        {
            EditorGUILayout.Separator();
            Rect startButton = EditorGUILayout.BeginHorizontal();
            startButton.x      = startButton.width / 2 - 100;
            startButton.width  = 200;
            startButton.height = 18;

            if (GUI.Button(startButton, "Add path node"))
            {
                pathScript.addNodeMode = true;

                GUIUtility.ExitGUI();
            }

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            Rect endButton = EditorGUILayout.BeginHorizontal();
            endButton.x      = endButton.width / 2 - 100;
            endButton.width  = 200;
            endButton.height = 18;

            if (GUI.Button(endButton, "Finalize Path"))
            {
                if (pathScript.nodeObjects.Length > 1)
                {
                    // define terrain cells
                    pathScript.terrainCells = new TerrainPathCell[pathScript.terData.heightmapResolution * pathScript.terData.heightmapResolution];

                    for (int x = 0; x < pathScript.terData.heightmapResolution; x++)
                    {
                        for (int y = 0; y < pathScript.terData.heightmapResolution; y++)
                        {
                            pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].position.y   = y;
                            pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].position.x   = x;
                            pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].heightAtCell = pathScript.terrainHeights[y, x];
                            pathScript.terrainCells[(y) + (x * pathScript.terData.heightmapResolution)].isAdded      = false;
                        }
                    }

                    // finalize path
                    Undo.RegisterUndo(pathScript.terData, "Undo finalize path");
                    bool success = pathScript.FinalizePath();

                    if (success)
                    {
                        if (pathScript.isRoad)
                        {
                            pathScript.pathMesh.renderer.enabled = true;
                        }

                        else
                        {
                            MeshFilter meshFilter  = (MeshFilter)pathScript.pathMesh.GetComponent(typeof(MeshFilter));
                            Mesh       destroyMesh = meshFilter.sharedMesh;

                            DestroyImmediate(destroyMesh);
                            DestroyImmediate(meshFilter);
                        }
                    }
                }

                else
                {
                    Debug.Log("Not enough nodes to finalize");
                }
            }


            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
            EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.Separator();
        EditorGUILayout.Separator();
        EditorGUILayout.Separator();

        if (pathScript.isFinalized)
        {
            Rect smoothPathButton = EditorGUILayout.BeginHorizontal();
            smoothPathButton.x      = smoothPathButton.width / 2 - 100;
            smoothPathButton.width  = 200;
            smoothPathButton.height = 18;

            if (GUI.Button(smoothPathButton, "Smooth Path"))
            {
                Undo.RegisterUndo(pathScript.terData, "Undo smooth path");
                pathScript.AreaSmooth(pathScript.innerPathVerts, 1.0f, false);

                foreach (TerrainPathCell tc in pathScript.totalPathVerts)
                {
                    pathScript.terrainCells[Convert.ToInt32((tc.position.y) + ((tc.position.x) * (pathScript.terData.heightmapResolution)))].isAdded = false;
                }
            }

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            Rect smoothPathSlopeButton = EditorGUILayout.BeginHorizontal();
            smoothPathSlopeButton.x      = smoothPathSlopeButton.width / 2 - 100;
            smoothPathSlopeButton.width  = 200;
            smoothPathSlopeButton.height = 18;

            if (GUI.Button(smoothPathSlopeButton, "Smooth Path Slope"))
            {
                Undo.RegisterUndo(pathScript.terData, "Undo smooth path slope");
                pathScript.AreaSmooth(pathScript.totalPathVerts, 1.0f, true);

                foreach (TerrainPathCell tc in pathScript.totalPathVerts)
                {
                    pathScript.terrainCells[Convert.ToInt32((tc.position.y) + ((tc.position.x) * (pathScript.terData.heightmapResolution)))].isAdded = false;
                }
            }

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
            EditorGUILayout.Separator();
        }

        if (GUI.changed)
        {
            if (pathScript.isFinalized && !pathScript.isRoad)
            {
            }
            // do nothing

            else
            {
                EditorUtility.SetDirty(pathScript);

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

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

                else if (pathScript.isRoad && pathScript.isFinalized)
                {
                    pathScript.CreatePath(pathScript.pathSmooth, true, true);
                }
            }
        }
    }
Пример #8
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();
    }