2024-08-03 22:24:42 +02:00
|
|
|
|
using UnityEngine;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
|
|
|
|
|
namespace ABI.CCK.Components
|
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
|
[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;
|
|
|
|
|
|
2024-08-03 22:24:42 +02:00
|
|
|
|
private Renderer render;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
private Vector3 lastPos;
|
|
|
|
|
private Vector3 velocity;
|
|
|
|
|
private Vector3 lastRot;
|
|
|
|
|
private Vector3 angularVelocity;
|
2024-08-03 22:24:42 +02:00
|
|
|
|
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()
|
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
|
render = GetComponent<Renderer>();
|
2023-01-22 16:38:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
|
if (updateType == UpdateType.FixedUpdate || render == null) return;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
ProcessUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
|
{
|
2024-08-03 22:24:42 +02:00
|
|
|
|
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);
|
2024-08-03 22:24:42 +02:00
|
|
|
|
Quaternion rotation = transform.rotation;
|
|
|
|
|
angularVelocity = rotation.eulerAngles - lastRot;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
|
2024-08-03 22:24:42 +02:00
|
|
|
|
render.material.SetVector(_cvrVelocity, velocity);
|
|
|
|
|
render.material.SetVector(_cvrAngularVelocity, angularVelocity);
|
2023-01-22 16:38:23 +01:00
|
|
|
|
|
|
|
|
|
lastPos = transform.position;
|
2024-08-03 22:24:42 +02:00
|
|
|
|
lastRot = rotation.eulerAngles;
|
2023-01-22 16:38:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|