很简单的东西,上代码:
using UnityEngine; using System.Collections; public class particle_ocean : MonoBehaviour { public ParticleSystem particleSystem; private ParticleSystem.Particle[] particlesArray; public int resolution = 256; public float spacing = 0.5f; public float noiseScale = 0.2f; public float[] heightScale; private float perlinNoiseAnimX = 0.01f; private float perlinNoiseAnimY = 0.01f; public Gradient colorGradient; public float[] heightToGoto; private float heightGotoSpeed = 20f; private float heightRestoreSpeed = 2f; public float heightGoToTime = 1f; public float heightRestoreTime = 0.1f; public float heightClampMax = 40f; public float heightClampMin = 4f; public float particleSampleRate = 0.05f; private int frameCount = 0; //Audio public AudioSource audio; private float[] spectrum; public int spectrumScale = 60000; void Awake() { Application.targetFrameRate = 60; } void Start() { particlesArray = new ParticleSystem.Particle[resolution * resolution]; particleSystem.maxParticles = resolution * resolution; particleSystem.Emit(resolution * resolution); particleSystem.GetParticles(particlesArray); heightScale = new float[resolution]; heightToGoto = new float[resolution]; spectrum = new float[resolution]; InvokeRepeating ("updateHeight", 0, particleSampleRate); } void Update() { for (int i = 0; i < resolution; i++) { for (int j = 0; j < resolution; j++) { float zPos = Mathf.PerlinNoise (i * noiseScale + perlinNoiseAnimX, j * noiseScale + perlinNoiseAnimY) * heightScale[j]; particlesArray [i * resolution + j].color = colorGradient.Evaluate (zPos * 0.025f); particlesArray [i * resolution + j].position = new Vector3 (i * spacing, zPos, j * spacing); } } perlinNoiseAnimX += 0.01f; perlinNoiseAnimY += 0.01f; particleSystem.SetParticles (particlesArray, particlesArray.Length); gotoHeight (); restoreTo2(); } void updateHeight(){ audio.GetSpectrumData (spectrum, 0, FFTWindow.BlackmanHarris); for (int k = 0; k < resolution; k++) { heightToGoto[k] = spectrum [k] * spectrumScale ; heightToGoto[k] = Mathf.Clamp (heightToGoto[k], heightClampMin, heightClampMax); } } void gotoHeight() { for (int k = 0; k < resolution; k++) { heightScale[k] = Mathf.SmoothDamp(heightScale[k], heightToGoto[k], ref heightGotoSpeed, heightGoToTime); } } void restoreTo2() { for (int k = 0; k < resolution; k++) { heightScale[k] = Mathf.SmoothDamp(heightScale[k], 2f, ref heightRestoreSpeed, heightRestoreTime); } } }
时间: 2024-10-13 04:57:10