我正在尝试使用 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 的混合。
所以现在您只需要一些公式来设置 mixAmount
作为一个示例,假设您在 Canvas 相对坐标中传递鼠标,您可以计算距该坐标的距离
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;
您可以看到how to set that matrix using 2d or 3d matrices by following these articles
"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,因为边缘像素将重复越过蒙版的边界。或者当 mask 使用的纹理坐标为 < 0 或 > 1 时,您需要修改着色器以使用 0。
我还使用了矩阵来操纵 UV 坐标。因为它是一个矩阵,所以很容易缩放、偏移和/或旋转蒙版,而无需更改着色器。
至于动画,目前还不清楚你想要什么样的动画。如果您想要颜色随着时间推移而逐渐消失的东西,您可以 use a techinque like the one in this answer 。您可以在另一对纹理中绘制蒙版。您可以使用这对纹理作为您的 mixAmount 蒙版。通过将一个纹理绘制到另一个纹理中并每帧减去一定的量,可以将这些纹理淡化回 0
gl_FragColor = texture2D(mixTexture, uv).rgba - vec4(0.01);
例如。
关于javascript - WebGL 着色器根据鼠标位置为纹理着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270803/