Esempio n. 1
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);
                }
            }
        }
    }
Esempio n. 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();
    }