예제 #1
0
        private void StampLayer(GStylizedTerrain t, RenderTexture brush, int layerIndex)
        {
            GFoliageStampLayer layer = Layers[layerIndex];

            if (layer.Ignore)
            {
                return;
            }
            if (layer.TreeInstanceCount == 0)
            {
                return;
            }
            Texture2D tex = new Texture2D(brush.width, brush.height, TextureFormat.ARGB32, false, true);

            GCommon.CopyFromRT(tex, brush);
            Color[] maskData = tex.GetPixels();

            if (layer.StampTrees &&
                layer.TreeIndices.Count >= 0 &&
                t.TerrainData.Foliage.Trees != null)
            {
                SpawnTreeOnTerrain(t, maskData, layerIndex);
            }

            if (layer.StampGrasses &&
                layer.GrassIndices.Count >= 0 &&
                t.TerrainData.Foliage.Grasses != null)
            {
                SpawnGrassOnTerrain(t, maskData, layerIndex);
            }
        }
        private void ConfirmAndRemoveLayerAt(int index)
        {
            GFoliageStampLayer layer     = instance.Layers[index];
            string             layerName = string.IsNullOrEmpty(layer.Name) ? ("Layer " + index) : ("Layer " + layer.Name);

            if (EditorUtility.DisplayDialog(
                    "Confirm",
                    "Remove " + layerName + "?",
                    "Yes", "Cancel"))
            {
                instance.Layers.RemoveAt(index);
            }
        }
        public static GFoliageStampLayer Create()
        {
            GFoliageStampLayer layer = new GFoliageStampLayer();

            layer.VisualizeColor    = GGriffinSettings.Instance.StampToolSettings.VisualizeColor;
            layer.StampTrees        = true;
            layer.TreeIndices       = null;
            layer.StampGrasses      = false;
            layer.GrassIndices      = null;
            layer.TreeInstanceCount = 1000;
            layer.MinRotation       = GGriffinSettings.Instance.StampToolSettings.MinRotation;
            layer.MaxRotation       = GGriffinSettings.Instance.StampToolSettings.MaxRotation;
            layer.MinScale          = GGriffinSettings.Instance.StampToolSettings.MinScale;
            layer.MaxScale          = GGriffinSettings.Instance.StampToolSettings.MaxScale;

            layer.UpdateCurveTextures();
            return(layer);
        }
예제 #4
0
        public static GFoliageStampLayer Create()
        {
            GFoliageStampLayer layer = new GFoliageStampLayer();

            layer.StampTrees        = true;
            layer.TreeIndices       = null;
            layer.StampGrasses      = false;
            layer.GrassIndices      = null;
            layer.TreeInstanceCount = 1000;

#if UNITY_EDITOR
            layer.VisualizeColor = GEditorSettings.Instance.stampTools.visualizeColor;
            layer.MinRotation    = GEditorSettings.Instance.stampTools.minRotation;
            layer.MaxRotation    = GEditorSettings.Instance.stampTools.maxRotation;
            layer.MinScale       = GEditorSettings.Instance.stampTools.minScale;
            layer.MaxScale       = GEditorSettings.Instance.stampTools.maxScale;
#endif

            layer.UpdateCurveTextures();
            return(layer);
        }
        private void DrawStampLayersGUI()
        {
            string label = "Layers";
            string id    = "layers" + instance.GetInstanceID().ToString();

            GEditorCommon.Foldout(label, true, id, () =>
            {
                List <GFoliageStampLayer> layers = instance.Layers;
                for (int i = 0; i < layers.Count; ++i)
                {
                    DrawLayer(layers[i], i);
                    GEditorCommon.Separator();
                }

                Rect r = EditorGUILayout.GetControlRect();
                if (GUI.Button(r, "Add Layer"))
                {
                    GFoliageStampLayer layer = GFoliageStampLayer.Create();
                    layers.Add(layer);
                }
            });
        }
        private void DrawLayer(GFoliageStampLayer layer, int index)
        {
            string label = string.Format("Layer: {0} {1}",
                                         !string.IsNullOrEmpty(layer.Name) ? layer.Name : index.ToString(),
                                         layer.Ignore ? "[Ignored]" : string.Empty);
            string id = "stamperlayer" + index + instance.GetInstanceID().ToString();

            string prefKey  = GEditorCommon.GetProjectRelatedEditorPrefsKey("foldout", id);
            bool   expanded = EditorPrefs.GetBool(prefKey, true);

            Rect headerRect = EditorGUILayout.BeginHorizontal();

            expanded = EditorGUILayout.Foldout(expanded, label);
            EditorPrefs.SetBool(prefKey, expanded);
            GUILayout.FlexibleSpace();
            Rect deleteButtonRect = EditorGUILayout.GetControlRect(GUILayout.Width(15));

            if (headerRect.Contains(Event.current.mousePosition))
            {
                if (GUI.Button(deleteButtonRect, "X", EditorStyles.label))
                {
                    ConfirmAndRemoveLayerAt(index);
                }
            }
            EditorGUILayout.EndHorizontal();

            if (expanded)
            {
                EditorGUI.indentLevel += 1;
                layer.Ignore           = EditorGUILayout.Toggle("Ignore", layer.Ignore);
                layer.Name             = EditorGUILayout.TextField("Name", layer.Name);
                layer.VisualizeColor   = EditorGUILayout.ColorField("Visualize Color", layer.VisualizeColor);
                layer.MinRotation      = EditorGUILayout.FloatField("Min Rotation", layer.MinRotation);
                layer.MaxRotation      = EditorGUILayout.FloatField("Max Rotation", layer.MaxRotation);
                layer.MinScale         = GEditorCommon.InlineVector3Field("Min Scale", layer.MinScale);
                layer.MaxScale         = GEditorCommon.InlineVector3Field("Max Scale", layer.MaxScale);

                layer.StampTrees = EditorGUILayout.Toggle("Stamp Trees", layer.StampTrees);
                if (layer.StampTrees)
                {
                    EditorGUI.indentLevel  += 1;
                    layer.TreeIndices       = GEditorCommon.TreeSetMultiSelectionGrid(instance.GroupId, layer.TreeIndices);
                    layer.TreeInstanceCount = EditorGUILayout.IntField("Instance Count", layer.TreeInstanceCount);
                    EditorGUI.indentLevel  -= 1;
                }
                layer.StampGrasses = EditorGUILayout.Toggle("Stamp Grasses", layer.StampGrasses);
                if (layer.StampGrasses)
                {
                    EditorGUI.indentLevel   += 1;
                    layer.GrassIndices       = GEditorCommon.GrassSetMultiSelectionGrid(instance.GroupId, layer.GrassIndices);
                    layer.GrassInstanceCount = EditorGUILayout.IntField("Instance Count", layer.GrassInstanceCount);
                    EditorGUI.indentLevel   -= 1;
                }

                layer.BlendHeight = EditorGUILayout.Toggle("Blend Height", layer.BlendHeight);
                if (layer.BlendHeight)
                {
                    EditorGUI.indentLevel += 1;
                    layer.MinHeight        = EditorGUILayout.FloatField("Min", layer.MinHeight);
                    layer.MaxHeight        = EditorGUILayout.FloatField("Max", layer.MaxHeight);
                    EditorGUI.BeginChangeCheck();
                    layer.HeightTransition = EditorGUILayout.CurveField("Transition", layer.HeightTransition, Color.red, new Rect(0, 0, 1, 1));
                    if (EditorGUI.EndChangeCheck())
                    {
                        layer.UpdateCurveTextures();
                    }
                    EditorGUI.indentLevel -= 1;
                }

                layer.BlendSlope = EditorGUILayout.Toggle("Blend Slope", layer.BlendSlope);
                if (layer.BlendSlope)
                {
                    EditorGUI.indentLevel += 1;
                    layer.NormalMapMode    = (GNormalMapMode)EditorGUILayout.EnumPopup("Mode", layer.NormalMapMode);
                    layer.MinSlope         = EditorGUILayout.FloatField("Min", layer.MinSlope);
                    layer.MaxSlope         = EditorGUILayout.FloatField("Max", layer.MaxSlope);
                    EditorGUI.BeginChangeCheck();
                    layer.SlopeTransition = EditorGUILayout.CurveField("Transition", layer.SlopeTransition, Color.red, new Rect(0, 0, 1, 1));
                    if (EditorGUI.EndChangeCheck())
                    {
                        layer.UpdateCurveTextures();
                    }
                    EditorGUI.indentLevel -= 1;
                }

                layer.BlendNoise = EditorGUILayout.Toggle("Blend Noise", layer.BlendNoise);
                if (layer.BlendNoise)
                {
                    EditorGUI.indentLevel += 1;
                    layer.NoiseOrigin      = GEditorCommon.InlineVector2Field("Origin", layer.NoiseOrigin);
                    layer.NoiseFrequency   = EditorGUILayout.FloatField("Frequency", layer.NoiseFrequency);
                    layer.NoiseLacunarity  = EditorGUILayout.FloatField("Lacunarity", layer.NoiseLacunarity);
                    layer.NoisePersistence = EditorGUILayout.Slider("Persistence", layer.NoisePersistence, 0.01f, 1f);
                    layer.NoiseOctaves     = EditorGUILayout.IntSlider("Octaves", layer.NoiseOctaves, 1, 4);
                    EditorGUI.BeginChangeCheck();
                    layer.NoiseRemap = EditorGUILayout.CurveField("Remap", layer.NoiseRemap, Color.red, new Rect(0, 0, 1, 1));
                    if (EditorGUI.EndChangeCheck())
                    {
                        layer.UpdateCurveTextures();
                    }
                    EditorGUI.indentLevel -= 1;
                }

                EditorGUI.indentLevel -= 1;
            }
        }
예제 #7
0
        private void SpawnGrassOnTerrain(GStylizedTerrain t, Color[] maskData, int layerIndex)
        {
            GFoliageStampLayer layer     = Layers[layerIndex];
            Vector3            centerPos = Vector3.zero;
            Vector3            samplePos = Vector3.zero;
            Vector2            uv        = Vector2.zero;
            float   maskValue            = 0;
            Vector3 terrainSize          = new Vector3(
                t.TerrainData.Geometry.Width,
                t.TerrainData.Geometry.Height,
                t.TerrainData.Geometry.Length);
            Vector3 scale = new Vector3(
                GUtilities.InverseLerpUnclamped(0, terrainSize.x, Scale.x),
                1,
                GUtilities.InverseLerpUnclamped(0, terrainSize.z, Scale.z));
            Matrix4x4 matrix = Matrix4x4.TRS(
                t.WorldPointToNormalized(Position),
                Rotation,
                scale);

            int grassIndex                = -1;
            int instanceCount             = 0;
            int attempt                   = 0;
            int maxAttempt                = layer.GrassInstanceCount * 100;
            List <GGrassInstance> grasses = new List <GGrassInstance>();

#if UNITY_EDITOR
            string title           = "Stamping on " + t.name;
            string info            = string.Format("Layer: {0}", !string.IsNullOrEmpty(layer.Name) ? layer.Name : layerIndex.ToString());
            int    currentPercent  = 0;
            int    attemptPercent  = 0;
            int    instancePercent = 0;
            GCommonGUI.CancelableProgressBar(title, info, 0);
#endif

            while (instanceCount < layer.GrassInstanceCount && attempt <= maxAttempt)
            {
                attempt += 1;

#if UNITY_EDITOR
                attemptPercent  = (int)(attempt * 100.0f / maxAttempt);
                instancePercent = (int)(instanceCount * 100.0f / layer.GrassInstanceCount);
                if (currentPercent != Mathf.Max(attemptPercent, instancePercent))
                {
                    currentPercent = Mathf.Max(attemptPercent, instancePercent);
                    GCommonGUI.CancelableProgressBar(title, string.Format("{0} ... {1}%", info, currentPercent), currentPercent / 100.0f);
                }
#endif

                grassIndex = layer.GrassIndices[Random.Range(0, layer.GrassIndices.Count)];

                centerPos.Set(Random.value - 0.5f, 0, Random.value - 0.5f);
                samplePos = matrix.MultiplyPoint(centerPos);
                if (samplePos.x < 0 || samplePos.x > 1 ||
                    samplePos.z < 0 || samplePos.z > 1)
                {
                    continue;
                }
                uv.Set(samplePos.x, samplePos.z);
                maskValue = GUtilities.GetColorBilinear(maskData, MaskResolution, MaskResolution, uv).r;
                if (Random.value > maskValue)
                {
                    continue;
                }

                GGrassInstance grass = GGrassInstance.Create(grassIndex);
                grass.Position = new Vector3(samplePos.x, 0, samplePos.z);
                grass.Rotation = Quaternion.Euler(0, Random.Range(layer.MinRotation, layer.MaxRotation), 0);
                grass.Scale    = Vector3.Lerp(layer.MinScale, layer.MaxScale, Random.value);

                grasses.Add(grass);
                instanceCount += 1;
            }

            t.TerrainData.Foliage.AddGrassInstances(grasses);

#if UNITY_EDITOR
            GCommonGUI.ClearProgressBar();
#endif
        }
예제 #8
0
        private void DrawLayer(GFoliageStampLayer layer, int index)
        {
            string label = string.Format("Layer: {0} {1}",
                                         !string.IsNullOrEmpty(layer.Name) ? layer.Name : index.ToString(),
                                         layer.Ignore ? "[Ignored]" : string.Empty);
            string id = "stamperlayer" + index + instance.GetInstanceID().ToString();

            GenericMenu menu = new GenericMenu();

            menu.AddItem(
                new GUIContent("Remove"),
                false,
                () =>
            {
                ConfirmAndRemoveLayerAt(index);
            });

            GEditorCommon.Foldout(label, false, id, () =>
            {
                EditorGUI.indentLevel -= 1;
                layer.Ignore           = EditorGUILayout.Toggle("Ignore", layer.Ignore);
                layer.Name             = EditorGUILayout.TextField("Name", layer.Name);
                layer.VisualizeColor   = EditorGUILayout.ColorField("Visualize Color", layer.VisualizeColor);
                layer.MinRotation      = EditorGUILayout.FloatField("Min Rotation", layer.MinRotation);
                layer.MaxRotation      = EditorGUILayout.FloatField("Max Rotation", layer.MaxRotation);
                layer.MinScale         = GEditorCommon.InlineVector3Field("Min Scale", layer.MinScale);
                layer.MaxScale         = GEditorCommon.InlineVector3Field("Max Scale", layer.MaxScale);

                layer.StampTrees = EditorGUILayout.Toggle("Stamp Trees", layer.StampTrees);
                if (layer.StampTrees)
                {
                    EditorGUI.indentLevel  += 1;
                    layer.TreeIndices       = GEditorCommon.TreeSetMultiSelectionGrid(instance.GroupId, layer.TreeIndices);
                    layer.TreeInstanceCount = EditorGUILayout.IntField("Instance Count Per Terrain", layer.TreeInstanceCount);
                    EditorGUI.indentLevel  -= 1;
                }
                layer.StampGrasses = EditorGUILayout.Toggle("Stamp Grasses", layer.StampGrasses);
                if (layer.StampGrasses)
                {
                    EditorGUI.indentLevel   += 1;
                    layer.GrassIndices       = GEditorCommon.GrassSetMultiSelectionGrid(instance.GroupId, layer.GrassIndices);
                    layer.GrassInstanceCount = EditorGUILayout.IntField("Instance Count Per Terrain", layer.GrassInstanceCount);
                    EditorGUI.indentLevel   -= 1;
                }

                layer.BlendHeight = EditorGUILayout.Toggle("Blend Height", layer.BlendHeight);
                if (layer.BlendHeight)
                {
                    EditorGUI.indentLevel += 1;
                    layer.MinHeight        = EditorGUILayout.FloatField("Min", layer.MinHeight);
                    layer.MaxHeight        = EditorGUILayout.FloatField("Max", layer.MaxHeight);
                    EditorGUI.BeginChangeCheck();
                    layer.HeightTransition = EditorGUILayout.CurveField("Transition", layer.HeightTransition, Color.red, new Rect(0, 0, 1, 1));
                    if (EditorGUI.EndChangeCheck())
                    {
                        layer.UpdateCurveTextures();
                    }
                    EditorGUI.indentLevel -= 1;
                }

                layer.BlendSlope = EditorGUILayout.Toggle("Blend Slope", layer.BlendSlope);
                if (layer.BlendSlope)
                {
                    EditorGUI.indentLevel += 1;
                    layer.NormalMapMode    = (GNormalMapMode)EditorGUILayout.EnumPopup("Mode", layer.NormalMapMode);
                    layer.MinSlope         = EditorGUILayout.FloatField("Min", layer.MinSlope);
                    layer.MaxSlope         = EditorGUILayout.FloatField("Max", layer.MaxSlope);
                    EditorGUI.BeginChangeCheck();
                    layer.SlopeTransition = EditorGUILayout.CurveField("Transition", layer.SlopeTransition, Color.red, new Rect(0, 0, 1, 1));
                    if (EditorGUI.EndChangeCheck())
                    {
                        layer.UpdateCurveTextures();
                    }
                    EditorGUI.indentLevel -= 1;
                }

                layer.BlendNoise = EditorGUILayout.Toggle("Blend Noise", layer.BlendNoise);
                if (layer.BlendNoise)
                {
                    EditorGUI.indentLevel += 1;
                    layer.NoiseOrigin      = GEditorCommon.InlineVector2Field("Origin", layer.NoiseOrigin);
                    layer.NoiseFrequency   = EditorGUILayout.FloatField("Frequency", layer.NoiseFrequency);
                    layer.NoiseLacunarity  = EditorGUILayout.FloatField("Lacunarity", layer.NoiseLacunarity);
                    layer.NoisePersistence = EditorGUILayout.Slider("Persistence", layer.NoisePersistence, 0.01f, 1f);
                    layer.NoiseOctaves     = EditorGUILayout.IntSlider("Octaves", layer.NoiseOctaves, 1, 4);
                    EditorGUI.BeginChangeCheck();
                    layer.NoiseRemap = EditorGUILayout.CurveField("Remap", layer.NoiseRemap, Color.red, new Rect(0, 0, 1, 1));
                    if (EditorGUI.EndChangeCheck())
                    {
                        layer.UpdateCurveTextures();
                    }
                    EditorGUI.indentLevel -= 1;
                }
                EditorGUI.indentLevel += 1;
            },
                                  menu);
        }