nannou icon indicating copy to clipboard operation
nannou copied to clipboard

Coloring a texture (instancing and color attachment?)

Open DataScienceLeadJr opened this issue 3 years ago • 1 comments

Hi,

I'm trying to colourize the same texture over and over, but I can't figure out how to do that to the texture which is why I'm doing it to the image of the brush stroke and loading that into a texture.

Where the problem occurs (if we look away from my lack of knowledge about all this as a problem):

let mut brush_stroke = photon_rs::PhotonImage::new(model.brush.to_bytes(), BRUSH_SIZE, BRUSH_SIZE);

for _ in 0..10 {
    // Pick random pixel to get position of stroke
    let rand_x = random_range(0, IMAGE_WIDTH) as f32;
    let rand_y = random_range(0, IMAGE_HEIGHT) as f32;

    // center stroke around pos
    let brush_draw = tex_draw.x_y(
        rand_x - BRUSH_SIZE as f32 * 0.5,
        rand_y - BRUSH_SIZE as f32 * 0.5
        );
    
    // get actual color at center
    let pixel_color = model.orig_img.get_pixel(rand_x as u32, rand_y as u32).0;
    
    // change image of stroke to be tinted to pixel_color
    photon_rs::channels::alter_channels(
        &mut brush_stroke,
        - (prev_brush_color[0] as i16 - (pixel_color[0] as i16)),
        - (prev_brush_color[1] as i16 - (pixel_color[1] as i16)),
        - (prev_brush_color[2] as i16 - (pixel_color[2] as i16)),
    );

    let brush_stroke_dyn = photon_rs::helpers::dyn_image_from_raw(&brush_stroke);

    // Load image-with-new-color data to a new(?) texture variable
    let texture = wgpu::Texture::from_image(app, &brush_stroke_dyn);
    // and Begin drawing texture
    brush_draw.texture(&texture);
}

What happens is that the colour of the texture and the instance of it being drawn doesn't sync up, making it a random splatter method instead. And any combination of finish() / finish_remaining_drawings() or otherwise hasn't helped.

I can't figure out if I can enforce this through nannou, or if I'm about to travel outside of nannou and learn BindGroup/ColorAttachment/RenderPass etc.? (or is that not even what I'm after for this?)

DataScienceLeadJr avatar Jan 19 '22 13:01 DataScienceLeadJr

You would normally do this kind of stuff in the fragment shader and nannou does this when rendering text. The fragment shader is passed a VertexMode which it uses to decide how to mix the colours it's given and for text it blends the text color with the color from the the font image sample. However for texture it does not do this; it wouldn't be many changes to get this working, however that would mean forking etc...

trojanfoe avatar Oct 30 '22 11:10 trojanfoe