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);
            }
        }
    }
  • 相关阅读:
    外贸视频教程[外贸人zencart自助建站视频教程]:第一课
    外贸视频教程[外贸人zencart自助建站视频教程]:第二课
    行sqlSQL*PLUS使用(三)
    消息函数windows 程序设计 第三章 (下)
    优化性能[置顶] Android应用性能优化方案
    自定义方法JSP自定义标签
    发票名称<iframe name=document.getElementById("cellFrame").src = "dyszAction!showFpDyMb.do?fpzldm=" + fpzldm;
    纹理寻址DirectX入门 (8) TextureAddressMode
    空间复杂度分段分段有序数组合并成有序(空间复杂度为O(1))
    数据库生成T4模版在代码生成中的应用心得
  • 原文地址:https://www.cnblogs.com/wangsta/p/5451897.html
Copyright © 2011-2022 走看看