Skip to content

Commit

Permalink
Add node export for desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
attackgoat committed Dec 24, 2023
1 parent 6efc9e7 commit 2ace1be
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 231 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# `noise_gui`

[![GitHub](https://img.shields.io/badge/github-attackgoat/noise__gui-blue?logo=github)](https://github.com/attackgoat/noise_gui)
[![crates.io](https://img.shields.io/crates/v/noise_gui)](https://crates.io/crates/noise_gui)
[![docs.rs](https://img.shields.io/docsrs/noise_gui)](https://docs.rs/noise_gui/latest/noise_gui/)
[![GitHub Pages](https://img.shields.io/github/actions/workflow/status/attackgoat/noise_gui/main.yml)](https://github.com/attackgoat/noise_gui/actions/workflows/main.yml)
[![MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/attackgoat/noise_gui/blob/master/LICENSE-MIT)
[![Apache](https://img.shields.io/badge/license-Apache-blue.svg)](https://github.com/attackgoat/noise_gui/blob/master/LICENSE-APACHE)
[![GitHub Pages](https://img.shields.io/github/actions/workflow/status/attackgoat/noise_gui/main.yml)](https://github.com/attackgoat/noise_gui/actions/workflows/main.yml)

---

Expand All @@ -18,11 +20,13 @@ A graphical user interface for [Noise-rs](https://github.com/Razaekel/noise-rs).

- [x] Support for all [Noise-rs](https://github.com/Razaekel/noise-rs) `NoiseFn` implementations
- [ ] Allow zoom/pan on preview images
- [x] Allow saving the graph project to a file (_desktop only_)
- [ ] Allow image/data export
- [x] Allow saving the graph project to a file[^1]
- [x] Allow noise function export[^1]
- [ ] Automatic `NoiseFn` cached values
- [x] WASM support using [Trunk](https://trunkrs.dev/)

[^1]: Available on desktop only

> [!WARNING]
> `noise_gui` is currently in the proof-of-concept phase and may contain bugs and missing features.
Expand Down Expand Up @@ -55,7 +59,18 @@ Browser:
trunk serve --open
```

## Data model export
## Noise Function Export

Completed noise graphs may be exported (_right-click on any node_). The output file is `.ron` format
and may be deserialized for use in your programs.

TBD: Once a graph has been completed you may export it to a file (RON?) and later reload the noise
graph and set named constants before evaluating points.
Once deserialized into an `Expr` instance you may replace any decimal or integer values using their
name and the `Expr::set_f64` and `Expr::set_u32` functions. Note that node names do not have to be
unique and that all nodes sharing the provided name will be updated. The `Expr::noise` function may
be used to retrieve a Noise-rs `NoiseFn` implementation.

See the example for more details:

```bash
cargo run --example read_file
```
17 changes: 17 additions & 0 deletions examples/read_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use noise_gui::Expr;

fn main() {
let mut test_expr: Expr = ron::from_str(include_str!("test.ron")).unwrap();

// This gets a noise value which is identical to what was designed in the GUI
println!("Sampled value: {}", test_expr.noise().get([0.0, 0.0, 0.0]));

// This replaces the value of a variable and samples a new noise function:
println!(
"Updated value: {}",
test_expr
.set_f64("my-var", 42.0)
.noise()
.get([0.0, 0.0, 0.0])
);
}
4 changes: 4 additions & 0 deletions examples/test.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Displace((
source: Cylinders(Value(0.78)),
axes: (Add((Perlin(Value(0)), Constant(Variable("my-var", 0.18)))), Add((Perlin(Value(0)), Constant(Variable("my-var", 0.18)))), Constant(Value(0.0)), Constant(Value(0.0))),
))
16 changes: 10 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use {
de::from_reader,
ser::{to_writer_pretty, PrettyConfig},
},
serde::Serialize,
std::{
fs::OpenOptions,
path::{Path, PathBuf},
Expand All @@ -52,7 +53,7 @@ pub struct App {

impl App {
#[cfg(not(target_arch = "wasm32"))]
const EXTENSION: &'static str = "ron";
pub const EXTENSION: &'static str = "ron";

const IMAGE_COUNT: usize = Threads::IMAGE_COORDS as usize * Threads::IMAGE_COORDS as usize;
const IMAGE_SIZE: [usize; 2] = [
Expand Down Expand Up @@ -93,7 +94,7 @@ impl App {
}

#[cfg(not(target_arch = "wasm32"))]
fn file_dialog() -> FileDialog {
pub fn file_dialog() -> FileDialog {
FileDialog::new().add_filter("Noise Project", &[Self::EXTENSION])
}

Expand Down Expand Up @@ -127,7 +128,10 @@ impl App {
}

#[cfg(not(target_arch = "wasm32"))]
fn save_as(&self, path: impl AsRef<Path>) -> anyhow::Result<()> {
pub fn save_as<T>(path: impl AsRef<Path>, value: &T) -> anyhow::Result<()>
where
T: ?Sized + Serialize,
{
let mut path = path.as_ref().to_path_buf();

if path.extension().is_none() {
Expand All @@ -143,7 +147,7 @@ impl App {
warn!("Unable to create file");
err
})?;
to_writer_pretty(file, &self.snarl, PrettyConfig::default()).map_err(|err| {
to_writer_pretty(file, value, PrettyConfig::default()).map_err(|err| {
warn!("Unable to write file");
err
})?;
Expand Down Expand Up @@ -332,7 +336,7 @@ impl eframe::App for App {

if let Some(path) = &self.path {
if ui.button("Save").clicked() {
self.save_as(path).unwrap_or_default();
Self::save_as(path, &self.snarl).unwrap_or_default();

ui.close_menu();
}
Expand All @@ -345,7 +349,7 @@ impl eframe::App for App {

if ui.button("Save As...").clicked() {
if let Some(path) = Self::file_dialog().save_file() {
self.save_as(&path).unwrap_or_default();
Self::save_as(&path, &self.snarl).unwrap_or_default();
self.path = Some(path);
}

Expand Down
Loading

0 comments on commit 2ace1be

Please sign in to comment.