mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-08-04 05:18:19 +00:00
Implement the Brush without relying on a stamp texture
Test Plan: Test the BrushNode in the editor Reviewers: Keavon Reviewed By: Keavon Pull Request: https://github.com/GraphiteEditor/Graphite/pull/1184
This commit is contained in:
parent
5d9c0cb4d5
commit
1020eb6835
31 changed files with 221 additions and 178 deletions
|
@ -13,7 +13,7 @@ pub struct PushConstants {
|
|||
impl Sample for SampledImage<Image2d> {
|
||||
type Pixel = Color;
|
||||
|
||||
fn sample(&self, pos: glam::DVec2) -> Option<Self::Pixel> {
|
||||
fn sample(&self, pos: glam::DVec2, _area: glam::DVec2) -> Option<Self::Pixel> {
|
||||
let color = self.sample(pos);
|
||||
Color::from_rgbaf32(color.x, color.y, color.z, color.w)
|
||||
}
|
||||
|
|
|
@ -252,7 +252,7 @@ mod test {
|
|||
let value: ClonedNode<Result<&u32, ()>> = ClonedNode(Ok(&4u32));
|
||||
assert_eq!(value.eval(()), Ok(&4u32));
|
||||
//let type_erased_clone = clone as &dyn for<'a> Node<'a, &'a u32, Output = u32>;
|
||||
let map_result = MapResultNode::new(ValueNode::new(FnNode::new(|x: &u32| x.clone())));
|
||||
let map_result = MapResultNode::new(ValueNode::new(FnNode::new(|x: &u32| *x)));
|
||||
//et type_erased = &map_result as &dyn for<'a> Node<'a, Result<&'a u32, ()>, Output = Result<u32, ()>>;
|
||||
assert_eq!(map_result.eval(Ok(&4u32)), Ok(4u32));
|
||||
let fst = value.then(map_result);
|
||||
|
|
|
@ -171,7 +171,7 @@ pub trait Luminance {
|
|||
pub trait Sample {
|
||||
type Pixel: Pixel;
|
||||
// TODO: Add an area parameter
|
||||
fn sample(&self, pos: DVec2) -> Option<Self::Pixel>;
|
||||
fn sample(&self, pos: DVec2, area: DVec2) -> Option<Self::Pixel>;
|
||||
}
|
||||
|
||||
// TODO: We might rename this to Bitmap at some point
|
||||
|
|
|
@ -715,6 +715,9 @@ impl Color {
|
|||
}
|
||||
|
||||
pub fn to_unassociated_alpha(&self) -> Self {
|
||||
if self.alpha == 0. {
|
||||
return *self;
|
||||
}
|
||||
let unmultiply = 1. / self.alpha;
|
||||
Self {
|
||||
red: self.red * unmultiply,
|
||||
|
|
|
@ -26,7 +26,7 @@ mod base64_serde {
|
|||
{
|
||||
use serde::de::Error;
|
||||
|
||||
let color_from_chunk = |chunk: &[u8]| P::from_bytes(chunk.try_into().unwrap()).clone();
|
||||
let color_from_chunk = |chunk: &[u8]| P::from_bytes(chunk.try_into().unwrap());
|
||||
|
||||
let colors_from_bytes = |bytes: Vec<u8>| bytes.chunks_exact(P::byte_size()).map(color_from_chunk).collect();
|
||||
|
||||
|
@ -129,7 +129,7 @@ where
|
|||
pub fn into_flat_u8(self) -> (Vec<u8>, u32, u32) {
|
||||
let Image { width, height, data } = self;
|
||||
|
||||
let to_gamma = |x| SRGBGammaFloat::from_linear(x);
|
||||
let to_gamma = SRGBGammaFloat::from_linear;
|
||||
let to_u8 = |x| (num_cast::<_, f32>(x).unwrap() * 255.) as u8;
|
||||
|
||||
let result_bytes = data
|
||||
|
@ -201,7 +201,8 @@ pub struct ImageFrame<P: Pixel> {
|
|||
impl<P: Debug + Copy + Pixel> Sample for ImageFrame<P> {
|
||||
type Pixel = P;
|
||||
|
||||
fn sample(&self, pos: DVec2) -> Option<Self::Pixel> {
|
||||
// TODO: Improve sampling logic
|
||||
fn sample(&self, pos: DVec2, _area: DVec2) -> Option<Self::Pixel> {
|
||||
let image_size = DVec2::new(self.image.width() as f64, self.image.height() as f64);
|
||||
let pos = (DAffine2::from_scale(image_size) * self.transform.inverse()).transform_point2(pos);
|
||||
if pos.x < 0. || pos.y < 0. || pos.x >= image_size.x || pos.y >= image_size.y {
|
||||
|
|
|
@ -81,7 +81,7 @@ mod test {
|
|||
fn test_ref_eval() {
|
||||
let value = ValueNode::new(5);
|
||||
|
||||
assert_eq!((&value).eval(()), &5);
|
||||
assert_eq!(value.eval(()), &5);
|
||||
let id = IdNode::new();
|
||||
|
||||
let compose = ComposeNode::new(&value, &id);
|
||||
|
|
|
@ -7,6 +7,7 @@ pub struct IntNode<const N: u32>;
|
|||
|
||||
impl<'i, const N: u32> Node<'i, ()> for IntNode<N> {
|
||||
type Output = u32;
|
||||
#[inline(always)]
|
||||
fn eval(&'i self, _input: ()) -> Self::Output {
|
||||
N
|
||||
}
|
||||
|
@ -17,6 +18,7 @@ pub struct ValueNode<T>(pub T);
|
|||
|
||||
impl<'i, T: 'i> Node<'i, ()> for ValueNode<T> {
|
||||
type Output = &'i T;
|
||||
#[inline(always)]
|
||||
fn eval(&'i self, _input: ()) -> Self::Output {
|
||||
&self.0
|
||||
}
|
||||
|
@ -45,6 +47,7 @@ pub struct ClonedNode<T: Clone>(pub T);
|
|||
|
||||
impl<'i, T: Clone + 'i> Node<'i, ()> for ClonedNode<T> {
|
||||
type Output = T;
|
||||
#[inline(always)]
|
||||
fn eval(&'i self, _input: ()) -> Self::Output {
|
||||
self.0.clone()
|
||||
}
|
||||
|
@ -62,11 +65,34 @@ impl<T: Clone> From<T> for ClonedNode<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
/// The DebugClonedNode logs every time it is evaluated.
|
||||
/// This is useful for debugging.
|
||||
pub struct DebugClonedNode<T: Clone>(pub T);
|
||||
|
||||
impl<'i, T: Clone + 'i> Node<'i, ()> for DebugClonedNode<T> {
|
||||
type Output = T;
|
||||
#[inline(always)]
|
||||
fn eval(&'i self, _input: ()) -> Self::Output {
|
||||
// KEEP THIS `debug!()` - It acts as the output for the debug node itself
|
||||
log::debug!("DebugClonedNode::eval");
|
||||
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> DebugClonedNode<T> {
|
||||
pub const fn new(value: T) -> ClonedNode<T> {
|
||||
ClonedNode(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct CopiedNode<T: Copy>(pub T);
|
||||
|
||||
impl<'i, T: Copy + 'i> Node<'i, ()> for CopiedNode<T> {
|
||||
type Output = T;
|
||||
#[inline(always)]
|
||||
fn eval(&'i self, _input: ()) -> Self::Output {
|
||||
self.0
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ fn set_vector_data_fill(
|
|||
positions: Vec<(f64, Option<Color>)>,
|
||||
) -> VectorData {
|
||||
vector_data.style.set_fill(match fill_type {
|
||||
FillType::None | FillType::Solid => solid_color.map_or(Fill::None, |solid_color| Fill::Solid(solid_color)),
|
||||
FillType::None | FillType::Solid => solid_color.map_or(Fill::None, Fill::Solid),
|
||||
FillType::Gradient => Fill::Gradient(Gradient {
|
||||
start,
|
||||
end,
|
||||
|
|
|
@ -169,8 +169,7 @@ pub struct UniformNode<Executor> {
|
|||
|
||||
#[node_macro::node_fn(UniformNode)]
|
||||
fn uniform_node<T: ToUniformBuffer, E: GpuExecutor>(data: T, executor: &'any_input E) -> ShaderInput<E::BufferHandle> {
|
||||
let handle = executor.create_uniform_buffer(data).unwrap();
|
||||
handle
|
||||
executor.create_uniform_buffer(data).unwrap()
|
||||
}
|
||||
|
||||
pub struct StorageNode<Executor> {
|
||||
|
@ -179,7 +178,7 @@ pub struct StorageNode<Executor> {
|
|||
|
||||
#[node_macro::node_fn(StorageNode)]
|
||||
fn storage_node<T: ToStorageBuffer, E: GpuExecutor>(data: T, executor: &'any_input E) -> ShaderInput<E::BufferHandle> {
|
||||
let handle = executor
|
||||
executor
|
||||
.create_storage_buffer(
|
||||
data,
|
||||
StorageBufferOptions {
|
||||
|
@ -188,8 +187,7 @@ fn storage_node<T: ToStorageBuffer, E: GpuExecutor>(data: T, executor: &'any_inp
|
|||
cpu_readable: false,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
handle
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub struct PushNode<Value> {
|
||||
|
|
|
@ -292,7 +292,7 @@ impl NodeNetwork {
|
|||
}
|
||||
|
||||
pub fn input_types<'a>(&'a self) -> impl Iterator<Item = Type> + 'a {
|
||||
self.inputs.iter().map(move |id| self.nodes[id].inputs.get(0).map(|i| i.ty().clone()).unwrap_or(concrete!(())))
|
||||
self.inputs.iter().map(move |id| self.nodes[id].inputs.get(0).map(|i| i.ty()).unwrap_or(concrete!(())))
|
||||
}
|
||||
|
||||
/// An empty graph
|
||||
|
@ -500,7 +500,7 @@ impl NodeNetwork {
|
|||
}
|
||||
FlowIter {
|
||||
stack: self.outputs.iter().map(|output| output.node_id).collect(),
|
||||
network: &self,
|
||||
network: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use glam::{DAffine2, DVec2};
|
||||
use graphene_core::raster::{Color, Image, ImageFrame, RasterMut};
|
||||
use graphene_core::transform::TransformMut;
|
||||
use graphene_core::raster::{Alpha, Color, Pixel, Sample};
|
||||
use graphene_core::transform::{Transform, TransformMut};
|
||||
use graphene_core::vector::VectorData;
|
||||
use graphene_core::Node;
|
||||
use node_macro::node_fn;
|
||||
|
@ -73,8 +73,49 @@ fn vector_points(vector: VectorData) -> Vec<DVec2> {
|
|||
vector.subpaths.iter().flat_map(|subpath| subpath.manipulator_groups().iter().map(|group| group.anchor)).collect()
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct BrushStampGenerator<P: Pixel + Alpha> {
|
||||
color: P,
|
||||
feather_exponent: f32,
|
||||
transform: DAffine2,
|
||||
}
|
||||
|
||||
impl<P: Pixel + Alpha> Transform for BrushStampGenerator<P> {
|
||||
fn transform(&self) -> DAffine2 {
|
||||
self.transform
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel + Alpha> TransformMut for BrushStampGenerator<P> {
|
||||
fn transform_mut(&mut self) -> &mut DAffine2 {
|
||||
&mut self.transform
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Pixel + Alpha> Sample for BrushStampGenerator<P> {
|
||||
type Pixel = P;
|
||||
|
||||
#[inline]
|
||||
fn sample(&self, position: DVec2, area: DVec2) -> Option<P> {
|
||||
let position = self.transform.inverse().transform_point2(position);
|
||||
let area = self.transform.inverse().transform_vector2(area);
|
||||
let center = DVec2::splat(0.5);
|
||||
|
||||
let distance = (position + area / 2. - center).length() as f32 * 2.;
|
||||
|
||||
let result = if distance < 1. {
|
||||
1. - distance.powf(self.feather_exponent)
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
use graphene_core::raster::Channel;
|
||||
Some(self.color.multiplied_alpha(P::AlphaChannel::from_f32(result)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct BrushTextureNode<ColorNode, Hardness, Flow> {
|
||||
pub struct BrushStampGeneratorNode<ColorNode, Hardness, Flow> {
|
||||
pub color: ColorNode,
|
||||
pub hardness: Hardness,
|
||||
pub flow: Flow,
|
||||
|
@ -92,17 +133,14 @@ fn erase(input: (Color, Color), flow: f64) -> Color {
|
|||
Color::from_unassociated_alpha(input.r(), input.g(), input.b(), alpha)
|
||||
}
|
||||
|
||||
#[node_fn(BrushTextureNode)]
|
||||
fn brush_texture(diameter: f64, color: Color, hardness: f64, flow: f64) -> ImageFrame<Color> {
|
||||
#[node_fn(BrushStampGeneratorNode)]
|
||||
fn brush_stamp_generator_node(diameter: f64, color: Color, hardness: f64, flow: f64) -> BrushStampGenerator<Color> {
|
||||
// Diameter
|
||||
let radius = diameter / 2.;
|
||||
// TODO: Remove the 4px padding after figuring out why the brush stamp gets randomly offset by 1px up/down/left/right when clicking with the Brush tool
|
||||
let dimension = diameter.ceil() as u32 + 4;
|
||||
let center = DVec2::splat(radius + (dimension as f64 - diameter) / 2.);
|
||||
|
||||
// Hardness
|
||||
let hardness = hardness / 100.;
|
||||
let feather_exponent = 1. / (1. - hardness);
|
||||
let feather_exponent = 1. / (1. - hardness) as f32;
|
||||
|
||||
// Flow
|
||||
let flow = flow / 100.;
|
||||
|
@ -110,33 +148,8 @@ fn brush_texture(diameter: f64, color: Color, hardness: f64, flow: f64) -> Image
|
|||
// Color
|
||||
let color = color.apply_opacity(flow as f32);
|
||||
|
||||
// Initial transparent image
|
||||
let mut image = Image::new(dimension, dimension, Color::TRANSPARENT);
|
||||
|
||||
for y in 0..dimension {
|
||||
for x in 0..dimension {
|
||||
let summation = MULTISAMPLE_GRID.iter().fold(0., |acc, (offset_x, offset_y)| {
|
||||
let position = DVec2::new(x as f64 + offset_x, y as f64 + offset_y);
|
||||
let distance = (position - center).length();
|
||||
|
||||
if distance < radius {
|
||||
acc + (1. - (distance / radius).powf(feather_exponent)).clamp(0., 1.)
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
});
|
||||
|
||||
let pixel_fill = summation / MULTISAMPLE_GRID.len() as f64;
|
||||
|
||||
let pixel = image.get_pixel_mut(x, y).unwrap();
|
||||
*pixel = color.apply_opacity(pixel_fill as f32);
|
||||
}
|
||||
}
|
||||
|
||||
ImageFrame {
|
||||
image,
|
||||
transform: DAffine2::from_scale_angle_translation(DVec2::splat(dimension as f64), 0., -DVec2::splat(radius)),
|
||||
}
|
||||
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(diameter), 0., -DVec2::splat(radius));
|
||||
BrushStampGenerator { color, feather_exponent, transform }
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
@ -183,19 +196,17 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn test_brush_texture() {
|
||||
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(100.), ClonedNode::new(100.));
|
||||
let brush_texture_node = BrushStampGeneratorNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(100.), ClonedNode::new(100.));
|
||||
let size = 20.;
|
||||
let image = brush_texture_node.eval(size);
|
||||
assert_eq!(image.image.width, size.ceil() as u32 + 4);
|
||||
assert_eq!(image.image.height, size.ceil() as u32 + 4);
|
||||
assert_eq!(image.transform, DAffine2::from_scale_angle_translation(DVec2::splat(size.ceil() + 4.), 0., -DVec2::splat(size / 2.)));
|
||||
assert_eq!(image.transform(), DAffine2::from_scale_angle_translation(DVec2::splat(size.ceil()), 0., -DVec2::splat(size / 2.)));
|
||||
// center pixel should be BLACK
|
||||
assert_eq!(image.image.get_pixel(11, 11), Some(Color::BLACK));
|
||||
assert_eq!(image.sample(DVec2::splat(0.), DVec2::ONE), Some(Color::BLACK));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_brush() {
|
||||
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(1.0), ClonedNode::new(1.0));
|
||||
let brush_texture_node = BrushStampGeneratorNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(1.0), ClonedNode::new(1.0));
|
||||
let image = brush_texture_node.eval(20.);
|
||||
let trace = vec![DVec2::new(0.0, 0.0), DVec2::new(10.0, 0.0)];
|
||||
let trace = ClonedNode::new(trace.into_iter());
|
||||
|
@ -203,7 +214,6 @@ mod test {
|
|||
let frames = MapNode::new(ValueNode::new(translate_node));
|
||||
let frames = trace.then(frames).eval(()).collect::<Vec<_>>();
|
||||
assert_eq!(frames.len(), 2);
|
||||
assert_eq!(frames[0].image.width, 24);
|
||||
let background_bounds = ReduceNode::new(ClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
|
||||
let background_bounds = background_bounds.eval(frames.clone().into_iter());
|
||||
let background_bounds = ClonedNode::new(background_bounds.unwrap().to_transform());
|
||||
|
@ -211,8 +221,8 @@ mod test {
|
|||
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(1.0));
|
||||
let final_image = ReduceNode::new(background_image, ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
|
||||
let final_image = final_image.eval(frames.into_iter());
|
||||
assert_eq!(final_image.image.height, 24);
|
||||
assert_eq!(final_image.image.width, 34);
|
||||
assert_eq!(final_image.image.height, 20);
|
||||
assert_eq!(final_image.image.width, 30);
|
||||
drop(final_image);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,12 +28,12 @@ where
|
|||
|
||||
if let Some((_, cached_value, keep)) = self.cache.iter().find(|(h, _, _)| *h == hash) {
|
||||
keep.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
return cached_value;
|
||||
cached_value
|
||||
} else {
|
||||
trace!("Cache miss");
|
||||
let output = self.node.eval(input);
|
||||
let index = self.cache.push((hash, output, AtomicBool::new(true)));
|
||||
return &self.cache[index].1;
|
||||
&self.cache[index].1
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ where
|
|||
|
||||
fn serialize(&self) -> Option<String> {
|
||||
let output = self.output.lock().unwrap();
|
||||
(&*output).as_ref().map(|output| serde_json::to_string(output).ok()).flatten()
|
||||
(*output).as_ref().and_then(|output| serde_json::to_string(output).ok())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ impl<'i, T: 'i + Hash> Node<'i, Option<T>> for LetNode<T> {
|
|||
}
|
||||
trace!("Cache miss");
|
||||
let index = self.cache.push((hash, input));
|
||||
return &self.cache[index].1;
|
||||
&self.cache[index].1
|
||||
}
|
||||
None => &self.cache.iter().last().expect("Let node was not initialized").1,
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use dyn_any::{DynAny, StaticType};
|
|||
use glam::{DAffine2, DVec2};
|
||||
use graphene_core::raster::{Alpha, Channel, Image, ImageFrame, Luminance, Pixel, RasterMut, Sample};
|
||||
use graphene_core::transform::Transform;
|
||||
use graphene_core::value::{ClonedNode, ValueNode};
|
||||
|
||||
use graphene_core::Node;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
@ -240,6 +240,7 @@ fn mask_image<
|
|||
// Transforms a point from the background image to the forground image
|
||||
let bg_to_fg = image.transform() * DAffine2::from_scale(1. / image_size);
|
||||
|
||||
let area = bg_to_fg.transform_point2(DVec2::new(1., 1.)) - bg_to_fg.transform_point2(DVec2::ZERO);
|
||||
for y in 0..image.height() {
|
||||
for x in 0..image.width() {
|
||||
let image_point = DVec2::new(x as f64, y as f64);
|
||||
|
@ -247,8 +248,8 @@ fn mask_image<
|
|||
let local_mask_point = stencil.transform().inverse().transform_point2(mask_point);
|
||||
mask_point = stencil.transform().transform_point2(local_mask_point.clamp(DVec2::ZERO, DVec2::ONE));
|
||||
|
||||
let image_pixel = image.get_pixel_mut(x as u32, y as u32).unwrap();
|
||||
if let Some(mask_pixel) = stencil.sample(mask_point) {
|
||||
let image_pixel = image.get_pixel_mut(x, y).unwrap();
|
||||
if let Some(mask_pixel) = stencil.sample(mask_point, area) {
|
||||
*image_pixel = image_pixel.multiplied_alpha(mask_pixel.l().to_channel());
|
||||
}
|
||||
}
|
||||
|
@ -258,20 +259,20 @@ fn mask_image<
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BlendImageTupleNode<P, MapFn> {
|
||||
pub struct BlendImageTupleNode<P, Fg, MapFn> {
|
||||
map_fn: MapFn,
|
||||
_p: PhantomData<P>,
|
||||
_fg: PhantomData<Fg>,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(BlendImageTupleNode<_P>)]
|
||||
fn blend_image_tuple<_P: Pixel + Debug, MapFn>(images: (ImageFrame<_P>, ImageFrame<_P>), map_fn: &'any_input MapFn) -> ImageFrame<_P>
|
||||
#[node_macro::node_fn(BlendImageTupleNode<_P, _Fg>)]
|
||||
fn blend_image_tuple<_P: Pixel + Debug, MapFn, _Fg: Sample<Pixel = _P> + Transform>(images: (ImageFrame<_P>, _Fg), map_fn: &'any_input MapFn) -> ImageFrame<_P>
|
||||
where
|
||||
MapFn: for<'any_input> Node<'any_input, (_P, _P), Output = _P> + 'input + Clone,
|
||||
{
|
||||
let (background, foreground) = images;
|
||||
|
||||
let node = BlendImageNode::new(ClonedNode::new(background), ValueNode::new(map_fn.clone()));
|
||||
node.eval(foreground)
|
||||
blend_image(foreground, background, map_fn)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
@ -283,13 +284,20 @@ pub struct BlendImageNode<P, Background, MapFn> {
|
|||
|
||||
// TODO: Implement proper blending
|
||||
#[node_macro::node_fn(BlendImageNode<_P>)]
|
||||
fn blend_image<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(
|
||||
fn blend_image_node<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(
|
||||
foreground: Frame,
|
||||
mut background: Background,
|
||||
background: Background,
|
||||
map_fn: &'any_input MapFn,
|
||||
) -> Background
|
||||
where
|
||||
MapFn: for<'any_input> Node<'any_input, (_P, _P), Output = _P> + 'input,
|
||||
{
|
||||
blend_image(foreground, background, map_fn)
|
||||
}
|
||||
|
||||
fn blend_image<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(foreground: Frame, mut background: Background, map_fn: &MapFn) -> Background
|
||||
where
|
||||
MapFn: for<'any_input> Node<'any_input, (_P, _P), Output = _P>,
|
||||
{
|
||||
let background_size = DVec2::new(background.width() as f64, background.height() as f64);
|
||||
|
||||
|
@ -303,12 +311,13 @@ where
|
|||
let start = (bg_aabb.start * background_size).max(DVec2::ZERO).as_uvec2();
|
||||
let end = (bg_aabb.end * background_size).min(background_size).as_uvec2();
|
||||
|
||||
let area = bg_to_fg.transform_point2(DVec2::new(1., 1.)) - bg_to_fg.transform_point2(DVec2::ZERO);
|
||||
for y in start.y..end.y {
|
||||
for x in start.x..end.x {
|
||||
let bg_point = DVec2::new(x as f64, y as f64);
|
||||
let fg_point = bg_to_fg.transform_point2(bg_point);
|
||||
|
||||
if let Some(src_pixel) = foreground.sample(fg_point) {
|
||||
if let Some(src_pixel) = foreground.sample(fg_point, area) {
|
||||
if let Some(dst_pixel) = background.get_pixel_mut(x, y) {
|
||||
*dst_pixel = map_fn.eval((src_pixel, dst_pixel.clone()));
|
||||
}
|
||||
|
|
|
@ -176,8 +176,13 @@ impl BorrowTree {
|
|||
}
|
||||
|
||||
pub fn push_node(&mut self, id: NodeId, proto_node: ProtoNode, typing_context: &TypingContext) -> Result<(), String> {
|
||||
let ProtoNode { construction_args, identifier, .. } = proto_node;
|
||||
self.source_map.insert(proto_node.document_node_path, id);
|
||||
let ProtoNode {
|
||||
construction_args,
|
||||
identifier,
|
||||
document_node_path,
|
||||
..
|
||||
} = proto_node;
|
||||
self.source_map.insert(document_node_path, id);
|
||||
|
||||
match construction_args {
|
||||
ConstructionArgs::Value(value) => {
|
||||
|
|
|
@ -8,7 +8,7 @@ use std::collections::HashMap;
|
|||
use graphene_core::raster::color::Color;
|
||||
use graphene_core::raster::*;
|
||||
use graphene_core::structural::Then;
|
||||
use graphene_core::value::{ClonedNode, ForgetNode, ValueNode};
|
||||
use graphene_core::value::{ClonedNode, CopiedNode, ForgetNode, ValueNode};
|
||||
use graphene_core::{Node, NodeIO, NodeIOTypes};
|
||||
use graphene_std::brush::*;
|
||||
use graphene_std::raster::*;
|
||||
|
@ -175,6 +175,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
|
|||
vec![(
|
||||
NodeIdentifier::new("graphene_std::brush::BrushNode"),
|
||||
|args| {
|
||||
use graphene_core::value::*;
|
||||
use graphene_std::brush::*;
|
||||
|
||||
let trace: DowncastBothNode<(), Vec<DVec2>> = DowncastBothNode::new(args[0]);
|
||||
|
@ -183,24 +184,23 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
|
|||
let flow: DowncastBothNode<(), f64> = DowncastBothNode::new(args[3]);
|
||||
let color: DowncastBothNode<(), Color> = DowncastBothNode::new(args[4]);
|
||||
|
||||
let stamp = BrushTextureNode::new(color, ClonedNode::new(hardness.eval(())), ClonedNode::new(flow.eval(())));
|
||||
let stamp = BrushStampGeneratorNode::new(color, CopiedNode::new(hardness.eval(())), CopiedNode::new(flow.eval(())));
|
||||
let stamp = stamp.eval(diameter.eval(()));
|
||||
|
||||
let frames = TranslateNode::new(ClonedNode::new(stamp));
|
||||
let frames = TranslateNode::new(CopiedNode::new(stamp));
|
||||
let frames = MapNode::new(ValueNode::new(frames));
|
||||
let frames = frames.eval(trace.eval(()).into_iter()).collect::<Vec<_>>();
|
||||
|
||||
let background_bounds = ReduceNode::new(ClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
|
||||
let background_bounds = ReduceNode::new(DebugClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
|
||||
let background_bounds = background_bounds.eval(frames.clone().into_iter());
|
||||
let background_bounds = ClonedNode::new(background_bounds.unwrap().to_transform());
|
||||
let background_bounds = DebugClonedNode::new(background_bounds.unwrap().to_transform());
|
||||
|
||||
let background_image = background_bounds.then(EmptyImageNode::new(ClonedNode::new(Color::TRANSPARENT)));
|
||||
let background_image = background_bounds.then(EmptyImageNode::new(CopiedNode::new(Color::TRANSPARENT)));
|
||||
|
||||
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(100.));
|
||||
let blend_node = graphene_core::raster::BlendNode::new(CopiedNode::new(BlendMode::Normal), CopiedNode::new(100.));
|
||||
|
||||
let final_image = ReduceNode::new(background_image, ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
|
||||
let final_image = final_image.eval(frames.into_iter());
|
||||
let final_image = ClonedNode::new(final_image);
|
||||
let final_image = DebugClonedNode::new(frames.into_iter()).then(final_image);
|
||||
|
||||
let any: DynAnyNode<(), _, _> = graphene_std::any::DynAnyNode::new(ValueNode::new(final_image));
|
||||
Box::pin(any)
|
||||
|
@ -241,7 +241,7 @@ fn node_registry() -> HashMap<NodeIdentifier, HashMap<NodeIOTypes, NodeConstruct
|
|||
let image: DowncastBothNode<(), ImageFrame<Color>> = DowncastBothNode::new(args[0]);
|
||||
let blend_mode: DowncastBothNode<(), BlendMode> = DowncastBothNode::new(args[1]);
|
||||
let opacity: DowncastBothNode<(), f64> = DowncastBothNode::new(args[2]);
|
||||
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(blend_mode.eval(())), ClonedNode::new(opacity.eval(())));
|
||||
let blend_node = graphene_core::raster::BlendNode::new(CopiedNode::new(blend_mode.eval(())), CopiedNode::new(opacity.eval(())));
|
||||
let node = graphene_std::raster::BlendImageNode::new(image, ValueNode::new(blend_node));
|
||||
let _ = &node as &dyn for<'i> Node<'i, ImageFrame<Color>, Output = ImageFrame<Color>>;
|
||||
let any: DynAnyNode<ImageFrame<Color>, _, _> = graphene_std::any::DynAnyNode::new(graphene_core::value::ValueNode::new(node));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue