瀏覽代碼

Better DayNight Change

Erik Wittkorn 9 年之前
父節點
當前提交
0d700e2b5c
共有 4 個文件被更改,包括 398 次插入105 次删除
  1. 9 0
      game/Assets/Animation.meta
  2. 9 0
      game/Assets/Editor.meta
  3. 227 1
      game/Assets/Scenes/DayNightChange.unity
  4. 153 104
      game/Assets/Scripts/DayTimeManager.cs

+ 9 - 0
game/Assets/Animation.meta

@@ -0,0 +1,9 @@
+fileFormatVersion: 2
+guid: d50ec002efdd94687908f0e27e2d45c8
+folderAsset: yes
+timeCreated: 1501346877
+licenseType: Free
+DefaultImporter:
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 9 - 0
game/Assets/Editor.meta

@@ -0,0 +1,9 @@
+fileFormatVersion: 2
+guid: 8db886966ab03481798a18eb702a04fe
+folderAsset: yes
+timeCreated: 1501419311
+licenseType: Free
+DefaultImporter:
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 227 - 1
game/Assets/Scenes/DayNightChange.unity

@@ -756,6 +756,16 @@ Prefab:
   m_Modification:
     m_TransformParent: {fileID: 0}
     m_Modifications:
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.size
+      value: 3
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.size
+      value: 4
+      objectReference: {fileID: 0}
     - target: {fileID: 4618518221219600, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
       propertyPath: m_LocalPosition.x
       value: 0
@@ -788,7 +798,223 @@ Prefab:
       propertyPath: m_RootOrder
       value: 5
       objectReference: {fileID: 0}
-    m_RemovedComponents: []
+    - target: {fileID: 4086179075244344, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
+      propertyPath: m_LocalRotation.x
+      value: 0.7071068
+      objectReference: {fileID: 0}
+    - target: {fileID: 4086179075244344, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
+      propertyPath: m_LocalRotation.y
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4086179075244344, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
+      propertyPath: m_LocalRotation.z
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 4086179075244344, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
+      propertyPath: m_LocalRotation.w
+      value: 0.7071068
+      objectReference: {fileID: 0}
+    - target: {fileID: 4086179075244344, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
+      propertyPath: m_LocalEulerAnglesHint.y
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: NightColor.r
+      value: 0.1254902
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: NightColor.g
+      value: 0.1254902
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: NightColor.b
+      value: 0.18431373
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: EveningColor.r
+      value: 0.23529412
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: EveningColor.g
+      value: 0.3137255
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: EveningColor.b
+      value: 0.47058824
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: NoonColor.r
+      value: 0.9485294
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: NoonColor.g
+      value: 0.84463364
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: NoonColor.b
+      value: 0.6695502
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: MorningColor.r
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: MorningColor.g
+      value: 0.83205926
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: MorningColor.b
+      value: 0.36764705
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: secondsPerDay
+      value: 60
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[0].inSlope
+      value: 0.14161837
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[0].outSlope
+      value: 0.14161837
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[1].time
+      value: 0.74611384
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[1].value
+      value: 0.101069294
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_RotationOrder
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[0].value
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[1].inSlope
+      value: 0.43818286
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[1].outSlope
+      value: 0.43818286
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[2].time
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[2].value
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[0].time
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[2].inSlope
+      value: 2
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: aCurve.m_Curve.Array.data[2].outSlope
+      value: 2
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[1].time
+      value: 0.50038546
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[1].value
+      value: 0.105107576
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[1].inSlope
+      value: 0.26548746
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[1].outSlope
+      value: 0.26548746
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_RotationOrder
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[2].time
+      value: 0.7999664
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[2].value
+      value: 0.20412965
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[2].inSlope
+      value: 1.2750924
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[2].outSlope
+      value: 1.2750924
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[3].time
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[3].value
+      value: 1
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[3].inSlope
+      value: 2
+      objectReference: {fileID: 0}
+    - target: {fileID: 114038366060908416, guid: 673629bba9f02458cacf9d13115e1078,
+        type: 2}
+      propertyPath: colorCurve.m_Curve.Array.data[3].outSlope
+      value: 2
+      objectReference: {fileID: 0}
+    m_RemovedComponents:
+    - {fileID: 95461355863206716, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
   m_ParentPrefab: {fileID: 100100000, guid: 673629bba9f02458cacf9d13115e1078, type: 2}
   m_IsPrefabParent: 0
 --- !u!1 &2104481372

+ 153 - 104
game/Assets/Scripts/DayTimeManager.cs

@@ -4,112 +4,161 @@ using UnityEngine;
 
 public enum RotationAxis
 {
-	X, Y
+    X, Y
 }
 
-public class DayTimeManager : MonoBehaviour {
+public class DayTimeManager : MonoBehaviour
+{
 
-	public Light sunlight;
-	public float secondsPerDay; 
-
-	public RotationAxis rotAxis = RotationAxis.X; 
-
-	public float maxAngle = 50.0f;
-
-	private float _elapsedTime;
-
-	public float timeFactor {
-		get {
-			return _elapsedTime / secondsPerDay;
-		}
-	}
-
-	public DayTimeLabel dayTimeLabel {
-		get {
-			if (timeFactor >= 6.0f / 24.0f && timeFactor < 11.0f / 24.0f)
-			{
-				return DayTimeLabel.Morning;
-			}
-
-			if (timeFactor >= 11.0f / 24.0f && timeFactor < 17.0f / 24.0f)
-			{
-				return DayTimeLabel.Noon;
-			}
-
-			if (timeFactor >= 17.0f / 24.0f && timeFactor < 22.0f / 24.0f)
-			{
-				return DayTimeLabel.Evening;
-			}
-
-			if (timeFactor >= 23.0f / 24.0f && timeFactor < 24.0f / 24.0f)
-			{
-				return DayTimeLabel.Night;
-			}
-
-			if (timeFactor >= 0.0f / 24.0f && timeFactor < 6.0f / 24.0f)
-			{
-				return DayTimeLabel.Night;
-			}
-
-			return DayTimeLabel.Morning;
-		}
-	}
-
-	private float _timeDirection = 1.0f;
-
-	private Vector3 _position;
-	private Vector3 _rotation;
-
-	void Awake () {
-
-	}
-
-	// Use this for initialization
-	void Start () {
-		_position = sunlight.gameObject.transform.position;
-		_rotation = sunlight.gameObject.transform.rotation.ToEulerAngles();
-		Debug.Log("Got Rotation " + _rotation);
-	}
-	
-	// Update is called once per frame
-	void Update () {
-		
-		_elapsedTime += Time.deltaTime;
-		if (_elapsedTime >= secondsPerDay)
-		{
-			_elapsedTime = 0;
-		}
-
-		float intensityAngle = this.timeFactor * Mathf.PI;
-		sunlight.intensity = Mathf.Sin(intensityAngle);
-
-		float rotationDirection = Mathf.Sign(this.timeFactor - 0.5f);
-
-		Debug.Log("DayTimeLabel: " + dayTimeLabel.ToString());
-
-		switch (rotAxis)
-		{
-			case RotationAxis.X:
-			{
-				sunlight.gameObject.transform.rotation.SetEulerAngles(_rotation.x + rotationDirection * maxAngle, _rotation.y, _rotation.z);
-			} break;
-
-			case RotationAxis.Y:
-			{
-				sunlight.gameObject.transform.rotation.SetEulerAngles(_rotation.x, _rotation.y + rotationDirection * maxAngle, _rotation.z);
-			} break;
-		}
-
-	}
-
-	float FactorToDayTime(float factor)
-	{
-		return factor * 24.0f;
-	}
-
-	float DayTimeToFactor(float dayTime)
-	{
-		return dayTime / 24.0f;
-	}
+    public Light sunlight;
+    public float secondsPerDay;
+
+    public Color MorningColor;
+    public Color NoonColor;
+    public Color EveningColor;
+    public Color NightColor;
+
+    public float maxAngle = 50.0f;
+
+    private float _elapsedTime;
+
+    public float timeFactor
+    {
+        get
+        {
+            return _elapsedTime / secondsPerDay;
+        }
+    }
+
+    public AnimationCurve colorCurve;
+
+    public float dayTime;
+
+    float _morningStartFactor;
+    float _noonStartFactor;
+    float _eveningStartFactor;
+    float _nightStartFactor;
+    public float morningStartTime = 8.0f;
+    public float noonStartTime = 11.0f;
+    public float eveningStartTime = 17.0f;
+    public float nightStartTime = 20.0f;
+
+    public DayTimeLabel startTime = DayTimeLabel.Noon;
+
+    public DayTimeLabel dayTimeLabel = DayTimeLabel.Night;
+
+    private float _timeDirection = 1.0f;
+
+    private Vector3 _position;
+    private Vector3 _rotation;
+
+    void Awake()
+    {
+
+    }
+
+    // Use this for initialization
+    void Start()
+    {
+        _morningStartFactor = morningStartTime / 24.0f;
+        _noonStartFactor = noonStartTime / 24.0f;
+        _eveningStartFactor = eveningStartTime / 24.0f;
+        _nightStartFactor = nightStartTime / 24.0f;
+
+        switch (startTime)
+        {
+            case DayTimeLabel.Morning:
+                {
+                    _elapsedTime = _morningStartFactor * secondsPerDay;
+                }
+                break;
+            case DayTimeLabel.Noon:
+                {
+                    _elapsedTime = _noonStartFactor * secondsPerDay;
+                }
+                break;
+            case DayTimeLabel.Evening:
+                {
+                    _elapsedTime = _eveningStartFactor * secondsPerDay;
+                }
+                break;
+            case DayTimeLabel.Night:
+                {
+                    _elapsedTime = _nightStartFactor * secondsPerDay;
+                }
+                break;
+        }
+        _position = sunlight.gameObject.transform.position;
+        _rotation = sunlight.gameObject.transform.rotation.eulerAngles;
+    }
+
+    // Update is called once per frame
+    void Update()
+    {
+
+        _elapsedTime += Time.deltaTime;
+        if (_elapsedTime >= secondsPerDay)
+        {
+            _elapsedTime = 0;
+        }
+
+        float intensityAngle = this.timeFactor * Mathf.PI;
+        sunlight.intensity = Mathf.Sin(intensityAngle);
+
+        float rotationDirection = (this.timeFactor - 0.5f) * 2;
+
+        // Debug.Log("DayTimeLabel: " + dayTimeLabel.ToString());
+
+        sunlight.gameObject.transform.rotation = Quaternion.Euler(_rotation.x + rotationDirection * maxAngle, _rotation.y + rotationDirection * maxAngle, _rotation.z);
+        // Debug.Log("Rotate around x: " + (_rotation.x + rotationDirection * maxAngle));
+
+        Color resultColor = Color.white;
+        if (timeFactor >= _morningStartFactor && timeFactor < _noonStartFactor)
+        {
+            float morningFactor = (timeFactor - _morningStartFactor) / (_noonStartFactor - _morningStartFactor);
+            resultColor = Color.Lerp(MorningColor, NoonColor, colorCurve.Evaluate(morningFactor));
+            dayTimeLabel = DayTimeLabel.Morning;
+        }
+
+        if (timeFactor >= _noonStartFactor && timeFactor < _eveningStartFactor)
+        {
+            float noonFactor = (timeFactor - _noonStartFactor) / (_eveningStartFactor - _noonStartFactor);
+            resultColor = Color.Lerp(NoonColor, EveningColor, colorCurve.Evaluate(noonFactor));
+            dayTimeLabel = DayTimeLabel.Noon;
+        }
+
+        if (timeFactor >= _eveningStartFactor && timeFactor < _nightStartFactor)
+        {
+            float eveningFactor = (timeFactor - _eveningStartFactor) / (_nightStartFactor - _eveningStartFactor);
+            resultColor = Color.Lerp(EveningColor, NightColor, colorCurve.Evaluate(eveningFactor));
+            dayTimeLabel = DayTimeLabel.Evening;
+        }
+
+        if (timeFactor >= _nightStartFactor && timeFactor < 1.0f)
+        {
+            float nightFactor1 = (timeFactor - _nightStartFactor) / ((1.0f - _nightStartFactor) + _morningStartFactor);
+            resultColor = Color.Lerp(NightColor, MorningColor, colorCurve.Evaluate(nightFactor1));
+            dayTimeLabel = DayTimeLabel.Night;
+        }
+
+        if (timeFactor >= 0 && timeFactor < _morningStartFactor)
+        {
+            float nightFactor2 = (timeFactor + (1.0f - _nightStartFactor)) / ((1.0f - _nightStartFactor) + _morningStartFactor);
+            resultColor = Color.Lerp(NightColor, MorningColor, colorCurve.Evaluate(nightFactor2));
+            dayTimeLabel = DayTimeLabel.Night;
+        }
+        sunlight.color = resultColor;
+        dayTime = timeFactor * 24.0f;
+    }
+
+    float FactorToDayTime(float factor)
+    {
+        return factor * 24.0f;
+    }
+
+    float DayTimeToFactor(float dayTime)
+    {
+        return dayTime / 24.0f;
+    }
 
 }