- Home /
Creating a dynamic mesh with audio = crash after 30secs ?
Hello all
I'm trying to make a mesh using audio. I have just a rectangle which size and color are set on samples and spectrum values.
When i run game mode, it "works" for 30seconds, then slow and finally, unity crash, even if i try to stop the game mode.
Maybe i have to destroy my mesh while creating another ?
I would like to instantiate this mesh, in point to make a shape with sound, is it possible in the same script or is it a bad way to do that ?
Thanks for reading
Here is the script :
#pragma strict
var numSamples : int=1024;
var numSpectrum : int=1024;
var widthBranche : float;
var heightBranche : float;
private var h:float;
h *= 0.66;
private var samples:float[];
private var spectrums:float[];
var tronc:GameObject;
var brancheObject:GameObject;
var branche:Mesh;
var brancheMaterial:Material;
var verticesBranche:Vector3[];
var filterBranche:MeshFilter;
var triangleBranche: int[];
var normalsBranche:Vector3[];
var uvBranche:Vector2[];
var rendererBranche:MeshRenderer;
function Start () {
samples=new float[numSamples];
spectrums=new float[numSpectrum];
tronc=GameObject.CreatePrimitive(PrimitiveType.Sphere);
tronc.name="tronc";
brancheObject=new GameObject();
brancheObject.name="branche";
var filterBranche:MeshFilter=brancheObject.AddComponent(MeshFilter);
branche=new Mesh();
filterBranche.mesh=branche;
rendererBranche=brancheObject.AddComponent(MeshRenderer);
}
function Update () {
audio.GetOutputData(samples,0);
audio.GetSpectrumData(spectrums,0,FFTWindow.BlackmanHarris);
for (var i:int=0;i<numSamples;i++) {
if (samples[i]>0) {
if (spectrums[i]>0) {
var couleur:Color=new Color(spectrums[i],spectrums[i],samples[i]);
var couleur2:Color=new Color(spectrums[i]*1000,spectrums[i]*2000,samples[i]*200);
var duration:float=1.0;
var lerp=Mathf.PingPong(Time.time,duration)/duration;
widthBranche=spectrums[i]*1000;
heightBranche=samples[i]*200;
verticesBranche=new Vector3[4];
verticesBranche[0] = new Vector3(0, 0, 0);
verticesBranche[1] = new Vector3(widthBranche, 0, 0);
verticesBranche[2] = new Vector3(0, heightBranche, 0);
verticesBranche[3] = new Vector3(widthBranche, heightBranche, 0);
branche.vertices=verticesBranche;
triangleBranche=new int[6];
triangleBranche[0] = 0;
triangleBranche[1] = 2;
triangleBranche[2] = 1;
triangleBranche[3] = 2;
triangleBranche[4] = 3;
triangleBranche[5] = 1;
branche.triangles=triangleBranche;
normalsBranche=new Vector3[4];
normalsBranche[0] = -Vector3.forward;
normalsBranche[1] = -Vector3.forward;
normalsBranche[2] = -Vector3.forward;
normalsBranche[3] = -Vector3.forward;
branche.normals=normalsBranche;
uvBranche=new Vector2[4];
uvBranche[0] = new Vector2(0, 0);
uvBranche[1] = new Vector2(1, 0);
uvBranche[2] = new Vector2(0, 1);
uvBranche[3] = new Vector2(1, 1);
branche.uv = uvBranche;
brancheMaterial=new Material(Shader.Find("Particles/Additive"));
rendererBranche.material.color=Color.Lerp(couleur,couleur2,lerp);
}}else{}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to connect walls corners 3 Answers
Adding Verticies to Mesh 1 Answer
Dynamic Mesh Collider - Optimization 1 Answer
Dynamic Pathfinding 1 Answer