2.0.1 • Published 2 years ago

regl-insta-lines v2.0.1

Weekly downloads
10
License
MIT
Repository
-
Last release
2 years ago

Instanced Lines for REGL

Highly extendable instanced line rendering in a single draw call.

Based on Regl.

Loosely based on Rye Terrell's Instanced Line Rendering, highly recommended as introduction.

Live demo

Installation

npm install --save regl-insta-lines
# or
yarn add regl-insta-lines

Features:

  • batch rendering of lines
  • vertex shader expanded
  • start / end caps (butt, square, round, arrow(size, angle))
  • joins (bevel, miter(limit), miterSquare, round)
  • closed loop support
  • GLSL injectable to tailor to your needs (e.g. do you have a non-linear camera?)
  • full type support
filledwireframe
npm.ionpm.io
UVsDecoration using UV.y
npm.ionpm.io

API

Example usage:

import createRegl from 'regl';
import { createLines, CapType, JoinType } from 'regl-insta-lines';

const regl = createRegl();

// create lines
const lines = createLines(regl, {
  // points can be specified using 2 or 3 floats, typescript will help you enforce this
  dimension: 2,
  width: 60,  // in pixels
  cap: CapType.square,
  join: JoinType.miter(2),  // specify limit here
  joinCount: 3,
  cameraTransform: glsl`
    // optional
    vec4 cameraTransform(vec4 pos) {
      return ... your cool non-linear camera
    }
  `,
  ... other props
});

// describe a batch of lines
lines.setLines([{
  // if dimension=2
  points: [[0, 0], [1, 0], [1, 1]],
  // if dimension=3
  points: [[0, 0, 0], [1, 0, 0], [1, 1, 0]],
  // each line in the batch can be closed or open
  closed: true
}, {
  ...
}]);


// render them
regl.frame(()=>{
  // set width each frame
  lines.setWidth(30 * Math.sin(ctx.time))  // in pixels
  // single draw call rendering
  lines.render();
})

Constructor:

PropertyTypeDefaultDescription
widthnumber1.0Width of lines in pixels. Can also be set using .setWidth(width) function after creation.
capstring (GLSL) or { start: GLSL; end: GLSL; }CapType.buttAny of CapTypes or your own custom GLSL function. Supported: butt, square, round
joinstring (GLSL)JoinType.bevelAny of JoinTypes or your own custom GLSL function. Supported: bevel, miter, round, miterSquare
joinCountint4Number of triangles approximating the joins. NOTE: joins (like miter or round) effectively become bevel joins when set to 1.
distanceFn(a: vec3, b: vec3) => numbervec3.distanceFunction that calculates distance between two points. Used to determine distanceAlongPath varying for fragment shader (useful for dashes for example)
fragstring (GLSL)fill whiteOptional fragment shader, gets the following varyings: vec3 vPos; vec2 vUv; vec3 distanceAlongPath;. NOTE: Even though it's optional here, if you don't specify it, you must wrap the render call inside a regl command that does provide it, otherwise regl will scream at you.
reverseMiterLimitnumber0.5How far up the segment can a reverse miter go. Anything more than 0.5 runs a risk of failure, but allows sharper angles to still be reverse mitered.
cameraTransformGLSLstring (GLSL)identityOptional GLSL code for a function of the following definition: vec4 cameraTransform(vec4 pos);
declarationsGLSLstring (GLSL)identityOptional GLSL declarations code. At least vec4 cameraTransform(vec4 pos);. Useful for custom attributes.
defineVerticesGLSLstring (GLSL)undefinedOptional GLSL code that defines vertices vec3 p0, p1, p2, p3;, or skips a segment by setting float skip; to something other than 0.0
postprocessVerticesGLSLstring (GLSL)undefinedOptional GLSL code that modifies clip or screen-space coordinates.
mainEndGLSLstring (GLSL)undefinedOptional GLSL code that runs at the end of main body.
primitiveregl's primitive typetrianglesUsed for debug when rendering with lines for example

Extending the base

The main function createLines is actually a wrapper around the base implementation createLineBase that provides convenience around batch rendering and distance calculation. If you would like to manage your own attribute buffers, you can use createLineBase function directly. In this case, you can use createLines as a reference implementation since describing how to use it here is a bit verbose, but the interface is very close to what is described in the API section.

Future Improvements

  • provide a few useful fragment shaders (e.g. dashes). Until then, check out the ones in Live demo.

Combination Test

Combination Test Filled

Combination Test

NOTE: "extra" lines are simply an artifact of rendering with 'line strip'. In reality each segment is represented as 4 triangles + 2 join accordions, one on each side. Even though each segment renders an accordion on both sides, only one is visible per join because the other one gets back-face culled.

Anatomy of a single segment

You can view this at Geogebra

Segment

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.8

4 years ago

0.2.7-c

4 years ago

0.2.7-b

4 years ago

0.2.7-a

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago