11import type { Broadcast } from "../broadcast" ;
22import { Canvas } from "../canvas" ;
3+ import audioFragSource from "./audio.frag" ;
4+ import audioVertSource from "./audio.vert" ;
35import type { Camera } from "./camera" ;
4- import outlineFragSource from "./outline.frag?raw" ;
5- import outlineVertSource from "./outline.vert?raw" ;
6+ import type { MeshBuffer } from "./mesh" ;
67import { Attribute , Shader , Uniform1f , Uniform2f , Uniform3f , Uniform4f , UniformMatrix4fv } from "./shader" ;
78
8- export class OutlineRenderer {
9+ export class AudioRenderer {
910 #canvas: Canvas ;
1011 #program: Shader ;
11- #vao: WebGLVertexArrayObject ;
12- #positionBuffer: WebGLBuffer ;
13- #indexBuffer: WebGLBuffer ;
12+ #vaos = new Map < MeshBuffer , WebGLVertexArrayObject > ( ) ;
1413
1514 // Typed uniforms
1615 #u_projection: UniformMatrix4fv ;
@@ -24,13 +23,18 @@ export class OutlineRenderer {
2423 #u_color: Uniform3f ;
2524 #u_time: Uniform1f ;
2625 #u_finalAlpha: Uniform1f ;
26+ #u_dragPoint: Uniform2f ;
27+ #u_velocity: Uniform2f ;
28+ #u_dragStrength: Uniform1f ;
29+ #u_zoomDeform: Uniform1f ;
30+ #u_zoomCenter: Uniform2f ;
2731
2832 // Typed attributes
2933 #a_position: Attribute ;
3034
3135 constructor ( canvas : Canvas ) {
3236 this . #canvas = canvas ;
33- this . #program = new Shader ( canvas . gl , outlineVertSource , outlineFragSource ) ;
37+ this . #program = new Shader ( canvas . gl , audioVertSource , audioFragSource ) ;
3438
3539 // Initialize typed uniforms
3640 this . #u_projection = this . #program. createUniformMatrix4fv ( "u_projection" ) ;
@@ -44,63 +48,51 @@ export class OutlineRenderer {
4448 this . #u_color = this . #program. createUniform3f ( "u_color" ) ;
4549 this . #u_time = this . #program. createUniform1f ( "u_time" ) ;
4650 this . #u_finalAlpha = this . #program. createUniform1f ( "u_finalAlpha" ) ;
51+ this . #u_dragPoint = this . #program. createUniform2f ( "u_dragPoint" ) ;
52+ this . #u_velocity = this . #program. createUniform2f ( "u_velocity" ) ;
53+ this . #u_dragStrength = this . #program. createUniform1f ( "u_dragStrength" ) ;
54+ this . #u_zoomDeform = this . #program. createUniform1f ( "u_zoomDeform" ) ;
55+ this . #u_zoomCenter = this . #program. createUniform2f ( "u_zoomCenter" ) ;
4756
4857 // Initialize typed attributes
4958 this . #a_position = this . #program. createAttribute ( "a_position" ) ;
50-
51- const vao = this . #canvas. gl . createVertexArray ( ) ;
52- if ( ! vao ) throw new Error ( "Failed to create VAO" ) ;
53- this . #vao = vao ;
54-
55- const positionBuffer = this . #canvas. gl . createBuffer ( ) ;
56- if ( ! positionBuffer ) throw new Error ( "Failed to create position buffer" ) ;
57- this . #positionBuffer = positionBuffer ;
58-
59- const indexBuffer = this . #canvas. gl . createBuffer ( ) ;
60- if ( ! indexBuffer ) throw new Error ( "Failed to create index buffer" ) ;
61- this . #indexBuffer = indexBuffer ;
62-
63- this . #setupBuffers( ) ;
6459 }
6560
66- #setupBuffers( ) {
67- const gl = this . #canvas. gl ;
68-
69- // Quad vertices (0-1 range, will be scaled by bounds)
70- const positions = new Float32Array ( [
71- 0 ,
72- 0 , // Top-left
73- 1 ,
74- 0 , // Top-right
75- 1 ,
76- 1 , // Bottom-right
77- 0 ,
78- 1 , // Bottom-left
79- ] ) ;
61+ #getOrCreateVAO( mesh : MeshBuffer ) : WebGLVertexArrayObject {
62+ let vao = this . #vaos. get ( mesh ) ;
63+ if ( vao ) return vao ;
8064
81- // Indices for two triangles
82- const indices = new Uint16Array ( [ 0 , 1 , 2 , 0 , 2 , 3 ] ) ;
65+ const gl = this . #canvas. gl ;
66+ vao = gl . createVertexArray ( ) ;
67+ if ( ! vao ) throw new Error ( "Failed to create VAO" ) ;
8368
84- gl . bindVertexArray ( this . # vao) ;
69+ gl . bindVertexArray ( vao ) ;
8570
8671 // Position attribute
87- gl . bindBuffer ( gl . ARRAY_BUFFER , this . #positionBuffer) ;
88- gl . bufferData ( gl . ARRAY_BUFFER , positions , gl . STATIC_DRAW ) ;
72+ gl . bindBuffer ( gl . ARRAY_BUFFER , mesh . positionBuffer ) ;
8973 gl . enableVertexAttribArray ( this . #a_position. location ) ;
9074 gl . vertexAttribPointer ( this . #a_position. location , 2 , gl . FLOAT , false , 0 , 0 ) ;
9175
9276 // Index buffer
93- gl . bindBuffer ( gl . ELEMENT_ARRAY_BUFFER , this . #indexBuffer) ;
94- gl . bufferData ( gl . ELEMENT_ARRAY_BUFFER , indices , gl . STATIC_DRAW ) ;
77+ gl . bindBuffer ( gl . ELEMENT_ARRAY_BUFFER , mesh . indexBuffer ) ;
9578
9679 gl . bindVertexArray ( null ) ;
80+
81+ this . #vaos. set ( mesh , vao ) ;
82+ return vao ;
9783 }
9884
9985 render ( broadcast : Broadcast , camera : Camera , maxZ : number , now : DOMHighResTimeStamp ) {
10086 const gl = this . #canvas. gl ;
10187 const bounds = broadcast . bounds . peek ( ) ;
10288 const scale = broadcast . zoom . peek ( ) ;
10389 const volume = broadcast . audio . volume ;
90+ const dragPoint = broadcast . dragPoint . peek ( ) ;
91+ const deformVelocity = broadcast . deformVelocity ;
92+ const zoomCenter = broadcast . zoomCenter . peek ( ) ;
93+
94+ // Get or create VAO for this broadcast's mesh
95+ const vao = this . #getOrCreateVAO( broadcast . mesh ) ;
10496
10597 this . #program. use ( ) ;
10698
@@ -180,17 +172,27 @@ export class OutlineRenderer {
180172
181173 this . #u_color. set ( r , g , b ) ;
182174
175+ // Set drag deformation uniforms (using deformVelocity which decays separately)
176+ this . #u_dragPoint. set ( dragPoint . x , dragPoint . y ) ;
177+ this . #u_velocity. set ( deformVelocity . x , deformVelocity . y ) ;
178+ this . #u_dragStrength. set ( 0.5 ) ; // Halved for subtler effect
179+
180+ // Set zoom deformation uniforms
181+ this . #u_zoomDeform. set ( broadcast . zoomDeform ) ;
182+ this . #u_zoomCenter. set ( zoomCenter . x , zoomCenter . y ) ;
183+
183184 // Draw
184- gl . bindVertexArray ( this . # vao) ;
185- gl . drawElements ( gl . TRIANGLES , 6 , gl . UNSIGNED_SHORT , 0 ) ;
185+ gl . bindVertexArray ( vao ) ;
186+ gl . drawElements ( gl . TRIANGLES , broadcast . mesh . indexCount , gl . UNSIGNED_SHORT , 0 ) ;
186187 gl . bindVertexArray ( null ) ;
187188 }
188189
189190 close ( ) {
190191 const gl = this . #canvas. gl ;
191- gl . deleteVertexArray ( this . #vao) ;
192- gl . deleteBuffer ( this . #positionBuffer) ;
193- gl . deleteBuffer ( this . #indexBuffer) ;
192+ for ( const vao of this . #vaos. values ( ) ) {
193+ gl . deleteVertexArray ( vao ) ;
194+ }
195+ this . #vaos. clear ( ) ;
194196 this . #program. cleanup ( ) ;
195197 }
196198}
0 commit comments