using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;

#if UNITY_EDITOR
using UnityEditor;
[ExecuteInEditMode]
#endif

public class WeatherLight : MonoBehaviour
{
    public string globalLightDirVectorName = "_GlobalLight";
    public Vector4 lightDir = new Vector4(0, 0, 0, 1);
    public Color lightColor = Color.white;
    private Light mLight;
    private Quaternion mLightRotation;
    private float mLightIntensity;

    void Start()
    {
        mLight = GetComponent<Light>();
    }


#if UNITY_EDITOR
    void Update()
    {
        if (mLightRotation == mLight.transform.localRotation &&
            lightColor == mLight.color &&
            mLightIntensity == mLight.intensity)
            return;
        StringBuilder sb = new StringBuilder();
        mLightRotation = mLight.transform.localRotation;
        lightColor = mLight.color;
        mLightIntensity = mLight.intensity;
        Vector3 forward = mLightRotation * Vector3.forward;
        lightDir = new Vector4(forward.x, forward.y, forward.z, 1);
        sb.Length = 0;
        sb.Append(globalLightDirVectorName);
        sb.Append("DirWorld");
        Shader.SetGlobalVector(sb.ToString(), lightDir);
        sb.Length = 0;
        sb.Append(globalLightDirVectorName);
        sb.Append("Color");
        Shader.SetGlobalVector(sb.ToString(), lightColor);
        //Shader.SetGlobalFloat("_GlobalLightIntensity", mLightIntensity);
    }
#endif

}
