cvr-props/Assets/ABI.CCK/Components/CVRGlobalMaterialPropertyUpdater.cs

79 lines
3 KiB
C#
Raw Normal View History

2023-01-22 16:38:23 +01:00
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
2023-01-22 16:38:23 +01:00
using UnityEngine;
using UnityEngine.Animations;
2023-01-22 16:38:23 +01:00
namespace ABI.CCK.Components
{
[AddComponentMenu("ChilloutVR/CVR Global Material Property Updater")]
[HelpURL("https://developers.abinteractive.net/cck/components/global-material-property-updater/")]
public class CVRGlobalMaterialPropertyUpdater : MonoBehaviour, ICCK_Component
2023-01-22 16:38:23 +01:00
{
public enum PropertyType
{
// For legacy reasons, this was used for Int type in shader. Needed for legacy content.
// In reality it represents a float, and should be handled like a float.
// Newer content will use paramFloat for Int shader types (because it's what it is)
2023-01-22 16:38:23 +01:00
paramInt = 0,
paramFloat = 1,
paramVector4 = 2,
// The true Integer, this was added in unity 2021. The shader type is Integer
paramInteger = 3,
2023-01-22 16:38:23 +01:00
}
[SerializeField, NotKeyable]
public Material material;
[SerializeField, NotKeyable]
2023-01-22 16:38:23 +01:00
public string propertyName;
[SerializeField, NotKeyable]
2023-01-22 16:38:23 +01:00
public PropertyType propertyType = PropertyType.paramFloat;
// Variables only used to populate the cvr interactable set property value
[NonSerialized]
2023-01-22 16:38:23 +01:00
public int intValue;
[NonSerialized]
2023-01-22 16:38:23 +01:00
public float floatValue;
[NonSerialized]
2023-01-22 16:38:23 +01:00
public Vector4 vector4Value;
[NonSerialized]
public int integerValue;
2023-01-22 16:38:23 +01:00
// Animatable parameters, value changes to these will trigger material updates
[SerializeField, CVRInteractableActionOperation.HideFromSetPropertyByValue]
public int intValueAnimatable;
[SerializeField, CVRInteractableActionOperation.HideFromSetPropertyByValue]
public float floatValueAnimatable;
[SerializeField, CVRInteractableActionOperation.HideFromSetPropertyByValue]
public Vector4 vector4ValueAnimatable;
[SerializeField, CVRInteractableActionOperation.HideFromSetPropertyByValue]
public int integerValueAnimatable;
#if UNITY_EDITOR
private void OnValidate() {
if (material == null || material.shader.FindPropertyIndex(propertyName) < 0 || AnimationMode.InAnimationMode()) return;
// Fetch the default value from the material
switch (propertyType) {
case PropertyType.paramInt:
intValueAnimatable = material!.GetInt(propertyName);
break;
case PropertyType.paramFloat:
floatValueAnimatable = material!.GetFloat(propertyName);
break;
case PropertyType.paramVector4:
vector4ValueAnimatable = material!.GetVector(propertyName);
break;
case PropertyType.paramInteger:
integerValueAnimatable = material!.GetInteger(propertyName);
break;
}
2023-01-22 16:38:23 +01:00
}
#endif
2023-01-22 16:38:23 +01:00
}
}