@@ -87,11 +87,12 @@ const sceneController = {
8787 else if ( name === 'Specturm' ) switchScene ( SpectrumScene ) ;
8888 else if ( name === 'V4' ) switchScene ( SparticleScene ) ;
8989 else if ( name === 'Vlow' ) switchScene ( PlaneScene ) ;
90+ else if ( name === 'Oscilloscope' ) switchScene ( RawVisual ) ;
9091 } }
9192
9293
9394
94- gui . add ( sceneController , 'currentScene' , [ 'Sphere' , 'Particles' , 'Specturm' , 'V4' , 'Vlow' ] )
95+ gui . add ( sceneController , 'currentScene' , [ 'Sphere' , 'Particles' , 'Specturm' , 'V4' , 'Vlow' , 'Oscilloscope' ] )
9596 . name ( 'Scene' )
9697 . onChange ( sceneController . swapScene ) ;
9798camera . position . set ( 6 , 8 , 14 ) ;
@@ -213,6 +214,8 @@ export function loadwhat(){
213214 if ( window . localStorage . scene == 'sc3' ) return SpectrumScene ( )
214215 if ( window . localStorage . scene == 'sc4' ) return SparticleScene ( )
215216 if ( window . localStorage . scene == 'sc5' ) return PlaneScene ( )
217+ if ( window . localStorage . scene == 'sc6' ) return RawVisual ( )
218+
216219}
217220export function initThreeScene ( ) {
218221window . localStorage . scene = 'sc1'
@@ -597,7 +600,7 @@ function loadpart(){
597600}
598601
599602export function destroyThreeScene ( ) {
600- cancelAnimationFrame ( animationId ) ;
603+ cancelAnimationFrame ( animationId ) ;
601604 // 1. Stop animation loop
602605 if ( gui ) {
603606 gui . destroy ( ) ;
@@ -618,9 +621,7 @@ export function destroyThreeScene() {
618621
619622 } ) ;
620623 }
621-
622- // 5. Optionally dispose audio
623-
624+ document . body . querySelectorAll ( '.disposable' ) . forEach ( ( node ) => node . remove ( ) )
624625 // 6. Optional: dispose renderer
625626 renderer ?. dispose ( ) ;
626627// 7. Dispose audio and analyser
@@ -662,7 +663,6 @@ export function SpectrumScene() {
662663 scene = new THREE . Scene ( ) ;
663664 gui = new GUI ( ) ;
664665 analyser = new THREE . AudioAnalyser ( sound , 64 ) ;
665- analyser . fftSize = 1024 ;
666666
667667 let settings = { pov : 36.0 , bloom : true } ;
668668 camera = new THREE . PerspectiveCamera ( settings . pov , aspect , 1 , 1000 ) ;
@@ -1741,8 +1741,8 @@ void main() {
17411741 const thnote = document . createElement ( 'div' ) ;
17421742 // secondnote.innerText = "Double click to lock";
17431743 thnote . innerText = "ESC to unlock" ;
1744- thnote . className = "notes" ;
1745- note . className = "notes" ;
1744+ thnote . className = "notes disposable " ;
1745+ note . className = "notes disposable " ;
17461746 // secondnote.className = "notes";
17471747 thnote . style . top = '20vh' ;
17481748 thnote . style . position = 'absolute' ;
@@ -1953,3 +1953,105 @@ void main() {
19531953 renderer . render ( scene , camera ) ;
19541954 }
19551955}
1956+
1957+ export function RawVisual ( ) {
1958+ window . localStorage . scene = 'sc6'
1959+ scene = new THREE . Scene ( )
1960+ camera = new THREE . PerspectiveCamera ( 60 , window . innerWidth / window . innerHeight , 0.1 , 1000 )
1961+
1962+ analyser = new THREE . AudioAnalyser ( sound , 256 ) ;
1963+ const bufferLength = analyser . analyser . frequencyBinCount ;
1964+ let dataArray = new Float32Array ( bufferLength )
1965+
1966+ gui = new GUI ( ) ;
1967+ renderer = new THREE . WebGLRenderer ( { antialias :true } )
1968+ renderer . setSize ( window . innerWidth , window . innerHeight )
1969+ main . appendChild ( renderer . domElement )
1970+
1971+ initControllers ( 'Oscilloscope' )
1972+
1973+
1974+ camera . position . z = 15
1975+
1976+ let buffer = new THREE . BufferGeometry ( )
1977+ buffer . setAttribute ( "position" , new THREE . BufferAttribute ( new Float32Array ( [
1978+ 1.0 , 1.0 , 0.0 ,
1979+ 1.0 , - 1.0 , 0.0 ,
1980+ - 1.0 , - 1.0 , 0.0 ,
1981+ - 1.0 , 1.0 , 0.0 ]
1982+ ) , 3 ) )
1983+ buffer . setIndex ( new THREE . BufferAttribute (
1984+ new Uint16Array ( [
1985+ 0 , 1 , 3 ,
1986+ 1 , 2 , 3
1987+ ] ) ,
1988+ 1
1989+ )
1990+ )
1991+
1992+ let mat = new THREE . ShaderMaterial ( {
1993+ uniforms :{
1994+ resolution : { value : new THREE . Vector2 ( window . innerWidth , window . innerHeight ) } ,
1995+ uAudio : { value : new Float32Array ( 128 ) }
1996+ } ,
1997+ side : THREE . DoubleSide ,
1998+ vertexShader : `
1999+ varying vec3 pos;
2000+ void main() {
2001+ pos = position;
2002+ gl_Position = vec4(position,1);
2003+ }
2004+ ` ,
2005+ fragmentShader : `
2006+ uniform vec2 resolution;
2007+ varying vec3 pos;
2008+ uniform float uAudio[128];
2009+
2010+ float getSample(float x){
2011+
2012+ float index = x * 127.0 / resolution.x;
2013+
2014+ int i0 = int(floor(index));
2015+ int i1 = int(ceil(index));
2016+
2017+ float t = fract(index);
2018+
2019+ return mix(uAudio[i0], uAudio[i1], t);
2020+ }
2021+ void main(){
2022+
2023+ float x = gl_FragCoord.x;
2024+ float y = gl_FragCoord.y / resolution.y;
2025+
2026+ float amp = getSample(x);
2027+
2028+ amp = 0.5 - amp * 0.5;
2029+
2030+ float thickness = 0.0004;
2031+
2032+ float line = abs(thickness / (amp - y));
2033+
2034+ gl_FragColor = vec4(vec3(line),1.0);
2035+ }
2036+ `
2037+ } )
2038+ scene . add ( new THREE . Mesh ( buffer , mat ) )
2039+ let frame = 0
2040+ function animate ( time ) {
2041+ frame ++
2042+ if ( analyser && frame % 4 == 0 ) {
2043+ analyser . analyser . getFloatTimeDomainData ( dataArray )
2044+ mat . uniforms . uAudio . value = dataArray
2045+ }
2046+ renderer . render ( scene , camera )
2047+ requestAnimationFrame ( animate )
2048+ }
2049+ animate ( )
2050+ window . addEventListener ( 'resize' , ( ) => {
2051+ camera . aspect = window . innerWidth / window . innerHeight ;
2052+ camera . updateProjectionMatrix ( ) ;
2053+ renderer . setSize ( window . innerWidth , window . innerHeight ) ;
2054+ renderer . setPixelRatio ( window . devicePixelRatio ) ;
2055+ mat . uniforms . resolution . value = new THREE . Vector2 ( window . innerWidth , window . innerHeight ) ;
2056+ } )
2057+ }
0 commit comments