Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shader/slug-ring #69

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ ______________________________________________________________________
- The shader will also be versioned, alongside that screenshot
- Rename and move the screenshot to `assets/screenshots/<NAME YOUR OWN DIR>/screenshot.png`
- Rename and move the versioned shader to `assets/shaders/<NAME YOUR OWN DIR/YOURSHADER.wgsl`
- If renaming and moving things manually is not your style, you can use the provided script in `/scripts/`:

```shell
python scripts/screenshots-to-gallery.py "screenshots/17-11-23/06-10-55/screenshot.png" "Your Title"
```

- Add it to the #Gallery in the `README.md`

# Shadertoy ports:
Expand Down
72 changes: 72 additions & 0 deletions assets/Gallery/slug-ring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## slug-ring

![photo](screenshot.png)

## Fragment:

````
```rust
#import bevy_sprite::mesh2d_view_bindings::globals
````

#import shadplay::shader_utils::common::{NEG_HALF_PI, PI, shader_toy_default, rotate2D, TWO_PI}
#import bevy_render::view::View
#import bevy_pbr::forward_io::VertexOutput;

@group(0) @binding(0) var<uniform> view: View;

const SPEED:f32 = 0.4;
const N:i32 = 120;

/// This is a sharderoy port of 'slug-ring' by amagitakayosi: https://www.shadertoy.com/view/clVyR1c
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {

```
// Setup
var uv = (in.uv * 2.0) - 1.0;
let resolution = view.viewport.zw;
let t = globals.time * SPEED;
uv.x *= resolution.x / resolution.y;
uv *= rotate2D(NEG_HALF_PI);
```

/// Their actual shader...
return slug_ring(uv, resolution, t);
}

/// Their actual shader...
fn slug_ring(uv: vec2f, resolution: vec2f, time: f32)->vec4f{

```
let R: vec2<f32> = resolution.xy;
let y_inverted_location = vec2<i32>(i32(uv.x), i32(R.y) - i32(uv.y));
let location = vec2<i32>(i32(uv.x), i32(uv.y));

var fragCoord = vec2<f32>(f32(location.x), f32(location.y) );

var p: vec2<f32> = uv;
p.x = p.x * (resolution.x / resolution.y);
p = p * (2.);
let a: f32 = atan2(p.y, p.x);
var col: vec3<f32>;

for (var i: i32 = 0; i < N; i = i + 1) {
let fi: f32 = f32(i);
let t: f32 = fi / f32(N);
let aa: f32 = (t + time / 12.) * 2. * PI;
let size: f32 = 0.3 + sin(t * 6. * PI) * 0.1;
var a1: f32 = -time * PI / 3. + aa;
a1 = a1 + (sin(length(p) * 3. + time * PI / 2.) * 0.3);
let c1: vec2<f32> = vec2<f32>(cos(a1), sin(a1));
let a2: f32 = aa * 4.;
let c2: vec2<f32> = vec2<f32>(cos(a2), sin(a2)) * 0.3 + c1;
col.r = col.r + (0.001 / abs(length(p - c2) - size));
col.g = col.g + (0.0013 / abs(length(p - c2) - size * 1.05));
col.b = col.b + (0.0015 / abs(length(p - c2) - size * 1.09));
}

return vec4<f32>(col, 1.);
```

}
Binary file added assets/Gallery/slug-ring/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion assets/shaders/myshader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {

let tex: vec4f = textureSample(texture, texture_sampler, texture_uvs);
return tex;
}
}


fn circle(p: vec2<f32>, r: f32) -> f32 {
return smoothstep(0.1, 0., abs(length(p) - r));
}
46 changes: 46 additions & 0 deletions scripts/screenshots-to-gallery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import argparse
import os
import shutil


def create_directory_and_move_files(file_path, title):
target_directory = f"./assets/Gallery/{title}"
os.makedirs(target_directory, exist_ok=True)

screenshot_png_path = file_path
screenshot_wgsl_path = file_path.replace(".png", ".wgsl")

shutil.move(screenshot_png_path, os.path.join(target_directory, "screenshot.png"))
shutil.move(screenshot_wgsl_path, os.path.join(target_directory, "screenshot.wgsl"))

with open(os.path.join(target_directory, "screenshot.wgsl"), "r") as wgsl_file:
wgsl_contents = wgsl_file.read()

readme_contents = f"""
## {title}

![photo](screenshot.png)

## Fragment:
```rust
{wgsl_contents}

```
"""

with open(os.path.join(target_directory, "README.md"), "w") as readme_file:
readme_file.write(readme_contents)

# as the contents of the shader are moved into the README for the named shader in the README we create, we remove the previous source.
os.remove(os.path.join(target_directory, "screenshot.wgsl"))


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Move screenshots and create a README file."
)
parser.add_argument("input", type=str, help="Path to the screenshot.png file")
parser.add_argument("title", type=str, help="Title for the directory and README")
args = parser.parse_args()

create_directory_and_move_files(args.input, args.title)