237 lines
6.4 KiB
C#
237 lines
6.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using uNature.Core.FoliageClasses;
|
|
using uNature.Core.Utility;
|
|
|
|
namespace uNature.Demo
|
|
{
|
|
public class UN_ProceduralDemo_UIController : MonoBehaviour
|
|
{
|
|
private static UN_ProceduralDemo_UIController _instance;
|
|
|
|
private CanvasGroup _group;
|
|
|
|
[SerializeField]
|
|
[Header("General")]
|
|
private GameObject prototypePrefab;
|
|
|
|
[SerializeField]
|
|
private EventSystem eventSystem;
|
|
|
|
[SerializeField]
|
|
[Header("Grass")]
|
|
private Transform grassPrototypesParent;
|
|
|
|
[SerializeField]
|
|
private Slider densitySlider;
|
|
|
|
[SerializeField]
|
|
private Slider viewDistanceSlider;
|
|
|
|
[SerializeField]
|
|
private Toggle castShadowsToggle;
|
|
|
|
[SerializeField]
|
|
private Toggle windEnabledToggle;
|
|
|
|
[SerializeField]
|
|
private Toggle colorMapsEnabledToggle;
|
|
|
|
private List<UN_DemoPrototypeUI> grassPrototypes = new List<UN_DemoPrototypeUI>();
|
|
|
|
[HideInInspector]
|
|
public List<UN_DemoPrototypeUI> chosenPrototypes = new List<UN_DemoPrototypeUI>();
|
|
|
|
private List<FoliagePrototype> chosenPrototypes_Converted = new List<FoliagePrototype>();
|
|
|
|
[SerializeField]
|
|
[Header("Paint")]
|
|
private Transform brushPrototypesParent;
|
|
|
|
[SerializeField]
|
|
private Slider brushSizeSlider;
|
|
|
|
[SerializeField]
|
|
private Slider paintDensitySlider;
|
|
|
|
[SerializeField]
|
|
private Slider eraseDensitySlider;
|
|
|
|
private List<UN_DemoPrototypeUI> paintPrototypes = new List<UN_DemoPrototypeUI>();
|
|
|
|
[HideInInspector]
|
|
public UN_DemoPrototypeUI chosenBrush;
|
|
|
|
private Vector3 lastBrushPosition = Vector3.zero;
|
|
|
|
public static UN_ProceduralDemo_UIController instance
|
|
{
|
|
get
|
|
{
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
private CanvasGroup group
|
|
{
|
|
get
|
|
{
|
|
if (_group == null)
|
|
{
|
|
_group = GetComponent<CanvasGroup>();
|
|
}
|
|
return _group;
|
|
}
|
|
}
|
|
|
|
public new bool enabled
|
|
{
|
|
get
|
|
{
|
|
return group.alpha > 0f;
|
|
}
|
|
set
|
|
{
|
|
if (enabled != value)
|
|
{
|
|
group.alpha = (value ? 1 : 0);
|
|
group.interactable = value;
|
|
group.blocksRaycasts = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnDensityChanged(float tempValue)
|
|
{
|
|
float value = densitySlider.value;
|
|
FoliageCore_MainManager.instance.density = value;
|
|
}
|
|
|
|
public void OnViewDistanceChanged(float tempValue)
|
|
{
|
|
float value = viewDistanceSlider.value;
|
|
int globalFadeDistance = (int)(500f * value);
|
|
FoliageCore_MainManager.instance.globalFadeDistance = globalFadeDistance;
|
|
}
|
|
|
|
public void OnCastShadowsChanged(bool tempValue)
|
|
{
|
|
bool isOn = castShadowsToggle.isOn;
|
|
for (int i = 0; i < FoliageDB.unSortedPrototypes.Count; i++)
|
|
{
|
|
FoliageDB.unSortedPrototypes[i].castShadows = isOn;
|
|
}
|
|
}
|
|
|
|
public void OnWindChanged(bool tempValue)
|
|
{
|
|
bool isOn = windEnabledToggle.isOn;
|
|
FoliageDB.instance.globalWindSettings.windSpeed = ((!isOn) ? 0f : 0.5f);
|
|
}
|
|
|
|
public void OnColorMapsChanged(bool tempValue)
|
|
{
|
|
bool isOn = colorMapsEnabledToggle.isOn;
|
|
for (int i = 0; i < FoliageDB.unSortedPrototypes.Count; i++)
|
|
{
|
|
FoliageDB.unSortedPrototypes[i].useColorMap = isOn;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_instance = this;
|
|
PoppulateBrushesAndGrassPrototypes();
|
|
for (int i = 0; i < FoliageDB.unSortedPrototypes.Count; i++)
|
|
{
|
|
FoliageDB.unSortedPrototypes[i].useColorMap = false;
|
|
FoliageDB.unSortedPrototypes[i].castShadows = true;
|
|
}
|
|
FoliageCore_MainManager.instance.density = 1f;
|
|
FoliageCore_MainManager.instance.globalFadeDistance = 100;
|
|
}
|
|
|
|
private void PoppulateBrushesAndGrassPrototypes()
|
|
{
|
|
grassPrototypes.Clear();
|
|
paintPrototypes.Clear();
|
|
List<FoliagePrototype> unSortedPrototypes = FoliageDB.unSortedPrototypes;
|
|
List<PaintBrush> brushes = FoliageDB.instance.brushes;
|
|
for (int i = 0; i < unSortedPrototypes.Count; i++)
|
|
{
|
|
FoliagePrototype foliagePrototype = unSortedPrototypes[i];
|
|
if (foliagePrototype.FoliageType == FoliageType.Texture)
|
|
{
|
|
GameObject gameObject = Object.Instantiate(prototypePrefab);
|
|
UN_DemoPrototypeUI component = gameObject.GetComponent<UN_DemoPrototypeUI>();
|
|
component.Initialize(foliagePrototype.FoliageTexture, null, foliagePrototype);
|
|
gameObject.transform.SetParent(grassPrototypesParent);
|
|
gameObject.transform.position = Vector3.zero;
|
|
gameObject.transform.rotation = Quaternion.identity;
|
|
gameObject.transform.localScale = Vector3.one;
|
|
grassPrototypes.Add(component);
|
|
}
|
|
}
|
|
for (int j = 0; j < brushes.Count; j++)
|
|
{
|
|
PaintBrush paintBrush = brushes[j];
|
|
GameObject gameObject = Object.Instantiate(prototypePrefab);
|
|
UN_DemoPrototypeUI component = gameObject.GetComponent<UN_DemoPrototypeUI>();
|
|
component.Initialize(paintBrush.brushTexture, paintBrush, null);
|
|
gameObject.transform.SetParent(brushPrototypesParent);
|
|
gameObject.transform.position = Vector3.zero;
|
|
gameObject.transform.rotation = Quaternion.identity;
|
|
gameObject.transform.localScale = Vector3.one;
|
|
paintPrototypes.Add(component);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Tab))
|
|
{
|
|
enabled = !enabled;
|
|
}
|
|
if (enabled && chosenBrush != null && chosenPrototypes.Count > 0)
|
|
{
|
|
Ray ray = UN_CameraFly.instance.camera.ScreenPointToRay(Input.mousePosition);
|
|
List<RaycastResult> list = new List<RaycastResult>();
|
|
eventSystem.RaycastAll(new PointerEventData(eventSystem)
|
|
{
|
|
position = Input.mousePosition,
|
|
pointerId = -1
|
|
}, list);
|
|
if (list.Count > 0)
|
|
{
|
|
return;
|
|
}
|
|
RaycastHit hitInfo;
|
|
if (Physics.Raycast(ray, out hitInfo, float.PositiveInfinity))
|
|
{
|
|
UNBrushUtility.instance.DrawBrush(chosenBrush.paintBrush.brushTexture, Color.white, hitInfo.point, ray.origin.y, brushSizeSlider.value * 100f);
|
|
if (Input.GetMouseButton(0) && Vector3.Distance(lastBrushPosition, hitInfo.point) > 1f)
|
|
{
|
|
lastBrushPosition = hitInfo.point;
|
|
chosenPrototypes_Converted.Clear();
|
|
for (int i = 0; i < chosenPrototypes.Count; i++)
|
|
{
|
|
chosenPrototypes_Converted.Add(chosenPrototypes[i].prototype);
|
|
}
|
|
UNBrushUtility.PaintBrush(Input.GetKey(KeyCode.LeftShift), (int)(brushSizeSlider.value * 100f), (byte)(paintDensitySlider.value * 10f), (byte)(eraseDensitySlider.value * 10f), true, false, null, false, chosenPrototypes_Converted, new Vector2(hitInfo.point.x, hitInfo.point.z), chosenBrush.paintBrush);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UNBrushUtility.projector.enabled = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
UNBrushUtility.projector.enabled = false;
|
|
}
|
|
}
|
|
}
|
|
}
|