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
use rin_material::texture::*;
#[cfg(image_enabled)]
use rin_graphics::image;
use rin_util::LogErr;
#[cfg(image_enabled)]
use rin_scene::CreationContext;
#[cfg(all(image_ty="image", feature="squish"))]
use rin_graphics::image::buffer::ConvertBuffer;
use rin_material::texture::TextureCreationFlags;

pub fn sampler_from_blender(image: &rinblender::Image) -> Sampler{
    let wrap = match image.wrap{
        rinblender::texture::Wrap::Repeat => Wrap::Repeat,
        rinblender::texture::Wrap::Extend => Wrap::Extend,
        rinblender::texture::Wrap::Clip => Wrap::Clip,
    };

    let (minfilter, magfilter) = match image.interpolation{
        rinblender::texture::Interpolation::Closest => (Filter::NearestMipNearest, Filter::Nearest),
        rinblender::texture::Interpolation::Linear => (Filter::LinearMipLinear, Filter::Linear),
        _ => (Filter::LinearMipLinear, Filter::Linear),
    };

    Sampler{
        wrap,
        minfilter,
        magfilter
    }
}

#[cfg(image_ty="freeimage")]
pub fn load_texture<'a, C: CreationContext<'a>>(i: rinblender::Texture, world: &mut C) -> Option<TextureRef>{
    let flags = TextureCreationFlags::MIPMAPS;

    let tex = if !i.image.modifiers.is_empty(){
        log::error!("texture modifiers not supported yet when using freeimage");
        None
    }else{
        image::load_from_memory(&i.data)
            .log_err("Error loading blender texture").ok()
            .and_then(|img| world.register_image(&img, flags).ok())
    };

    tex
}

#[cfg(image_ty="image")]
pub fn load_texture<'a, C: CreationContext<'a>>(i: rinblender::Texture, world: &mut C) -> Option<TextureRef>{
    let flags = TextureCreationFlags::MIPMAPS;

    let tex = if let Ok(image::ImageFormat::Hdr) = image::guess_format(&i.data){
        if !i.image.modifiers.is_empty(){
            log::error!("texture modifiers not supported yet when using freeimage");
        }
        rin_graphics::Hdr::from_bytes(&i.data)
            .log_err("Error loading blender texture").ok()
            .map(|img| img.flip_v())
            .and_then(|img| world.register_image(&img, flags).ok())
    }else{
        image::load_from_memory(&i.data)
            .log_err("").ok()
            .and_then(|img| {
                let mut img = img.to_rgba();

                for (m, factor) in i.image.modifiers.iter(){
                    match m {
                        rinblender::texture::Modifier::Hsv(h,s,v) => {
                            if *h != 0. {
                                img = image::imageops::huerotate(&img, (h * factor * 360.) as i32);
                            }
                            if *s != 1.{

                            }
                            if *v != 1.{
                                img = image::imageops::brighten(&img, ((v - 1.) * 255.) as i32);
                            }
                        }

                        rinblender::texture::Modifier::Hue(h) => if *h != 0. {
                            img = image::imageops::huerotate(&img, (h * factor * 360.) as i32);
                        }

                        rinblender::texture::Modifier::Value(v) => if *v != 1.{
                            img = image::imageops::brighten(&img, ((v - 1.) * 255.) as i32);
                        }

                        _ => (),
                    }
                }

                world.register_image(&img, flags).ok()
            })
    };

    tex
}

#[cfg(image_ty="freeimage")]
fn convert_pre_compress(img: image::Image) -> image::Image {
    #[cfg(feature="squish")]
    if img.ty() == image::Type::BITMAP {
        let mut img = if img.bpp() != 32 {
            img.to_32bits().unwrap()
        }else{
            img
        };

        let r = img.channel(image::ColorChannel::RED).unwrap();
        let b = img.channel(image::ColorChannel::BLUE).unwrap();
        img.set_channel(&r, image::ColorChannel::BLUE);
        img.set_channel(&b, image::ColorChannel::RED);
        return img;
    }

    img
}

#[cfg(image_ty="image")]
fn convert_pre_compress(img: image::Image) -> image::Image {
    match img{
        #[cfg(feature="squish")]
        image::DynamicImage::ImageRgba8(rgba) => {
            image::DynamicImage::ImageRgba8(rgba)
        }

        #[cfg(feature="squish")]
        image::DynamicImage::ImageRgb8(rgb) => {
            let rgba: image::RgbaImage = rgb.convert();
            image::DynamicImage::ImageRgba8(rgba)
        }

        _ => img
    }
}

#[cfg(all(feature="squish", image_ty="freeimage"))]
fn can_be_compressed(img: &image::Image) -> bool {
    img.ty() == image::Type::BITMAP
}


#[cfg(all(feature="squish", image_ty="image"))]
fn can_be_compressed(img: &image::Image) -> bool {
    match img{
        image::DynamicImage::ImageRgba8(_) => true,
        _ => false,
    }
}

#[cfg(feature="rayon")]
#[cfg(image_enabled)]
pub fn load_images_data<'a, 'w, 'c, C, I>(images: I, world: &'w mut C) -> impl Iterator<Item = (rinblender::ObjectId, Option<TextureRef>)> + 'w
where
    C: CreationContext<'c>,
    I: rayon::iter::ParallelIterator<Item = (&'a rinblender::ObjectId, String, &'a rinblender::texture::Data)>
{
    let flags = TextureCreationFlags::MIPMAPS;
    let (tx, rx) = crossbeam::channel::unbounded();
    images.for_each(|(objectid, name, i)| {
        if let Ok(img) = image::load_from_memory(&i.data)
            .log_err("Error loading blender texture")
        {
            let img = convert_pre_compress(img);
            tx.send((objectid.to_owned(), Some((name.to_owned(), img)))).unwrap();
        }else{
            tx.send((objectid.to_owned(), None)).unwrap();
        }
    });

    rx.into_iter().map(move |(objectid, name_img)| {
        if let Some((name, img)) = name_img {
            #[cfg(feature="squish")]
            if can_be_compressed(&img) {
                (objectid, world.register_compress_named_image(&name, &img, flags).ok())
            }else{
                (objectid, world.register_named_image(&name, &img, flags).ok())
            }

            #[cfg(not(feature="squish"))]
            (objectid, world.register_named_image(&name, &img, flags).ok())
        }else{
            (objectid, None)
        }
    })
}

#[cfg(not(feature="rayon"))]
#[cfg(image_enabled)]
pub fn load_images_data<'a, 'w, 'c, C, I>(images: I, world: &'w mut C) -> impl Iterator<Item = (rinblender::ObjectId, Option<TextureRef>)>
where
    C: CreationContext<'c>,
    I: Iterator<Item = (&'a rinblender::ObjectId, String, &'a rinblender::texture::Data)>
{
    let flags = TextureCreationFlags::MIPMAPS;
    images.map(|(objectid, name, i)| {
        if let Ok(img) = image::load_from_memory(&i.data)
            .log_err("Error loading blender texture")
        {
            let img = convert_pre_compress(img);

            #[cfg(feature="squish")]
            if can_be_compressed(&img) {
                (objectid, world.register_compress_named_image(&name, &img, flags).ok())
            }else{
                (objectid, world.register_named_image(&name, &img, flags).ok())
            }

            #[cfg(not(feature="squish"))]
            (objectid.to_owned(), world.register_named_image(&name, &img, flags).ok())
        }else{
            (objectid.to_owned(), None)
        }
    }).collect::<Vec<_>>().into_iter()
}