Unity3D完成随时间变化的昼夜交替及光线变化

Unity3D环境中的昼夜交替,并且控制好光线随着时间的变化而变化,太阳位置跟随经纬度的不同而不同。

重点:

  1. 时间范围为0~24小时;
  2. 太阳位置跟随当前经纬度;
  3. 太阳完成东升西落,世界坐标系中的x正为东方,x负为西方,z正为北;
  4. 太阳光线强度变化范围0~1,按照24小时的正弦变化。

白天效果如下 :

当前的系统时间:

夜晚效果如下:

当前的系统时间:

 以下是部分代码:

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.AzureSky;
using UTools.UCoordinate;

[RequireComponent(typeof(AzureController))]
public class AzureController : MonoBehaviour
{
    [Tooltip("AzureSky控制器")]
    public AzureSkyController azureSkyController;
    [Tooltip("当前地理位置的经纬度")]
    public CoordinateConvert curGeographicCoordinate;
    [Tooltip("本地地理位置的经纬度")]
    LngLatVector localGeographicPos;
    [Tooltip("光照对象")]
    public Light lightObj = null;
    [Tooltip("光照强度")]
    [Range(0, 1f)]
    float lightIntensity = 0.8f;
    float LightIntensity
    {
        get { return lightIntensity; }
        set {
            if (lightObj != null)
            {
                lightIntensity = value;
                lightObj.intensity = value;
            }
        }
    }
    [Tooltip("光照变化比例")]
    float intensityRate = 0f;
    public LngLatVector LocalGeographicPos
    {
        get { return localGeographicPos; }
        set {
            if (azureSkyController != null)
            {
                localGeographicPos = value;
                azureSkyController.timeOfDay.latitude = (float)value.latitude;
                azureSkyController.timeOfDay.longitude = (float)value.longitude;
            }            
        }
    }

    private void Awake()
    {
        if (azureSkyController == null)
        {
            azureSkyController = GetComponent<AzureSkyController>();
        }
        // 计算光照比例(按照24小时的正弦比例计算)
        intensityRate = 1f / 24;
    }

    // Start is called before the first frame update
    void Start()
    {
        if (azureSkyController)
        {
            // 本地系统时间
            azureSkyController.timeOfDay.year = DateTime.Now.Year;
            azureSkyController.timeOfDay.month = DateTime.Now.Month;
            azureSkyController.timeOfDay.day = DateTime.Now.Day;
            // 经纬度
            if (curGeographicCoordinate)
            {
                LocalGeographicPos = curGeographicCoordinate.centerLngLat;
            }
            // 光照强度
            if (lightObj != null)
            {
                float vale = Mathf.Sin(6 * intensityRate * 3.1415926f);
                LightIntensity = Mathf.Sin(DateTime.Now.Hour * intensityRate * 3.1415926f);
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        if (azureSkyController)
        {
            // 本地经纬度坐标
            if (!LngLatVector.Appropriately(localGeographicPos, curGeographicCoordinate.centerLngLat))
            {
                LocalGeographicPos = curGeographicCoordinate.centerLngLat;
            }
            // 本地系统时间
            if (azureSkyController.timeOfDay.hour != DateTime.Now.Hour)
            {
                // 时间太阳位置
                azureSkyController.timeOfDay.hour = DateTime.Now.Hour;
                // 光照变化
                LightIntensity = Mathf.Sin(DateTime.Now.Hour * intensityRate * 3.1415926f);                
            }
        }
    }
}

特别说明:

该功能借助了Azure[Sky] Dynamic Skybox包,导入工程之后使用以上脚本可完成各位童鞋们想要的效果,当然啦,有需要交流的可随时留言!祝各位工作愉快^o^!

猜你喜欢

转载自blog.csdn.net/weixin_39638287/article/details/128343565