Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/overlay/shaders/background_frag.glsl
| uniform sampler2D colorBuffer; | uniform sampler2D colorBuffer; | ||||
| uniform sampler2D depthBuffer; | uniform sampler2D depthBuffer; | ||||
| uniform int bgType; | uniform int bgType; | ||||
| uniform vec4 colorOverride; | uniform vec4 colorOverride; | ||||
| in vec4 uvcoordsvar; | in vec4 uvcoordsvar; | ||||
| out vec4 fragColor; | out vec4 fragColor; | ||||
| #define BG_SOLID 0 | #define BG_SOLID 0 | ||||
| #define BG_GRADIENT 1 | #define BG_GRADIENT 1 | ||||
| #define BG_CHECKER 2 | #define BG_CHECKER 2 | ||||
| #define BG_RADIAL 3 | #define BG_RADIAL 3 | ||||
| #define BG_SOLID_CHECKER 4 | #define BG_SOLID_CHECKER 4 | ||||
| #define BG_MASK 5 | |||||
| #define SQRT2 1.4142135623730950488 | #define SQRT2 1.4142135623730950488 | ||||
| /* 4x4 bayer matrix prepared for 8bit UNORM precision error. */ | /* 4x4 bayer matrix prepared for 8bit UNORM precision error. */ | ||||
| #define P(x) (((x + 0.5) * (1.0 / 16.0) - 0.5) * (1.0 / 255.0)) | #define P(x) (((x + 0.5) * (1.0 / 16.0) - 0.5) * (1.0 / 255.0)) | ||||
| const vec4 dither_mat4x4[4] = vec4[4](vec4(P(0.0), P(8.0), P(2.0), P(10.0)), | const vec4 dither_mat4x4[4] = vec4[4](vec4(P(0.0), P(8.0), P(2.0), P(10.0)), | ||||
| vec4(P(12.0), P(4.0), P(14.0), P(6.0)), | vec4(P(12.0), P(4.0), P(14.0), P(6.0)), | ||||
| vec4(P(3.0), P(11.0), P(1.0), P(9.0)), | vec4(P(3.0), P(11.0), P(1.0), P(9.0)), | ||||
| vec4(P(15.0), P(7.0), P(13.0), P(5.0))); | vec4(P(15.0), P(7.0), P(13.0), P(5.0))); | ||||
| float dither(void) | float dither(void) | ||||
| { | { | ||||
| ivec2 co = ivec2(gl_FragCoord.xy) % 4; | ivec2 co = ivec2(gl_FragCoord.xy) % 4; | ||||
| return dither_mat4x4[co.x][co.y]; | return dither_mat4x4[co.x][co.y]; | ||||
| } | } | ||||
| void main() | void main() | ||||
| { | { | ||||
| /* The blend equation is: | /* The blend equation is: | ||||
| * resutl.rgb = SRC.rgb * (1 - DST.a) + DST.rgb * (SRC.a) | * resutl.rgb = SRC.rgb * (1 - DST.a) + DST.rgb * (SRC.a) | ||||
fclem: You could put this inside the switch statement like this
```
case BG_MASK:
fragColor… | |||||
| * result.a = SRC.a * 0 + DST.a * SRC.a | * result.a = SRC.a * 0 + DST.a * SRC.a | ||||
| * This removes the alpha channel and put the background behind reference images | * This removes the alpha channel and put the background behind reference images | ||||
| * while masking the reference images by the render alpha. | * while masking the reference images by the render alpha. | ||||
| */ | */ | ||||
| float alpha = texture(colorBuffer, uvcoordsvar.st).a; | float alpha = texture(colorBuffer, uvcoordsvar.st).a; | ||||
| float depth = texture(depthBuffer, uvcoordsvar.st).r; | float depth = texture(depthBuffer, uvcoordsvar.st).r; | ||||
| vec3 bg_col; | vec3 bg_col; | ||||
| Show All 32 Lines | case BG_RADIAL: | ||||
| bg_col += dither(); | bg_col += dither(); | ||||
| break; | break; | ||||
| case BG_CHECKER: | case BG_CHECKER: | ||||
| float size = sizeChecker * sizePixel; | float size = sizeChecker * sizePixel; | ||||
| ivec2 p = ivec2(floor(gl_FragCoord.xy / size)); | ivec2 p = ivec2(floor(gl_FragCoord.xy / size)); | ||||
| bool check = mod(p.x, 2) == mod(p.y, 2); | bool check = mod(p.x, 2) == mod(p.y, 2); | ||||
| bg_col = (check) ? colorCheckerPrimary.rgb : colorCheckerSecondary.rgb; | bg_col = (check) ? colorCheckerPrimary.rgb : colorCheckerSecondary.rgb; | ||||
| break; | break; | ||||
| case BG_MASK: | |||||
| fragColor = vec4(vec3(1.0 - alpha), 0.0); | |||||
| return; | |||||
| } | } | ||||
| bg_col = mix(bg_col, colorOverride.rgb, colorOverride.a); | bg_col = mix(bg_col, colorOverride.rgb, colorOverride.a); | ||||
| /* Mimic alpha under behavior. Result is premultiplied. */ | /* Mimic alpha under behavior. Result is premultiplied. */ | ||||
| fragColor = vec4(bg_col, 1.0) * (1.0 - alpha); | fragColor = vec4(bg_col, 1.0) * (1.0 - alpha); | ||||
| /* Special case: If the render is not transparent, do not clear alpha values. */ | /* Special case: If the render is not transparent, do not clear alpha values. */ | ||||
| if (depth == 1.0 && alpha == 1.0) { | if (depth == 1.0 && alpha == 1.0) { | ||||
| fragColor.a = 1.0; | fragColor.a = 1.0; | ||||
| } | } | ||||
| } | } | ||||
You could put this inside the switch statement like this