如何在 WebGL 中实现键盘输入?

How to implement keyboard input in WebGL?

我正在寻求实现允许用户更改生成形状颜色的键盘输入。下面是我已经实现的生成红色三角形的当前代码。我怎样才能改变代码,以便如果用户在键盘上按下 '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;
}

您可以通过将颜色作为统一值传递给着色器来轻松完成这项工作

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])

要处理键盘事件,请将其添加到您的初始化方法中

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);
}