GP: Correct Tonemapping for Blend Layers
authorAntonioya <blendergit@gmail.com>
Mon, 26 Nov 2018 21:52:28 +0000 (22:52 +0100)
committerAntonioya <blendergit@gmail.com>
Mon, 26 Nov 2018 21:52:28 +0000 (22:52 +0100)
source/blender/draw/engines/gpencil/gpencil_engine.c
source/blender/draw/engines/gpencil/shaders/gpencil_blend_frag.glsl

index a7141ff93bbcf30d4746fbd61913a1e450d53168..483d6a80991c14cd484e6792a10f78166c9376a9 100644 (file)
@@ -506,6 +506,7 @@ void GPENCIL_cache_init(void *vedata)
                DRW_shgroup_uniform_int(blend_shgrp, "mode", &stl->storage->blend_mode, 1);
                DRW_shgroup_uniform_int(blend_shgrp, "clamp_layer", &stl->storage->clamp_layer, 1);
                DRW_shgroup_uniform_float(blend_shgrp, "blend_opacity", &stl->storage->blend_opacity, 1);
+               DRW_shgroup_uniform_int(mix_shgrp, "tonemapping", &stl->storage->tonemapping, 1);
 
                /* create effects passes */
                if (!stl->storage->simplify_fx) {
@@ -813,7 +814,9 @@ void GPENCIL_draw_scene(void *ved)
                                                        stl->storage->blend_mode = array_elm->mode;
                                                        stl->storage->clamp_layer = (int)array_elm->clamp_layer;
                                                        stl->storage->blend_opacity = array_elm->blend_opacity;
+                                                       stl->storage->tonemapping = stl->storage->is_render ? 1 : 0;
                                                        DRW_draw_pass(psl->blend_pass);
+                                                       stl->storage->tonemapping = 0;
 
                                                        /* Copy B texture to A texture to follow loop */
                                                        e_data.input_depth_tx = e_data.temp_depth_tx_b;
index 8eeb0b8bc7fdde8833b1d4b1ec763b96c0ec4b31..877693724f93a425e2bd93096252f21d368a299a 100644 (file)
@@ -9,6 +9,7 @@ uniform sampler2D blendDepth;
 uniform int mode;
 uniform int clamp_layer;
 uniform float blend_opacity;
+uniform int tonemapping;
 
 #define ON 1
 #define OFF 0
@@ -76,6 +77,28 @@ vec4 get_blend_color(int mode, vec4 src_color, vec4 blend_color)
        return outcolor;
 }
 
+float linearrgb_to_srgb(float c)
+{
+       if (c < 0.0031308)
+               return (c < 0.0) ? 0.0 : c * 12.92;
+       else
+               return 1.055 * pow(c, 1.0 / 2.4) - 0.055;
+}
+
+vec4 tone(vec4 stroke_color)
+{
+       if (tonemapping == 1) {
+               vec4 color = vec4(0, 0, 0, stroke_color.a);
+               color.r = linearrgb_to_srgb(stroke_color.r);
+               color.g = linearrgb_to_srgb(stroke_color.g);
+               color.b = linearrgb_to_srgb(stroke_color.b);
+               return color;
+       }
+       else {
+               return stroke_color;
+       }
+}              
+
 void main()
 {
        vec4 outcolor;
@@ -115,17 +138,18 @@ void main()
                                gl_FragDepth = mix_depth;
                        }
                }
+               FragColor = tone(FragColor);
                return;
        }
        
        /* if not using mask, return mix color */
        if ((stroke_color.a == 0) && (clamp_layer == OFF)) {
-               FragColor = mix_color;
+               FragColor = tone(mix_color);
                gl_FragDepth = mix_depth;
                return;
        }
 
        /* apply blend mode */
-       FragColor = get_blend_color(mode, stroke_color, mix_color);
+       FragColor = tone(get_blend_color(mode, stroke_color, mix_color));
        gl_FragDepth = stroke_depth;
 }