您可以做的是将图像放入纹理中,使用着色器计算差异,然后从帧缓冲区(使用readPixels)读取它们并将它们相加,例如计算误差。
要放置Base64编码的图像,你需要创建一个Image对象,然后将它分配给一个纹理:
var image = new Image();
// You can omit using onload and asynchronous code,
// but unfortunately it's not fully reliable :(
image.onload = function () {
/* ... */
};
// Here you'll need to know format of the image (png, jpeg...).
image.src = 'data:image/jpeg;base64,' + base64EncodedImage;
// gl here is the context
var textureId = gl.createTexture();
gl.bindTexture(gl.TEXTURE2D, texture);
gl.texImage2D(gl.TEXTURE2D, /* ... */, image);
之后,您可以使用上传的图像(甚至渲染的图像)来计算差异,例如,该着色器:
// texture coordinates
varying vec2 uv;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main(void) {
vec4 texel1 = texture2D(texture1, uv);
vec4 texel2 = texture2D(texture2, uv);
gl_FragColor = abs(texel1 - texel2);
}
比你从帧缓冲区读取差异数据:
var diffData = new Uint8Array();
gl.readPixels(/* ... */, diffData);
// Process diffData...
但是,如果这是您第一次使用 WebGL,我强烈建议您遵循 gman 的建议并学习 WebGL 本身。这是一项强大的技术,并且知道它将成为您工具箱中的好工具:)