execute method

  1. @override
void execute(
  1. RenderingContext gl,
  2. Camera cam, {
  3. Matrix4? projectionMatrix,
  4. Matrix4? viewMatrix,
})
override

Record commands and draw.

Implementation

@override
void execute(
  RenderingContext gl,
  Camera cam, {
  Matrix4? projectionMatrix,
  Matrix4? viewMatrix,
}) {
  if (_prog == null ||
      source.texture == null ||
      source.splatCount == 0 ||
      projectionMatrix == null ||
      viewMatrix == null) {
    return;
  }

  // Pipeline state (viewport already set by main renderer)
  gl
    // We need to enable if we want to merge with 3D content
    ..disable(WebGL.DEPTH_TEST)
    ..depthMask(false)
    // ..depthFunc(WebGL.LEQUAL)
    ..enable(WebGL.BLEND)
    ..blendFuncSeparate(
      WebGL.ONE,
      WebGL.ONE_MINUS_SRC_ALPHA,
      WebGL.ONE,
      WebGL.ONE_MINUS_SRC_ALPHA,
    )
    ..blendEquationSeparate(WebGL.FUNC_ADD, WebGL.FUNC_ADD);

  if (disableAlphaWrite) gl.colorMask(true, true, true, false);

  // Program + uniforms
  gl.useProgram(_prog);
  if (_uProjection != null) {
    gl.uniformMatrix4fv(_uProjection!, false, projectionMatrix.storage);
  }
  if (_uView != null) {
    gl.uniformMatrix4fv(_uView!, false, viewMatrix.storage);
  }
  if (_uFocal != null) {
    gl.uniform2f(_uFocal!, cam.focalXForShader(), cam.focalYForShader());
  }
  if (_uViewport != null) {
    gl.uniform2f(_uViewport!, cam.width.toDouble(), cam.height.toDouble());
  }
  if (_uSplatCount != null) gl.uniform1i(_uSplatCount!, source.splatCount);
  if (_uMaxSplatSize != null) {
    gl.uniform1f(_uMaxSplatSize!, maxSplatPixelSize);
  }

  // Bind textures to the units we fixed above.
  gl
    ..activeTexture(WebGL.TEXTURE0)
    ..bindTexture(WebGL.TEXTURE_2D, source.texture);

  if (order.texture != null) {
    gl
      ..activeTexture(WebGL.TEXTURE1)
      ..bindTexture(WebGL.TEXTURE_2D, order.texture);
  }

  // Geometry
  if (_aPosition != null) {
    gl
      ..enableVertexAttribArray(_aPosition!)
      ..bindBuffer(WebGL.ARRAY_BUFFER, _vbo)
      ..vertexAttribPointer(_aPosition!, 3, WebGL.FLOAT, false, 0, 0);
  }

  gl
    ..bindBuffer(WebGL.ELEMENT_ARRAY_BUFFER, _ebo)
    ..drawElementsInstanced(
      WebGL.TRIANGLES,
      _indicesPerBatch,
      WebGL.UNSIGNED_SHORT,
      0,
      _instanceCount,
    );

  if (_aPosition != null) gl.disableVertexAttribArray(_aPosition!);
  if (disableAlphaWrite) gl.colorMask(true, true, true, true);
}