You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
81 lines
2.4 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace UltraCombos
|
|
{
|
|
public class UniformStructuredBuffer : StructuredBuffer
|
|
{
|
|
public ScriptableObject uniform;
|
|
|
|
void Start()
|
|
{
|
|
int stride = GetStride(uniform.GetType());
|
|
Allocate(stride / sizeof(float), sizeof(float));
|
|
//Debug.LogFormat("stride: {0}", stride);
|
|
}
|
|
|
|
protected virtual void FixedUpdate()
|
|
{
|
|
var data = GetData(uniform);
|
|
SetData(data);
|
|
}
|
|
|
|
protected int GetStride(System.Type type)
|
|
{
|
|
int res = 0;
|
|
foreach (var field in type.GetFields())
|
|
{
|
|
int s = System.Runtime.InteropServices.Marshal.SizeOf(field.FieldType);
|
|
res += s;
|
|
//Debug.LogFormat("{0}: {1}", field.Name, s);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
protected List<float> GetData(object obj)
|
|
{
|
|
List<float> values = new List<float>();
|
|
foreach (var field in obj.GetType().GetFields())
|
|
{
|
|
var value = field.GetValue(obj);
|
|
|
|
if (field.FieldType.Equals(typeof(int)))
|
|
{
|
|
values.Add((int)value);
|
|
}
|
|
else if (field.FieldType.Equals(typeof(float)))
|
|
{
|
|
values.Add((float)value);
|
|
}
|
|
else if (field.FieldType.Equals(typeof(Vector2)))
|
|
{
|
|
var v = (Vector2)value;
|
|
values.Add(v.x);
|
|
values.Add(v.y);
|
|
}
|
|
else if (field.FieldType.Equals(typeof(Vector3)))
|
|
{
|
|
var v = (Vector3)value;
|
|
values.Add(v.x);
|
|
values.Add(v.y);
|
|
values.Add(v.z);
|
|
}
|
|
else if (field.FieldType.Equals(typeof(Vector4)))
|
|
{
|
|
var v = (Vector4)value;
|
|
values.Add(v.x);
|
|
values.Add(v.y);
|
|
values.Add(v.z);
|
|
values.Add(v.w);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogFormat("Field is invalid: {0} - {1}", field.Name, field.FieldType);
|
|
}
|
|
}
|
|
return values;
|
|
}
|
|
}
|
|
}
|
|
|
|
|