Skip to content

Commit

Permalink
Merge pull request #151 from gltf-rs/incoming
Browse files Browse the repository at this point in the history
Merge incoming
  • Loading branch information
alteous authored Mar 4, 2018
2 parents 6101b7c + de6dcc5 commit 16949b0
Show file tree
Hide file tree
Showing 14 changed files with 1,792 additions and 560 deletions.
2 changes: 1 addition & 1 deletion glTF-Sample-Models
1 change: 1 addition & 0 deletions gltf-importer/tests/import_examples.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unknown_lints)]
extern crate gltf_importer;

use std::{fs, path};
Expand Down
6 changes: 6 additions & 0 deletions gltf-json/src/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ pub mod sparse {
pub component_type: Checked<IndexComponentType>,

/// Extension specific data.
#[serde(default)]
pub extensions: extensions::accessor::sparse::Indices,

/// Optional application specific data.
#[serde(default)]
pub extras: Extras,
}

Expand All @@ -147,9 +149,11 @@ pub mod sparse {
pub values: Values,

/// Extension specific data.
#[serde(default)]
pub extensions: extensions::accessor::sparse::Sparse,

/// Optional application specific data.
#[serde(default)]
pub extras: Extras,
}

Expand All @@ -169,9 +173,11 @@ pub mod sparse {
pub byte_offset: u32,

/// Extension specific data.
#[serde(default)]
pub extensions: extensions::accessor::sparse::Values,

/// Optional application specific data.
#[serde(default)]
pub extras: Extras,
}
}
Expand Down
8 changes: 4 additions & 4 deletions gltf-json/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ pub struct PbrMetallicRoughness {
///
/// This texture has two components:
///
/// * The first component (R) contains the metallic-ness of the material.
/// * The second component (G) contains the roughness of the material.
/// * If the third component (B) and/or the fourth component (A) are present
/// then they are ignored.
/// The metalness values are sampled from the B channel.
/// The roughness values are sampled from the G channel.
/// These values are linear. If other channels are present (R or A),
/// they are ignored for metallic-roughness calculations.
#[serde(rename = "metallicRoughnessTexture")]
pub metallic_roughness_texture: Option<texture::Info>,

Expand Down
322 changes: 322 additions & 0 deletions gltf-utils/src/casts/colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
use std::marker::PhantomData;

use super::Normalize;

use Colors;

/// Casting iterator for `Colors`.
#[derive(Clone, Debug)]
pub struct CastingIter<'a, T>(Colors<'a>, PhantomData<T>);

/// Type which describes how to cast any color into RGB u8.
#[derive(Clone, Debug)]
pub struct RgbU8;

/// Type which describes how to cast any color into RGB u16.
#[derive(Clone, Debug)]
pub struct RgbU16;

/// Type which describes how to cast any color into RGB f32.
#[derive(Clone, Debug)]
pub struct RgbF32;

/// Type which describes how to cast any color into RGBA u8.
#[derive(Clone, Debug)]
pub struct RgbaU8;

/// Type which describes how to cast any color into RGBA u16.
#[derive(Clone, Debug)]
pub struct RgbaU16;

/// Type which describes how to cast any color into RGBA f32.
#[derive(Clone, Debug)]
pub struct RgbaF32;

trait ColorChannel {
fn max_color() -> Self;
}

impl ColorChannel for u8 {
fn max_color() -> Self { u8::max_value() }
}

impl ColorChannel for u16 {
fn max_color() -> Self { u16::max_value() }
}

impl ColorChannel for f32 {
fn max_color() -> Self { 1.0 }
}

trait ColorArray<T> {
fn into_rgb(self) -> [T; 3];
fn into_rgba(self) -> [T; 4];
}

impl<T: Copy + ColorChannel> ColorArray<T> for [T; 3] {
fn into_rgb(self) -> [T; 3] { self }
fn into_rgba(self) -> [T; 4] { [self[0], self[1], self[2], T::max_color()] }
}

impl<T: Copy + ColorChannel> ColorArray<T> for [T; 4] {
fn into_rgb(self) -> [T; 3] { [self[0], self[1], self[2]] }
fn into_rgba(self) -> [T; 4] { self }
}

/// Trait for types which describe casting behaviour.
pub trait Cast {
/// Output type.
type Output;

/// Cast from RGB u8.
fn cast_rgb_u8(x: [u8; 3]) -> Self::Output;

/// Cast from RGB u16.
fn cast_rgb_u16(x: [u16; 3]) -> Self::Output;

/// Cast from RGB f32.
fn cast_rgb_f32(x: [f32; 3]) -> Self::Output;

/// Cast from RGBA u8.
fn cast_rgba_u8(x: [u8; 4]) -> Self::Output;

/// Cast from RGBA u16.
fn cast_rgba_u16(x: [u16; 4]) -> Self::Output;

/// Cast from RGBA f32.
fn cast_rgba_f32(x: [f32; 4]) -> Self::Output;
}

impl<'a, A> CastingIter<'a, A> {
pub(crate) fn new(iter: Colors<'a>) -> Self {
CastingIter(iter, PhantomData)
}

/// Unwrap underlying `Colors` object.
pub fn unwrap(self) -> Colors<'a> {
self.0
}
}

impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
type Item = A::Output;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
Colors::RgbU8(ref mut i) => i.next().map(A::cast_rgb_u8),
Colors::RgbU16(ref mut i) => i.next().map(A::cast_rgb_u16),
Colors::RgbF32(ref mut i) => i.next().map(A::cast_rgb_f32),
Colors::RgbaU8(ref mut i) => i.next().map(A::cast_rgba_u8),
Colors::RgbaU16(ref mut i) => i.next().map(A::cast_rgba_u16),
Colors::RgbaF32(ref mut i) => i.next().map(A::cast_rgba_f32),
}
}

#[inline]
fn nth(&mut self, x: usize) -> Option<Self::Item> {
match self.0 {
Colors::RgbU8(ref mut i) => i.nth(x).map(A::cast_rgb_u8),
Colors::RgbU16(ref mut i) => i.nth(x).map(A::cast_rgb_u16),
Colors::RgbF32(ref mut i) => i.nth(x).map(A::cast_rgb_f32),
Colors::RgbaU8(ref mut i) => i.nth(x).map(A::cast_rgba_u8),
Colors::RgbaU16(ref mut i) => i.nth(x).map(A::cast_rgba_u16),
Colors::RgbaF32(ref mut i) => i.nth(x).map(A::cast_rgba_f32),
}
}

fn last(self) -> Option<Self::Item> {
match self.0 {
Colors::RgbU8(i) => i.last().map(A::cast_rgb_u8),
Colors::RgbU16(i) => i.last().map(A::cast_rgb_u16),
Colors::RgbF32(i) => i.last().map(A::cast_rgb_f32),
Colors::RgbaU8(i) => i.last().map(A::cast_rgba_u8),
Colors::RgbaU16(i) => i.last().map(A::cast_rgba_u16),
Colors::RgbaF32(i) => i.last().map(A::cast_rgba_f32),
}
}

fn count(self) -> usize {
self.size_hint().0
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.0 {
Colors::RgbU8(ref i) => i.size_hint(),
Colors::RgbU16(ref i) => i.size_hint(),
Colors::RgbF32(ref i) => i.size_hint(),
Colors::RgbaU8(ref i) => i.size_hint(),
Colors::RgbaU16(ref i) => i.size_hint(),
Colors::RgbaF32(ref i) => i.size_hint(),
}
}
}

impl Cast for RgbU8 {
type Output = [u8; 3];

fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.into_rgb().normalize()
}
}

impl Cast for RgbU16 {
type Output = [u16; 3];

fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.into_rgb().normalize()
}
}

impl Cast for RgbF32 {
type Output = [f32; 3];

fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.into_rgb().normalize()
}

fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.into_rgb().normalize()
}
}

impl Cast for RgbaU8 {
type Output = [u8; 4];

fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.normalize().into_rgba()
}
}

impl Cast for RgbaU16 {
type Output = [u16; 4];

fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.normalize().into_rgba()
}
}

impl Cast for RgbaF32 {
type Output = [f32; 4];

fn cast_rgb_u8(x: [u8; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgb_u16(x: [u16; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgb_f32(x: [f32; 3]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_u8(x: [u8; 4]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_u16(x: [u16; 4]) -> Self::Output {
x.normalize().into_rgba()
}

fn cast_rgba_f32(x: [f32; 4]) -> Self::Output {
x.normalize().into_rgba()
}
}
Loading

0 comments on commit 16949b0

Please sign in to comment.