EXT_texture_norm16 texture filterable
webgl
HTML
<canvas id="canvas" width=512 height=512></canvas>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See https://webgl2fundamentals.org/webgl/lessons/webgl-boilerplate.html
and https://webgl2fundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<script src="https://webgl2fundamentals.org/webgl/resources/webgl-utils.js"></script>
<script src="https://webgl2fundamentals.org/webgl/resources/m4.js"></script>
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
JavaScript
function main() {
const gl = document.querySelector('canvas').getContext('webgl2');
if (!gl) {
return alert('need WebGL2');
}
const ext = gl.getExtension("EXT_texture_norm16");
if (!ext) {
console.log("sorry, can't render to floating point textures");
return;
}
const vs = `#version 300 es
void main() {
gl_PointSize = 300.0;
gl_Position = vec4(0, 0, 0, 1);
}
`;
const fs = `#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
// our texture
uniform sampler2D u_image;
out vec4 color;
void main() {
vec4 intColor = texture(u_image, gl_PointCoord.xy);
color = vec4(vec3(intColor.rrr), 1);
}
`;
const program = twgl.createProgram(gl, [vs, fs]);
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
ext.R16_SNORM_EXT, // internal format
//ext.R16_EXT,
10, // width
1, // height
0, // border
gl.RED, // source format
gl.SHORT, // source type
new Int16Array([
32767, 2000, 3000, 4000, 5000, 16784, 7000, 8000, 9000, 32767
]));
// const alignment = 1;
// gl.pixelStorei(gl.UNPACK_ALIGNMENT, alignment);
// can't filter integer textures
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.useProgram(program);
// no need to set any attributes or
// uniforms as we're not using attributes
// and uniforms default to zero so will use
// texture unit zero
gl.drawArrays(gl.POINTS, 0, 1);
console.log('max point size:', gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE)[1]);
}
main();