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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use super::{
    PostFragment, PropertyChanged, Material, TexturesPool, FullMaterial,
    MaterialParameterAsUniformValue
};
use glin::program::AsUniform;
use rin_material::{ Parameter, ParameterAny, ParameterMutAny };
use rin_gl::program::UniformValue;
use indexmap::IndexMap;
use std::marker;
use rinecs::EntitiesThreadLocal;
use derive_builder::Builder;

#[derive(Builder)]
#[builder(pattern = "owned")]
#[builder(build_fn(name = "_build", private))]
#[builder(setter(into))]
pub struct PostFragmentMaterial<M>{
    #[builder(private)]
    material: M,

    #[builder(default)]
    post_fragment: Option<PostFragment>,

    #[builder(default="true")]
    #[builder(setter(skip))]
    post_fragment_changed: bool,

    #[builder(default)]
    #[builder(setter(skip))]
    extra_fragment_uniform_values: Parameter<IndexMap<String, UniformValue>>,

    #[builder(default)]
    #[builder(setter(skip))]
    material_uniforms: PropertyChanged<Vec<glin::program::Uniform>>,
}

impl<M: Clone> Clone for PostFragmentMaterial<M>{
    fn clone(&self) -> PostFragmentMaterial<M>{
        PostFragmentMaterial{
            material: self.material.clone(),
            post_fragment: self.post_fragment.clone(),
            extra_fragment_uniform_values: self.extra_fragment_uniform_values.clone(),
            material_uniforms: self.material_uniforms.clone(),
            post_fragment_changed: true,
        }
    }
}

impl<M> PostFragmentMaterialBuilder<M>{
    pub fn new(material: M) -> PostFragmentMaterialBuilder<M>{
        PostFragmentMaterialBuilder{
            material: Some(material),
            post_fragment: None,
            extra_fragment_uniform_values: marker::PhantomData,
            material_uniforms: marker::PhantomData,
            post_fragment_changed: marker::PhantomData,
        }
    }

    pub fn build(self) -> PostFragmentMaterial<M>{
        self._build().unwrap()
    }
}

impl<M> PostFragmentMaterial<M>{
    pub fn set_extra_uniform<S: Into<String>, T: AsUniform<U>, U: ?Sized>(&mut self, name: S, value: T){
        self.extra_fragment_uniform_values.insert(name.into(), value.as_uniform());
    }

    pub fn set_post_fragment<S: Into<Option<PostFragment>>>(&mut self, post_fragment: S){
        self.post_fragment = post_fragment.into();
        self.post_fragment_changed = true;
    }

    pub fn extra_uniform_value(&self, name: &str) -> Option<&UniformValue>{
        self.extra_fragment_uniform_values.get().get(name)
    }

    pub fn extra_uniform_value_mut(&mut self, name: &str) -> Option<&mut UniformValue>{
        self.extra_fragment_uniform_values.get_mut().get_mut(name)
    }

    pub fn set_extra_uniform_value<T: AsUniform<U>, U: ?Sized>(&mut self, name: &str, value: T) {
        if let Some(extra_uniform) = self.extra_uniform_value_mut(name) {
            *extra_uniform = value.as_uniform()
        }
    }

    pub fn material(&self) -> &M{
        &self.material
    }

    pub fn material_mut(&mut self) -> &mut M{
        &mut self.material
    }
}

impl<M: rin_material::Material> rin_material::Material for PostFragmentMaterial<M>{
    fn type_name(&self) -> &str {
        "PostFragmentMaterial<>"
    }

    fn parameter_names(&self) -> Vec<&str>{
        self.material.parameter_names()
    }

    fn parameters(&self) -> Vec<ParameterAny>{
        let parameters = self.material.parameters();
        // Since this can now be a path, it's not accesible as a parameter anymore
        // Is it worth adding a Parameter::Path and expose it as such in case it's a path
        // the rest of fields of the post fragment, like replacements or defines won't be accesible
        // anymore so probably doesn't make sense and we just need to access this parameter from a
        // typed material
        // parameters.push(self.post_fragment.as_parameter_ref("post_fragment"));
        parameters
    }

    fn parameter(&self, name: &str) -> Option<ParameterAny>{
        match name{
            // "post_fragment" => Some(self.post_fragment.as_parameter_ref("post_fragment")),
            _ => self.material.parameter(name)
        }
    }

    fn parameters_mut(&mut self) -> Vec<ParameterMutAny>{
        let parameters = self.material.parameters_mut();
        // parameters.push(self.post_fragment.as_parameter_mut("post_fragment"));
        parameters
    }

    fn parameter_mut(&mut self, name: &str) -> Option<ParameterMutAny>{
        match name{
            // "post_fragment" => Some(self.post_fragment.as_parameter_mut("post_fragment")),
            _ => self.material.parameter_mut(name)
        }
    }

    fn parameter_type_name(&self, name: &str) -> Option<&str>{
        self.material.parameter_type_name(name)
    }

    fn reset_changed(&mut self){
        self.material.reset_changed();
        self.post_fragment_changed = false;
        self.extra_fragment_uniform_values.reset_changed();
    }
}

impl<M: Material + 'static> FullMaterial for PostFragmentMaterial<M>{
    fn post_fragment(&self) -> PropertyChanged<Option<&PostFragment>>{
        if self.post_fragment_changed {
            PropertyChanged::New(self.post_fragment.as_ref())
        }else{
            PropertyChanged::Old
        }
    }

    fn full_uniforms(&mut self) -> PropertyChanged<Vec<glin::program::Uniform>> {
        let material_uniforms = if self.material_uniforms.is_always(){
            self.material_uniforms.clone()
        }else{
            self.material.full_uniforms()
        };
        if material_uniforms.is_new() {
            self.material_uniforms = material_uniforms.clone();
        }
        if self.extra_fragment_uniform_values.has_changed() || material_uniforms.is_new() {
            let uniforms = material_uniforms
                .unwrap_or_else(|| self.material_uniforms.clone().unwrap_or_else(|| vec![]))
                .into_iter()
                .chain(self.extra_fragment_uniform_values.get().iter().map(|(name, value)|{
                    glin::program::uniform(name, value)
                }))
                .collect();

            PropertyChanged::New(uniforms)
        }else{
            PropertyChanged::Old
        }
    }

    fn full_textures(&self,
        textures_pool: &TexturesPool,
        texture_offset: &mut u32) -> PropertyChanged<(Vec<glin::program::Uniform>, u64)>
    {
        self.material.full_textures(textures_pool, texture_offset)
    }

    fn full_cubemaps(&self,
        textures_pool: &TexturesPool,
        texture_offset: &mut u32) -> PropertyChanged<(Vec<glin::program::Uniform>, u64)>
    {
        self.material.full_cubemaps(textures_pool, texture_offset)
    }

    fn full_renderplanes(&self,
        entities: &EntitiesThreadLocal,
        texture_offset: u32) -> PropertyChanged<Option<(Vec<glin::program::Uniform>, u64)>>
    {
        self.material.full_renderplanes(entities, texture_offset)
    }

    fn full_reset_changed(&mut self){
        self.post_fragment_changed = false;
        self.extra_fragment_uniform_values.reset_changed();
        self.material.reset_changed();
    }

    #[inline]
    fn material(&self) -> &dyn Material{
        &self.material
    }

    #[inline]
    fn material_mut(&mut self) -> &mut dyn Material{
        &mut self.material
    }
}