我正在尝试使用 WebGL 制作有趣的效果。 在我的片段着色器中,我有以下行以黑白方式绘制纹理:
gl_FragColor = vec4(vec3(color0.r+color0.g+color0.b)/3.0, color0.a);
其中
color0
是纹理的颜色。
在着色器中,我还有一个 uniform vec2 u_mouse
,它是从我的 javascript 代码传入的,作为屏幕上的鼠标坐标。
现在我想要的是能够移动鼠标,并且图像的一部分将在给定半径内着色,如图所示:
我的想法是有一个遮罩,上面有一个白色圆圈,会随着鼠标移动,但我不知道之后如何进行图像处理...... 我还希望动画不平滑,就像鼠标值之间的插值一样。
谢谢!
您想将黑白版本与彩色版本混合
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.);
你会得到一个渐变圆圈,中心的颜色在边缘逐渐变成黑色和白色。
如果您想要中心更大的区域有颜色,那么可以传入
minRadius
和 maxRadius
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);
body { margin: 0; }
canvas { display: block; width: 100vw; height: 100vh; }
<canvas></canvas>
<script src="https://twgljs.org/dist/3.x/twgl-full.min.js"></script>
就像您提到的,您还可以传递蒙版纹理。这将使您可以轻松制作其他形状。同样,您只需要
mixAmount
的值
所以,类似
uniform mat4 maskMatrix;
...
vec2 maskUV = (maskMatrix * vec4(v_texcoord, 0, 1)).xy;
float mixAmount = texture2D(mask, maskUV).a;
您可以按照以下文章了解如何使用 2d 或 3d 矩阵设置该矩阵
"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);
body { margin: 0; }
canvas { display: block; width: 100vw; height: 100vh; }
<canvas></canvas>
<script src="https://twgljs.org/dist/3.x/twgl-full.min.js"></script>
注意我使用了带有框架的
F
来清楚地显示蒙版。另请注意,必须将蒙版的边缘保持为 0,因为边缘像素将重复越过蒙版的边界。或者当与蒙版一起使用的纹理坐标为 < 0 or > 1 时,您需要修改着色器以使用 0。
我还使用了矩阵来操纵 UV 坐标。因为它是一个矩阵,所以很容易缩放、偏移和/或旋转遮罩,而无需更改着色器。
至于动画,目前还不清楚你想要什么样的动画。如果您想要颜色随着时间的推移而褪色的东西,您可以使用类似于此答案中的技术。您可以在另一对纹理中绘制蒙版。您可以使用这对纹理作为您的
mixAmount
蒙版。您可以通过将一个纹理绘制到另一个纹理中并每帧减去一定量来将这些纹理淡化回 0
gl_FragColor = texture2D(mixTexture, uv).rgba - vec4(0.01);
例如。
您需要一个片段着色器,它根据距离为图像着色。 通常使用公式
gray = 0.2126 * red + 0.7152 * green + 0.0722 * blue
创建黑白颜色(网上有不同的亮度公式和解释:Luma(视频)、七种灰度转换算法。)
根据距离,您可以在原始颜色和黑白颜色之间进行插值。
为此,您需要不同的统一变量:
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>