webgl_helper 0.4.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 38

webgl_helper #

A small helper library to reduce boilerplate code when using the WebGL API.

Features #

The development of this library has just started. Many features are not completely implemented yet or missing at all.

  • Reduces boilerplate code, e.g. connecting buffers to vertex attrib locations and draw in 1 call.
  • Makes the WebGL API more type safe by adding types for a number of WebGL constants.

This library is not a high level library like three.js.

Version 0.4.0 #

  • Added support for Dart 2.

Version 0.3.0 #

  • Added support for Dart strong mode.

example/main.dart

import 'dart:html';
import 'package:webgl_helper/webgl_helper.dart';

ShaderProgram program;
Buffer buffer;

main() {
  // query the canvas element
  var canvasElement = querySelector('#webgl-canvas');

  // init the webgl rendering context
  glInit(canvasElement);

  // set the color used to clear the color buffer
  glClearColor(0, 0, 0, 1);

  // enable blending and set the blend function
  glEnableBlend(true);
  glBlendFunc(BlendingCoefficient.SRC_ALPHA, BlendingCoefficient.ONE_MINUS_SRC_ALPHA);

  // create a shader program
  program = new ShaderProgram.fromSources(vertexShader, fragmentShader);

  // create a buffer that contains 2 types of per vertex data: positions and colors
  buffer = new Buffer(BufferTarget.ARRAY_BUFFER);
  buffer.data({
    'a_position': {
      Buffer.SIZE: 2, // 2 components per vertex (x, y)
      Buffer.DATA: [1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0]
    },
    'a_color': {
      Buffer.SIZE: 3, // 3 components per vertex (r, g, b)
      Buffer.DATA: [1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0]
    }
  });

  // start rendering
  window.requestAnimationFrame(render);
}

render(tick) {
  // clear the color buffer
  glClear([ClearMode.COLOR_BUFFER]);

  // draw the vertices using the given shader program and the vertex data stored in the buffer. additionally, set an
  // uniform.
  gluDrawArrays(program, buffer, uniformSetters: {
    'u_alpha': (uniform) => uniform.set1f(0.5)
  });

  // next frame
  window.requestAnimationFrame(render);
}

// the vertex shader...
final vertexShader = '''
#version 100

precision mediump float;

attribute vec2 a_position;
attribute vec3 a_color;
varying vec3 v_color;

void main() {
  v_color = a_color;
  gl_Position = vec4(a_position / 2.0, 0.0, 1.0);
}
''';

// ...and the fragment shader
final fragmentShader = '''
#version 100

precision mediump float;

uniform float u_alpha;
varying vec3 v_color;

void main() {
  gl_FragColor = vec4(v_color, u_alpha);
}
''';

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  webgl_helper: ^0.4.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:webgl_helper/webgl_helper.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
90
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
57
Overall:
Weighted score of the above. [more]
38
Learn more about scoring.

We analyzed this package on Oct 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: web

Primary library: package:webgl_helper/webgl_helper.dart with components: html.

Health issues and suggestions

Document public APIs. (-1 points)

158 out of 158 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/context.dart. (-3.93 points)

Analysis of lib/src/context.dart reported 8 hints, including:

line 24 col 24: Don't explicitly initialize variables to null.

line 26 col 23: Avoid types as parameter names.

line 28 col 28: Avoid types as parameter names.

line 102 col 76: Use = to separate a named parameter from its default value.

line 103 col 1: Don't explicitly initialize variables to null.

Fix lib/webgl_helper.dart. (-1.99 points)

Analysis of lib/webgl_helper.dart reported 4 hints:

line 25 col 25: Use lowercase_with_underscores when specifying a library prefix.

line 26 col 29: Use lowercase_with_underscores when specifying a library prefix.

line 27 col 23: Use lowercase_with_underscores when specifying a library prefix.

line 28 col 50: Use lowercase_with_underscores when specifying a library prefix.

Fix lib/src/framebuffer.dart. (-1 points)

Analysis of lib/src/framebuffer.dart reported 2 hints:

line 5 col 22: Don't explicitly initialize variables to null.

line 35 col 23: Don't explicitly initialize variables to null.

Fix additional 6 files with analysis or formatting issues. (-2.50 points)

Additional issues in the following files:

  • lib/src/uniform.dart (2 hints)
  • lib/src/vertexattrib.dart (2 hints)
  • lib/src/constants.dart (1 hint)
  • lib/src/buffer.dart (Run dartfmt to format lib/src/buffer.dart.)
  • lib/src/shader.dart (Run dartfmt to format lib/src/shader.dart.)
  • lib/src/texture.dart (Run dartfmt to format lib/src/texture.dart.)

Maintenance suggestions

Package is getting outdated. (-23.01 points)

The package was last published 64 weeks ago.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.69.0 <3.0.0
vector_math ^2.0.8 2.0.8
Dev dependencies
build_runner ^0.9.2
build_web_compilers ^0.4.1