├── .gitignore ├── Assets ├── Material.meta ├── Material │ ├── shockwave.mat │ └── shockwave.mat.meta ├── Script.meta ├── Script │ ├── MicAnalyzer.cs │ ├── MicAnalyzer.cs.meta │ ├── MousePosition.cs │ ├── MousePosition.cs.meta │ ├── Shader_color.cs │ ├── Shader_color.cs.meta │ ├── SoundVisualize.cs │ └── SoundVisualize.cs.meta ├── Shader.meta ├── Shader │ ├── soundshock.shader │ └── soundshock.shader.meta ├── main.unity └── main.unity.meta ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset ├── README.md └── logo.gif /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | 6 | # Autogenerated VS/MD solution and project files 7 | *.csproj 8 | *.unityproj 9 | *.sln 10 | *.suo 11 | *.tmp 12 | *.user 13 | *.userprefs 14 | *.pidb 15 | *.booproj 16 | 17 | # Unity3D generated meta files 18 | *.pidb.meta 19 | 20 | # Unity3D Generated File On Crash Reports 21 | sysinfo.txt 22 | -------------------------------------------------------------------------------- /Assets/Material.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a7068a5c2272248c3b94621b33d76d41 3 | folderAsset: yes 4 | timeCreated: 1432197189 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Material/shockwave.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/Assets/Material/shockwave.mat -------------------------------------------------------------------------------- /Assets/Material/shockwave.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0a11e5f6620854680a187e8cda229bb3 3 | timeCreated: 1432198178 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/Script.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9c9ddc1cb7baa49da95d26b8ec3810e1 3 | folderAsset: yes 4 | timeCreated: 1432197209 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Script/MicAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | // 空のAudio Sourceをアタッチ 5 | [RequireComponent (typeof (AudioSource))] 6 | public class MicAnalyzer : MonoBehaviour { 7 | // 現在の音量を読み取る 8 | public float GetLoundness() { 9 | return loudness; 10 | } 11 | // 感度 12 | public float sensitivity = 100; 13 | // 音量 14 | float loudness; 15 | // 前フレームの音量 16 | float lastLoudness; 17 | 18 | // レンジでコントロールできるようにする 19 | [Range(0,0.95f)] 20 | public float lastLoudnessInfluence; 21 | 22 | private AudioSource audio; 23 | 24 | // マテリアル 25 | private Material mat; 26 | 27 | public int resolution = 1024; 28 | public float lowFreqThreshold = 14700, midFreqThreshold = 29400, highFreqThreshold = 44100; 29 | public float lowEnhance = 1f, midEnhance = 10f, highEnhance = 100f; 30 | 31 | // Use this for initialization 32 | void Start () { 33 | // マテリアルの作成 34 | mat = GetComponent().material; 35 | // 空のAudio Sourceを取得 36 | audio = GetComponent(); 37 | // Audio Source の Audio Clipをマイク入力に設定 38 | audio.clip = Microphone.Start(null, true, 10, 44100); 39 | 40 | // 無音状態でずっと再生しておく 41 | audio.loop = true; 42 | audio.mute = true; 43 | 44 | // マイクがReady状態になるまで待機 45 | while (Microphone.GetPosition(null) <= 0) {} 46 | // 再生開始 47 | audio.Play(); 48 | } 49 | 50 | // Update is called once per frame 51 | void Update () { 52 | CalcLoudness(); 53 | AnalyzeSpectrum(); 54 | } 55 | // 現フレームの音量を計算 56 | void CalcLoudness() { 57 | lastLoudness = loudness; 58 | loudness = GetAveragedVolume() * sensitivity * (1 - lastLoudnessInfluence ) + lastLoudness * lastLoudnessInfluence; 59 | 60 | // ボリューム 61 | mat.SetFloat("_Loudness", loudness); 62 | } 63 | // 現フレームのAudioClipから平均的な音量を取得 64 | float GetAveragedVolume() { 65 | int sampleData = 256; 66 | float[] data = new float[sampleData]; 67 | float _audio = 0; 68 | GetComponent().GetOutputData(data, 0); 69 | foreach( float d in data) { 70 | _audio += Mathf.Abs(d); 71 | } 72 | return _audio / sampleData; 73 | } 74 | void AnalyzeSpectrum() { 75 | var spectrum = audio.GetSpectrumData(resolution, 0, FFTWindow.BlackmanHarris); 76 | var deltaFreq = AudioSettings.outputSampleRate / resolution; 77 | float low = 0f, mid = 0f, high = 0f; 78 | 79 | for (var i = 0; i < resolution; ++i) { 80 | var freq = deltaFreq * i; 81 | if (freq <= lowFreqThreshold) { 82 | low += spectrum[i]; 83 | } 84 | else if (freq <= midFreqThreshold) { 85 | mid += spectrum[i]; 86 | } 87 | else if (freq <= highFreqThreshold) { 88 | high += spectrum[i]; 89 | } 90 | } 91 | 92 | low *= lowEnhance; 93 | mid *= midEnhance; 94 | high *= highEnhance; 95 | // ボリューム 96 | mat.SetFloat("_LowSpectrum", low); 97 | mat.SetFloat("_MidSpectrum", mid); 98 | mat.SetFloat("_HighSpectrum", high); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Assets/Script/MicAnalyzer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4e5d8724225904e4bb5b330750b7b665 3 | timeCreated: 1432996142 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Script/MousePosition.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class MousePosition : MonoBehaviour { 5 | Material mat; 6 | // Use this for initialization 7 | void Start () { 8 | mat = GetComponent().material; 9 | } 10 | 11 | // Update is called once per frame 12 | void Update () { 13 | Vector3 mpos = Input.mousePosition; 14 | mat.SetVector("_MousePosition", mpos); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assets/Script/MousePosition.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d63501899cb7446b280c32d1bdc47158 3 | timeCreated: 1432199848 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Script/Shader_color.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Shader_color : MonoBehaviour { 5 | // 色を指定 6 | public Color color1 = new Color (0.0f,0.0f,1.0f,1.0f); 7 | public Color color2 = new Color (1.0f,0.0f,0.0f,1.0f); 8 | 9 | Material mat; 10 | // Use this for initialization 11 | void Start () { 12 | mat = GetComponent().material; 13 | StartCoroutine(BlinkerCoroutine()); 14 | } 15 | 16 | // Update is called once per frame 17 | void Update () { 18 | } 19 | 20 | IEnumerator BlinkerCoroutine() { 21 | var original_mat = new Material(mat); 22 | for(;;){ 23 | mat.EnableKeyword("_EMISSION"); 24 | mat.SetColor("_EmissionColor", color1); 25 | yield return new WaitForSeconds(1.0f); 26 | mat.SetColor("_EmissionColor", color2); 27 | yield return new WaitForSeconds(1.0f); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Assets/Script/Shader_color.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ee83dc70ea7924462983d33bb1614317 3 | timeCreated: 1432118887 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Script/SoundVisualize.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class NewBehaviourScript : MonoBehaviour { 5 | 6 | // Use this for initialization 7 | void Start () { 8 | 9 | } 10 | 11 | // Update is called once per frame 12 | void Update () { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Assets/Script/SoundVisualize.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5010b9f8bfa814782984bc946a8381bd 3 | timeCreated: 1432548742 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/Shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cceaad544cbfc4055924dcdf01fa845f 3 | folderAsset: yes 4 | timeCreated: 1432197160 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/Shader/soundshock.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/soundshock" { 2 | Properties { 3 | // インスペクタに表示される 4 | _WaveNum ("WaveNum", Range (0.0, 100)) = 10 5 | _WaveScale ("WaveScale", Range (0.0, 10)) = 0.6 6 | _Opacity ("Opacity", Range (0.0, 1.0)) = 1.0 7 | _Color ("Color", Color) = (1.0, 0.6, 0.6, 1.0) 8 | _Loudness ("Loudness", Float) = 0.1 9 | } 10 | SubShader { 11 | // レンダリングの指定 12 | Tags { "RenderType" = "Opaque"} 13 | CGPROGRAM 14 | // surfaceシェーダを指定 SurfaceFanction LightModel alphaブレンディング 15 | #pragma surface surf Lambert vertex:vert alpha 16 | // #pragma fragment frag 17 | // #pragma target 3.0 18 | // UV(テクスチャ)座標が渡ってくる 19 | struct Input { 20 | float4 color : COLOR; 21 | float4 pos : SV_POSITION; 22 | float2 uv_MainTex; 23 | float3 worldPos; 24 | float3 customColor; 25 | }; 26 | // Propertiesで指定した値を取得 27 | float _WaveNum, _WaveScale, _Opacity, _Loudness, 28 | _LowSpectrum, _MidSpectrum, _HighSpectrum; 29 | float4 _Color; 30 | // 31 | void surf (Input IN, inout SurfaceOutput o) { 32 | o.Albedo = float4(_LowSpectrum, _MidSpectrum, _HighSpectrum, 1.0); 33 | // o.Albedo *= IN.customColor * 10; 34 | // 半透明 35 | o.Alpha = _Opacity; 36 | // clip (frac((IN.worldPos.x * _SinTime.x + IN.worldPos.y * _SinTime.y + IN.worldPos.z * _SinTime.z) * _SinTime.y * 100) -0.5); 37 | } 38 | // vertexシェーダ (頂点) 39 | void vert (inout appdata_full v, out Input o) { 40 | // 波を打つ 41 | UNITY_INITIALIZE_OUTPUT(Input,o); 42 | v.vertex.x += _WaveScale * _Loudness * v.normal.x * sin(v.vertex.y * 3.14 * _WaveNum); 43 | v.vertex.z += _WaveScale * _Loudness * v.normal.z * sin(v.vertex.x * 3.14 * _WaveNum); 44 | v.vertex.y += _WaveScale * _Loudness * v.normal.y * sin(v.vertex.z * 3.14 * _WaveNum); 45 | o.customColor = abs(v.vertex); 46 | o.pos = mul( UNITY_MATRIX_MVP, v.vertex ); 47 | // calculate binormal 48 | float3 binormal = cross( v.normal, v.tangent.xyz ) * v.tangent.w; 49 | o.color.xyz = binormal * 0.5 + 0.5; 50 | o.color.w = 1.0; 51 | } 52 | fixed4 frag () : SV_Target { 53 | return _Color; 54 | } 55 | ENDCG 56 | } 57 | FallBack "Diffuse" 58 | } 59 | -------------------------------------------------------------------------------- /Assets/Shader/soundshock.shader.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1b694666bec5345a69765c761c5206d4 3 | timeCreated: 1434005507 4 | licenseType: Free 5 | ShaderImporter: 6 | defaultTextures: [] 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/main.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/Assets/main.unity -------------------------------------------------------------------------------- /Assets/main.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 240494faa82db45299d6629389c273c6 3 | timeCreated: 1432033734 4 | licenseType: Free 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.0.0f1 2 | m_StandardAssetsVersion: 0 3 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unity-sound-shock 2 | 3 | Shaders that change vertices with sound 4 | 5 | ![logo](logo.gif) 6 | 7 | This Unity script, `MicAnalyzer`, is designed to attach to a GameObject with an AudioSource component, allowing it to analyze and process audio input from a microphone in real-time. 8 | 9 | ## Features 10 | 11 | - **Real-Time Audio Analysis:** Capture and analyze audio data from the microphone. 12 | - **Volume Control:** Dynamically read and adjust the volume level. 13 | - **Spectrum Analysis:** Break down the audio into low, mid, and high frequencies for further processing. 14 | 15 | ## How to Use 16 | 17 | 1. Attach this script to a GameObject in your Unity scene. 18 | 2. Ensure the GameObject has an AudioSource component. 19 | 3. Configure the sensitivity and frequency thresholds to match your audio analysis requirements. 20 | 21 | ## Script Functions 22 | 23 | - `GetLoundness`: Returns the current loudness level. 24 | - `Start`: Initializes the microphone input and starts capturing audio. 25 | - `Update`: Continuously calculates loudness and analyzes the audio spectrum each frame. 26 | - `CalcLoudness`: Calculates the current frame's loudness. 27 | - `GetAveragedVolume`: Gets the average volume from the current audio clip. 28 | - `AnalyzeSpectrum`: Analyzes the audio spectrum and sets the material properties based on the low, mid, and high frequencies. 29 | 30 | ## Parameters 31 | 32 | - `sensitivity`: Adjusts the microphone sensitivity. 33 | - `resolution`: Sets the resolution of the spectrum analysis. 34 | - `lowFreqThreshold`, `midFreqThreshold`, `highFreqThreshold`: Define the frequency ranges for the spectrum analysis. 35 | - `lowEnhance`, `midEnhance`, `highEnhance`: Multipliers for enhancing the respective frequency ranges. 36 | 37 | ## Customization 38 | 39 | You can control the influence of the last frame's loudness and the thresholds for frequency analysis through the public variables provided. 40 | 41 | ## Materials 42 | 43 | The script interacts with the material on the GameObject to visually represent the audio analysis through shader properties. 44 | 45 | ## Notes 46 | 47 | - The script uses the `Microphone` class to start capturing audio. 48 | - It uses `AudioSource.GetSpectrumData` for spectrum analysis with a Blackman-Harris window for smoothing. 49 | - The script assumes that the GameObject's material has shader properties named `_Loudness`, `_LowSpectrum`, `_MidSpectrum`, and `_HighSpectrum` for audio visualization. 50 | 51 | Make sure to tailor the script's public variables to fit the audio characteristics of your project. 52 | -------------------------------------------------------------------------------- /logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/valbeat/unity-sound-shock/af397f774b11ea5a65e62a1d796ca16aa1396eca/logo.gif --------------------------------------------------------------------------------