yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
ac54b32
master
1#ifndef AUDIOLINK_CGINC_INCLUDED 2 #define AUDIOLINK_CGINC_INCLUDED 3 4 // Map of where features in AudioLink are. 5 #define ALPASS_DFT uint2(0,4) //Size: 128, 2 6 #define ALPASS_WAVEFORM uint2(0,6) //Size: 128, 16 7 #define ALPASS_AUDIOLINK uint2(0,0) //Size: 128, 4 8 #define ALPASS_AUDIOBASS uint2(0,0) //Size: 128, 1 9 #define ALPASS_AUDIOLOWMIDS uint2(0,1) //Size: 128, 1 10 #define ALPASS_AUDIOHIGHMIDS uint2(0,2) //Size: 128, 1 11 #define ALPASS_AUDIOTREBLE uint2(0,3) //Size: 128, 1 12 #define ALPASS_AUDIOLINKHISTORY uint2(1,0) //Size: 127, 4 13 #define ALPASS_GENERALVU uint2(0,22) //Size: 12, 1 14 #define ALPASS_GENERALVU_INSTANCE_TIME uint2(2,22) 15 #define ALPASS_GENERALVU_LOCAL_TIME uint2(3,22) 16 #define ALPASS_GENERALVU_NETWORK_TIME uint2(4,22) 17 #define ALPASS_GENERALVU_PLAYERINFO uint2(6,22) 18 #define ALPASS_THEME_COLOR0 uint2(0,23) 19 #define ALPASS_THEME_COLOR1 uint2(1,23) 20 #define ALPASS_THEME_COLOR2 uint2(2,23) 21 #define ALPASS_THEME_COLOR3 uint2(3,23) 22 #define ALPASS_GENERALVU_UNIX_DAYS uint2(5,23) 23 #define ALPASS_GENERALVU_UNIX_SECONDS uint2(6,23) 24 #define ALPASS_GENERALVU_SOURCE_POS uint2(7,23) 25 #define ALPASS_MEDIASTATE uint2(5,22) 26 27 #define ALPASS_CCINTERNAL uint2(12,22) //Size: 12, 2 28 #define ALPASS_CCCOLORS uint2(25,22) //Size: 12, 1 (Note Color #0 is always black, Colors start at 1) 29 #define ALPASS_CCSTRIP uint2(0,24) //Size: 128, 1 30 #define ALPASS_CCLIGHTS uint2(0,25) //Size: 128, 2 31 #define ALPASS_AUTOCORRELATOR uint2(0,27) //Size: 128, 1 32 #define ALPASS_FILTEREDAUDIOLINK uint2(0,28) //Size: 16, 4 33 #define ALPASS_CHRONOTENSITY uint2(16,28) //Size: 8, 4 34 #define ALPASS_FILTEREDVU uint2(24,28) //Size: 4, 4 35 #define ALPASS_FILTEREDVU_INTENSITY uint2(24,28) //Size: 4, 1 36 #define ALPASS_FILTEREDVU_MARKER uint2(24,29) //Size: 4, 1 37 #define ALPASS_GLOBAL_STRINGS uint2(40,28) //Size: 8, 4 38 39 // Some basic constants to use (Note, these should be compatible with 40 // future version of AudioLink, but may change. 41 #define AUDIOLINK_SAMPHIST 3069 // Internal use for algos, do not change. 42 #define AUDIOLINK_SAMPLEDATA24 2046 43 #define AUDIOLINK_EXPBINS 24 44 #define AUDIOLINK_EXPOCT 10 45 #define AUDIOLINK_ETOTALBINS (AUDIOLINK_EXPBINS * AUDIOLINK_EXPOCT) 46 #define AUDIOLINK_WIDTH 128 47 #define AUDIOLINK_SPS 48000 // Samples per second 48 #define AUDIOLINK_ROOTNOTE 0 49 #define AUDIOLINK_4BAND_FREQFLOOR 0.123 50 #define AUDIOLINK_4BAND_FREQCEILING 1 51 #define AUDIOLINK_BOTTOM_FREQUENCY 13.75 52 #define AUDIOLINK_BASE_AMPLITUDE 2.5 53 #define AUDIOLINK_DELAY_COEFFICIENT_MIN 0.3 54 #define AUDIOLINK_DELAY_COEFFICIENT_MAX 0.9 55 #define AUDIOLINK_DFT_Q 4.0 56 #define AUDIOLINK_TREBLE_CORRECTION 5.0 57 #define AUDIOLINK_4BAND_TARGET_RATE 90.0 58 #define AUDIOLINK_LUT {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, \ 59 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, \ 60 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, \ 61 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, \ 62 0.008, 0.01, 0.012, 0.014, 0.017, 0.02, 0.022, 0.025, 0.029, 0.032, 0.036, \ 63 0.04, 0.044, 0.048, 0.053, 0.057, 0.062, 0.067, 0.072, 0.078, 0.083, 0.089, \ 64 0.095, 0.101, 0.107, 0.114, 0.121, 0.128, 0.135, 0.142, 0.149, 0.157, 0.164, \ 65 0.172, 0.18, 0.188, 0.196, 0.205, 0.213, 0.222, 0.23, 0.239, 0.248, 0.257, \ 66 0.266, 0.276, 0.285, 0.294, 0.304, 0.313, 0.323, 0.333, 0.342, 0.352, 0.362, \ 67 0.372, 0.381, 0.391, 0.401, 0.411, 0.421, 0.431, 0.441, 0.451, 0.46, 0.47, \ 68 0.48, 0.49, 0.499, 0.509, 0.519, 0.528, 0.538, 0.547, 0.556, 0.565, 0.575, \ 69 0.584, 0.593, 0.601, 0.61, 0.619, 0.627, 0.636, 0.644, 0.652, 0.66, 0.668, \ 70 0.676, 0.684, 0.691, 0.699, 0.706, 0.713, 0.72, 0.727, 0.734, 0.741, 0.747, \ 71 0.754, 0.76, 0.766, 0.772, 0.778, 0.784, 0.79, 0.795, 0.801, 0.806, 0.811, \ 72 0.816, 0.821, 0.826, 0.831, 0.835, 0.84, 0.844, 0.848, 0.853, 0.857, 0.861, \ 73 0.864, 0.868, 0.872, 0.875, 0.879, 0.882, 0.885, 0.888, 0.891, 0.894, 0.897, \ 74 0.899, 0.902, 0.904, 0.906, 0.909, 0.911, 0.913, 0.914, 0.916, 0.918, 0.919, \ 75 0.921, 0.922, 0.924, 0.925, 0.926, 0.927, 0.928, 0.928, 0.929, 0.929, 0.93, \ 76 0.93, 0.93, 0.931, 0.931, 0.93, 0.93, 0.93, 0.93, 0.929, 0.929, 0.928, 0.927, \ 77 0.926, 0.925, 0.924, 0.923, 0.922, 0.92, 0.919, 0.917, 0.915, 0.913, 0.911, \ 78 0.909, 0.907, 0.905, 0.903, 0.9} 79 80 // Text constants 81 #define AUDIOLINK_STRING_MAX_CHARS 32 82 #define AUDIOLINK_STRING_LOCALPLAYER 0 83 #define AUDIOLINK_STRING_MASTER 1 84 #define AUDIOLINK_STRING_CUSTOM1 2 85 #define AUDIOLINK_STRING_CUSTOM2 3 86 87 // ColorChord constants 88 #define COLORCHORD_EMAXBIN 192 89 #define COLORCHORD_NOTE_CLOSEST 3.0 90 #define COLORCHORD_NEW_NOTE_GAIN 8.0 91 #define COLORCHORD_MAX_NOTES 10 92 93 // We use glsl_mod for most calculations because it behaves better 94 // on negative numbers, and in some situations actually outperforms 95 // HLSL's modf(). 96 #ifndef glsl_mod 97 #define glsl_mod(x,y) (((x)-(y)*floor((x)/(y)))) 98 #endif 99 100 uniform float4 _AudioTexture_TexelSize; 101 102 #ifdef SHADER_TARGET_SURFACE_ANALYSIS 103 #define AUDIOLINK_STANDARD_INDEXING 104 #endif 105 106 // Mechanism to index into texture. 107 #ifdef AUDIOLINK_STANDARD_INDEXING 108 sampler2D _AudioTexture; 109 #define AudioLinkData(xycoord) tex2Dlod(_AudioTexture, float4(uint2(xycoord) * _AudioTexture_TexelSize.xy, 0, 0)) 110 #else 111 uniform Texture2D<float4> _AudioTexture; 112 #define AudioLinkData(xycoord) _AudioTexture[uint2(xycoord)] 113 #endif 114 115 // Convenient mechanism to read from the AudioLink texture that handles reading off the end of one line and onto the next above it. 116 float4 AudioLinkDataMultiline(uint2 xycoord) { return AudioLinkData(uint2(xycoord.x % AUDIOLINK_WIDTH, xycoord.y + xycoord.x/AUDIOLINK_WIDTH)); } 117 118 // Mechanism to sample between two adjacent pixels and lerp between them, like "linear" supesampling 119 float4 AudioLinkLerp(float2 xy) { return lerp( AudioLinkData(xy), AudioLinkData(xy+int2(1,0)), frac( xy.x ) ); } 120 121 // Same as AudioLinkLerp but properly handles multiline reading. 122 float4 AudioLinkLerpMultiline(float2 xy) { return lerp(AudioLinkDataMultiline(xy), AudioLinkDataMultiline(xy+float2(1,0)), frac(xy.x)); } 123 124 //Tests to see if Audio Link texture is available 125 bool AudioLinkIsAvailable() 126 { 127 #if !defined(AUDIOLINK_STANDARD_INDEXING) 128 int width, height; 129 _AudioTexture.GetDimensions(width, height); 130 return width > 16; 131 #else 132 return _AudioTexture_TexelSize.z > 16; 133 #endif 134 } 135 136 // DEPRECATED! Use AudioLinkGetVersionMajor and AudioLinkGetVersionMinor() instead. 137 //Get version of audiolink present in the world, 0 if no audiolink is present 138 float AudioLinkGetVersion() 139 { 140 int2 dims; 141 #if !defined(AUDIOLINK_STANDARD_INDEXING) 142 _AudioTexture.GetDimensions(dims.x, dims.y); 143 #else 144 dims = _AudioTexture_TexelSize.zw; 145 #endif 146 147 if (dims.x >= 128) 148 return AudioLinkData(ALPASS_GENERALVU).x; 149 else if (dims.x > 16) 150 return 1; 151 else 152 return 0; 153 } 154 155 float AudioLinkGetVersionMajor() 156 { 157 return AudioLinkData(ALPASS_GENERALVU).y; 158 } 159 160 float AudioLinkGetVersionMinor() 161 { 162 // If the major version is 1 or greater, we are using the new versioning system. 163 if (AudioLinkGetVersionMajor() > 0) 164 { 165 return AudioLinkData(ALPASS_GENERALVU).w; 166 } 167 // Otherwise, defer to the old logic for determining version. 168 else 169 { 170 int2 dims; 171 #if !defined(AUDIOLINK_STANDARD_INDEXING) 172 _AudioTexture.GetDimensions(dims.x, dims.y); 173 #else 174 dims = _AudioTexture_TexelSize.zw; 175 #endif 176 177 if (dims.x >= 128) 178 return AudioLinkData(ALPASS_GENERALVU).x; 179 else if (dims.x > 16) 180 return 1; 181 else 182 return 0; 183 } 184 } 185 186 // This pulls data from this texture. 187 #define AudioLinkGetSelfPixelData(xy) _SelfTexture2D[xy] 188 189 // Extra utility functions for time. 190 uint AudioLinkDecodeDataAsUInt(uint2 indexloc) 191 { 192 uint4 rpx = AudioLinkData(indexloc); 193 return rpx.x + rpx.y*1024 + rpx.z * 1048576 + rpx.w * 1073741824; 194 } 195 196 //Note: This will truncate time to every 134,217.728 seconds (~1.5 days of an instance being up) to prevent floating point aliasing. 197 // if your code will alias sooner, you will need to use a different function. It should be safe to use this on all times. 198 float AudioLinkDecodeDataAsSeconds(uint2 indexloc) 199 { 200 uint time = AudioLinkDecodeDataAsUInt(indexloc) & 0x7ffffff; 201 //Can't just divide by float. Bug in Unity's HLSL compiler. 202 return float(time / 1000) + float( time % 1000 ) / 1000.; 203 } 204 205 #define ALDecodeDataAsSeconds( x ) AudioLinkDecodeDataAsSeconds( x ) 206 #define ALDecodeDataAsUInt( x ) AudioLinkDecodeDataAsUInt( x ) 207 208 float AudioLinkRemap(float t, float a, float b, float u, float v) { return ((t-a) / (b-a)) * (v-u) + u; } 209 210 float3 AudioLinkHSVtoRGB(float3 HSV) 211 { 212 float3 RGB = 0; 213 float C = HSV.z * HSV.y; 214 float H = HSV.x * 6; 215 float X = C * (1 - abs(fmod(H, 2) - 1)); 216 if (HSV.y != 0) 217 { 218 float I = floor(H); 219 if (I == 0) { RGB = float3(C, X, 0); } 220 else if (I == 1) { RGB = float3(X, C, 0); } 221 else if (I == 2) { RGB = float3(0, C, X); } 222 else if (I == 3) { RGB = float3(0, X, C); } 223 else if (I == 4) { RGB = float3(X, 0, C); } 224 else { RGB = float3(C, 0, X); } 225 } 226 float M = HSV.z - C; 227 return RGB + M; 228 } 229 230 float3 AudioLinkCCtoRGB(float bin, float intensity, int rootNote) 231 { 232 float note = bin / AUDIOLINK_EXPBINS; 233 234 float hue = 0.0; 235 note *= 12.0; 236 note = glsl_mod(4. - note + rootNote, 12.0); 237 { 238 if(note < 4.0) 239 { 240 //Needs to be YELLOW->RED 241 hue = (note) / 24.0; 242 } 243 else if(note < 8.0) 244 { 245 // [4] [8] 246 //Needs to be RED->BLUE 247 hue = (note-2.0) / 12.0; 248 } 249 else 250 { 251 // [8] [12] 252 //Needs to be BLUE->YELLOW 253 hue = (note - 4.0) / 8.0; 254 } 255 } 256 float val = intensity - 0.1; 257 return AudioLinkHSVtoRGB(float3(fmod(hue, 1.0), 1.0, clamp(val, 0.0, 1.0))); 258 } 259 260 // Sample the amplitude of a given frequency in the DFT, supports frequencies in [13.75; 14080]. 261 float4 AudioLinkGetAmplitudeAtFrequency(float hertz) 262 { 263 float note = AUDIOLINK_EXPBINS * log2(hertz / AUDIOLINK_BOTTOM_FREQUENCY); 264 return AudioLinkLerpMultiline(ALPASS_DFT + float2(note, 0)); 265 } 266 267 // Sample the amplitude of a given quartertone in an octave. Octave is in [0; 9] while quarter is [0; 23]. 268 float4 AudioLinkGetAmplitudeAtQuarterNote(float octave, float quarter) 269 { 270 return AudioLinkLerpMultiline(ALPASS_DFT + float2(octave * AUDIOLINK_EXPBINS + quarter, 0)); 271 } 272 273 // Sample the amplitude of a given semitone in an octave. Octave is in [0; 9] while note is [0; 11]. 274 float4 AudioLinkGetAmplitudeAtNote(float octave, float note) 275 { 276 float quarter = note * 2.0; 277 return AudioLinkGetAmplitudeAtQuarterNote(octave, quarter); 278 } 279 280 // Sample the amplitude of a given quartertone across all octaves. Quarter is [0; 23]. 281 float4 AudioLinkGetAmplitudesAtQuarterNote(float quarter) 282 { 283 float amplitude = 0; 284 UNITY_UNROLL 285 for (int i = 0; i < AUDIOLINK_EXPOCT; i++) 286 { 287 amplitude += AudioLinkGetAmplitudeAtQuarterNote(i,quarter); 288 } 289 return amplitude; 290 } 291 292 // Sample the amplitude of a given semitone across all octaves. Note is [0; 11]. 293 float4 AudioLinkGetAmplitudesAtNote(float note) 294 { 295 float quarter = note * 2.0; 296 return AudioLinkGetAmplitudesAtQuarterNote(quarter); 297 } 298 299 // Get a reasonable drop-in replacement time value for _Time.y with the 300 // given chronotensity index [0; 7] and AudioLink band [0; 3]. 301 float AudioLinkGetChronoTime(uint index, uint band) 302 { 303 return (AudioLinkDecodeDataAsUInt(ALPASS_CHRONOTENSITY + uint2(index, band))) / 100000.0; 304 } 305 306 // Get a chronotensity value in the interval [0; 1], modulated by the speed input, 307 // with the given chronotensity index [0; 7] and AudioLink band [0; 3]. 308 float AudioLinkGetChronoTimeNormalized(uint index, uint band, float speed) 309 { 310 return frac(AudioLinkGetChronoTime(index, band) * speed); 311 } 312 313 // Get a chronotensity value in the interval [0; interval], modulated by the speed input, 314 // with the given chronotensity index [0; 7] and AudioLink band [0; 3]. 315 float AudioLinkGetChronoTimeInterval(uint index, uint band, float speed, float interval) 316 { 317 return AudioLinkGetChronoTimeNormalized(index, band, speed) * interval; 318 } 319 320 // Get time of day. The return value is a float4 with the values float3(hour, minute, second). 321 float3 AudioLinkGetTimeOfDay() 322 { 323 float value = AudioLinkDecodeDataAsSeconds(ALPASS_GENERALVU_UNIX_SECONDS); 324 float hour = floor(value / 3600.0); 325 float minute = floor(value / 60.0) % 60.0; 326 float second = value % 60.0; 327 return float3(hour, minute, second); 328 } 329 330 // Get a character from a globally synced string, given an index of string in range [0; 3], and 331 // a character index in range [0; 31]. The string at the 0th index is the local player name. 332 // The 1st index is the master name, and index 2 and 3 are custom strings. 333 // Returns a unsigned integer represented a unicode codepoint, i.e. UTF32. 334 uint AudioLinkGetGlobalStringChar(uint stringIndex, uint charIndex) 335 { 336 uint4 fourChars = asuint(AudioLinkData(ALPASS_GLOBAL_STRINGS + uint2(charIndex / 4, stringIndex))); 337 return fourChars[charIndex % 4]; 338 } 339 340 // Get a character from the local player name given a character index in the range [0; 31]. 341 // Returns a unsigned integer represented a unicode codepoint, i.e. UTF32. 342 uint AudioLinkGetLocalPlayerNameChar(uint charIndex) 343 { 344 return AudioLinkGetGlobalStringChar(AUDIOLINK_STRING_LOCALPLAYER, charIndex); 345 } 346 347 // Get a character from the master player name given a character index in the range [0; 31]. 348 // Returns a unsigned integer represented a unicode codepoint, i.e. UTF32. 349 uint AudioLinkGetMasterNameChar(uint charIndex) 350 { 351 return AudioLinkGetGlobalStringChar(AUDIOLINK_STRING_MASTER, charIndex); 352 } 353 354 // Get a character from the first custom string given a character index in the range [0; 31]. 355 // Returns a unsigned integer represented a unicode codepoint, i.e. UTF32. 356 uint AudioLinkGetCustomString1Char(uint charIndex) 357 { 358 return AudioLinkGetGlobalStringChar(AUDIOLINK_STRING_CUSTOM1, charIndex); 359 } 360 361 // Get a character from the second custom string given a character index in the range [0; 31]. 362 // Returns a unsigned integer represented a unicode codepoint, i.e. UTF32. 363 uint AudioLinkGetCustomString2Char(uint charIndex) 364 { 365 return AudioLinkGetGlobalStringChar(AUDIOLINK_STRING_CUSTOM2, charIndex); 366 } 367 368 // Returns the position of the AudioLink AudioSource in world space. 369 float4 AudioLinkGetAudioSourcePosition() 370 { 371 return float4(AudioLinkData(ALPASS_GENERALVU_SOURCE_POS).xyz,1); 372 } 373#endif