From e942b6f0bb15551aa96bcff3a992ff2ad4220221 Mon Sep 17 00:00:00 2001 From: FMS-Cat Date: Wed, 17 Mar 2021 11:11:42 +0900 Subject: [PATCH] feature: PORTER FUCKING ROBINSON --- package.json | 1 - shader-minifier-tips.md | 24 ++++ src/Music.ts | 52 +++++-- src/config.ts | 2 +- src/opus.d.ts | 4 + src/samples.opus | Bin 0 -> 2390 bytes src/shaders/music.vert | 303 ++++++++++++++++++++++++++++++++++++++-- webpack.config.js | 8 +- yarn.lock | 8 -- 9 files changed, 369 insertions(+), 33 deletions(-) create mode 100644 src/opus.d.ts create mode 100644 src/samples.opus diff --git a/package.json b/package.json index 632f75a..b87c167 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "glslify-loader": "^2.0.0", "html-webpack-plugin": "^5.3.0", "loader-utils": "^2.0.0", - "raw-loader": "^4.0.2", "schema-utils": "^3.0.0", "tempy": "^1.0.0", "ts-loader": "^8.0.17", diff --git a/shader-minifier-tips.md b/shader-minifier-tips.md index 439a9cd..968ff0b 100644 --- a/shader-minifier-tips.md +++ b/shader-minifier-tips.md @@ -122,3 +122,27 @@ const float TAU = 6.283185307; ``` You might want to also avoid macros with arguments for the same reason. + +## Hentai Preprocessors + +You can't use your favorite `beat` preprocessor. damn. + +```glsl +// won't compile +#define beat *60.0/BPM + +t = mod( time, 2.0 beat ); + +// use this instead +const float BEAT = 60.0 / BPM; + +t = mod( time, 2.0 * BEAT ); +``` + +### Error Examples + +``` + int prog = int( mod( time.z / ( 8.0 beat ), 8.0 ) ); + ^ +Expecting: infix operator, postfix operator, ',' or ';' +``` diff --git a/src/Music.ts b/src/Music.ts index b456041..6bad0bc 100644 --- a/src/Music.ts +++ b/src/Music.ts @@ -1,7 +1,10 @@ -import { GLCat, GLCatBuffer, GLCatProgram, GLCatTransformFeedback } from '@fms-cat/glcat-ts'; +import { GLCat, GLCatBuffer, GLCatProgram, GLCatTexture, GLCatTransformFeedback } from '@fms-cat/glcat-ts'; import { MUSIC_BPM, MUSIC_BUFFER_LENGTH } from './config'; import { Pool } from './utils/Pool'; import musicVert from './shaders/music.vert'; +import { gl, glCat } from './globals/canvas'; +import samplesOpus from './samples.opus'; +import { randomTextureStatic } from './globals/randomTexture'; const discardFrag = '#version 300 es\nvoid main(){discard;}'; @@ -10,7 +13,7 @@ export class Music { public time: number; public deltaTime: number; public audio: AudioContext; - public glCat: GLCat; + public samples?: GLCatTexture; private __program: GLCatProgram; private __bufferOff: GLCatBuffer; @@ -24,9 +27,6 @@ export class Music { private __prevBufferSource: AudioBufferSourceNode | null = null; constructor( glCat: GLCat, audio: AudioContext ) { - this.glCat = glCat; - const { gl } = glCat; - this.audio = audio; // == yoinked from wavenerd-deck =============================================================== @@ -70,6 +70,8 @@ export class Music { audio.createBuffer( 2, MUSIC_BUFFER_LENGTH, audio.sampleRate ), ] ); + this.__loadSamples(); + // == I think these are gonna be required ====================================================== this.isPlaying = false; this.time = 0.0; @@ -83,11 +85,11 @@ export class Music { './shaders/music.vert', ], async () => { - const program = await this.glCat.lazyProgramAsync( + const program = await glCat.lazyProgramAsync( musicVert, discardFrag, { transformFeedbackVaryings: [ 'outL', 'outR' ] }, - ).catch( ( error ) => { + ).catch( ( error: any ) => { console.error( error ); return null; } ); @@ -134,9 +136,35 @@ export class Music { this.__prevAudioTime = genTime; } + private async __loadSamples(): Promise { + const inputBuffer = await fetch( samplesOpus ).then( ( res ) => res.arrayBuffer() ); + const audioBuffer = await this.audio.decodeAudioData( inputBuffer ); + + const buffer = new Float32Array( 96000 ); + + const data = audioBuffer.getChannelData( 0 ); + for ( let i = 0; i < audioBuffer.length; i ++ ) { + buffer[ i ] = data[ i ]; + } + + const texture = glCat.createTexture()!; + texture.setTextureFromArray( + 6000, + 16, + buffer, + { + internalformat: gl.R32F, + format: gl.RED, + type: gl.FLOAT, + } + ); + texture.textureFilter( gl.LINEAR ); + + this.samples = texture; + } + private __prepareBuffer( buffer: AudioBuffer ): void { - const { glCat, time } = this; - const { gl } = this.glCat; + const { time } = this; const program = this.__program; const beatLength = 60.0 / MUSIC_BPM; @@ -161,6 +189,12 @@ export class Music { time ); + program.uniformTexture( 'samplerRandom', randomTextureStatic.texture ); + + if ( this.samples ) { + program.uniformTexture( 'samplerSamples', this.samples ); + } + glCat.useProgram( program, () => { glCat.bindTransformFeedback( this.__transformFeedback, () => { gl.enable( gl.RASTERIZER_DISCARD ); diff --git a/src/config.ts b/src/config.ts index db911ed..482f91c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,5 +3,5 @@ export const STATIC_RANDOM_RESOLUTION = [ 2048, 2048 ], AO_RESOLUTION_RATIO = 1.0, RESOLUTION = [ 1280, 720 ], - MUSIC_BPM = 140, + MUSIC_BPM = 180, MUSIC_BUFFER_LENGTH = 16384; diff --git a/src/opus.d.ts b/src/opus.d.ts new file mode 100644 index 0000000..ed91dd7 --- /dev/null +++ b/src/opus.d.ts @@ -0,0 +1,4 @@ +declare module '*.opus' { + const uri: string; + export default uri; +} diff --git a/src/samples.opus b/src/samples.opus new file mode 100644 index 0000000000000000000000000000000000000000..2034702fcaaf9bdbf621ef8b47db97c7df3bb638 GIT binary patch literal 2390 zcmX|Dc{tSDAD*$a5ONc_QI=`UVl0C(7z`~Mgckc4WF#Tmr7z0b$T})9Uy~N4MOmte zJ7aB%EFs2L5?Lc#gvNfy{oUvHKF@j1^1kQ&ob&$mvGwse4H1I;2V%>t<<>G;3K9X` zV;d9}dfeNS3KhabV=MlZ*lx-Hk1aV=uv1c6S%;cz$EShW^)eLWJwEauLh-!8o=$U9vwQ#KjXkWg^H9C&9R>>N%%df?+zC3>I3_&Ti zGCjc$&q@I!;(F=VeNedot&|-Pr7>4i%-ZiA&Gw}%1RP7F_#N`~2v~1xIcC{%IiVjU138xs4``;IE+B6@RTQdM7@JHLyFgoZ$T>=By&)-4OcSE=uEu|jo z>l`UV$A3BNV!o$Y8K4z@#>t)KwHK~P8f%onV%XZP$4oBf$gWl-w|VNQp$Ff9qL*gG zAz;EPK>Mf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SC1=m&CX!QsolMa9(s<-kIP9WZTq9j(&`(&E)zKYbu z{x}0m8NKZ{NKv`4uabf~g#^GL)%|;~kwq0YdJBm^`;`bwGKS(^5HA98beK{Zk5eoH zK(~k6i;3eVury~3Oy6~)gSrh{QI(`8-OWn~%Wv2OK+*)SplNY(=Lr7@+^-JPUK?W= ztr7=$G8NiEY+Z>bpX4Z}Pd$I?0)WIE1i5E+IAA)=8o3G~UvX^J&_9B~%3djB^)~%h zJ|{5K=LBeN-QaFc#=>-T{7LSe+4k9`7+!DKuo(T(`shS>6VqgEpZV)uUaKVQ7Ll)&dWUJIreS-^^5m%s3Yvz(@2xdgd=y?+ zO|b^*=_dyPTBPpJ>U}uRx#8^UK82MVB;t#|^b_67u?Le1?{0ULfn+7XRlBR6wcqm} z-c3Oq==>=JWIvP%7SX;pY~?WV(eGUIyWm(1XV{c$H9ZU{b?Gi3k z7N{tU7aMMxS^4xmlC8CK>{_ExR`c0Et}GwBQcPQe9q)Sip@8s)HneZ$6RvdP?;ZIzrdYe}vLm$s`G(nM35(3 zla#NnPLE1n+hiF??NTJwaotr?|! zOfUW1p?5XG)|1aGlG^q@E|I_fSwEF5clV6U@7lY0QyJP8_C?;j@qb$p5q71Z{}5oS z6%iK9HPaKeP&lwH?8G;ZK)5gNJzg { }, { test: /\.(glsl|frag|vert)$/, + type: 'asset/source', use: [ - 'raw-loader', // shader minifier is kinda jej // want to try it works well in development phase { @@ -52,12 +52,16 @@ module.exports = ( env, argv ) => { 'glslify-loader', ], }, + { + test: /\.opus$/, + type: 'asset/inline', + }, { test: /\.tsx?$/, exclude: /node_modules/, use: [ 'ts-loader', - ] + ], }, ], }, diff --git a/yarn.lock b/yarn.lock index 4d7f9d7..2462f9d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3256,14 +3256,6 @@ raw-body@2.4.0: iconv-lite "0.4.24" unpipe "1.0.0" -raw-loader@^4.0.2: - version "4.0.2" - resolved "https://registry.yarnpkg.com/raw-loader/-/raw-loader-4.0.2.tgz#1aac6b7d1ad1501e66efdac1522c73e59a584eb6" - integrity sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA== - dependencies: - loader-utils "^2.0.0" - schema-utils "^3.0.0" - "readable-stream@>=1.0.33-1 <1.1.0-0": version "1.0.34" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"