@@ -4,6 +4,7 @@ import { CopyShader, get_copy_shader, get_icon_shader, IconShader, ShaderHolder
4
4
import { RenderingCmd } from "../../player/rendering/commands" ;
5
5
import { ViewportElement } from "../viewport" ;
6
6
import { render_maptext } from "./maptext" ;
7
+ import { BlendMode } from "../../misc/constants" ;
7
8
8
9
export class DemoPlayerGlHolder {
9
10
gl : WebGLRenderingContext ;
@@ -16,6 +17,7 @@ export class DemoPlayerGlHolder {
16
17
max_texture_size : number ;
17
18
copy_framebuffer : WebGLFramebuffer ;
18
19
white_texture : WebGLTexture ;
20
+ canvas_copy : WebGLTexture ;
19
21
20
22
shader : IconShader ;
21
23
shader_matrix : IconShader ;
@@ -27,7 +29,7 @@ export class DemoPlayerGlHolder {
27
29
if ( this . gl2 ) {
28
30
this . gl = this . gl2 ;
29
31
} else {
30
- let gl = canvas . getContext ( "webgl" , { desynchronized : true } ) ;
32
+ let gl = canvas . getContext ( "webgl" , { desynchronized : true , alpha : false } ) ;
31
33
if ( ! gl ) throw new Error ( "Could not initialize WebGL" ) ;
32
34
this . gl = gl ;
33
35
}
@@ -49,11 +51,15 @@ export class DemoPlayerGlHolder {
49
51
50
52
this . white_texture = not_null ( gl . createTexture ( ) ) ;
51
53
gl . bindTexture ( gl . TEXTURE_2D , this . white_texture ) ;
52
- gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , 1 , 1 , 0 , gl . RGBA , gl . UNSIGNED_BYTE , new Uint8Array ( [ 255 , 255 , 255 , 255 ] ) ) ;
54
+ gl . texImage2D ( gl . TEXTURE_2D , 0 , gl . RGBA , 1 , 1 , 0 , gl . RGBA , gl . UNSIGNED_BYTE , new Uint8Array ( [ 0 , 0 , 0 , 0 ] ) ) ;
53
55
gl . bindTexture ( gl . TEXTURE_2D , null ) ;
54
56
this . copy_framebuffer = not_null ( gl . createFramebuffer ( ) ) ;
55
57
this . max_texture_size = Math . min ( gl . getParameter ( gl . MAX_TEXTURE_SIZE ) , 32768 ) ;
56
58
59
+ this . canvas_copy = not_null ( gl . createTexture ( ) ) ;
60
+ gl . activeTexture ( gl . TEXTURE1 ) ;
61
+ gl . bindTexture ( gl . TEXTURE_2D , this . canvas_copy ) ;
62
+
57
63
this . square_buffer = not_null ( gl . createBuffer ( ) ) ;
58
64
gl . bindBuffer ( gl . ARRAY_BUFFER , this . square_buffer ) ;
59
65
gl . bufferData ( gl . ARRAY_BUFFER , new Float32Array ( [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 ] ) , gl . STATIC_DRAW ) ;
@@ -169,7 +175,7 @@ export class DemoPlayerGlHolder {
169
175
}
170
176
}
171
177
gl . bindTexture ( gl . TEXTURE_2D , null ) ;
172
- } else if ( cmd . cmd == "atlestexcopywithin " ) {
178
+ } else if ( cmd . cmd == "atlastexcopywithin " ) {
173
179
let tex = not_null ( this . atlas_textures [ cmd . index ] ) ;
174
180
let shader = this . shader_copy ;
175
181
this . set_shader ( shader ) ;
@@ -257,6 +263,8 @@ export class DemoPlayerGlHolder {
257
263
ia . vertexAttribDivisorANGLE ( shader . a_layer , 1 ) ;
258
264
ia . drawArraysInstancedANGLE ( gl . TRIANGLES , 0 , 6 , cmd . num_elements ) ;
259
265
gl . deleteBuffer ( buf ) ;
266
+ // gl.activeTexture(gl.TEXTURE0 + 1);
267
+ // gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, gl.canvas);
260
268
} else if ( cmd . cmd == "copytoviewport" ) {
261
269
let this_viewport_pixel = curr_viewport_pixel ;
262
270
let this_viewport = curr_viewport ;
@@ -338,26 +346,54 @@ export class DemoPlayerGlHolder {
338
346
// this one was fun - I made test cases in BYOND and took screenshots and tried to reverse-engineer the blending equations from that.
339
347
// fun fact BYOND uses premultiplied alpha. However, when you
340
348
set_blend_mode ( blend_mode : number ) : void {
341
- if ( blend_mode == 0 ) blend_mode = 1 ;
349
+ // if(blend_mode == 0) blend_mode = 1;
342
350
if ( blend_mode == this . curr_blend_mode ) return ;
343
351
this . curr_blend_mode = blend_mode ;
344
352
const gl = this . gl ;
345
- if ( blend_mode == 2 ) { // BLEND_ADD
346
- gl . blendEquation ( gl . FUNC_ADD ) ;
347
- gl . blendFuncSeparate ( gl . ONE , gl . ONE , gl . ONE , gl . ONE_MINUS_SRC_ALPHA ) ;
348
- } else if ( blend_mode == 3 ) { // BLEND_SUBTRACT
349
- gl . blendEquation ( gl . FUNC_REVERSE_SUBTRACT ) ;
350
- gl . blendFuncSeparate ( gl . ONE , gl . ONE , gl . ZERO , gl . ONE ) ;
351
- } else if ( blend_mode == 4 ) { // BLEND_MULTIPLY
352
- gl . blendEquation ( gl . FUNC_ADD ) ;
353
- gl . blendFunc ( gl . DST_COLOR , gl . ONE_MINUS_SRC_ALPHA ) ; // fun fact if you do the math everything cancels out so that the destination alpha doesn't change at all.
354
- } else if ( blend_mode == 5 ) { // BLEND_INSET_OVERLAY
355
- // TODO figure out if this is actually right
356
- gl . blendEquation ( gl . FUNC_ADD ) ;
357
- gl . blendFuncSeparate ( gl . ONE , gl . ONE_MINUS_SRC_ALPHA , gl . ZERO , gl . ONE )
358
- } else { // BLEND_OVERLAY or BLEND_DEFAULT
359
- gl . blendEquation ( gl . FUNC_ADD ) ;
360
- gl . blendFunc ( gl . ONE , gl . ONE_MINUS_SRC_ALPHA ) ;
353
+ switch ( blend_mode ) {
354
+ case BlendMode . DEFAULT : {
355
+ gl . blendEquation ( gl . FUNC_ADD ) ;
356
+ gl . blendFunc ( gl . ONE , gl . ONE_MINUS_SRC_ALPHA ) ;
357
+ break ;
358
+ }
359
+ case BlendMode . ADD : {
360
+ gl . blendEquation ( gl . FUNC_ADD ) ;
361
+ gl . blendFuncSeparate ( gl . ONE , gl . ONE , gl . ONE , gl . ONE_MINUS_SRC_ALPHA ) ;
362
+ break ;
363
+ }
364
+ case BlendMode . SUBTRACT : {
365
+ gl . blendEquation ( gl . FUNC_REVERSE_SUBTRACT ) ;
366
+ gl . blendFuncSeparate ( gl . ONE , gl . ONE , gl . ZERO , gl . ONE ) ;
367
+ break ;
368
+ }
369
+ case BlendMode . MULTIPLY : {
370
+ gl . blendEquation ( gl . FUNC_ADD ) ;
371
+ gl . blendFunc ( gl . DST_COLOR , gl . ONE_MINUS_SRC_ALPHA ) ; // fun fact if you do the math everything cancels out so that the destination alpha doesn't change at all.
372
+ break ;
373
+ }
374
+ case BlendMode . INSET_OVERLAY : {
375
+ // TODO figure out if this is actually right
376
+ gl . blendEquation ( gl . FUNC_ADD ) ;
377
+ gl . blendFuncSeparate ( gl . ONE , gl . ONE_MINUS_SRC_ALPHA , gl . ZERO , gl . ONE )
378
+ break ;
379
+ }
380
+ case BlendMode . ALPHA : {
381
+ gl . blendEquation ( gl . FUNC_ADD ) ;
382
+ //gl.blendFuncSeparate(gl.DST_COLOR, gl.ZERO, gl.DST_ALPHA, gl.ZERO)
383
+ gl . blendFunc ( gl . DST_COLOR , gl . ONE_MINUS_SRC_ALPHA ) ;
384
+ break ;
385
+ }
386
+ case BlendMode . ALPHA_INVERTED : {
387
+ gl . blendEquation ( gl . FUNC_ADD ) ;
388
+ gl . blendFuncSeparate ( gl . DST_COLOR , gl . ONE_MINUS_SRC_ALPHA , gl . ONE , gl . ONE )
389
+ break ;
390
+ }
391
+ //Just in case there's a weird value we'll use the default
392
+ default : {
393
+ gl . blendEquation ( gl . FUNC_ADD ) ;
394
+ gl . blendFunc ( gl . ONE , gl . ONE_MINUS_SRC_ALPHA ) ;
395
+ break ;
396
+ }
361
397
}
362
398
}
363
399
0 commit comments