npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

screen-projected-lines

v2.0.1

Published

draw wireframes using triangles in screen-projected coordinates

Downloads

51

Readme

screen-projected-lines

draw wireframes using triangles in screen-projected coordinates

This module triangulates edges from a simplicial complex and returns arrays you can use as attributes in a vertex shader.

This module does not currently support miter joins, but that would be nice to have.

example

In this demo, we will draw a wireframe sphere with an exaggerated sense of depth by dividing the offset vector by p.z.

view this demo

var regl = require('regl')()
var camera = require('regl-camera')(regl, { distance: 1.7 })
var icosphere = require('icosphere')
var glsl = require('glslify')
var wireframe = require('screen-projected-lines')

var draw = sphere(regl)
regl.frame(function () {
  regl.clear({ color: [0,0,0,1], depth: true })
  camera(function () { draw() })
})

function sphere (regl) {
  var mesh = wireframe(icosphere(2))
  return regl({
    frag: `
      precision mediump float;
      void main () {
        gl_FragColor = vec4(0,1,1,1);
      }
    `,
    vert: glsl`
      #pragma glslify: linevoffset = require('screen-projected-lines')
      precision mediump float;
      uniform mat4 projection, view;
      uniform float aspect;
      attribute vec3 position, nextpos;
      attribute float direction;
      void main () {
        mat4 proj = projection * view;
        vec4 p = proj*vec4(position,1);
        vec4 n = proj*vec4(nextpos,1);
        vec4 offset = linevoffset(p, n, direction, aspect);
        gl_Position = p + offset*0.02/p.z;
      }
    `,
    attributes: {
      position: mesh.positions,
      nextpos: mesh.nextPositions,
      direction: mesh.directions
    },
    elements: mesh.cells,
    uniforms: {
      aspect: function (context) {
        return context.viewportWidth / context.viewportHeight
      }
    }
  })
}

To compile this code, use browserify with the glslify transform:

browserify sphere.js -t glslify > bundle.js

api

var wireframe = require('screen-projected-lines')
#pragma glslify: linevoffset = require('screen-projected-lines')

var wmesh = wireframe(mesh, opts={})

Create a wireframe mesh given an existing triangular mesh.

The wireframe mesh has these properties:

  • wmesh.positions - array of vertex [x,y,z] arrays
  • wmesh.cells - array of triangle indices
  • wmesh.nextPositions - array of the next vertex coordinates
  • wmesh.directions - array of values to use for which side of each vertex
  • wmesh.attributes - extra attributes declared alongside vertices
  • wmesh.angles - array of angles betwen the triangles that share an edge

The normals are not computed on the CPU here so that you can apply additional displacements in your vertex shader.

Optionally provide opts.attributes to declare attributes alongside the original vertices. For example, if you want to use computed surface normals to displace your wireframe mesh, you could do:

var anormals = require('angle-normals')
var wmesh = wireframe(mesh, {
  attributes: {
    normals: anormals(mesh.cells, mesh.positions)
  }
})

and then you can use wmesh.attributes.normals alongside wmesh.positions as an attribute.

vec4 offset = linevoffset(vec4 pos, vec4 nextpos, float direction, float aspect)

Return the screen offset to apply to pos given the current vertex screen position pos, the screen position of the next vertex in the edge nextpos, the direction of the vertex (-1.0 or 1.0), and the aspect ratio.

The offset vector is normalized, so multiply by a constant to adjust thickness.

To have the thinkness vary with depth like everything else:

vec4 offset = linevoffset(p, n, direction, aspect);
gl_Position = p + offset*0.02;

To have the thinkness have an exaggerated sense of depth:

vec4 offset = linevoffset(p, n, direction, aspect);
gl_Position = p + offset*0.02/p.z;

To have the thinkness not vary with depth:

vec4 offset = linevoffset(p, n, direction, aspect);
gl_Position = p + offset*0.02*p.z;

install

npm install screen-projected-lines

license

BSD