simplex-noise.js

Javascript 语言中的快速单纯形噪声实现。(A fast simplex noise implementation in Javascript.)

Github stars Tracking Chart

simplex-noise.js

simplex-noise.js 是 Javascript 中的一种快速的单纯形噪声实现。它可以在浏览器和 nodejs 上运行。

安装

npm i -S simplex-noise

用法

在默认情况下,simplex-noise.js 将使用 Math.random() 来植入噪声。

// initializing a new simplex instance
// do this only once as it is relatively expensive
var simplex = new SimplexNoise(),
    value2d = simplex.noise2D(x, y),
    value3d = simplex.noise3D(x, y, z),
    value4d = simplex.noise4D(x, y, z, w);

你也可以传入一个种子字符串,然后用它来初始化在 alea PRNG 中构建的噪音。

var simplex = new SimplexNoise('seed'),
    value2d = simplex.noise2D(x, y),
    sameSeed = new SimplexNoise('seed'),
    differentSeed = new SimplexNoise('different seed');

sameSeed.noise2D(x, y) === value2d
differentSeed.noise2D(x, y) !== value2d

您还可以向用于构建置换表的构造函数传递一个可选的随机函数。这可以与自定义伪随机数生成器一起使用。

var random = new Alea(seed),
    simplex = new SimplexNoise(random),
    value2d = simplex.noise2D(x, y);

ALEA PRNG 可以在 npm 包 alea 中找到。

Node.js

Node.js 也受支持,您可以使用 npm 安装该软件包。

var SimplexNoise = require('simplex-noise'),
    simplex = new SimplexNoise(Math.random),
    value2d = simplex.noise2D(x, y);

基准测试

对于开发,您可以打开 perf/index.html 并查看控制台,或在 shell 中运行 node perf/benchmark.js。还有一个 rake 任务,用于比较您当前的更改,也可以运行 make compare。 该命令使用 git stash 起作用。

测试

这个库有一些简单的单元测试来运行它们。

要求

它需要类型化的数组。如果要在不支持的浏览器中使用它,则需要使用诸如 typedarray.js 之类的 polyfill。

许可

版权所有(c)2015 Jonas Wagner,已根据 MIT 许可获得许可(随附)

致谢

这主要是 Stefan Gustavson 和 Peter Eastman 编写的 Java 实现的直接 javascript 移植。

集成的伪随机发生器基于 JohannesBaagøe 的代码。

typescript 定义由 Neonit 提供。


(The first version translated by vz on 2020.08.08)

Main metrics

Overview
Name With Ownerjwagner/simplex-noise.js
Primary LanguageTypeScript
Program languageJavaScript (Language Count: 5)
PlatformLinux, Mac, Web browsers, Windows
License:MIT License
所有者活动
Created At2012-08-07 19:31:59
Pushed At2024-07-26 17:06:06
Last Commit At2021-08-22 10:37:53
Release Count9
Last Release Name4.0.3 (Posted on )
First Release Namelist (Posted on )
用户参与
Stargazers Count1.7k
Watchers Count28
Fork Count133
Commits Count143
Has Issues Enabled
Issues Count30
Issue Open Count5
Pull Requests Count16
Pull Requests Open Count2
Pull Requests Close Count21
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

simplex-noise.js

Build Status

simplex-noise.js is a fast simplex noise implementation in Javascript. It works in the browser and on nodejs.

Demos

Installation

npm i -S simplex-noise

Usage

By default simplex-noise.js will use Math.random() to seed the noise.

// initializing a new simplex instance
// do this only once as it is relatively expensive
var simplex = new SimplexNoise(),
    value2d = simplex.noise2D(x, y),
    value3d = simplex.noise3D(x, y, z),
    value4d = simplex.noise4D(x, y, z, w);

You can also pass in a seed string which will then be used to initialize
the noise using the built in alea PRNG.

var simplex = new SimplexNoise('seed'),
    value2d = simplex.noise2D(x, y),
    sameSeed = new SimplexNoise('seed'),
    differentSeed = new SimplexNoise('different seed');

sameSeed.noise2D(x, y) === value2d
differentSeed.noise2D(x, y) !== value2d

You can also pass an alternative random function to the constructor that is
used to build the permutation table.
This can be used with a custom pseudo random number generator:

var random = new Alea(seed),
    simplex = new SimplexNoise(random),
    value2d = simplex.noise2D(x, y);

The ALEA PRNG can be found on in the npm package alea.

node.js

Node.js is also supported, you can install the package using npm.

var SimplexNoise = require('simplex-noise'),
    simplex = new SimplexNoise(Math.random),
    value2d = simplex.noise2D(x, y);

Benchmarks

For development you can open perf/index.html and watch the console or run node perf/benchmark.js in a shell.
There is also a rake task for comparing your current changes can also run make compare.
The command works using git stash.

Tests

There are some simple unit tests for this library to run them

npm install && npm test

Changelog

2.4.0

  • Included a PRNG based on ALEA to directly allow seeding
  • Included typescript definitions

2.3.0

:warning: This release changes the output of the noise functions. :warning:

In the future such changes will be released as a new major version.

  • Corrected generation of permutation table
  • Moved tests to mocha/chai
  • Cleanup

2.2.0

  • Small performance improvement for 2D noise

2.1.1

  • Increased entropy by fixing a little initialization issue.

2.1.0

  • AMD support

2.0.0

  • Changed node.js api, SimplexNoise is now exported directly.
  • Added unit tests

1.0.0

  • Initial Release

Requirements

It requires typed arrays. If you want to use it in browsers without support
you will need to use a polyfill like typedarray.js.

License

Copyright (c) 2015 Jonas Wagner, licensed under the MIT License (enclosed)

Credits

This is mostly a direct javascript port of the Java implementation
by Stefan Gustavson and Peter Eastman.

The integrated pseudo random generator is based on code by by Johannes Baagøe.

The typescript definition has been provided by Neonit.