Skip to content
This repository has been archived by the owner on Dec 22, 2024. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
matanlurey committed Aug 4, 2024
0 parents commit d2a4e46
Show file tree
Hide file tree
Showing 35 changed files with 1,706 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
29 changes: 29 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Check

on:
# Post-submit.
push:
branches: [ main ]

# Pre-submit.
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: dart-lang/[email protected]
- uses: actions/setup-node@v4
- uses: browser-actions/setup-chrome@v1
- run: dart pub get
- run: dart format --output none --set-exit-if-changed .
- run: dart analyze
- run: dart test
- run: ./chore coverage -- -P coverage
- name: Upload coverage
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: coverage/lcov.info
21 changes: 21 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Deploy

on:
# Post-submit.
push:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: dart-lang/[email protected]
- run: dart pub get
- name: Build API Docs
run: ./chore dartdoc
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./doc/api
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dart_tool/
coverage/
doc/api/
pubspec.lock
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"dart-code.dart-code"
]
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.1.0

- Initial release.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Matan Lurey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Mirage

Generates noise and patterns algorithmically for use in software, textures, and graphics.

![Mirage on pub.dev][pub_img]][pub_url]
[![Code coverage][cov_img]][cov_url]
[![Github action status][gha_img]][gha_url]
[![Dartdocs][doc_img]][doc_url]
[![Style guide][sty_img]][sty_url]

[pub_url]: https://pub.dartlang.org/packages/chaos
[pub_img]: https://img.shields.io/pub/v/mirage.svg
[gha_url]: https://github.com/matanlurey/mirage.dart/actions
[gha_img]: https://github.com/matanlurey/mirage.dart/actions/workflows/check.yaml/badge.svg
[cov_url]: https://coveralls.io/github/matanlurey/mirage.dart?branch=main
[cov_img]: https://coveralls.io/repos/github/matanlurey/mirage.dart/badge.svg?branch=main
[doc_url]: https://www.dartdocs.org/documentation/chaos/latest
[doc_img]: https://img.shields.io/badge/Documentation-mirage-blue.svg
[sty_url]: https://pub.dev/packages/oath
[sty_img]: https://img.shields.io/badge/style-oath-9cf.svg

## Usage

An example of generating a Simplex noise pattern:

![Simlex](./example/out/simplex_noise.png)

```dart
import 'package:mirage/mirage.dart';
void main() {
final noise = Simplex();
for (var y = 0; y < 256; y++) {
for (var x = 0; x < 256; x++) {
final value = noise.get2d(x / 256, y / 256);
// Do something with the value...
}
}
}
```

For a full example, see the [example](./example) directory.

## Features

- Full implementations of popular 2D noise functions.
- Simple and predictable API for generating noise patterns.
- Build your own generator functions with the `Pattern2d` interface.
- Thoughtfully tested and documented for ease of use.

## Contributing

To run the tests, run:

```shell
dart test
```

To check code coverage locally, run:

```shell
./chore coverage
```

To preview `dartdoc` output locally, run:

```shell
./chore dartodc
```
3 changes: 3 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Format: https://dart.dev/tools/analysis#the-analysis-options-file.

include: package:oath/strict.yaml
15 changes: 15 additions & 0 deletions chore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env sh

# Chore is a tiny task runner with some pre-defined commands.

set -e

GIT_URL="https://github.com/matanlurey/chore.dart.git"
GIT_REF="8b252e7"

# Silently activate the pinned version of `chore.dart` from GitHub.
# Only print if there is an error.
dart pub global activate -sgit $GIT_URL --git-ref=$GIT_REF 2>&1 > /dev/null

# Now run and forward arguments and the output.
dart pub global run chore "$@"
22 changes: 22 additions & 0 deletions dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://github.com/dart-lang/test/blob/master/pkgs/test/doc/configuration.md

presets:
# Runs VM-specific tests and captures coverage.
coverage:
reporter: github

# Runs tests on the VM and NodeJS.
all:
platforms:
- vm
- node

platforms:
- vm

fold_stack_frames:
except:
- checks
- test
- test_api
- stream_channel
33 changes: 33 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Examples

The provided script, [`generate_pngs.dart`](./generate_pngs.dart) uses this
library combined with `package:image` to generate the following images for each
pattern function:

## `Checkerboard`

![Checkerboard](./out/checkerboard.png)

## `White`

![White](./out/white_noise.png)

## `Value`

![Value](./out/value_noise.png)

## `Perlin`

![Perlin](./out/perlin_noise.png)

## `Simplex`

![Simplex](./out/simplex_noise.png)

## `Worley.distance`

![Worley.distance](./out/worley_distance.png)

## `Worley.value`

![Worley.value](./out/worley_value.png)
73 changes: 73 additions & 0 deletions example/generate_pngs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env dart

import 'dart:math';

import 'package:image/image.dart' as i;
import 'package:mirage/mirage.dart';
import 'package:path/path.dart' as p;

/// This script generates PNG previews for all patterns in the library.
///
/// ## Usage
///
/// ```sh
/// dart example/generate_pngs.dart [seed]
/// ```
void main(List<String> args) async {
final Random random;
if (args.isNotEmpty) {
random = Random(int.parse(args.first));
} else {
random = Random(0xDEADBEEF);
}

final output = p.join('example', 'out');

const width = 256;
const height = 256;
Future<void> generate(
String name,
Pattern2d Function(Random) generate,
) async {
final pattern = generate(random).normalized;
final image = i.Image(width: width, height: height);
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
// 0.0 = black, 1.0 = white
final value = pattern.get2d(x, y);
final color = (value * 255).toInt();
image.setPixel(x, y, i.ColorRgb8(color, color, color));
}
}
await i.writeFile(p.join(output, '$name.png'), i.encodePng(image));
}

await generate(
'checkerboard',
(_) => Checkerboard.odd,
);
await generate(
'white_noise',
White.new,
);
await generate(
'value_noise',
(r) => Value(NoiseTable(r)),
);
await generate(
'perlin_noise',
(r) => Perlin(NoiseTable(r)),
);
await generate(
'simplex_noise',
(r) => Simplex(NoiseTable(r)),
);
await generate(
'worley_value',
(r) => Worley.value(hasher: NoiseTable(r)),
);
await generate(
'worley_distance',
(r) => Worley.distance(hasher: NoiseTable(r)),
);
}
Binary file added example/out/checkerboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/out/perlin_noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/out/simplex_noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/out/value_noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/out/white_noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/out/worley_distance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/out/worley_value.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions lib/mirage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// Generates noise and patterns algorithmically for use in software, textures,
/// and graphics.
library;

export 'src/generator.dart';
export 'src/math.dart';
export 'src/noise/hasher.dart';
export 'src/noise/perlin.dart';
export 'src/noise/simplex.dart';
export 'src/noise/value.dart';
export 'src/noise/white.dart';
export 'src/noise/worley.dart';
export 'src/patterns.dart';
Loading

0 comments on commit d2a4e46

Please sign in to comment.