2023-01-22 16:38:23 +01:00
|
|
|
using System;
|
2024-08-03 22:24:42 +02:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
using UnityEditor;
|
|
|
|
#endif
|
2023-01-22 16:38:23 +01:00
|
|
|
using UnityEngine;
|
2024-08-03 22:24:42 +02:00
|
|
|
using UnityEngine.Animations;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
|
|
|
namespace ABI.CCK.Components
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
[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
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
// 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,
|
2024-08-03 22:24:42 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:42 +02:00
|
|
|
[SerializeField, NotKeyable]
|
|
|
|
public Material material;
|
|
|
|
|
|
|
|
[SerializeField, NotKeyable]
|
2023-01-22 16:38:23 +01:00
|
|
|
public string propertyName;
|
|
|
|
|
2024-08-03 22:24:42 +02:00
|
|
|
[SerializeField, NotKeyable]
|
2023-01-22 16:38:23 +01:00
|
|
|
public PropertyType propertyType = PropertyType.paramFloat;
|
|
|
|
|
2024-08-03 22:24:42 +02:00
|
|
|
// Variables only used to populate the cvr interactable set property value
|
|
|
|
[NonSerialized]
|
2023-01-22 16:38:23 +01:00
|
|
|
public int intValue;
|
2024-08-03 22:24:42 +02:00
|
|
|
[NonSerialized]
|
2023-01-22 16:38:23 +01:00
|
|
|
public float floatValue;
|
2024-08-03 22:24:42 +02:00
|
|
|
[NonSerialized]
|
2023-01-22 16:38:23 +01:00
|
|
|
public Vector4 vector4Value;
|
2024-08-03 22:24:42 +02:00
|
|
|
[NonSerialized]
|
|
|
|
public int integerValue;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
2024-08-03 22:24:42 +02: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
|
|
|
}
|
2024-08-03 22:24:42 +02:00
|
|
|
#endif
|
2023-01-22 16:38:23 +01:00
|
|
|
}
|
|
|
|
}
|