private void DrawObjectSelectorGUI(GObjectStampLayer layer)
        {
            if (layer.Prototypes.Count > 0)
            {
                GSelectionGridArgs args = new GSelectionGridArgs();
                args.collection           = layer.Prototypes;
                args.selectedIndices      = layer.PrototypeIndices;
                args.itemSize             = GEditorCommon.selectionGridTileSizeSmall;
                args.itemPerRow           = 4;
                args.drawPreviewFunction  = GEditorCommon.DrawGameObjectPreview;
                args.contextClickFunction = OnObjectSelectorContextClick;
                layer.PrototypeIndices    = GEditorCommon.MultiSelectionGrid(args);
            }
            else
            {
                EditorGUILayout.LabelField("No Game Object found!", GEditorCommon.WordWrapItalicLabel);
            }

            Rect       r1     = EditorGUILayout.GetControlRect(GUILayout.Height(GEditorCommon.objectSelectorDragDropHeight));
            GameObject prefab = GEditorCommon.ObjectSelectorDragDrop <GameObject>(r1, "Drop a Game Object here!", "t:GameObject");

            if (prefab != null)
            {
                layer.Prototypes.AddIfNotContains(prefab);
            }

            GEditorCommon.SpacePixel(0);
            Rect r2 = EditorGUILayout.GetControlRect(GUILayout.Height(GEditorCommon.objectSelectorDragDropHeight));
            GPrefabPrototypeGroup group = GEditorCommon.ObjectSelectorDragDrop <GPrefabPrototypeGroup>(r2, "Drop a Prefab Prototype Group here!", "t:GPrefabPrototypeGroup");

            if (group != null)
            {
                layer.Prototypes.AddIfNotContains(group.Prototypes);
            }
        }
        private void ConfirmAndRemoveLayerAt(int index)
        {
            GObjectStampLayer 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);
            }
        }
Example #3
0
        public static GObjectStampLayer Create()
        {
            GObjectStampLayer layer = new GObjectStampLayer();

            layer.VisualizeColor   = GGriffinSettings.Instance.StampToolSettings.VisualizeColor;
            layer.Prototypes       = null;
            layer.PrototypeIndices = null;
            layer.InstanceCount    = 100;
            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.AlignToSurface   = false;

            layer.UpdateCurveTextures();
            return(layer);
        }
Example #4
0
        public static GObjectStampLayer Create()
        {
            GObjectStampLayer layer = new GObjectStampLayer();

            layer.Prototypes       = null;
            layer.PrototypeIndices = null;
            layer.InstanceCount    = 100;
            layer.AlignToSurface   = false;

#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);
        }
Example #5
0
        private void DrawStampLayersGUI()
        {
            string label = "Layers";
            string id    = "layers" + instance.GetInstanceID().ToString();

            GEditorCommon.Foldout(label, true, id, () =>
            {
                List <GObjectStampLayer> 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"))
                {
                    GObjectStampLayer layer = GObjectStampLayer.Create();
                    layers.Add(layer);
                }
            });
        }
        private void StampLayer(GStylizedTerrain t, RenderTexture brush, int layerIndex)
        {
            GObjectStampLayer layer = Layers[layerIndex];

            if (layer.Ignore)
            {
                return;
            }
            if (layer.InstanceCount == 0)
            {
                return;
            }
            if (layer.Prototypes.Count == 0 ||
                layer.PrototypeIndices.Count == 0)
            {
                return;
            }

            Texture2D tex = new Texture2D(brush.width, brush.height, TextureFormat.ARGB32, false, true);

            GCommon.CopyFromRT(tex, brush);
            Color[] maskData = tex.GetPixels();
            SpawnObjectOnTerrain(t, maskData, layerIndex);
        }
        private void SpawnObjectOnTerrain(GStylizedTerrain t, Color[] maskData, int layerIndex)
        {
            GObjectStampLayer 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        index         = -1;
            int        instanceCount = 0;
            int        attempt       = 0;
            int        maxAttempt    = layer.InstanceCount * 100;
            RaycastHit hit;

#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.InstanceCount && attempt <= maxAttempt)
            {
                attempt += 1;

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

                index = layer.PrototypeIndices[Random.Range(0, layer.PrototypeIndices.Count)];
                if (index < 0 || index >= layer.Prototypes.Count)
                {
                    continue;
                }
                GameObject g = layer.Prototypes[index];
                if (g == null)
                {
                    continue;
                }

                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;
                }

                if (t.Raycast(samplePos, out hit))
                {
                    GameObject instance = GSpawner.Spawn(t, g, hit.point);
                    instance.transform.rotation   = Quaternion.Euler(0, Random.Range(layer.MinRotation, layer.MaxRotation), 0);
                    instance.transform.localScale = Vector3.Lerp(layer.MinScale, layer.MaxScale, Random.value);
                    if (layer.AlignToSurface)
                    {
                        instance.transform.up = hit.normal;
                    }

                    instanceCount += 1;
                }
            }
#if UNITY_EDITOR
            GCommonGUI.ClearProgressBar();
#endif
        }
        private void DrawLayer(GObjectStampLayer 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.InstanceCount    = EditorGUILayout.IntField("Instance Count", layer.InstanceCount);
                DrawObjectSelectorGUI(layer);

                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;
                }
                layer.AlignToSurface   = EditorGUILayout.Toggle("Align To Surface", layer.AlignToSurface);
                EditorGUI.indentLevel += 1;
            },
                                  menu);
        }
Example #9
0
        private void DrawLayer(GObjectStampLayer 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.InstanceCount    = EditorGUILayout.IntField("Instance Count", layer.InstanceCount);
                DrawObjectSelectorGUI(layer);

                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;
                }
                layer.AlignToSurface   = EditorGUILayout.Toggle("Align To Surface", layer.AlignToSurface);
                EditorGUI.indentLevel -= 1;
            }
        }