53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
|
using UnityEngine;
|
|||
|
using System.Collections;
|
|||
|
using Module.Log;
|
|||
|
|
|||
|
[ExecuteInEditMode]
|
|||
|
public class MaterialIELightTool : MonoBehaviour {
|
|||
|
|
|||
|
|
|||
|
// Update is called once per frame
|
|||
|
void Update ()
|
|||
|
{
|
|||
|
if (_Renders == null)
|
|||
|
{
|
|||
|
_Renders = GetComponentsInChildren<Renderer>();
|
|||
|
if (_Renders == null || _Renders.Length == 0)
|
|||
|
{
|
|||
|
LogModule.DebugLog("Not Render attach");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (_Renders != null && _Renders.Length != 0)
|
|||
|
{
|
|||
|
foreach (var render in _Renders)
|
|||
|
{
|
|||
|
UpdateLight(render);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private Renderer[] _Renders;
|
|||
|
|
|||
|
public void UpdateLight(Renderer render)
|
|||
|
{
|
|||
|
var eyeDir = transform.position - Camera.main.transform.position;
|
|||
|
|
|||
|
var lightH = render.sharedMaterial.GetFloat("_SubLightDirH");
|
|||
|
var lightA = render.sharedMaterial.GetFloat("_SubLightDirA");
|
|||
|
eyeDir.y = lightH;
|
|||
|
var lightDir = RotVector(eyeDir, lightA);
|
|||
|
|
|||
|
render.sharedMaterial.SetFloat("_SubLightDirX", lightDir.x);
|
|||
|
render.sharedMaterial.SetFloat("_SubLightDirY", lightDir.y);
|
|||
|
render.sharedMaterial.SetFloat("_SubLightDirZ", lightDir.z);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public Vector3 RotVector(Vector3 vec, float angle)
|
|||
|
{
|
|||
|
Vector3 newVector = new Vector3(vec.x * (float)Mathf.Cos(angle) - vec.z * (float)Mathf.Sin(angle), vec.y, vec.x * (float)Mathf.Sin(angle) - vec.z * (float)Mathf.Cos(angle));
|
|||
|
return newVector;
|
|||
|
}
|
|||
|
}
|