zoukankan      html  css  js  c++  java
  • Unity3D Particle System + Audio Source = 音乐可视化

    很简单的东西,上代码:

    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);
            }
        }
    }
  • 相关阅读:
    nose测试中修改nose_html_reporting插件,使生成的html报告加入显示截图功能
    python selenium中等待元素出现及等待元素消失操作
    在python pydev中使用todo标注任务
    云存储命令行工具---libs3
    关于qt creator各种假死的问题
    小端存储转大端存储 & 大端存储转小端存储
    C++判断计算式是大端存储模式,还是小端存储模式
    请教网友:#pragma pack(push) #pragma pack(pop)无效
    sizeof 计算 struct 占字节数的方法总结
    const 变量在多个文件共享,如何验证两种不同的方式下,编译器是否会在多个文件下建立多个副本
  • 原文地址:https://www.cnblogs.com/wangsta/p/5451897.html
Copyright © 2011-2022 走看看