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

55 lines
1.8 KiB
C#
Raw Normal View History

using UnityEngine;
2023-01-22 16:38:23 +01:00
namespace ABI.CCK.Components
{
[AddComponentMenu("ChilloutVR/CVR Material Updater")]
[HelpURL("https://developers.abinteractive.net/cck/components/material-updater/")]
public class CVRMaterialUpdater : MonoBehaviour, ICCK_Component
2023-01-22 16:38:23 +01:00
{
public enum UpdateType
{
Update = 0,
FixedUpdate = 1,
}
public UpdateType updateType = UpdateType.Update;
private Renderer render;
2023-01-22 16:38:23 +01:00
private Vector3 lastPos;
private Vector3 velocity;
private Vector3 lastRot;
private Vector3 angularVelocity;
private static readonly int _cvrVelocity = Shader.PropertyToID("_CVR_Velocity");
private static readonly int _cvrAngularVelocity = Shader.PropertyToID("_CVR_Angular_Velocity");
2023-01-22 16:38:23 +01:00
private void Start()
{
render = GetComponent<Renderer>();
2023-01-22 16:38:23 +01:00
}
private void Update()
{
if (updateType == UpdateType.FixedUpdate || render == null) return;
2023-01-22 16:38:23 +01:00
ProcessUpdate();
}
private void FixedUpdate()
{
if (updateType == UpdateType.Update || render == null) return;
2023-01-22 16:38:23 +01:00
ProcessUpdate();
}
private void ProcessUpdate()
{
velocity = (lastPos - transform.position) / (updateType == UpdateType.Update?Time.deltaTime:Time.fixedDeltaTime);
Quaternion rotation = transform.rotation;
angularVelocity = rotation.eulerAngles - lastRot;
2023-01-22 16:38:23 +01:00
render.material.SetVector(_cvrVelocity, velocity);
render.material.SetVector(_cvrAngularVelocity, angularVelocity);
2023-01-22 16:38:23 +01:00
lastPos = transform.position;
lastRot = rotation.eulerAngles;
2023-01-22 16:38:23 +01:00
}
}
}