コード例 #1
0
        void OnGUI()
        {
            if (env == null)
            {
                env = VoxelPlayEnvironment.instance;
                if (env == null)
                {
                    world = null;
                }
                EditorGUILayout.HelpBox("Biome Explorer cannot find a Voxel Play Environment instance in the current scene.", MessageType.Error);
                GUIUtility.ExitGUI();
            }
            else
            {
                world = env.world;
            }

            if (world == null)
            {
                EditorGUILayout.HelpBox("Assign a World Definition to the Voxel Play Environment instance.", MessageType.Warning);
                GUIUtility.ExitGUI();
            }

            if (terrainTex == null || moistureTex == null)
            {
                RefreshTextures();
                GUIUtility.ExitGUI();
            }

            GUIStyle labelStyle = new GUIStyle(GUI.skin.label);

            if (titleLabelStyle == null)
            {
                titleLabelStyle = new GUIStyle(EditorStyles.label);
            }
            titleLabelStyle.normal.textColor = titleColor;
            titleLabelStyle.fontStyle        = FontStyle.Bold;

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.HelpBox("Preview terrain generation and biome distribution based on current settings.", MessageType.Info);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Min X", GUILayout.Width(100));
            proposedMinX = EditorGUILayout.FloatField(proposedMinX, GUILayout.MaxWidth(120));
            EditorGUILayout.LabelField("Max X", GUILayout.Width(100));
            proposedMaxX = EditorGUILayout.FloatField(proposedMaxX, GUILayout.MaxWidth(120));
            if (GUILayout.Button("<<", GUILayout.Width(40)))
            {
                float shift = (maxX - minX) * 0.5f;
                proposedMinX  -= shift;
                proposedMaxX  -= shift;
                requestRefresh = true;
            }
            if (GUILayout.Button("<", GUILayout.Width(40)))
            {
                float shift = (maxX - minX) * 0.1f;
                proposedMinX  -= shift;
                proposedMaxX  -= shift;
                requestRefresh = true;
            }
            if (GUILayout.Button(">", GUILayout.Width(40)))
            {
                float shift = (maxX - minX) * 0.1f;
                proposedMinX  += shift;
                proposedMaxX  += shift;
                requestRefresh = true;
            }
            if (GUILayout.Button(">>", GUILayout.Width(40)))
            {
                float shift = (maxX - minX) * 0.5f;
                proposedMinX  += shift;
                proposedMaxX  += shift;
                requestRefresh = true;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Min Z", GUILayout.Width(100));
            proposedMinZ = EditorGUILayout.FloatField(proposedMinZ, GUILayout.MaxWidth(120));
            EditorGUILayout.LabelField("Max Z", GUILayout.Width(100));
            proposedMaxZ = EditorGUILayout.FloatField(proposedMaxZ, GUILayout.MaxWidth(120));
            if (GUILayout.Button("<<", GUILayout.Width(40)))
            {
                float shift = (maxZ - minZ) * 0.5f;
                proposedMinZ  -= shift;
                proposedMaxZ  -= shift;
                proposedSliceZ = (proposedMinZ + proposedMaxZ) * 0.5f;
                requestRefresh = true;
            }
            if (GUILayout.Button("<", GUILayout.Width(40)))
            {
                float shift = (maxZ - minZ) * 0.1f;
                proposedMinZ  -= shift;
                proposedMaxZ  -= shift;
                proposedSliceZ = (proposedMinZ + proposedMaxZ) * 0.5f;
                requestRefresh = true;
            }
            if (GUILayout.Button(">", GUILayout.Width(40)))
            {
                float shift = (maxZ - minZ) * 0.1f;
                proposedMinZ  += shift;
                proposedMaxZ  += shift;
                proposedSliceZ = (proposedMinZ + proposedMaxZ) * 0.5f;
                requestRefresh = true;
            }
            if (GUILayout.Button(">>", GUILayout.Width(40)))
            {
                float shift = (maxZ - minZ) * 0.5f;
                proposedMinZ  += shift;
                proposedMaxZ  += shift;
                proposedSliceZ = (proposedMinZ + proposedMaxZ) * 0.5f;
                requestRefresh = true;
            }

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Slice Z", GUILayout.Width(100));
            proposedSliceZ = EditorGUILayout.FloatField(proposedSliceZ, GUILayout.MaxWidth(120));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button(new GUIContent("Refresh Window", "Refresh textures to reflect new filters."), GUILayout.Width(140)))
            {
                requestRefresh = true;
            }
            if (GUILayout.Button(new GUIContent("-> World Definition", "Show World Definition in the inspector."), GUILayout.Width(140)))
            {
                Selection.activeObject = world;
            }
            if (GUILayout.Button(new GUIContent("-> Terrain Generator", "Show Terrain Generator in the inspector."), GUILayout.Width(140)))
            {
                Selection.activeObject = tg;
            }
            if (GUILayout.Button(new GUIContent("-> Environment", "Show Voxel Play Environment in the inspector."), GUILayout.Width(140)))
            {
                Selection.activeGameObject = env.gameObject;
            }
            if (GUILayout.Button(new GUIContent("Reload Config", "Resets heightmaps and biome cache and initializes terrain generator."), GUILayout.Width(140)))
            {
                env.NotifyTerrainGeneratorConfigurationChanged();
                requestRefresh = true;
                GUIUtility.ExitGUI();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();
            Rect space;

            if (previewTextureMat == null)
            {
                previewTextureMat = Resources.Load <Material> ("VoxelPlay/PreviewTexture");
            }

            // Draw heightmap distribution
            if (terrainTex != null)
            {
                EditorGUILayout.LabelField(new GUIContent("Height Map Preview"), titleLabelStyle);
                space        = EditorGUILayout.BeginVertical();
                space.width -= 20;
                GUILayout.Space(terrainTex.height);
                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(15);
                space.position += new Vector2(15, 0);
                EditorGUI.DrawPreviewTexture(space, terrainTex, previewTextureMat);
                GUILayout.Space(5);
                EditorGUILayout.EndHorizontal();

                // Draw 0-1 range
                space.position -= new Vector2(15, 0);
                EditorGUI.LabelField(space, "1");
                space.position += new Vector2(0, space.height - 10f);
                EditorGUI.LabelField(space, "0");

                // Draw x-axis labels
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(15);
                EditorGUILayout.LabelField("Min X = " + minX);
                labelStyle.alignment = TextAnchor.MiddleCenter;
                EditorGUILayout.LabelField("Slize Z = " + sliceZ + " / Min Y = " + calcMinAltitude.ToString("F3") + " / Max Y = " + calcMaxAltitude.ToString("F3"), labelStyle);
                labelStyle.alignment = TextAnchor.MiddleRight;
                EditorGUILayout.LabelField("Max X = " + maxX, labelStyle);
                EditorGUILayout.EndHorizontal();
            }

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


            // Draw moisture distribution
            if (terrainTex != null)
            {
                EditorGUILayout.LabelField(new GUIContent("Moisture Preview"), titleLabelStyle);
                space        = EditorGUILayout.BeginVertical();
                space.width -= 20;
                GUILayout.Space(moistureTex.height);
                EditorGUILayout.EndVertical();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(15);
                space.position += new Vector2(15, 0);
                EditorGUI.DrawPreviewTexture(space, moistureTex, previewTextureMat);
                GUILayout.Space(5);
                EditorGUILayout.EndHorizontal();

                // Draw 0-1 range
                space.position -= new Vector2(15, 0);
                EditorGUI.LabelField(space, "1");
                space.position += new Vector2(0, space.height - 10f);
                EditorGUI.LabelField(space, "0");

                // Draw x-axis labels
                EditorGUILayout.BeginHorizontal();
                GUILayout.Space(15);
                EditorGUILayout.LabelField("Min X = " + minX);
                labelStyle.alignment = TextAnchor.MiddleCenter;
                EditorGUILayout.LabelField("Slize Z = " + sliceZ + " / Min Y = " + calcMinMoisture.ToString("F3") + " / Max Y = " + calcMaxMoisture.ToString("F3"), labelStyle);
                labelStyle.alignment = TextAnchor.MiddleRight;
                EditorGUILayout.LabelField("Max X = " + maxX, labelStyle);
                EditorGUILayout.EndHorizontal();
            }

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


            if (world.biomes != null && biomeTex != null)
            {
                // Draw heightmap texture
                EditorGUILayout.LabelField(new GUIContent("Biome Map Preview"), titleLabelStyle);

                EditorGUILayout.BeginHorizontal();

                // Biome legend
                EditorGUILayout.BeginVertical(GUILayout.MaxWidth(180));
                EditorGUILayout.Separator();
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Hide All", GUILayout.Width(80)))
                {
                    ToggleBiomes(false);
                    requestRefresh = true;
                }
                if (GUILayout.Button("Show All", GUILayout.Width(80)))
                {
                    ToggleBiomes(true);
                    requestRefresh = true;
                }
                if (GUILayout.Button("Default Colors", GUILayout.Width(120)))
                {
                    env.SetBiomeDefaultColors(true);
                    requestRefresh = true;
                }
                EditorGUILayout.EndHorizontal();
                EditorGUI.BeginChangeCheck();
                for (int k = 0; k < world.biomes.Length; k++)
                {
                    BiomeDefinition biome = world.biomes [k];
                    if (biome == null)
                    {
                        continue;
                    }
                    float perc = 100f * (float)biome.biomeMapOccurrences / (biomeTex.width * biomeTex.height);
                    DrawLegend(biome.biomeMapColor, biome.name + " (" + perc.ToString("F2") + "%)", biome);
                }
                DrawLegend(waterColor, "Water", null);

                if (EditorGUI.EndChangeCheck())
                {
                    requestRefresh = true;
                }
                EditorGUILayout.Separator();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Grid Size", GUILayout.Width(100));
                gridStep = EditorGUILayout.IntField(gridStep, GUILayout.Width(60));
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Texture Size", GUILayout.Width(100));
                mapResolution = EditorGUILayout.IntField(mapResolution, GUILayout.Width(60));
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Separator();
                EditorGUILayout.Separator();
                // Tester
                EditorGUILayout.LabelField(new GUIContent("Biome Tester"), titleLabelStyle);
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Altitude?", GUILayout.Width(100));
                inputAltitude = EditorGUILayout.Slider(inputAltitude, 0, 1, GUILayout.Width(130));
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Moisture?", GUILayout.Width(100));
                inputMoisture = EditorGUILayout.Slider(inputMoisture, 0, 1, GUILayout.Width(130));
                EditorGUILayout.EndHorizontal();
                if (EditorGUI.EndChangeCheck())
                {
                    CalcBiome();
                }
                EditorGUILayout.LabelField(biomeTestResult);
                EditorGUILayout.EndVertical();

                // Biome map
                space = EditorGUILayout.BeginVertical();
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndVertical();
                EditorGUI.DrawPreviewTexture(space, biomeTex, previewTextureMat, ScaleMode.ScaleToFit);

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

            if (requestRefresh)
            {
                RefreshTextures();
            }
        }