Audio Visualiser
by SQShu
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My_Audio_Visualiser_SSQ</title>
<link href="https://fonts.googleapis.com/css?family=Saira" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div id="content">
<label for="thefile" class="file"> Choose an audio file
<input type="file" id="thefile" accept="audio/*" />
</label>
<audio id="audio" controls></audio>
<div id="container"></div>
</div>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js'></script>
<script src='https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.3/dat.gui.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/simplex-noise/2.3.0/simplex-noise.min.js'></script>
<script src="./script.js"></script>
</body>
</html>
CSS
:root {
--bgColor: rgba(255, 255, 255, 1);
--bgColorLight: rgb(141, 153, 174);
--textColor: rgb(237, 242, 244);
--textColorDark: rgb(0,0,0);
--paperColor: rgb(43, 45, 66);
--paperColorDark: rgb(43, 45, 66);
--shadowColorFaint: rgba(0,0,0,0.2);
}
::selected {
color: var(--textColorDark);
}
html,
body {
margin: 0;
padding: 0;
overflow: hidden;
height: 100vh;
width: 100vw;
background: var(--bgColor);
background: linear-gradient(135deg, var(--bgColor), var(--bgColorLight));
color: var(--textColor);
font-family: 'Saira', sans-serif;
position: relative;
}
* {
box-sizing: border-box;
transition: all 0.12s cubic-bezier(0.42, 0.54, 0.22, 1.26);
}
#canvas {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
audio {
position: fixed;
left: 10px;
bottom: -10px;
width: calc(100% - 20px);
}
audio.active {
bottom: 10px;
}
#thefile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: 1;
}
label.file {
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform: translate3d(-50%, -50%, 0);
padding: 1rem 2rem;
border-radius: 120px;
background: var(--paperColor);
color: var(--textColor);
font-size: 1.25em;
font-weight: 700;
box-shadow: 0 20px 60px var(--shadowColorFaint);
cursor: pointer;
}
label.file:hover {
background: var(--paperColorDark);
transform: translate3d(-50%, -55%, 0);
}
label.file:active {
background: var(--paperColorDark);
transform: translate3d(-50%, -45%, 0);
}
label.file.normal {
transform: translate3d(10%, 50%, 0);
padding: 0.2rem 2rem;
font-size: 1rem;
top: 0;
left: 0;
}
JavaScript
/* 1.13 把著色器顏色寫入了three.js中,留存的問題是:改寫的著色器無法正常顯示
原因1: 著色器中存在沒有uniform的變量
1.14 沒辦法,還是得自己寫新的著色器,尷尬了,明天看看能不能把mimic的Osc引入,把myWave給引入,不行就只能這樣了,唉
*/
function vertexShader() {
return `
//
// GLSL textureless classic 3D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson ([email protected])
// Version: 2011-10-11
//
// Many thanks to Ian McEwan of Ashima Arts for the
// ideas for permutation and gradient selection.
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec3 mod289(vec3 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec3 fade(vec3 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise
float cnoise(vec3 P)
{
vec3 Pi0 = floor(P); // Integer part for indexing
vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1
Pi0 = mod289(Pi0);
Pi1 = mod289(Pi1);
vec3 Pf0 = fract(P); // Fractional part for interpolation
vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
vec4 iy = vec4(Pi0.yy, Pi1.yy);
vec4 iz0 = Pi0.zzzz;
vec4 iz1 = Pi1.zzzz;
vec4 ixy = permute(permute(ix) + iy);
vec4 ixy0 = permute(ixy + iz0);
vec4 ixy1 = permute(ixy + iz1);
vec4 gx0 = ixy0 * (1.0 / 7.0);
vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
gx0 = fract(gx0);
vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
vec4 sz0 = step(gz0, vec4(0.0));
gx0 -= sz0 * (step(0.0, gx0) - 0.5);
gy0 -= sz0 * (step(0.0, gy0) - 0.5);
vec4 gx1 = ixy1 * (1.0 / 7.0);
vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
gx1 = fract(gx1);
vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
vec4 sz1 = step(gz1, vec4(0.0));
gx1 -= sz1 *...