Ever since I downloaded WinAmp as a kid, I have always wanted to make a music visualizer. Recently I have been inspired by others I had seen on the internet. So I decided to make one with Unity. This was also a fun project as I made the rounded cube myself in Maya! =D I painted the custom cube with white near the top so that I could sample the vertex color in the shader to control the scaling vertically. The shader uses instancing to offload more work onto the GPU. The music is analyzed in real-time. Therefore the analyzer can work with any song. Feel free to download the project and play around with it. Enjoy! =D
Scaler.shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
Shader "Custom/Scaler" { Properties { [PerRendererData]_InstancedScale("Scale", Float) = 0.0 _MainTex ("Albedo (RGB)", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 _YRange("Y Range", VECTOR) = (0.0, 1.0, 0.0, 0.0) } SubShader { Tags { "RenderType"="Opaque" } Cull Back CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows vertex:vert //#pragma multi_compile_instancing // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; float4 color : COLOR; }; half _Glossiness; half _Metallic; fixed4 _Color; float4 _YRange; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_DEFINE_INSTANCED_PROP(float4, _InstancedColor) UNITY_DEFINE_INSTANCED_PROP(float, _InstancedScale) UNITY_INSTANCING_BUFFER_END(Props) void vert(inout appdata_full v) { float s = lerp(_YRange.x, _YRange.y, UNITY_ACCESS_INSTANCED_PROP(Props,_InstancedScale)); v.vertex.xyz += (float3(0.0, 1.0, 0.0) * s) * v.color; } void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; half4 colors[5] = { float4(0.002436638, 0.1766521, 0.9904153, 1.0), float4(0.302436638, 0.3766521, 0.9904153, 1.0), float4(0.7532268, 0.06127048, 0.2830792, 1.0), float4(0.02209818, 0.6905376, 0.6030123, 1.0), float4(0.8351171, 0.2172941, 0.0141145, 1.0) }; int color = round( lerp(0, 4, UNITY_ACCESS_INSTANCED_PROP(Props, _InstancedScale)) ); o.Albedo = colors[color]; // Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" } |
Fantastic. Always learn so much from your posts.
I wish Unity could support you to create a Unity Authorized Udemy course on the subject.
Cheers,
b
Thanks Behram!