WebGL着色器根据鼠标位置为纹理着色

时间:2017-07-23 23:32:01

标签: position webgl shader mouse

我正在尝试使用WebGL创建一个有趣的效果。 在我的片段着色器中,我有以下行,用黑色和白色绘制我的纹理:

gl_FragColor = vec4(vec3(color0.r+color0.g+color0.b)/3.0, color0.a);

其中color0是纹理的颜色。 在着色器中,我还有一个uniform vec2 u_mouse,它从我的javascript代码传入,作为屏幕上的鼠标坐标。 现在我想要的是能够移动鼠标,部分图像将在给定半径内着色,如图所示:

Picture of effect

我的想法是有一个面具,上面有一个白色圆圈,随着鼠标移动,但我不知道如何进行图像处理...... 我也希望动画不会流畅,就像鼠标值之间的插值一样。

谢谢!

2 个答案:

答案 0 :(得分:5)

您想将黑白版本与彩色版本混合

vec4 bw = vec4(vec3(color0.r + color0.g + color0.b) / 3., color.a);
gl_FragColor = mix(bw, color0, mixAmount);

mix定义为

mix(a, b, l) = a + (b - a) * l

换句话说,如果mixAmount为0,您将获得bw,如果mixAmount为1,您将获得color0。对于介于0和1之间的值,您将得到2的混合。

所以现在你只需要setting mixAmount

的一些公式

作为一个示例,假设您在画布相对坐标中传入鼠标,则可以计算与该坐标的距离

uniform vec2 mousePos;  // in pixels where 0,0 is bottom left

...

  float dist = distance(mousePos, gl_FragCoord.xy);

然后,您可以使用它来计算mixAmount例如

uniform float mixRadius;

  float mixAmount = clamp(dist / mixRadius, 0., 1.);

你会得到一个褪色的圆圈,中心的颜色渐渐变成黑色和白色。

如果您希望中心区域的颜色更大,那么可以传入minRadiusmaxRadius

uniform float minRadius;
uniform float maxRadius;

  float range = maxRadius - minRadius
  float mixAmount = clamp((dist - minRadius) / range, 0., 1.);

或类似的东西

这是一个有效的例子



"use strict";
const vs = `
attribute vec4 position;
attribute vec2 texcoord;

uniform mat4 matrix;

varying vec2 v_texcoord;

void main() {
  gl_Position = matrix * position;
  v_texcoord = texcoord;
}
`;
const fs = `
precision mediump float;

varying vec2 v_texcoord;

uniform sampler2D tex;
uniform vec2 mousePos;
uniform float minRadius;
uniform float maxRadius;

void main() {
  vec4 color0 = texture2D(tex, v_texcoord);
  vec4 bw = vec4(vec3(color0.r + color0.g + color0.b) / 3., color0.a);
  
  float dist = distance(mousePos, gl_FragCoord.xy);
  float range = maxRadius - minRadius;
  float mixAmount = clamp((dist - minRadius) / range, 0., 1.);
  
  gl_FragColor = mix(color0, bw, mixAmount);
}
`;
const m4 = twgl.m4;
const gl = document.querySelector("canvas").getContext("webgl");
const info = document.querySelector("#info");

// compiles shaders, link program, looks up locations
const programInfo = twgl.createProgramInfo(gl, [vs, fs]);

// calls gl.createBuffer, gl.bindBuffer, gl.bufferData for each array
const bufferInfo = twgl.primitives.createXYQuadBufferInfo(gl);

const textureInfo = {
  width: 1,
  height: 1,
};
const texture = twgl.createTexture(gl, {
  src: "http://i.imgur.com/NzBzAdN.jpg",
  crossOrigin: '',
  flipY: true,
}, (err, tex, img) => {
  textureInfo.width = img.width;
  textureInfo.height = img.height;
  render();
});

const mousePos = [0, 0];

function render() {
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  
  gl.useProgram(programInfo.program);
  
  // calls gl.bindBuffer, gl.enableVertexAttribArray, gl.vertexAttribPointer
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  
  // cover canvas with image  
  const canvasAspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  const imageAspect = textureInfo.width / textureInfo.height;

  // this assumes we want to fill vertically
  let horizontalDrawAspect = imageAspect / canvasAspect;
  let verticalDrawAspect = 1;
  // does it fill horizontally?
  if (horizontalDrawAspect < 1) {
    // no it does not so scale so we fill horizontally and
    // adjust vertical to match
    verticalDrawAspect /= horizontalDrawAspect;
    horizontalDrawAspect = 1;
  }
  const mat = m4.scaling([horizontalDrawAspect, verticalDrawAspect, 1]);
  
  // calls gl.activeTexture, gl.bindTexture, gl.uniform
  twgl.setUniforms(programInfo, {
    minRadius: 25,
    maxRadius: 100,
    tex: texture,
    matrix: mat,
    mousePos: mousePos,
  });
  
  twgl.drawBufferInfo(gl, bufferInfo);
}
render();

gl.canvas.addEventListener('mousemove', e => {
  const canvas = e.target;
  const rect = canvas.getBoundingClientRect();

  const x = (e.clientX - rect.left) * canvas.width / rect.width;
  const y = (e.clientY - rect.top)  * canvas.height / rect.height;
  mousePos[0] = x;
  mousePos[1] = canvas.height - y - 1;
  
  render();
});

window.addEventListener('resize', render);
&#13;
body { margin: 0; }
canvas { display: block; width: 100vw; height: 100vh; }
&#13;
<canvas></canvas>  
<script src="https://twgljs.org/dist/3.x/twgl-full.min.js"></script>
&#13;
&#13;
&#13;

就像你提到的那样,你也可以传递一个蒙版纹理。这样可以轻松制作其他形状。同样,您只需要mixAmount

的值

所以,像

uniform mat4 maskMatrix;

...

vec2 maskUV = (maskMatrix * vec4(v_texcoord, 0, 1)).xy;
float mixAmount = texture2D(mask, maskUV).a;

您可以看到how to set that matrix using 2d or 3d matrices by following these articles

&#13;
&#13;
"use strict";
const vs = `
attribute vec4 position;
attribute vec2 texcoord;

uniform mat4 matrix;

varying vec2 v_texcoord;

void main() {
  gl_Position = matrix * position;
  v_texcoord = texcoord;
}
`;
const fs = `
precision mediump float;

varying vec2 v_texcoord;

uniform sampler2D tex;
uniform mat4 maskMatrix;
uniform sampler2D maskTex;

void main() {
  vec4 color0 = texture2D(tex, v_texcoord);
  vec4 bw = vec4(vec3(color0.r + color0.g + color0.b) / 3., color0.a);

  vec2 maskUV = (maskMatrix * vec4(v_texcoord, 0, 1)).xy;
  float mixAmount = texture2D(maskTex, maskUV).a;
  
  gl_FragColor = mix(bw, color0, mixAmount);
}
`;
const m4 = twgl.m4;
const gl = document.querySelector("canvas").getContext("webgl");
const info = document.querySelector("#info");

// compiles shaders, link program, looks up locations
const programInfo = twgl.createProgramInfo(gl, [vs, fs]);

// calls gl.createBuffer, gl.bindBuffer, gl.bufferData for each array
const bufferInfo = twgl.primitives.createXYQuadBufferInfo(gl);

const textureInfo = {
  width: 1,
  height: 1,
};
// calls gl.createTexture, gl.bindTexture, gl.texImage2D, gl.texParameteri
const texture = twgl.createTexture(gl, {
  src: "http://i.imgur.com/NzBzAdN.jpg",
  crossOrigin: '',
  flipY: true,
}, (err, tex, img) => {
  textureInfo.width = img.width;
  textureInfo.height = img.height;
  render();
});

// we could load a mask from an image but let's just make one from a canvas
// We'll use the letter F
const maskWidth = 128;
const maskHeight = 128;
const ctx = document.createElement("canvas").getContext("2d");
ctx.canvas.width = maskWidth;
ctx.canvas.height = maskHeight;
ctx.font = "bold 120px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";

ctx.strokeStyle = "white";
ctx.strokeRect(2, 2, 124, 124);

ctx.translate(64, 64);
ctx.fillStyle = "white";
ctx.fillText("F", 0, 0);

// calls gl.createTexture, gl.bindTexture, gl.texImage2D, gl.texParameteri
const maskTexture = twgl.createTexture(gl, {
  src: ctx.canvas,
  minMag: gl.LINEAR,
  wrap: gl.CLAMP_TO_EDGE,
  flipY: true,
});


const mousePos = [0, 0];

function render() {
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  
  gl.useProgram(programInfo.program);
  
  // calls gl.bindBuffer, gl.enableVertexAttribArray, gl.vertexAttribPointer
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  
  // cover canvas with image  
  const canvasAspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
  const imageAspect = textureInfo.width / textureInfo.height;

  // this assumes we want to fill vertically
  let horizontalDrawAspect = imageAspect / canvasAspect;
  let verticalDrawAspect = 1;
  // does it fill horizontally?
  if (horizontalDrawAspect < 1) {
    // no it does not so scale so we fill horizontally and
    // adjust vertical to match
    verticalDrawAspect /= horizontalDrawAspect;
    horizontalDrawAspect = 1;
  }
  const mat = m4.scaling([horizontalDrawAspect, verticalDrawAspect, 1]);
  
  // Our texcoord represent a unit square from 0,0 to 1,1. We want that center
  // centered around the mouse move scale by 2 and subtract 1
  
  // compute how large the image is (note it's larger than the canvas
  // because we computed a `cover` style above)
  const imageDisplayWidth = gl.canvas.width * horizontalDrawAspect;
  const imageDisplayHeight = gl.canvas.height * verticalDrawAspect;
  
  // compute how many pixels off the screen it is
  const xOff = gl.canvas.width * (horizontalDrawAspect - 1) / 2;
  const yOff = gl.canvas.height * (verticalDrawAspect - 1) / 2;

  // decide a size to draw the mask in pixel
  const maskDrawWidth = maskWidth;
  const maskDrawHeight = maskHeight;

  let maskMat = m4.identity();
  // translate the UV coords so they are centered
  maskMat = m4.translate(maskMat, [.5, .5, 0]);
  // scale the uvCoords to the mask
  maskMat = m4.scale(maskMat, [
    1 / (maskDrawWidth / imageDisplayWidth), 
    1 / (maskDrawHeight/ imageDisplayHeight), 
    1,
  ]);
  // move the UV coords so the origin is at th emouse
  maskMat = m4.translate(maskMat, [
    -(mousePos[0] + xOff) / imageDisplayWidth,
    -(mousePos[1] + yOff) / imageDisplayHeight, 
    0,
  ]);

  // calls gl.activeTexture, gl.bindTexture, gl.uniform
  twgl.setUniforms(programInfo, {
    tex: texture,
    matrix: mat,
    maskTex: maskTexture,
    maskMatrix: maskMat,
  });
  
  twgl.drawBufferInfo(gl, bufferInfo);
}
render();

gl.canvas.addEventListener('mousemove', e => {
  const canvas = e.target;
  const rect = canvas.getBoundingClientRect();

  const x = (e.clientX - rect.left) * canvas.width / rect.width;
  const y = (e.clientY - rect.top)  * canvas.height / rect.height;
  mousePos[0] = x;
  mousePos[1] = canvas.height - y - 1;
  
  render();
});

window.addEventListener('resize', render);
&#13;
body { margin: 0; }
canvas { display: block; width: 100vw; height: 100vh; }
&#13;
<canvas></canvas>  
<script src="https://twgljs.org/dist/3.x/twgl-full.min.js"></script>
&#13;
&#13;
&#13;

注意我使用带有框架的F来清楚地显示蒙版。另请注意,必须保持遮罩的边缘为0,因为边缘像素将重复超过遮罩的边框。当与掩模一起使用的纹理坐标为&lt;时,您需要修改着色器以使用0。 0或&gt; 1。

我还使用矩阵来操纵UV坐标。由于它是一个矩阵,因此无需更改着色器即可轻松缩放,偏移和/或旋转蒙版。

至于动画,它不清楚你想要什么样的动画。如果你想要颜色随着时间的推移逐渐消失,你可以use a techinque like the one in this answer。您可以在另一对纹理中绘制蒙版。您使用这对纹理作为mixAmount蒙版。通过将一个纹理绘制到另一个纹理中,将这些纹理淡化为0,每个帧减去一定数量

gl_FragColor = texture2D(mixTexture, uv).rgba - vec4(0.01);

例如。

答案 1 :(得分:2)

您需要一个片段着色器,它根据距离为图像着色。 黑色和白色通常使用公式gray = 0.2126 * red + 0.7152 * green + 0.0722 * blue创建(在网络上有不同的亮度公式和解释:Luma (video)Seven grayscale conversion algorithms。) 根据距离,您可以在原始颜色和黑白颜色之间进行插值。

为此你需要不同的统一变量:

uniform vec2 u_mouse;
uniform vec2 u_maxDistance;

这些是要着色的区域(u_mouse)的中心点和要着色的区域的最大半径(u_maxDistance)。

可以使用length计算当前片段与图像中心点之间的距离:

vec2 fragmentPos = ...;
float distance   = length( fragmentPos - u_mouse );

通过将距离distance和最大距离u_maxDistance相除来计算混合比率:

float mixK = distance / u_maxDistance;

您还可以尝试非线性混合比:

float mixK = pow( distance / u_maxDistance, 2.0 );

我们使用函数mix

在颜色之间进行插值
vec3 texColor   = ...;
vec3 grayColor  = vec3( 0.2126 * texColor.r + 0.7152 * texColor.g + 0.0722 * texColor.b );
vec3 finalColor = mix( texColor, grayColor, clamp( mixK, 0.0, 1.0 ) );

如果要点亮受影响的区域,则必须将颜色乘以大于1.0的值:

vec3 finalColor = mix( texColor * 1.2, grayColor, clamp( mixK, 0.0, 1.0 ) );

最终的顶点着色器和片段着色器:

顶点着色器

precision mediump float;

attribute vec2 inPos;
varying   vec2 vertPos;

void main()
{
    vertPos     = inPos;
    gl_Position = vec4( inPos, 0.0, 1.0 );
}

片段着色器

precision mediump float;

varying vec2      vertPos;
uniform sampler2D u_texture;
uniform vec2      u_mouse;
uniform float     u_maxDistance;

void main()
{
    float distance  = length( vertPos - u_mouse);
    float mixK      = pow( distance / u_maxDistance, 2.0 ); 
    vec2 texCoord   = vertPos * 0.5 + 0.5;
    vec3 texColor   = texture2D( u_texture, texCoord.st ).rgb;
    vec3 grayColor  = vec3( 0.2126 * texColor.r + 0.7152 * texColor.g + 0.0722 * texColor.b );
    vec3 finalColor = mix( texColor * 1.2, grayColor, clamp( mixK, 0.0, 1.0 ) ); 
    gl_FragColor    = vec4( finalColor.rgb, 1.0 );
}

面膜

如果您需要受影响区域的蒙版,则需要具有一个通道的蒙版纹理。通道表示该区域受影响的程度。您还必须计算与鼠标位置和受影响区域大小相关的遮罩的纹理坐标。

vec2 maskTexCoord = 0.5 + 0.5 * (vertPos - u_mouse) / u_maxDistance; 
float mask        = texture2D( u_maskTexture, maskTexCoord ).x;
vec3 finalColor   = mix( grayColor, texColor * u_lightup, mask );

为避免掩码重复,您必须实现范围检查:

vec2 rangeTest    = step( vec2(0.0), maskTexCoord ) * step( maskTexCoord, vec2(1.0) );   
mask             *= rangeTest.x * rangeTest.y; 

请参阅以下示例,其中圆形区域和蒙版的实现是混合的:

(function loadscene() {    

var canvas, gl, prog, bufObj = {}, textureObj, maskTextureObj;
       
function render(deltaMS){

    var maxDist = document.getElementById( "maxDist" ).value / 100;
    var exponent = document.getElementById( "exponent" ).value / 100;
    var lightup = document.getElementById( "lightup" ).value / 100;

    gl.viewport( 0, 0, vp_size[0], vp_size[1] );
    gl.enable( gl.DEPTH_TEST );
    gl.clearColor( 0.0, 0.0, 0.0, 1.0 );
    gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );
    var texUnit = 0;
    gl.activeTexture( gl.TEXTURE0 + texUnit );
    gl.bindTexture( gl.TEXTURE_2D, textureObj );
    var maskTexUnit = 1;
    gl.activeTexture( gl.TEXTURE0 + maskTexUnit );
    gl.bindTexture( gl.TEXTURE_2D, maskTextureObj );
    ShProg.Use( progDraw );
    var pos = [ 2.0, 2.0 ];
    if (mousePos[0] > 0 && mousePos[1] > 0 )
        pos = [2.0 * mousePos[0] / vp_size[0] - 1.0, 1.0 - 2.0 * mousePos[1] / vp_size[1]];
    ShProg.SetF2( progDraw, "u_vpsize", vp_size );
    ShProg.SetF2( progDraw, "u_mouse", pos );
    ShProg.SetF1( progDraw, "u_maxDistance", maxDist );
    ShProg.SetF1( progDraw, "u_exponent", exponent );
    ShProg.SetF1( progDraw, "u_lightup", lightup );
    ShProg.SetI1( progDraw, "u_texture", texUnit );
    ShProg.SetI1( progDraw, "u_maskTexture", maskTexUnit );
    gl.enableVertexAttribArray( progDraw.inPos );
    gl.bindBuffer( gl.ARRAY_BUFFER, bufObj.pos );
    gl.vertexAttribPointer( progDraw.inPos, 2, gl.FLOAT, false, 0, 0 ); 
    gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, bufObj.inx );
    gl.drawElements( gl.TRIANGLES, bufObj.inx.len, gl.UNSIGNED_SHORT, 0 );
    gl.disableVertexAttribArray( progDraw.pos );

    requestAnimationFrame(render);
}  

function initScene() {

    canvas = document.getElementById( "scene-canvas");
    gl = canvas.getContext( "experimental-webgl" );
    if ( !gl )
      return;

    textureObj = Texture.LoadTexture2D( "https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/supermario.jpg", true );  
    maskTextureObj = Texture.LoadTexture2D( "https://raw.githubusercontent.com/Rabbid76/graphics-snippets/master/resource/texture/bat.png", true );  

    
    progDraw = ShProg.Create( 
      [ { source : "draw-shader-vs", stage : gl.VERTEX_SHADER },
        { source : "draw-shader-fs", stage : gl.FRAGMENT_SHADER }
      ] );
    progDraw.inPos = gl.getAttribLocation( progDraw.progObj, "inPos" );
    if ( progDraw.progObj == 0 )
        return;

    var pos = [ -1, -1, 1, -1, 1, 1, -1, 1 ];
    var inx = [ 0, 1, 2, 0, 2, 3 ];
    bufObj.pos = gl.createBuffer();
    gl.bindBuffer( gl.ARRAY_BUFFER, bufObj.pos );
    gl.bufferData( gl.ARRAY_BUFFER, new Float32Array( pos ), gl.STATIC_DRAW );
    bufObj.inx = gl.createBuffer();
    bufObj.inx.len = inx.length;
    gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, bufObj.inx );
    gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( inx ), gl.STATIC_DRAW );

    window.onresize = resize;
    resize();
    requestAnimationFrame(render);
}

function resize() {
    //vp_size = [gl.drawingBufferWidth, gl.drawingBufferHeight];
    vp_size = [window.innerWidth, window.innerHeight]
    //vp_size = [256, 256]
    canvas.width = vp_size[0];
    canvas.height = vp_size[1];
}

var mousePos = [-1, -1];
(function() {
    document.onmousemove = handleMouseMove;
    function handleMouseMove(event) {
        var dot, eventDoc, doc, body, pageX, pageY;

        event = event || window.event; // IE-ism

        if (event.pageX == null && event.clientX != null) {
            eventDoc = (event.target && event.target.ownerDocument) || document;
            doc = eventDoc.documentElement;
            body = eventDoc.body;

            event.pageX = event.clientX +
              (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
              (doc && doc.clientLeft || body && body.clientLeft || 0);
            event.pageY = event.clientY +
              (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
              (doc && doc.clientTop  || body && body.clientTop  || 0 );
        }

        var x = event.pageX - canvas.offsetLeft;
        var y = event.pageY - canvas.offsetTop;
        mousePos = [-1, -1];
        if ( x >= 0 && x < canvas.width && y >= 0 && y < canvas.height ) {
            mousePos = [x, y]; 
        }
    }
})();

var ShProg = {
Create: function (shaderList) {
    var shaderObjs = [];
    for (var i_sh = 0; i_sh < shaderList.length; ++i_sh) {
        var shderObj = this.Compile(shaderList[i_sh].source, shaderList[i_sh].stage);
        if (shderObj) shaderObjs.push(shderObj);
    }
    var prog = {}
    prog.progObj = this.Link(shaderObjs)
    if (prog.progObj) {
        prog.attrInx = {};
        var noOfAttributes = gl.getProgramParameter(prog.progObj, gl.ACTIVE_ATTRIBUTES);
        for (var i_n = 0; i_n < noOfAttributes; ++i_n) {
            var name = gl.getActiveAttrib(prog.progObj, i_n).name;
            prog.attrInx[name] = gl.getAttribLocation(prog.progObj, name);
        }
        prog.uniLoc = {};
        var noOfUniforms = gl.getProgramParameter(prog.progObj, gl.ACTIVE_UNIFORMS);
        for (var i_n = 0; i_n < noOfUniforms; ++i_n) {
            var name = gl.getActiveUniform(prog.progObj, i_n).name;
            prog.uniLoc[name] = gl.getUniformLocation(prog.progObj, name);
        }
    }
    return prog;
},
AttrI: function (prog, name) { return prog.attrInx[name]; },
UniformL: function (prog, name) { return prog.uniLoc[name]; },
Use: function (prog) { gl.useProgram(prog.progObj); },
SetI1: function (prog, name, val) { if (prog.uniLoc[name]) gl.uniform1i(prog.uniLoc[name], val); },
SetF1: function (prog, name, val) { if (prog.uniLoc[name]) gl.uniform1f(prog.uniLoc[name], val); },
SetF2: function (prog, name, arr) { if (prog.uniLoc[name]) gl.uniform2fv(prog.uniLoc[name], arr); },
SetF3: function (prog, name, arr) { if (prog.uniLoc[name]) gl.uniform3fv(prog.uniLoc[name], arr); },
SetF4: function (prog, name, arr) { if (prog.uniLoc[name]) gl.uniform4fv(prog.uniLoc[name], arr); },
SetM33: function (prog, name, mat) { if (prog.uniLoc[name]) gl.uniformMatrix3fv(prog.uniLoc[name], false, mat); },
SetM44: function (prog, name, mat) { if (prog.uniLoc[name]) gl.uniformMatrix4fv(prog.uniLoc[name], false, mat); },
Compile: function (source, shaderStage) {
    var shaderScript = document.getElementById(source);
    if (shaderScript)
        source = shaderScript.text;
    var shaderObj = gl.createShader(shaderStage);
    gl.shaderSource(shaderObj, source);
    gl.compileShader(shaderObj);
    var status = gl.getShaderParameter(shaderObj, gl.COMPILE_STATUS);
    if (!status) alert(gl.getShaderInfoLog(shaderObj));
    return status ? shaderObj : null;
},
Link: function (shaderObjs) {
    var prog = gl.createProgram();
    for (var i_sh = 0; i_sh < shaderObjs.length; ++i_sh)
        gl.attachShader(prog, shaderObjs[i_sh]);
    gl.linkProgram(prog);
    status = gl.getProgramParameter(prog, gl.LINK_STATUS);
    if ( !status ) alert(gl.getProgramInfoLog(prog));
    return status ? prog : null;
} };

var Texture = {};
Texture.HandleLoadedTexture2D = function( image, texture, flipY ) {
    gl.activeTexture( gl.TEXTURE0 );
    gl.bindTexture( gl.TEXTURE_2D, texture );
    gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, flipY != undefined && flipY == true );
    gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image );
    gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR );
    gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR );
    gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT );
  	gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT );
    gl.bindTexture( gl.TEXTURE_2D, null );
    return texture;
}
Texture.LoadTexture2D = function( name, flip ) {
    var texture = gl.createTexture();
    texture.image = new Image();
    texture.image.setAttribute('crossorigin', 'anonymous');
    texture.image.onload = function () {
        Texture.HandleLoadedTexture2D( texture.image, texture, flip )
    }
    texture.image.src = name;
    return texture;
}

initScene();

})();
#gui { position : absolute; top : 0; left : 0; }
<script id="draw-shader-vs" type="x-shader/x-vertex">
precision mediump float;

attribute vec2 inPos;
varying   vec2 vertPos;

void main()
{
    vertPos     = inPos;
    gl_Position = vec4( inPos, 0.0, 1.0 );
}
</script>

<script id="draw-shader-fs" type="x-shader/x-fragment">
precision mediump float;

varying vec2      vertPos;
uniform sampler2D u_texture;
uniform sampler2D u_maskTexture;
uniform vec2      u_vpsize;
uniform vec2      u_mouse;
uniform float     u_maxDistance;
uniform float     u_exponent;
uniform float     u_lightup;

void main()
{
    float aspect       = u_vpsize[0]/u_vpsize[1];
    vec2  distVec      = (vertPos - u_mouse) * vec2(max(1.0,aspect), max(1.0,1.0/aspect));
    float distance     = length(distVec);
    float mixK         = pow( distance / u_maxDistance, u_exponent ); 
    vec2  texCoord     = vertPos * 0.5 + 0.5;
    vec3  texColor     = texture2D( u_texture, texCoord.st ).rgb;
    vec3  grayColor    = vec3( 0.2126 * texColor.r + 0.7152 * texColor.g + 0.0722 * texColor.b );
    vec2  maskTexCoord = 0.5 + 0.5 * distVec / u_maxDistance; 
    float mask         = texture2D( u_maskTexture, maskTexCoord ).a;
    vec3  finalColor   = mix( grayColor, texColor * u_lightup, ( 1.0 - clamp( mixK, 0.0, 1.0 ) ) * mask );
    gl_FragColor       = vec4( finalColor.rgb, 1.0 );
}
</script>

<form id="gui" name="inputs">
<table>
    <tr> <td> <font color= #CCF>max. distance</font> </td> 
            <td> <input type="range" id="maxDist" min="1" max="100" value="33"/></td> </tr>
    <tr> <td> <font color= #CCF>exponent</font> </td> 
            <td> <input type="range" id="exponent" min="1" max="300" value="100"/></td> </tr>
    <tr> <td> <font color= #CCF>light up</font> </td> 
            <td> <input type="range" id="lightup" min="50" max="200" value="150"/></td> </tr>
</table>
</form>

<canvas id="scene-canvas" style="border: none"></canvas>