diff --git a/Cargo.toml b/Cargo.toml index f083b05..b9948b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "unity-js-tools" -version = "1.2.0" +version = "2.0.0" edition = "2021" authors = ["神代綺凛 "] license = "MIT" diff --git a/README.md b/README.md index 4e4cc7a..2d668b4 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,7 @@ - [PSeitz/lz4_flex](https://github.com/PSeitz/lz4_flex) - [UniversalGameExtraction/texture2ddecoder](https://github.com/UniversalGameExtraction/texture2ddecoder) + +## 注意 + +wasm-bindgen 貌似有某种缺陷,rust 中极少数位运算场合编译成 wasm 后运算结果不正确,这导致 `decodeEtc2Rgba8` 解码后 alpha 通道数值错误,需要手动实现来修正,不排除还有其他情况有问题 diff --git a/src/lib.rs b/src/lib.rs index 01f8a61..c4a4611 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,7 @@ pub fn export_decode_astc( let result = decode_astc(data, width, height, block_width, block_height, &mut image); match result { - Ok(()) => Ok(image_to_rgba(image, width, height)), + Ok(()) => Ok(split_channel(image)), Err(e) => Err(to_js_err(e)), } } diff --git a/src/tools.rs b/src/tools.rs index 4285336..8827718 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -6,21 +6,10 @@ pub fn to_js_err(e: impl ToString) -> JsError { JsError::new(&e.to_string()) } -pub fn image_to_rgba(image: Vec, width: usize, height: usize) -> Vec { - let mut lines: Vec<&[u32]> = Vec::with_capacity(height); - for i in 0..height { - let start = i * width; - lines.push(&image[start..start + width]); - } - lines - .iter() - .copied() - .rev() - .flatten() - .flat_map(|x| { - let v = x.to_le_bytes(); - [v[2], v[1], v[0], v[3]] - }) +pub fn split_channel(image: Vec) -> Vec { + image + .into_iter() + .flat_map(|x| x.to_le_bytes()) .collect::>() } @@ -35,7 +24,7 @@ pub fn decode_texture( let result = decode_func(data, width, height, &mut image); match result { - Ok(()) => Ok(image_to_rgba(image, width, height)), + Ok(()) => Ok(split_channel(image)), Err(e) => Err(to_js_err(e)), } }