gpt4 book ai didi

javascript - WebGL2 将 Uint16Array 作为图像渲染到 Canvas

转载 作者:行者123 更新时间:2023-11-30 13:53:43 26 4
gpt4 key购买 nike

我正在尝试使用 webgl2 纹理将 Uint16Array 呈现为浏览器中的图像。我有一个 Uint8Array 的工作示例 fiddle ,由于 webgl 的学习曲线陡峭,我正在努力升级到 16 位。

工作 8 位 fiddle (与下面的代码片段相同): Uint8Array http://jsfiddle.net/njxvftc9/2/无效的 16 位尝试: Uint16Array http://jsfiddle.net/njxvftc9/3/

// image data
var w = 128;
var h = 128;
var size = w * h * 4;
var img = new Uint8Array(size); // need Uint16Array
for (var i = 0; i < img.length; i += 4) {
img[i + 0] = 255; // r
img[i + 1] = i/64; // g
img[i + 2] = 0; // b
img[i + 3] = 255; // a
}

// program
var canvas = document.getElementById('cv');
var gl = canvas.getContext('webgl2');

var program = gl.createProgram();
//var color_buffer_float_16ui = gl.getExtension('EXT_color_buffer_float'); // add for 16-bit

// texture
var tex = gl.createTexture(); // create empty texture
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(
gl.TEXTURE_2D, // target
0, // mip level
gl.RGBA, // internal format -> gl.RGBA16UI
w, h, // width and height
0, // border
gl.RGBA, //format -> gm.RGBA_INTEGER
gl.UNSIGNED_BYTE, // type -> gl.UNSIGNED_SHORT
img // texture data
);

// buffer
var buffer = gl.createBuffer();
var bufferData = new Float32Array([
-1, -1,
1, -1,
1, 1,
1, 1,
-1, 1,
-1, -1
]);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, bufferData, gl.STATIC_DRAW);

// shaders
program.vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(program.vs,
"attribute vec4 vertex;\n" + // incoming pixel input?
"varying vec2 pixelCoordinate;\n" + // variable used to pass position to fragment shader
"void main(){\n" +
" gl_Position = vertex;\n" + // set pixel output position to incoming position (pass through)
" pixelCoordinate = vertex.xy*0.5+0.5;\n" + // set coordinate for fragment shader
"}\n");

program.fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(program.fs,
"precision highp float;\n" + // ?
"uniform sampler2D tex;\n" + // ?
"varying vec2 pixelCoordinate;\n" + // receive pixel position from vertex shader
"void main(){\n" +
" gl_FragColor = texture2D(tex, pixelCoordinate);\n" + // lookup color in texture image at coordinate position and set color to
"}\n");

gl.compileShader(program.vs);
gl.compileShader(program.fs);

gl.attachShader(program,program.vs);
gl.attachShader(program,program.fs);

gl.deleteShader(program.vs);
gl.deleteShader(program.fs);

// program
gl.bindAttribLocation(program, 0, "vertex");
gl.linkProgram(program);
gl.useProgram(program);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 6); // execute program
<canvas id="cv" width="100" height="100"></canvas>

我尝试了很多其他技术,引用了规范,尝试在着色器中转换为 float ,并尝试组合此处看到的方法:Render to 16bits unsigned integer 2D texture in WebGL2没有成功。

我更喜欢 vanilla js,但我愿意使用 twgl 或 three.js 等库,只要输入是 Uint16Array,着色器可以输出任何格式(float 等)。

在 webgl2 方面有更多经验的任何人都可以为我指明正确的方向或在这里提供一个有效的示例 fiddle 吗?我的代码中是否有一个简单的错误,我是否遗漏了一个更大的概念,这甚至可能吗?非常感谢任何帮助!

最佳答案

如果要将 16 位数据放入纹理中,则需要选择 16 位纹理内部格式。看着the list of internal formats您对 RGBA 的选择是

gl.RGBA16I    // 16bit integers
gl.RGBA16UI // 16bit unsigned integers
gl.RGBA16F // 16bit half floats

看起来你选择了gl.RGBA16UI

此行要在着色器中访问该纹理

uniform sampler2D tex;

必须改变这个这个

uniform usampler2D tex;

该语法仅适用于 GLSL 3.00 es,因此您需要将 #version 300 es 添加到两个着色器的顶部。

切换到 #version 300 es 也意味着 other syntax changes . attribute 在顶点着色器中变为 invarying 在顶点着色器中变为 out,在顶点着色器中变为 in片段着色器。 gl_FragColor 消失了,您必须声明自己的输出,例如 out vec4 fooColor;texture2D 也变成了 texture

你不能对整数纹理使用过滤,所以这两行

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);

需要改成

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

当你从纹理中获取数据时,它将是无符号整数,所以这一行

gl_FragColor = texture2D(tex, pixelCoordinate);

最终会变成类似的东西

out vec4 fooColor;  // named this fooColor to make it clear you pick the name

void main() {
uvec4 unsignedIntValues = texture(tex, pixelCoordinate);
vec4 floatValues0To65535 = vec4(unsignedIntValues);
vec4 colorValues0To1 = floatValues0To65535 / 65535.0;
fooColor = colorValues0To1;
}

当然,您可以将数学运算缩短为一行

  fooColor = vec4(texture(tex, pixelCoordinate)) / 65535.0;

例子:

// image data
var w = 128;
var h = 128;
var size = w * h * 4;
var img = new Uint16Array(size); // need Uint16Array
for (var i = 0; i < img.length; i += 4) {
img[i + 0] = 65535; // r
img[i + 1] = i/64 * 256; // g
img[i + 2] = 0; // b
img[i + 3] = 65535; // a
}

// program
var canvas = document.getElementById('cv');
var gl = canvas.getContext('webgl2');

var program = gl.createProgram();
//var color_buffer_float_16ui = gl.getExtension('EXT_color_buffer_float'); // add for 16-bit

// texture
var tex = gl.createTexture(); // create empty texture
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(
gl.TEXTURE_2D, // target
0, // mip level
gl.RGBA16UI, // internal format -> gl.RGBA16UI
w, h, // width and height
0, // border
gl.RGBA_INTEGER, //format -> gm.RGBA_INTEGER
gl.UNSIGNED_SHORT, // type -> gl.UNSIGNED_SHORT
img // texture data
);

// buffer
var buffer = gl.createBuffer();
var bufferData = new Float32Array([
-1, -1,
1, -1,
1, 1,
1, 1,
-1, 1,
-1, -1
]);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, bufferData, gl.STATIC_DRAW);

// shaders
program.vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(program.vs, `#version 300 es
in vec4 vertex; // incoming pixel input?
out vec2 pixelCoordinate; // variable used to pass position to fragment shader
void main(){
gl_Position = vertex; // set pixel output position to incoming position (pass through)
pixelCoordinate = vertex.xy*0.5+0.5; // set coordinate for fragment shader
}
`);

program.fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(program.fs, `#version 300 es
precision highp float; // ?
uniform highp usampler2D tex; // ?
in vec2 pixelCoordinate; // receive pixel position from vertex shader
out vec4 fooColor;
void main() {
uvec4 unsignedIntValues = texture(tex, pixelCoordinate);
vec4 floatValues0To65535 = vec4(unsignedIntValues);
vec4 colorValues0To1 = floatValues0To65535 / 65535.0;
fooColor = colorValues0To1;
}
`);

gl.compileShader(program.vs);
checkCompileError(program.vs);
gl.compileShader(program.fs);
checkCompileError(program.fs);

function checkCompileError(s) {
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(s));
}
}

gl.attachShader(program,program.vs);
gl.attachShader(program,program.fs);

gl.deleteShader(program.vs);
gl.deleteShader(program.fs);

// program
gl.bindAttribLocation(program, 0, "vertex");
gl.linkProgram(program);
gl.useProgram(program);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 6); // execute program
<canvas id="cv" width="100" height="100"></canvas>

关于javascript - WebGL2 将 Uint16Array 作为图像渲染到 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57699322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com