1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use glin;
use crate::gl;
use super::{Material, Renderer};
use color::{Rgba,ToRgba};
use rin_math::Vec2;
use color::consts::WHITE;
use glin::RenderSurface;
use std::cell::UnsafeCell;
use glin::uniforms;

/// Material to draw an outline of an object
///
/// To use it draw the object with the original material
/// and then with this material
///
/// Needs depth testing to be enabled when drawing the
/// original object
pub struct OutlineMaterial{
    color: Rgba<f32>,
    offset: f32,
    viewport: Vec2,
    uniforms: UnsafeCell<Vec<glin::program::Uniform>>,
}

pub struct Builder{
    color: Rgba<f32>,
    offset: f32,
    viewport: Vec2,
}

impl Builder{
    /// Creates a material builder
    ///
    /// Needs the size of the viewport used to calculate
    /// the thickness of the outline precissely
    pub fn new(viewport: Vec2) -> Builder{
        Builder{
            color: WHITE.to_rgba(),
            offset: 0.01,
            viewport,
        }
    }

    /// Width of the outline in pixels
    pub fn width(&mut self, width: f32) -> &mut Builder{
        self.offset = width;
        self
    }

    /// Color of the outline
    pub fn color<C: ToRgba>(&mut self, color: C) -> &mut Builder{
        self.color = color.to_rgba().to_standard();
        self
    }

    /// Create the material
    pub fn build(&self) -> OutlineMaterial{
        OutlineMaterial{
            color: self.color,
            offset: self.offset,
            viewport: self.viewport,
            uniforms: UnsafeCell::new(vec![]),
        }
    }
}


impl OutlineMaterial{
    /// Viewport size, used by the shader to calculate the width of the outline in pixels
    pub fn set_viewport(&mut self, viewport: Vec2){
        self.viewport = viewport;
        self.uniforms_cache().clear();
    }

    /// Viewport size, used by the shader to calculate the width of the outline in pixels
    pub fn viewport(&self) -> Vec2{
        self.viewport
    }

    /// Width of the outline in pixels
    pub fn set_width(&mut self, width: f32){
        self.offset = width;
        self.uniforms_cache().clear();
    }

    /// Width of the outline in pixels
    pub fn width(&self) -> f32{
        self.offset
    }

    /// Color of the outline
    pub fn set_color<C: ToRgba>(&mut self, color: C){
        self.color = color.to_rgba().to_standard();
        self.uniforms_cache().clear();
    }

    /// Color of the outline
    pub fn color(&self) -> Rgba<f32>{
        self.color
    }

    fn uniforms_cache(&self) -> &mut Vec<glin::program::Uniform>{
        unsafe{ &mut *self.uniforms.get() }
    }
}

impl Material for OutlineMaterial{
    fn program<R: RenderSurface>(&self, renderer: &Renderer<R>) -> &glin::Program{
        get_program(renderer)
    }

    fn uniforms<R: RenderSurface>(&self, _: &Renderer<R>) -> &[glin::program::Uniform]{
        let uniforms = self.uniforms_cache();
        if uniforms.is_empty() {
            *uniforms = uniforms!{
                color_outline: self.color,
                offset: self.offset,
                viewport: self.viewport,
            };
        }

        uniforms
    }

    fn properties(&self) -> &[glin::Property]{
    	&[glin::Property::CullFace(Some(gl::FRONT))]
    }
}

program_cache!("shaders/outline.vs.glsl", "shaders/outline.fs.glsl");