@@ -357,7 +357,6 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
357
357
let width = this . image . width ;
358
358
let height = this . image . height ;
359
359
if ( this . image . height > 8000 || this . image . width > 8000 ) {
360
- console . log ( "too big, doing resize" ) ;
361
360
const scaleFactor = 8000 / Math . max ( this . image . width , this . image . height ) ;
362
361
width = Math . floor ( this . image . width * scaleFactor ) ;
363
362
height = Math . floor ( this . image . height * scaleFactor ) ;
@@ -369,8 +368,6 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
369
368
const ctx = canvas . getContext ( '2d' ) ;
370
369
ctx . drawImage ( this . image , 0 , 0 , width , height ) ;
371
370
372
- console . log ( "switched to canvas" , width , height ) ;
373
-
374
371
this . image . width = width ;
375
372
this . image . height = height ;
376
373
}
@@ -496,28 +493,24 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
496
493
this . drawGrid ( ) ;
497
494
}
498
495
499
- this . lastFogCount = this . fogOfWarShapes . length ;
500
496
this . updateFog = false ;
501
497
this . updateGrid = false ;
502
498
this . doMove = false ;
503
499
this . animationId = window . requestAnimationFrame ( this . nextFrame . bind ( this ) ) ;
504
500
}
505
501
506
502
private buildFogMask ( ) {
503
+ if ( this . lastFogCount === this . fogOfWarShapes . length ) return ;
504
+
507
505
this . gl . useProgram ( this . maskProgram . get_program ( ) ) ;
508
506
this . gl . bindFramebuffer ( this . gl . FRAMEBUFFER , this . maskProgram . get_fbo ( ) ) ;
509
507
this . gl . viewport ( 0 , 0 , this . image . width , this . image . height ) ;
510
508
this . gl . clearColor ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
511
509
this . gl . clear ( this . gl . COLOR_BUFFER_BIT ) ;
512
-
513
- //if (this.lastFogCount === this.fogOfWarShapes.length) {
514
- //return;
515
- //}
516
-
517
- // Mask
518
- this . gl . bindBuffer ( this . gl . ARRAY_BUFFER , this . maskProgram . get_buffer ( "verticies" ) ) ;
519
510
this . gl . bindTexture ( this . gl . TEXTURE_2D , this . maskProgram . get_texture ( ) ) ;
520
-
511
+ this . gl . bindBuffer ( this . gl . ARRAY_BUFFER , this . maskProgram . get_buffer ( "verticies" ) ) ;
512
+ // Mask
513
+ let allVertices = [ ] ;
521
514
for ( let shape of this . fogOfWarShapes ) {
522
515
let vertices = [ ] ;
523
516
@@ -540,13 +533,10 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
540
533
const br = this . world_to_clip ( xMax , yMin , this . image . width , this . image . height ) ; // bottom-right
541
534
const tr = this . world_to_clip ( xMax , yMax , this . image . width , this . image . height ) ; // top-right
542
535
543
- // Create an array of vertices (using TRIANGLE_STRIP order):
544
- vertices = [
545
- bl [ 0 ] , bl [ 1 ] ,
546
- tl [ 0 ] , tl [ 1 ] ,
547
- br [ 0 ] , br [ 1 ] ,
548
- tr [ 0 ] , tr [ 1 ] ,
549
- ] ;
536
+ vertices . push (
537
+ bl [ 0 ] , bl [ 1 ] , br [ 0 ] , br [ 1 ] , tr [ 0 ] , tr [ 1 ] , // Triangle 1
538
+ bl [ 0 ] , bl [ 1 ] , tr [ 0 ] , tr [ 1 ] , tl [ 0 ] , tl [ 1 ] // Triangle 2
539
+ ) ;
550
540
}
551
541
break ;
552
542
case "poly" :
@@ -557,45 +547,31 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
557
547
flatVertices . push ( clip [ 0 ] , clip [ 1 ] ) ;
558
548
} ) ;
559
549
560
- // Triangulate the polygon using earcut.
561
- // Since there are no holes, the second parameter is an empty array.
562
- const indices = earcut ( flatVertices , /* holeIndices */ [ ] ) ;
563
-
564
- // Now, create a vertices array that lists triangles:
565
- // (You can either use indices with an ELEMENT_ARRAY_BUFFER or expand them.)
566
- let verticesTriangles = [ ] ;
567
- indices . forEach ( idx => {
568
- verticesTriangles . push ( flatVertices [ 2 * idx ] , flatVertices [ 2 * idx + 1 ] ) ;
569
- } ) ;
570
-
571
- vertices = verticesTriangles ;
550
+ const indices = earcut ( flatVertices ) ;
551
+ for ( let i = 0 ; i < indices . length ; i ++ ) {
552
+ const idx = indices [ i ] ;
553
+ vertices . push ( flatVertices [ 2 * idx ] , flatVertices [ 2 * idx + 1 ] ) ;
554
+ }
572
555
}
573
556
break ;
574
557
}
575
558
576
- this . gl . bufferData ( this . gl . ARRAY_BUFFER , new Float32Array ( vertices ) , this . gl . STATIC_DRAW ) ;
559
+ allVertices . push ( ...vertices ) ;
560
+ }
577
561
578
- this . gl . enableVertexAttribArray ( this . maskProgram . get_attribute ( "a_position" ) ) ;
579
- this . gl . vertexAttribPointer (
580
- this . maskProgram . get_attribute ( "a_position" ) ,
581
- 2 ,
582
- this . gl . FLOAT ,
583
- false ,
584
- 0 ,
585
- 0
586
- ) ;
562
+ this . gl . bufferData ( this . gl . ARRAY_BUFFER , new Float32Array ( allVertices ) , this . gl . STATIC_DRAW ) ;
587
563
588
- switch ( shape . type ) {
589
- case "rect" :
590
- this . gl . drawArrays ( this . gl . TRIANGLE_STRIP , 0 , 4 ) ;
591
- break ;
592
- case "poly" :
593
- this . gl . drawArrays ( this . gl . TRIANGLES , 0 , vertices . length / 2 ) ;
594
- break ;
595
- default :
596
- break ;
597
- }
598
- }
564
+ this . gl . enableVertexAttribArray ( this . maskProgram . get_attribute ( "a_position" ) ) ;
565
+ this . gl . vertexAttribPointer (
566
+ this . maskProgram . get_attribute ( "a_position" ) ,
567
+ 2 ,
568
+ this . gl . FLOAT ,
569
+ false ,
570
+ 0 ,
571
+ 0
572
+ ) ;
573
+ this . gl . drawArrays ( this . gl . TRIANGLES , 0 , allVertices . length / 2 ) ;
574
+ this . lastFogCount = this . fogOfWarShapes . length ;
599
575
600
576
// reset
601
577
this . gl . bindTexture ( this . gl . TEXTURE_2D , null ) ;
@@ -615,11 +591,12 @@ export default class TableCanvas extends SuperComponent<ITableCanvas> {
615
591
this . gl . uniform2f ( this . fogProgram . get_uniform ( "u_resolution" ) , this . w , this . h ) ;
616
592
this . gl . uniform2f ( this . fogProgram . get_uniform ( "u_translation" ) , this . pos . x , this . pos . y ) ;
617
593
this . gl . uniform2f ( this . fogProgram . get_uniform ( "u_scale" ) , this . tabletop . zoom , this . tabletop . zoom ) ;
618
- let color = "#fafafaFF"
594
+ let r = 0.98 , g = 0.98 , b = 0.98 , a = 1 ;
619
595
if ( window . matchMedia && window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) {
620
- color = "#09090bFF"
596
+ r = 0.0 ;
597
+ g = 0.0 ;
598
+ b = 0.0 ;
621
599
}
622
- const [ r , g , b , a ] = this . hex_to_rgbaf ( color ) ;
623
600
this . gl . uniform4f ( this . fogProgram . get_uniform ( "u_color" ) , r , g , b , a ) ;
624
601
this . gl . uniform1i ( this . fogProgram . get_uniform ( "u_isGM" ) , room . isGM ? 1 : 0 ) ;
625
602
0 commit comments