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

image-to-heightmap

v1.0.0

Published

Convert an image into a heightmap, based on the pixels' colors

Downloads

38

Readme

image-to-heightmap npm version Build Status

Convert a JPG or PNG image into a heightmap array

Initial Motivation

To use when creating terrains in WebGL

ex:

  1. Make a hilly terrain in a 3d modeling tool
  2. Save as heightmap image
  3. Use image-to-heightmap on image
  4. Generate a grid of GL_TRIANGLES (tiles) using the heights from the heightmap

To Install

$ npm install --save image-to-heightmap

Usage

var toHM = require('image-to-heightmap')

// This will generate an 11 x 6 heightmap array from our image
// Why 11 x 6 ?
//
// 10 x 5 tiles means we have 11 x 6 different corners
toHM('some-image.png', 10, 5, 2, function (err, heightmap) {
    console.log(heightmap[0][0])
    // The height of the point at the bottom left corner
})

What are tiles ?

Picture a terrain that's made up of a bunch of equally sized rectangles

Each rectangle has 4 corners. Each of these corners has a height

Rectangles will share some corners with adjacent rectangles

1 tile wide x 1 tile high = 2 corners wide x 2 corners height

2 tile wide x 3 tile high = 3 corners wide x 4 corners height

etc ...

TODO:

Maybe call this a grid instead of tiles?

API

toHM(imageURL, numXTiles, numYTiles, scale, callback)

imageURL

Required

Type: string

imageURL is the path to the file. It can be a relative path, an http url, a data url, or an in-memory Buffer

This gets passed into scijs/get-pixels

numXTiles

Required

Type: number

The number of tiles wide that your terrain will be

numYTiles

Type: number

The number of tiles deep that your terrain will be

scale

Type: number

image-to-heightmap generates heights with 0 <= height <= scale

A black pixel will have a height of 0

A white pixel will have a height of scale

A pixel with rgba of 50, 50, 50, 255 would have a height of (50 / 255 * scale)

callback

Type: function

Called with an error and your 2-dimensional heightmap array

function cb (err, heightmap) {
  console.log(heightmap[1,1])
  // The height at the top right corner of the bottom left tile
}

TODO:

  • Talk about what types of images to use
  • Add defaults? Maybe a default scale of 1?
  • Test edge cases. ex: what happens when trying to make a 0 x 0 grid?

License

MIT