Files
com.misaki.hdrp-toon/Runtime/HDRP/BoxLightAdjustment.cs

272 lines
7.8 KiB
C#

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if HDRP_IS_INSTALLED_FOR_UTS
using UnityEngine.Rendering.HighDefinition;
using Unity.Properties;
namespace Unity.Rendering.HighDefinition.Toon
{
[ExecuteAlways]
[DisallowMultipleComponent]
[RequireComponent(typeof(Light))]
internal class BoxLightAdjustment : MonoBehaviour
{
private bool _initialized = false;
private bool _srpCallbackInitialized = false;
private HDAdditionalLightData _bindingSourceLightData;
private HDAdditionalLightData _targetBoxLightData;
private uint _layerMask;
private Light _bindingSourceLight;
private Light _targetBoxLight;
public Transform trackedTransform;
public bool followGameObjectPosition = false;
public bool followGameObjectRotation = false;
public Vector3 positionOffset;
public Quaternion rotationOffset;
[CreateProperty]
public Light BindingSourceLight
{
get => _bindingSourceLight;
set
{
_bindingSourceLight = value;
_bindingSourceLightData = _bindingSourceLight.GetComponent<HDAdditionalLightData>();
}
}
[CreateProperty]
public Light TargetBoxLight
{
get => _targetBoxLight;
set
{
_targetBoxLight = value;
_targetBoxLightData = _targetBoxLight.GetComponent<HDAdditionalLightData>();
}
}
[CreateProperty]
public uint LayerMask
{
get => _layerMask;
set
{
if (_targetBoxLight == null || _bindingSourceLight == null)
{
return;
}
UpdateShadowLayer(_bindingSourceLightData, _layerMask, value);
UpdateShadowLayer(_targetBoxLightData, _layerMask, value);
_layerMask = value;
}
}
#if UNITY_EDITOR
#pragma warning restore CS0414
private bool _isCompiling = false;
#endif
private void Reset()
{
OnDisable();
OnEnable();
}
internal void OnValidate()
{
Release();
Initialize();
}
private void Awake()
{
Initialize();
}
// Start is called before the first frame update
private void Start()
{
}
// Update is called once per frame
private void LateUpdate()
{
Initialize();
#if UNITY_EDITOR
// handle script recompile
if (EditorApplication.isCompiling && !_isCompiling)
{
// on compile begin
_isCompiling = true;
// Release(); no need
return; //
}
else if (!EditorApplication.isCompiling && _isCompiling)
{
// on compile end
_isCompiling = false;
}
#endif
if (_targetBoxLight == null || _bindingSourceLight == null)
{
return;
}
_targetBoxLight.enabled = _bindingSourceLight.enabled;
_targetBoxLight.intensity = _bindingSourceLight.intensity;
if (trackedTransform != null)
{
if (followGameObjectPosition)
{
_targetBoxLight.transform.position = trackedTransform.transform.position + positionOffset;
}
if (followGameObjectRotation)
{
_targetBoxLight.transform.rotation = trackedTransform.transform.rotation * rotationOffset;
}
}
}
private void EnableSrpCallbacks()
{
if (!_srpCallbackInitialized)
{
_srpCallbackInitialized = true;
}
}
private void DisableSrpCallbacks()
{
if (_srpCallbackInitialized)
{
_srpCallbackInitialized = false;
}
}
private void OnEnable()
{
Initialize();
EnableSrpCallbacks();
}
private void OnDisable()
{
DisableSrpCallbacks();
Release();
}
private void UpdateObjectLightLayers()
{
Initialize();
}
internal static GameObject CreateBoxLight(Transform transform)
{
if (transform == null)
{
Debug.LogError("Please, select a GameObject you want a Box Light to follow.");
return null;
}
var gameObjectName = "Box Light for " + transform.name;
var lightGameObject = new GameObject(gameObjectName);
#if UNITY_EDITOR
Undo.RegisterCreatedObjectUndo(lightGameObject, "Created Boxlight adjustment");
#endif
var hdLightData = lightGameObject.AddHDLight(LightType.Box);
// light size
hdLightData.SetBoxSpotSize(new Vector2(10.0f, 10.0f)); // Size should be culculated with more acculacy?
var boxLightAdjustment = lightGameObject.GetComponent<BoxLightAdjustment>();
if (boxLightAdjustment == null)
{
#if UNITY_EDITOR
boxLightAdjustment = Undo.AddComponent<BoxLightAdjustment>(lightGameObject);
#else
boxLightAdjustment = lightGameObject.AddComponent<BoxLightAdjustment>();
#endif
}
#if UNITY_EDITOR
Undo.RecordObject(boxLightAdjustment, "target " + boxLightAdjustment.name);
#endif
boxLightAdjustment._targetBoxLight = hdLightData.GetComponent<Light>();
#if UNITY_EDITOR
Undo.RecordObject(lightGameObject.transform, "Position " + lightGameObject.transform.name);
#endif
// position and rotation
var goPos = lightGameObject.transform.position;
goPos.y += 10.0f;
lightGameObject.transform.position = goPos;
var goRot = lightGameObject.transform.rotation;
goRot.eulerAngles = new Vector3(90.0f, 0.0f, 0.0f);
#if UNITY_EDITOR
Undo.RecordObject(lightGameObject.transform, "Rotation " + lightGameObject.transform.name);
#endif
hdLightData.gameObject.transform.rotation = goRot;
// must be put to gameObject model chain.
boxLightAdjustment.trackedTransform = transform;
return lightGameObject;
}
private void UpdateShadowLayer(HDAdditionalLightData lightData, uint oldValue, uint newValue)
{
lightData.linkShadowLayers = false;
var oldShadowLayer = lightData.GetShadowLayers();
oldShadowLayer &= ~oldValue;
var newShadowLayer = oldShadowLayer | newValue;
lightData.SetShadowLightLayer((UnityEngine.Rendering.HighDefinition.RenderingLayerMask)newShadowLayer);
}
private void Initialize()
{
if (_initialized)
{
return;
}
#if UNITY_EDITOR
// initializing renderer can interfere GI baking. so wait until it is completed.
if (EditorApplication.isCompiling)
return;
#endif
if (trackedTransform == null)
{
return;
}
if (_targetBoxLight != null)
{
positionOffset = _targetBoxLight.transform.position - trackedTransform.transform.position;
rotationOffset = Quaternion.Inverse(trackedTransform.transform.rotation) * _targetBoxLight.transform.rotation;
}
_initialized = true;
}
private void Release()
{
_initialized = false;
}
}
}
#endif // HDRP_IS_INSTALLED_FOR_UTS