Files
vfx-graph-gs/Assets/vfx-graph-gs/SplatData.cs
T

93 lines
3.3 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System;
using System.Linq.Expressions;
namespace SplatVfx
{
public sealed class SplatData : ScriptableObject
{
#region Public properties
public int SplatCount => PositionArray.Length;
public GraphicsBuffer PositionBuffer => GetCachedBuffers().position;
public GraphicsBuffer ScaleBuffer => GetCachedBuffers().scale;
public GraphicsBuffer RotationBuffer => GetCachedBuffers().rotation;
public GraphicsBuffer ColorBuffer => GetCachedBuffers().color;
public GraphicsBuffer OpacityBuffer => GetCachedBuffers().opacity;
public GraphicsBuffer SHBuffer => GetCachedBuffers().sh;
#endregion
#region Serialized data
[field: SerializeField] public Vector3[] PositionArray { get; set; }
[field: SerializeField] public Vector3[] ScaleArray { get; set; }
[field: SerializeField] public Quaternion[] RotationArray { get; set; }
[field: SerializeField] public Color[] ColorArray { get; set; }
[field: SerializeField] public float[] OpacityArray { get; set; }
[field: SerializeField] public float[,] SHArray { get; set; }
#endregion
#region Public methods
public void ReleaseGpuResources()
{
_cachedBuffers.position?.Release();
_cachedBuffers.scale?.Release();
_cachedBuffers.rotation?.Release();
_cachedBuffers.color?.Release();
_cachedBuffers.opacity?.Release();
_cachedBuffers.sh?.Release();
_cachedBuffers = (null, null, null, null, null, null);
}
#endregion
#region GPU resource management
(GraphicsBuffer position, GraphicsBuffer scale, GraphicsBuffer rotation,
GraphicsBuffer color, GraphicsBuffer opacity, GraphicsBuffer sh)
_cachedBuffers;
static unsafe GraphicsBuffer NewBuffer<T>(int count) where T : unmanaged
=> new GraphicsBuffer(GraphicsBuffer.Target.Structured, count, sizeof(T));
(GraphicsBuffer position, GraphicsBuffer scale, GraphicsBuffer rotation,
GraphicsBuffer color, GraphicsBuffer opacity, GraphicsBuffer sh)
GetCachedBuffers()
{
if (_cachedBuffers.position == null)
{
_cachedBuffers.position = NewBuffer<Vector3>(SplatCount);
_cachedBuffers.scale = NewBuffer<Vector3>(SplatCount);
_cachedBuffers.rotation = NewBuffer<Quaternion>(SplatCount);
_cachedBuffers.color = NewBuffer<Color>(SplatCount);
_cachedBuffers.opacity = NewBuffer<float>(SplatCount);
_cachedBuffers.sh = NewBuffer<float>(SplatCount * 45);
_cachedBuffers.position.SetData(PositionArray);
// _cachedBuffers.scale.SetData(ScaleArray);
// _cachedBuffers.rotation.SetData(RotationArray);
// _cachedBuffers.color.SetData(ColorArray);
// _cachedBuffers.opacity.SetData(OpacityArray);
// _cachedBuffers.sh.SetData(SHArray);
}
return _cachedBuffers;
}
#endregion
#region ScriptableObject implementation
void OnDisable() => ReleaseGpuResources();
#endregion
}
}