diff --git a/src/Music.ts b/src/Music.ts index 1511cd3..7c28c2c 100644 --- a/src/Music.ts +++ b/src/Music.ts @@ -251,22 +251,24 @@ export class Music { const sixteenBarLength = 3840.0 / MUSIC_BPM; program.attribute( 'off', this.__bufferOff, 1 ); - program.uniform1f( 'bpm', MUSIC_BPM ); - program.uniform1f( 'bufferLength', MUSIC_BUFFER_LENGTH ); - program.uniform1f( '_deltaSample', 1.0 / this.audio.sampleRate ); - program.uniform4f( + program.uniform( 'bpm', '1f', MUSIC_BPM ); + program.uniform( 'bufferLength', '1f', MUSIC_BUFFER_LENGTH ); + program.uniform( '_deltaSample', '1f', 1.0 / this.audio.sampleRate ); + program.uniform( 'timeLength', + '4f', beatLength, barLength, sixteenBarLength, 1E16 ); - program.uniform4f( + program.uniform( '_timeHead', + '4f', time % beatLength, time % barLength, time % sixteenBarLength, - time + time, ); program.uniformTexture( 'samplerRandom', randomTextureStatic.texture ); diff --git a/src/entities/CameraEntity.ts b/src/entities/CameraEntity.ts index 45c1dde..da06b9d 100644 --- a/src/entities/CameraEntity.ts +++ b/src/entities/CameraEntity.ts @@ -65,7 +65,7 @@ export class CameraEntity extends Entity { onUpdate: () => { const cameraView = this.transform.matrix.inverse!; - aoMaterial.addUniformVector( + aoMaterial.addUniformMatrixVector( 'cameraPV', 'Matrix4fv', this.camera.projectionMatrix.multiply( @@ -118,13 +118,13 @@ export class CameraEntity extends Entity { const cameraView = this.transform.matrix.inverse!; - shadingMaterial.addUniformVector( + shadingMaterial.addUniformMatrixVector( 'cameraView', 'Matrix4fv', cameraView.elements ); - shadingMaterial.addUniformVector( + shadingMaterial.addUniformMatrixVector( 'cameraPV', 'Matrix4fv', this.camera.projectionMatrix.multiply( @@ -164,7 +164,7 @@ export class CameraEntity extends Entity { ...light.color ); - shadingMaterial.addUniformVector( + shadingMaterial.addUniformMatrixVector( 'lightPV', 'Matrix4fv', light.camera.projectionMatrix.multiply( diff --git a/src/entities/Raymarcher.ts b/src/entities/Raymarcher.ts index dd12490..925afd8 100644 --- a/src/entities/Raymarcher.ts +++ b/src/entities/Raymarcher.ts @@ -79,7 +79,7 @@ export class Raymarcher extends Entity { event.camera.far ); - material.addUniformVector( + material.addUniformMatrixVector( 'inversePVM', 'Matrix4fv', event.projectionMatrix diff --git a/src/globals/automaton.ts b/src/globals/automaton.ts index da21f83..9a25d67 100644 --- a/src/globals/automaton.ts +++ b/src/globals/automaton.ts @@ -2,8 +2,8 @@ import { Automaton } from '@fms-cat/automaton'; import { AutomatonWithGUI } from '@fms-cat/automaton-with-gui'; import { fxDefinitions } from '../automaton-fxs/fxDefinitions'; import { getDivAutomaton } from './dom'; -import { music } from './music'; import automatonData from '../automaton.json'; +import type { Music } from '../Music'; export const automaton = ( () => { if ( process.env.DEV ) { @@ -18,13 +18,6 @@ export const automaton = ( () => { }, ); - automatonWithGUI.on( 'play', () => { music.isPlaying = true; } ); - automatonWithGUI.on( 'pause', () => { music.isPlaying = false; } ); - automatonWithGUI.on( 'seek', ( { time } ) => { - music.time = Math.max( 0.0, time ); - automatonWithGUI.reset(); - } ); - if ( module.hot ) { module.hot.accept( '../automaton.json', () => { // we probably don't need this feature for now... @@ -44,4 +37,20 @@ export const automaton = ( () => { } } )(); +/** + * Since automaton and music try to reference each other... + */ +export function automatonSetupMusic( music: Music ): void { + if ( process.env.DEV ) { + const automatonWithGUI = automaton as AutomatonWithGUI; + + automatonWithGUI.on( 'play', () => { music.isPlaying = true; } ); + automatonWithGUI.on( 'pause', () => { music.isPlaying = false; } ); + automatonWithGUI.on( 'seek', ( { time } ) => { + music.time = Math.max( 0.0, time ); + automatonWithGUI.reset(); + } ); + } +} + export const auto = automaton.auto; diff --git a/src/globals/music.ts b/src/globals/music.ts index 816c629..a7318a8 100644 --- a/src/globals/music.ts +++ b/src/globals/music.ts @@ -1,5 +1,7 @@ import { Music } from '../Music'; +import { automatonSetupMusic } from './automaton'; import { glCat } from './canvas'; export const audio = new AudioContext(); export const music = new Music( glCat, audio ); +automatonSetupMusic( music ); diff --git a/src/heck/Material.ts b/src/heck/Material.ts index 2dbbe0d..0b677d8 100644 --- a/src/heck/Material.ts +++ b/src/heck/Material.ts @@ -1,4 +1,4 @@ -import { GLCatProgram, GLCatProgramLinkOptions, GLCatProgramUniformType, GLCatTexture, GLCatTextureCubemap } from '@fms-cat/glcat-ts'; +import { GLCatProgram, GLCatProgramLinkOptions, GLCatProgramUniformMatrixVectorType, GLCatProgramUniformType, GLCatProgramUniformVectorType, GLCatTexture, GLCatTextureCubemap } from '@fms-cat/glcat-ts'; import { Geometry } from './Geometry'; import { RenderTarget } from './RenderTarget'; import { SHADERPOOL } from './ShaderPool'; @@ -31,11 +31,19 @@ export class Material { protected __uniformVectors: { [ name: string ]: { - type: GLCatProgramUniformType; + type: GLCatProgramUniformVectorType; value: Float32List | Int32List; }; } = {}; + protected __uniformMatrixVectors: { + [ name: string ]: { + type: GLCatProgramUniformMatrixVectorType; + value: Float32List | Int32List; + transpose?: boolean; + }; + } = {}; + protected __uniformTextures: { [ name: string ]: { texture: GLCatTexture | null; @@ -109,12 +117,20 @@ export class Material { public addUniformVector( name: string, - type: GLCatProgramUniformType, + type: GLCatProgramUniformVectorType, value: Float32List | Int32List ): void { this.__uniformVectors[ name ] = { type, value }; } + public addUniformMatrixVector( + name: string, + type: GLCatProgramUniformMatrixVectorType, + value: Float32List | Int32List + ): void { + this.__uniformMatrixVectors[ name ] = { type, value }; + } + public addUniformTexture( name: string, texture: GLCatTexture | null ): void { this.__uniformTextures[ name ] = { texture }; } @@ -134,6 +150,12 @@ export class Material { program.uniformVector( name, type, value ); } ); + Object.entries( this.__uniformMatrixVectors ).forEach( + ( [ name, { type, value, transpose } ] ) => { + program.uniformMatrixVector( name, type, value, transpose ); + } + ); + Object.entries( this.__uniformTextures ).forEach( ( [ name, { texture } ] ) => { program.uniformTexture( name, texture ); } ); diff --git a/src/heck/components/Mesh.ts b/src/heck/components/Mesh.ts index c5d9dee..255cc4f 100644 --- a/src/heck/components/Mesh.ts +++ b/src/heck/components/Mesh.ts @@ -58,16 +58,16 @@ export class Mesh extends Component { material.setUniforms(); - program.uniform1f( 'time', event.time ); - program.uniform1f( 'frameCount', event.frameCount ); - program.uniform2f( 'resolution', event.renderTarget.width, event.renderTarget.height ); - program.uniform3f( 'cameraPos', ...event.cameraTransform.position.elements ); - program.uniform2f( 'cameraNearFar', event.camera.near, event.camera.far ); + program.uniform( 'time', '1f', event.time ); + program.uniform( 'frameCount', '1f', event.frameCount ); + program.uniform( 'resolution', '2f', event.renderTarget.width, event.renderTarget.height ); + program.uniform( 'cameraPos', '3f', ...event.cameraTransform.position.elements ); + program.uniform( 'cameraNearFar', '2f', event.camera.near, event.camera.far ); - program.uniformMatrix4fv( 'normalMatrix', event.globalTransform.matrix.inverse!.transpose.elements ); - program.uniformMatrix4fv( 'modelMatrix', event.globalTransform.matrix.elements ); - program.uniformMatrix4fv( 'viewMatrix', event.viewMatrix.elements ); - program.uniformMatrix4fv( 'projectionMatrix', event.projectionMatrix.elements ); + program.uniformMatrixVector( 'normalMatrix', 'Matrix4fv', event.globalTransform.matrix.inverse!.transpose.elements ); + program.uniformMatrixVector( 'modelMatrix', 'Matrix4fv', event.globalTransform.matrix.elements ); + program.uniformMatrixVector( 'viewMatrix', 'Matrix4fv', event.viewMatrix.elements ); + program.uniformMatrixVector( 'projectionMatrix', 'Matrix4fv', event.projectionMatrix.elements ); this.geometry.draw(); } diff --git a/src/heck/components/Quad.ts b/src/heck/components/Quad.ts index e474551..3776205 100644 --- a/src/heck/components/Quad.ts +++ b/src/heck/components/Quad.ts @@ -45,11 +45,11 @@ export class Quad extends Component { const program = this.material.program; - program.uniform1f( 'time', event.time ); - program.uniform1f( 'deltaTime', event.deltaTime ); - program.uniform1f( 'frameCount', event.frameCount ); - program.uniform2f( 'resolution', this.target.width, this.target.height ); - program.uniform4f( 'range', ...this.range ); + program.uniform( 'time', '1f', event.time ); + program.uniform( 'deltaTime', '1f', event.deltaTime ); + program.uniform( 'frameCount', '1f', event.frameCount ); + program.uniform( 'resolution', '2f', this.target.width, this.target.height ); + program.uniform( 'range', '4f', ...this.range ); quadGeometry.draw(); }