public static void CreateVoxelTerrain()
        {
            GameObject terrainGo = new GameObject();

            terrainGo.name = newTerrainName;
            terrainGo.transform.position = new Vector3(0, 0, 0);
            MarchingCubesTerrain terrain = terrainGo.AddComponent <MarchingCubesTerrain>();

            EditorUtility.SetDirty(terrain.gameObject);
            terrain.AddChunk(new Vector3Int(0, 0, 0));

            Selection.activeObject = terrainGo;

            terrain.Generate();
        }
        public override void OnInspectorGUI()
        {
            selectedOption = GUILayout.Toolbar(selectedOption, textures, GUILayout.Width(40 * textures.Length), GUILayout.Height(40));

            if (selectedOption == TOOLS_MESH_ID)
            {
                //mesh tools
                mct.surfaceLevel = EditorGUILayout.Slider("Surface Level", mct.surfaceLevel, 0, 1);
                if (prevSurfLevel != mct.surfaceLevel)
                {
                    prevSurfLevel = mct.surfaceLevel;
                    mct.Generate();
                }
                mct.density = EditorGUILayout.Slider("Density", mct.density, 1, mct.density + 1);

                //brush Tools
                selectedBrush = (BrushType)EditorGUILayout.EnumPopup("Brush Type", selectedBrush);
                if (selectedBrush == BrushType.color)
                {
                    brushColor = EditorGUILayout.ColorField("Brush Color", brushColor);
                }
                brushSize     = EditorGUILayout.FloatField("Brush Size", brushSize);
                brushDistance = EditorGUILayout.FloatField("Brush Distance", brushDistance);
                brushOpacity  = EditorGUILayout.FloatField("Brush Opacity", brushOpacity);

                generateWhileEditing = EditorGUILayout.Toggle("Generate Mesh while editing", generateWhileEditing);

                //mesh buttons
                if (GUILayout.Button("Generate Mesh"))
                {
                    mct.Generate();
                }
                if (GUILayout.Button("Clear Mesh"))
                {
                    mct.ClearMesh();
                }
            }
            else if (selectedOption == TOOLS_FOLIAGE_ID)
            {
                //Foliage tools
                currFoliageObj = (GameObject)EditorGUILayout.ObjectField("", currFoliageObj, typeof(GameObject));
                randomRotation = GUILayout.Toggle(randomRotation, "random rotation");
            }
            else if (selectedOption == TOOLS_CHUNK_ID)
            {
                //chunk tools
                addChunkPosition = EditorGUILayout.Vector3IntField("add chunk position", addChunkPosition);
                if (GUILayout.Button("Add Chunk"))
                {
                    mct.AddChunk(addChunkPosition);
                }
                if (GUILayout.Button("Remove Chunk"))
                {
                    mct.EradicateChunk(addChunkPosition);
                }
            }
            else if (selectedOption == TOOLS_SAVELOAD_ID)
            {
                //save load
                mct.saveFile = EditorGUILayout.TextField("File path", mct.saveFile);
                if (GUILayout.Button("Save"))
                {
                    mct.Save(mct.saveFile);
                }
                if (GUILayout.Button("Load"))
                {
                    mct.Load(mct.saveFile);
                }
            }
            else if (selectedOption == TOOLS_SETTINGS_ID)
            {
                //terrain settings
                drawDebugField = EditorGUILayout.Toggle("Draw Debug points", drawDebugField);
                if (drawDebugField != prevDrawDebugField)
                {
                    EditorWindow.GetWindow <SceneView>().Repaint();
                    prevDrawDebugField = drawDebugField;
                }
            }
        }
Beispiel #3
0
        private void OnSceneGUI()
        {
            Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
            Handles.color = Color.red;
            Handles.DrawLine(mcc.transform.position, mcc.transform.position + new Vector3(1, 0, 0) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position, mcc.transform.position + new Vector3(0, 1, 0) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position, mcc.transform.position + new Vector3(0, 0, 1) * MarchingCubesChunk.size);

            Handles.DrawLine(mcc.transform.position + new Vector3(1, 1, 1) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(0, 1, 1) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(1, 1, 1) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(1, 0, 1) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(1, 1, 1) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(1, 1, 0) * MarchingCubesChunk.size);

            Handles.DrawLine(mcc.transform.position + new Vector3(1, 0, 0) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(1, 1, 0) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(1, 0, 0) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(1, 0, 1) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(0, 1, 0) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(1, 1, 0) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(0, 1, 0) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(0, 1, 1) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(0, 0, 1) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(1, 0, 1) * MarchingCubesChunk.size);
            Handles.DrawLine(mcc.transform.position + new Vector3(0, 0, 1) * MarchingCubesChunk.size, mcc.transform.position + new Vector3(0, 1, 1) * MarchingCubesChunk.size);

            // itself
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f)), Quaternion.identity, MarchingCubesChunk.size / 8, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                GameObject parent = mcc.transform.parent.gameObject;
                parent.GetComponent <MarchingCubesTerrain>().EradicateChunk(mcc.position);
                Selection.activeGameObject = parent;
                return;
            }

            // posX
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(MarchingCubesChunk.size * 0.5f + MarchingCubesChunk.size, MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f)), Quaternion.identity, MarchingCubesChunk.size / 8, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                MarchingCubesTerrain terrain = mcc.transform.parent.GetComponent <MarchingCubesTerrain>();
                Vector3Int           pos     = mcc.position + new Vector3Int(1, 0, 0);
                if (terrain.ChunkAvailable(pos))
                {
                    terrain.EradicateChunk(pos);
                    return;
                }
                else
                {
                    terrain.AddChunk(pos);
                }
            }

            // negX
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(-(MarchingCubesChunk.size * 0.5f), MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f)), Quaternion.identity, MarchingCubesChunk.size / 8, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                MarchingCubesTerrain terrain = mcc.transform.parent.GetComponent <MarchingCubesTerrain>();
                Vector3Int           pos     = mcc.position + new Vector3Int(-1, 0, 0);
                if (terrain.ChunkAvailable(pos))
                {
                    terrain.EradicateChunk(pos);
                    return;
                }
                else
                {
                    terrain.AddChunk(pos);
                }
            }

            // posY
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f + MarchingCubesChunk.size, MarchingCubesChunk.size * 0.5f)), Quaternion.identity, MarchingCubesChunk.size / 8, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                MarchingCubesTerrain terrain = mcc.transform.parent.GetComponent <MarchingCubesTerrain>();
                Vector3Int           pos     = mcc.position + new Vector3Int(0, 1, 0);
                if (terrain.ChunkAvailable(pos))
                {
                    terrain.EradicateChunk(pos);
                    return;
                }
                else
                {
                    terrain.AddChunk(pos);
                }
            }

            // negY
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(MarchingCubesChunk.size * 0.5f, -(MarchingCubesChunk.size * 0.5f), MarchingCubesChunk.size * 0.5f)), Quaternion.identity, MarchingCubesChunk.size / 8, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                MarchingCubesTerrain terrain = mcc.transform.parent.GetComponent <MarchingCubesTerrain>();
                Vector3Int           pos     = mcc.position + new Vector3Int(0, -1, 0);
                if (terrain.ChunkAvailable(pos))
                {
                    terrain.EradicateChunk(pos);
                    return;
                }
                else
                {
                    terrain.AddChunk(pos);
                }
            }

            // posZ
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f + MarchingCubesChunk.size)), Quaternion.identity, MarchingCubesChunk.size / 8, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                MarchingCubesTerrain terrain = mcc.transform.parent.GetComponent <MarchingCubesTerrain>();
                Vector3Int           pos     = mcc.position + new Vector3Int(0, 0, 1);
                if (terrain.ChunkAvailable(pos))
                {
                    terrain.EradicateChunk(pos);
                    return;
                }
                else
                {
                    terrain.AddChunk(pos);
                }
            }

            // negZ
            if (Handles.Button(Vector3.Scale(mcc.transform.parent.localScale, mcc.transform.position + new Vector3(MarchingCubesChunk.size * 0.5f, MarchingCubesChunk.size * 0.5f, -(MarchingCubesChunk.size * 0.5f))), Quaternion.identity, MarchingCubesChunk.size / 16, MarchingCubesChunk.size / 16, Handles.SphereHandleCap))
            {
                MarchingCubesTerrain terrain = mcc.transform.parent.GetComponent <MarchingCubesTerrain>();
                Vector3Int           pos     = mcc.position + new Vector3Int(0, 0, -1);
                if (terrain.ChunkAvailable(pos))
                {
                    terrain.EradicateChunk(pos);
                    return;
                }
                else
                {
                    terrain.AddChunk(pos);
                }
            }
        }