源码
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LightController : MonoBehaviour {
public AudioSource shenYuan;
public Renderer light1, light2, light3, light4;
public float dumpX=0.3f, dumpY=0.3f, dumpZ=0.3f;
// 采样宽度
public int sampWidth = 8;
public float scaleAmount = 4;
private float[] specTrum;
private float startDetal;
private List<Vector3> hights=new List<Vector3>();
// Use this for initialization
void Start () {
}
// Update is called once per frame
float jiange=0f;
void Update () {
specTrum = audio.GetSpectrumData(sampWidth, 0, FFTWindow.BlackmanHarris);
float totalValue = 0;
int i = 0;
while (i < sampWidth){
totalValue = totalValue + specTrum[i];
i++;
}
dumpZ = totalValue*scaleAmount;
Debug.Log(dumpZ);
startDetal+=Time.deltaTime;
if(startDetal>0.1f){
startDetal-=0.1f;
jiange+=0.05f;
hights.Add(new Vector3(jiange,dumpZ,0f));
}
// light1.material.SetFloat ("_DampZ", dumpZ);33we
// light1.material.SetFloat ("_DampX", dumpX);
//light1.material.SetFloat ("_DampY", dumpY);
// light2.material.SetFloat ("_DampZ", dumpZ);
// light2.material.SetFloat ("_DampX", dumpX);
//light2.material.SetFloat ("_DampY", dumpY);
// light3.material.SetFloat ("_DampZ", dumpZ);
// light3.material.SetFloat ("_DampX", dumpX);
//light3.material.SetFloat ("_DampY", dumpY);
// light4.material.SetFloat ("_DampZ", dumpZ);
// light4.material.SetFloat ("_DampX", dumpX);
//light4.material.SetFloat ("_DampY", dumpY);
}
private void OnDrawGizmos(){
for(int i=0;i<hights.Count;i++){
Gizmos.DrawLine(hights[i],new Vector3(hights[i].x,-hights[i].y,0f));
}
}
}