我期待实现键盘输入,这将允许用户改变生成的形状的颜色。下面是我实现的生成红色三角形的当前代码。我如何能够改变代码,以便如果用户按下'B‘在他们的键盘上,颜色的三角形将改变为蓝色,或绿色,如果'G’按下,等等。
最新执行情况:
// Regular Polygon - Generates Triangle (or 3-Sided Shape)
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'void main() {\n' +
' gl_Position = a_Position;\n' +
'}\n';
// Fragment shader program (assigns a color to fill in the polygon)
var FSHADER_SOURCE =
'void main() {\n' +
' uniform vec3 uColor;' +
' gl_FragColor = vec4(uColor, 1.0);\n' +
'}\n';
//Cyan vec4(0.0, 1.0, 1.0, 1.0)
//Brown vec(0.6, 0.5, 0.4, 1.0)
//Yellow vec4(1.0, 1.0, 0.0, 1.0)
function onKeyUp(event)
{
if (event.key == 'c')
{
gl.uniform3fv(u_Color, [0.0, 1.0, 1.0])
}
else if (event.key == 'y')
{
gl.uniform3fv(u_Color, [1.0, 1.0, 0.0])
}
else if (event.key == 'b')
{
gl.uniform3fv(u_Color, [0.6, 0.5, 0.4])
}
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
function main() {
// Retrieves canvas element
var canvas = document.getElementById('webgl');
// Acquires rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
// Write the positions of each vertex to a vertex shader
var n = initVertexBuffers(gl);
if (n < 0) {
console.log('Failed to set the positions of the vertices');
return;
}
u_Color = gl.getUniformLocation(gl.program, 'uColor')
if (u_Color === null)
{
console.log('Failed to get color uniform location');
return;
}
// Default value: brown color
gl.uniform3fv(u_Color, [0.6, 0.5, 0.4])
document.addEventListener('keyup', onKeyUp, false);
// Specify the color for clearing the canvas (in this case, white)
gl.clearColor(0, 0, 0, 0);
// Clears the canvas
gl.clear(gl.COLOR_BUFFER_BIT);
// Draws the triangle (or 3-sided shape)
gl.drawArrays(gl.TRIANGLES, 0, n);
function initVertexBuffers(gl) {
var vertices = new Float32Array([
0, 0.5, -0.5, -0.5, 0.5, -0.5
]);
var n = 3; // The number of vertices of the polygon
// Create a buffer object
var vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Buffer object binded to target
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// Data is written to buffer object
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
return -1;
}
// Position variable is assigned a buffer object
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
// Allows position variable to be assigned a buffer object
gl.enableVertexAttribArray(a_Position);
return n;
}
发布于 2020-05-17 09:49:24
通过将颜色作为统一值传递给着色器,您可以轻松地完成这项工作。
var FSHADER_SOURCE =
'precision mediump float;\n' +
'uniform vec3 uColor;\n' +
'void main() {\n' +
' gl_FragColor = vec4(uColor, 1.0);\n' +
'}\n';
编写一个keyup事件处理程序,并根据event.key值设置统一值
若要设置默认颜色,请在程序准备就绪并投入使用后,将其添加到init方法中。
u_Color = gl.getUniformLocation(gl.program, 'uColor')
if (u_Color === null)
{
console.log('Failed to get color uniform location');
return;
}
// Default value: white color
gl.uniform3fv(u_Color, [1.0, 1.0, 1.0])
要处理键盘事件,请将其添加到init方法中。
document.addEventListener('keyup', onKeyUp, false);
并将此函数放入代码中(我允许您设置变量和作用域)
function onKeyUp(event)
{
if (event.key == 'g')
{
gl.uniform3fv(u_Color, [0.0, 1.0, 0.0])
}
else if (event.key == 'b')
{
gl.uniform3fv(u_Color, [0.0, 0.0, 1.0])
}
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
https://stackoverflow.com/questions/61849638
复制相似问题