webgl_helper 0.4.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 46

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';
  
Version Uploaded Documentation Archive
0.4.0 Jul 25, 2018 Go to the documentation of webgl_helper 0.4.0 Download webgl_helper 0.4.0 archive
0.3.0 Oct 30, 2016 Go to the documentation of webgl_helper 0.3.0 Download webgl_helper 0.3.0 archive
0.2.0 Mar 28, 2016 Go to the documentation of webgl_helper 0.2.0 Download webgl_helper 0.2.0 archive
0.1.0 Mar 1, 2016 Go to the documentation of webgl_helper 0.1.0 Download webgl_helper 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
3
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

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

  • Dart: 2.2.0
  • pana: 0.12.14

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/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.

Format lib/src/buffer.dart.

Run dartfmt to format lib/src/buffer.dart.

Fix additional 6 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/constants.dart (Run dartfmt to format lib/src/constants.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.)
  • lib/src/uniform.dart (Run dartfmt to format lib/src/uniform.dart.)
  • lib/src/vertexattrib.dart (Run dartfmt to format lib/src/vertexattrib.dart.)
  • lib/webgl_helper.dart (Run dartfmt to format lib/webgl_helper.dart.)

Maintenance suggestions

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