Created
November 24, 2022 08:09
-
-
Save greggman/c635f5f1ff38cf9f588705fcfa24f254 to your computer and use it in GitHub Desktop.
WebGL getUniform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas></canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gl = document.querySelector('canvas').getContext('webgl'); | |
const prg = createProgram( | |
gl, | |
`void main() { | |
gl_Position = vec4(1); | |
} | |
`, | |
` | |
precision highp float; | |
uniform vec4 foo; | |
uniform vec4 bar; | |
void main() { | |
gl_FragColor = foo + bar; | |
}`); | |
const fooLoc = gl.getUniformLocation(prg, 'foo'); | |
const barLoc = gl.getUniformLocation(prg, 'bar'); | |
console.log('foo', gl.getUniform(prg, fooLoc)); | |
console.log('bar', gl.getUniform(prg, barLoc)); | |
console.log('err:', glEnumToString(gl, gl.getError())); | |
function glEnumToString(gl, value) { | |
const keys = []; | |
for (const key in gl) { | |
if (gl[key] === value) { | |
keys.push(key); | |
} | |
} | |
return keys.length ? keys.join(' | ') : `0x${value.toString(16)}`; | |
} | |
function createShader(gl, type, src) { | |
const shader = gl.createShader(type); | |
gl.shaderSource(shader, src); | |
gl.compileShader(shader); | |
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
throw new Error(gl.getShaderInfoLog(shader)); | |
} | |
return shader; | |
} | |
function createProgram(gl, vs, fs, tf) { | |
const program = gl.createProgram(); | |
gl.attachShader(program, createShader(gl, gl.VERTEX_SHADER, vs)); | |
gl.attachShader(program, createShader(gl, gl.FRAGMENT_SHADER, fs)); | |
if (tf) { | |
gl.transformFeedbackVaryings(program, tf, gl.INTERLEAVED_ATTRIBS); // gl.SEPARATE_ATTRIBS); | |
} | |
gl.linkProgram(program); | |
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { | |
throw new Error(gl.getProgramInfoLog(program)); | |
} | |
return program; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"name":"WebGL getUniform","settings":{},"filenames":["index.html","index.css","index.js"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment