Skip to content

Commit

Permalink
Adaptive Outline
Browse files Browse the repository at this point in the history
  • Loading branch information
exvacuum committed Jun 14, 2024
1 parent ac37b21 commit 294e8bb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 68 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/docs.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_outline_post_process"
version = "0.1.3"
version = "0.2.0"
edition = "2021"

[dependencies.bevy]
Expand Down
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![Build](https://img.shields.io/github/actions/workflow/status/exvacuum/bevy_outline_post_process/rust.yml)
[![Docs](https://img.shields.io/website?url=https%3A%2F%2Fexvacuum.github.io%2Fbevy_outline_post_process%2F&label=docs)](https://exvacuum.github.io/bevy_outline_post_process)

A plugin for the [Bevy](https://bevyengine.org) engine which adds an outline post-processing effect.
A plugin for the [Bevy](https://bevyengine.org) engine which adds an outline post-processing effect. Optionally supports adaptive outlining, so darker areas are outlined in white rather than black, based on luminance.

Note: This is a full-screen post process effect and cannot be enabled/disabled for specific objects.

Expand All @@ -14,16 +14,13 @@ Note: This is a full-screen post process effect and cannot be enabled/disabled f
![](./doc/screenshot_smooth.png)
Configuration Used:
```rs
bevy_outline_post_process::components::OutlinePostProcessSettings {
weight: 2.0,
threshold: 0.0,
}
bevy_outline_post_process::components::OutlinePostProcessSettings::new(2.0, 0.0, false);
```
## Compatibility

| Crate Version | Bevy Version |
|--- |--- |
| 0.1 | 0.13 |
| 0.2 | 0.13 |

## Installation

Expand Down Expand Up @@ -56,10 +53,7 @@ When spawning a camera:
commands.spawn((
// Camera3dBundle...
NormalPrepass,
bevy_outline_post_process::components::OutlinePostProcessSettings {
weight: 2.0,
threshold: 0.0,
}
bevy_outline_post_process::components::OutlinePostProcessSettings::new(2.0, 0.0, false);
));
```

Expand Down
10 changes: 6 additions & 4 deletions assets/shaders/outline_post_process.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
struct OutlinePostProcessSettings {
weight: f32,
threshold: f32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
_padding: vec2<f32>,
#endif
adaptive: u32,
}

@group(0) @binding(0) var screen_texture: texture_2d<f32>;
Expand Down Expand Up @@ -39,7 +37,11 @@ fn fragment(

let delta_clipped = clamp((delta_raw * 2.0) - settings.threshold, 0.0, 1.0);

let outline = vec4f(delta_clipped, delta_clipped, delta_clipped, 0.0);
var outline = vec4f(delta_clipped, delta_clipped, delta_clipped, 0.0);
let luma = (0.2126 * screen_color.r + 0.7152 * screen_color.g + 0.0722 * screen_color.b);
if settings.adaptive != 0 && luma < 0.5 {
outline = outline * -1;
}

return screen_color - outline;
}
29 changes: 26 additions & 3 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,34 @@ use bevy::{

/// Component which, when inserted into an entity with a camera and normal prepass, enables an outline effect for that
/// camera.
#[derive(Component, ShaderType, ExtractComponent, PartialEq, Clone, Default)]
#[derive(Component, ShaderType, ExtractComponent, PartialEq, Clone)]
pub struct OutlinePostProcessSettings {
/// Weight of outlines in pixels.
pub weight: f32,
weight: f32,
/// A threshold for normal differences, values below this threshold will not become outlines.
/// Higher values will result in more outlines which may look better on smooth surfaces.
pub threshold: f32,
threshold: f32,
/// Whether to use adaptive outlines. White outlines will be drawn around darker objects, while black ones will be drawn around lighter ones.
adaptive: u32,
}

impl OutlinePostProcessSettings {
/// Create a new instance with the given settings
pub fn new(weight: f32, threshold: f32, adaptive: bool) -> Self {
Self {
weight,
threshold,
adaptive: adaptive as u32,
}
}
}

impl Default for OutlinePostProcessSettings {
fn default() -> Self {
Self {
weight: 1.0,
threshold: 0.0,
adaptive: 0
}
}
}

0 comments on commit 294e8bb

Please sign in to comment.