Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
using Fluctio.FluctioSim.Common.Configuration;
using Fluctio.FluctioSim.Core.Components.Base;
using Fluctio.FluctioSim.Core.Components.Prefabs.HeightField.Base;
using Fluctio.FluctioSim.Utils.General;
using UnityEngine;
using Random = System.Random;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Fluctio.FluctioSim.Core.Components.Prefabs.HeightField.PerlinNoiseGround
{
	[AddComponentMenu(Config.PrefixedName+"/Physics/Terrain Layers/Perlin Noise", Config.ComponentMenuOrder + 20)]
	public class PerlinNoiseLayer : HeightFieldLayer, IRandomHeightFieldLayer
	{
		[field: SerializeField, HideInInspector]
		public float Scale { get; set; } = 1f;

		private Vector2 _noiseBasePosition;
		
		void IRandomHeightFieldLayer.InitializeLayer(Random random)
		{
			_noiseBasePosition = new Vector2(random.Next(-10000, 10000), random.Next(-10000, 10000));
		}

		public override float GetHeightAt(Vector2 heightmapPosition)
		{
			heightmapPosition -= Vector2.one * 0.5f; // convert to [-0.5; 0.5]
			var noisePosition = _noiseBasePosition + heightmapPosition * Scale;
			return Mathf.PerlinNoise(noisePosition.x, noisePosition.y);
		}
	}

	#if UNITY_EDITOR
	[CustomEditor(typeof(PerlinNoiseLayer), true)]
	[CanEditMultipleObjects]
	public class PerlinNoiseLayerEditor : EditorComponentEditor
	{
		// ReSharper disable once InconsistentNaming
		private new PerlinNoiseLayer target => (PerlinNoiseLayer)base.target;

		private float? _scaleSliderValue = null;
		
		private const float MinSliderValue = 0f;
		private const float MaxSliderValue = 10f;
		private const float MinScalePower = 2f;
		private const float MaxScalePower = 0f;
		
		private static float SliderValueToScale(float sliderValue)
		{
			var lerpPosition = Mathf.InverseLerp(MinSliderValue, MaxSliderValue, sliderValue);
			var scalePower = Mathf.Lerp(MinScalePower, MaxScalePower, lerpPosition);
			var scale = Mathf.Pow(10, scalePower);
			return scale;
		}

		private static float ScaleToSliderValue(float scale)
		{
			var scalePower = Mathf.Log10(scale);
			var lerpPosition = Mathf.InverseLerp(MinScalePower, MaxScalePower, scalePower);
			var sliderValue = Mathf.Lerp(MinSliderValue, MaxSliderValue, lerpPosition);
			return sliderValue;
		}

		public override void OnInspectorGUI()
		{
			serializedObject.Update();
			
			var scaleProperty = serializedObject.FindProperty(Util.GetBackingFieldName(nameof(target.Scale)));
			DrawPropertiesExcluding(serializedObject, "m_Script", scaleProperty.name);

			_scaleSliderValue ??= ScaleToSliderValue(scaleProperty.floatValue);
			_scaleSliderValue = EditorGUILayout.Slider(scaleProperty.displayName, _scaleSliderValue.Value, MinSliderValue, MaxSliderValue);
			scaleProperty.floatValue = SliderValueToScale(_scaleSliderValue.Value);
			
			serializedObject.ApplyModifiedProperties();
		}
	}
	#endif
}