Skip to content

Commit 2c0f2cb

Browse files
committed
force based audio visualizer to smooth out triangle movements
1 parent 1c1c8a8 commit 2c0f2cb

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

Runtime/AudioVisualizer.cs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ All rights reserved.
2525
using Unity.Burst;
2626
using Unity.Collections.LowLevel.Unsafe;
2727
using Voxell.Inspector;
28+
using Voxell.Mathx;
2829

2930
namespace Voxell.Audio
3031
{
@@ -38,45 +39,55 @@ public partial class AudioCore
3839
public VisualEffect audioVFX;
3940
public MeshFilter meshFilter;
4041
public Mesh sampleMesh;
42+
[Tooltip("Damping value to multiply the velocity of each triangles each frame.")]
43+
public float velocityMultiplier = 2.5f;
4144
public int batchSize = 100;
42-
[InspectOnly] public int totalTris;
45+
[InspectOnly] public int totalTriangles;
4346

4447
private Mesh modifiedSampleMesh;
45-
private NativeArray<float3> originVertices;
4648
private NativeArray<float3> normals;
4749
private NativeArray<int> triangles;
48-
private NativeArray<float3> vertices;
4950
private NativeArray<float> samples;
5051
private NativeArray<int> bandDistribution;
52+
private NativeArray<float3> vertices;
53+
private NativeArray<float> prevBands;
54+
private NativeArray<float> bandVelocities;
5155

5256
private void InitAudioVisualizer()
5357
{
54-
totalTris = (int)sampleMesh.GetIndexCount(0)/3;
55-
audioProfile.bandSize = totalTris;
58+
totalTriangles = (int)sampleMesh.GetIndexCount(0)/3;
59+
audioProfile.bandSize = totalTriangles;
5660

5761
audioProcessor = new AudioProcessor(ref audioSource, ref audioProfile);
5862

5963
MeshUtil.DeepCopyMesh(ref sampleMesh, out modifiedSampleMesh);
6064
audioVFX.SetMesh(VFXPropertyId.mesh_sampleMesh, modifiedSampleMesh);
61-
audioVFX.SetInt(VFXPropertyId.int_triangleCount, totalTris);
65+
audioVFX.SetInt(VFXPropertyId.int_triangleCount, totalTriangles);
6266
modifiedSampleMesh.MarkDynamic();
6367
meshFilter.mesh = modifiedSampleMesh;
6468

6569
// transferring mesh data to native arrays to be processed parallely
6670
Mesh.MeshDataArray sampleMeshData = Mesh.AcquireReadOnlyMeshData(sampleMesh);
67-
originVertices = MeshUtil.NativeGetVertices(sampleMeshData[0], Allocator.Persistent);
68-
originVertices.AsReadOnly();
6971
normals = MeshUtil.NativeGetNormals(sampleMeshData[0], Allocator.Persistent);
7072
normals.AsReadOnly();
73+
7174
triangles = MeshUtil.NativeGetIndices(sampleMeshData[0], Allocator.Persistent);
7275
triangles.AsReadOnly();
76+
7377
vertices = MeshUtil.NativeGetVertices(sampleMeshData[0], Allocator.Persistent);
7478

7579
// audio processing attributes
7680
samples = new NativeArray<float>(audioProfile.sampleSize, Allocator.Persistent);
7781
bandDistribution = new NativeArray<int>(audioProfile.bandSize+1, Allocator.Persistent);
78-
MathUtil.CopyToNativeArray<int>(ref audioProcessor.bandDistribution, ref bandDistribution);
7982

83+
prevBands = new NativeArray<float>(totalTriangles, Allocator.Persistent);
84+
MathUtil.SetNativeArray<float>(ref prevBands, 0);
85+
86+
bandVelocities = new NativeArray<float>(totalTriangles, Allocator.Persistent);
87+
MathUtil.SetNativeArray<float>(ref bandVelocities, 0);
88+
89+
MathUtil.CopyToNativeArray<int>(ref audioProcessor.bandDistribution, ref bandDistribution);
90+
bandDistribution.AsReadOnly();
8091
sampleMeshData.Dispose();
8192
}
8293

@@ -89,31 +100,35 @@ private void UpdateAudioVisualizer()
89100

90101
AudioMeshVisualizer audioMeshVisualizer = new AudioMeshVisualizer
91102
{
92-
originVertices = originVertices,
93103
normals = normals,
94104
triangles = triangles,
95105
samples = samples,
96106
bandDistribution = bandDistribution,
97107
power = audioProfile.power,
98108
scale = audioProfile.scale,
99109
bandAverage = audioProcessor.bandAverage,
100-
vertices = vertices
110+
vertices = vertices,
111+
prevBands = prevBands,
112+
bandVelocities = bandVelocities,
113+
velocityMultiplier = velocityMultiplier,
114+
deltaTime = Time.deltaTime
101115
};
102116

103-
JobHandle jobHandle = audioMeshVisualizer.Schedule<AudioMeshVisualizer>(totalTris, batchSize);
117+
JobHandle jobHandle = audioMeshVisualizer.Schedule<AudioMeshVisualizer>(totalTriangles, batchSize);
104118
jobHandle.Complete();
105119

106120
modifiedSampleMesh.SetVertices<float3>(vertices);
107121
}
108122

109123
void OnDisable()
110124
{
111-
originVertices.Dispose();
112125
normals.Dispose();
113126
triangles.Dispose();
114-
vertices.Dispose();
115127
samples.Dispose();
116128
bandDistribution.Dispose();
129+
vertices.Dispose();
130+
prevBands.Dispose();
131+
bandVelocities.Dispose();
117132
}
118133
}
119134
}
@@ -125,7 +140,6 @@ void OnDisable()
125140
)]
126141
public struct AudioMeshVisualizer : IJobParallelFor
127142
{
128-
[ReadOnly] public NativeArray<float3> originVertices;
129143
[ReadOnly] public NativeArray<float3> normals;
130144
[ReadOnly] public NativeArray<int> triangles;
131145

@@ -134,30 +148,36 @@ public struct AudioMeshVisualizer : IJobParallelFor
134148
public float power;
135149
public float scale;
136150
public int bandAverage;
137-
// public int maxBuffer;
138-
// private NativeArray<float> buffer;
139151

140-
// [ReadOnly] public NativeArray<float> bands;
141152
[NativeDisableContainerSafetyRestriction]
142153
[WriteOnly] public NativeArray<float3> vertices;
143154

155+
public NativeArray<float> prevBands;
156+
public NativeArray<float> bandVelocities;
157+
public float velocityMultiplier;
158+
public float deltaTime;
159+
144160
public void Execute(int index)
145161
{
146162
int t0 = triangles[index*3];
147163
int t1 = triangles[index*3 + 1];
148164
int t2 = triangles[index*3 + 2];
149165

150-
// TODO: use force based method to smooth things out (no smoothing algorithm needed for audio anymore!)
151-
// moves triangles towards direction of the triangle normal based on the amplitude of the particular frequency
152166
float3 normal = normals[t0];
153167

154168
float band = CreateBand(bandDistribution[index], bandDistribution[index+1]);
155169
band = math.pow(math.sqrt(band), power) * scale;
156-
float3 displacement = normal * band;
157170

158-
vertices[t0] = originVertices[t0] + displacement;
159-
vertices[t1] = originVertices[t1] + displacement;
160-
vertices[t2] = originVertices[t2] + displacement;
171+
bandVelocities[index] += band - prevBands[index];
172+
float magnitude = bandVelocities[index] * deltaTime;
173+
float3 displacement = normal * magnitude;
174+
175+
vertices[t0] += displacement;
176+
vertices[t1] += displacement;
177+
vertices[t2] += displacement;
178+
179+
bandVelocities[index] *= velocityMultiplier;
180+
prevBands[index] += magnitude;
161181
}
162182

163183
private float CreateBand(int start, int end)

0 commit comments

Comments
 (0)