diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 0ad9ef8f7..cfdcc1fd7 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -522,6 +522,7 @@ dependencies = [ "num_enum", "polymorph", "rand", + "texture2ddecoder", "wasm-bindgen", "web-sys", ] @@ -807,6 +808,14 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "texture2ddecoder" +version = "0.1.1" +source = "git+https://github.com/wgreenberg/texture2ddecoder#fff18dbf8e6ced202c5e05b236cbd018c9af6c71" +dependencies = [ + "paste", +] + [[package]] name = "thiserror" version = "1.0.69" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 0d3a9fdd7..90927d794 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -41,3 +41,4 @@ nalgebra-glm = "0.19.0" rand = "0.8.5" getrandom = { version = "0.2.15", features = ["js"] } noclip-macros = { version = "*", path = "./noclip-macros" } +texture2ddecoder = { git = "https://github.com/wgreenberg/texture2ddecoder" } diff --git a/rust/src/compression.rs b/rust/src/compression.rs index d884b6fb6..448eb43cb 100644 --- a/rust/src/compression.rs +++ b/rust/src/compression.rs @@ -38,3 +38,27 @@ pub fn deflate_raw_decompress(src: &[u8]) -> Vec { inflate::inflate_bytes(src).unwrap() } +#[wasm_bindgen(js_name = "CrunchTexture")] +pub struct CrunchTexture { + handle: texture2ddecoder::CrunchHandle, +} + +#[wasm_bindgen(js_class = "CrunchTexture")] +impl CrunchTexture { + pub fn new(data: &[u8]) -> Result { + let handle = texture2ddecoder::CrunchHandle::new(data) + .map_err(|err| format!("{:?}", err))?; + Ok(Self { + handle, + }) + } + + pub fn get_num_levels(&self) -> u32 { + self.handle.get_num_levels() + } + + pub fn decode_level(&self, data: &[u8], level_index: u32) -> Result, String> { + self.handle.unpack_level(data, level_index) + .map_err(|err| err.into()) + } +} diff --git a/src/Common/Unity/AssetManager.ts b/src/Common/Unity/AssetManager.ts index 33adf7990..28bfb02e2 100644 --- a/src/Common/Unity/AssetManager.ts +++ b/src/Common/Unity/AssetManager.ts @@ -1,6 +1,6 @@ import { vec2, vec3 } from 'gl-matrix'; -import { UnityAABB, UnityAssetFile, UnityAssetFileObject, UnityChannelInfo, UnityClassID, UnityGLTextureSettings, UnityMaterial, UnityMesh, UnityMeshCompression, UnityPPtr, UnityShader, UnityStreamingInfo, UnitySubMesh, UnityTexture2D, UnityTextureColorSpace, UnityTextureFormat, UnityVersion, UnityVertexFormat } from '../../../rust/pkg/noclip_support'; +import { UnityAABB, UnityAssetFile, UnityAssetFileObject, UnityChannelInfo, UnityClassID, UnityGLTextureSettings, UnityMaterial, UnityMesh, UnityMeshCompression, UnityPPtr, UnityShader, UnityStreamingInfo, UnitySubMesh, UnityTexture2D, UnityTextureColorSpace, UnityTextureFormat, UnityVersion, UnityVertexFormat, CrunchTexture } from '../../../rust/pkg/noclip_support'; import ArrayBufferSlice from '../../ArrayBufferSlice.js'; import { Color, TransparentBlack, colorNewFromRGBA } from '../../Color.js'; import { DataFetcher } from '../../DataFetcher.js'; @@ -712,19 +712,21 @@ export class UnityTexture2DData { this.gfxSampler = cache.createSampler(translateSampler(header.texture_settings)); - // TODO(jstpierre): Support crunched formats - if (header.texture_format === rust.UnityTextureFormat.DXT1Crunched) { - console.warn(`DXT1Crunched ${this.header.name}`); - return; - } - if (header.texture_format === rust.UnityTextureFormat.DXT5Crunched) { - console.warn(`DXT5Crunched ${this.header.name}`); - return; + if (header.texture_format === rust.UnityTextureFormat.DXT1Crunched || header.texture_format === rust.UnityTextureFormat.DXT5Crunched) { + const crunched = CrunchTexture.new(data); + const levels = []; + // FIXME: texture2ddecoder seems to be broken for higher mip levels + // let numLevels = crunched.get_num_levels(); + let numLevels = 1; + for (let i = 0; i < numLevels; i++) { + levels.push(crunched.decode_level(data, i)); + } + device.uploadTextureData(this.gfxTexture, 0, levels); + } else { + const oData = imageFormatConvertData(data, header.texture_format); + const levels = calcLevels(oData, header.texture_format, header.width, header.height, header.mip_count); + device.uploadTextureData(this.gfxTexture, 0, levels); } - - const oData = imageFormatConvertData(data, header.texture_format); - const levels = calcLevels(oData, header.texture_format, header.width, header.height, header.mip_count); - device.uploadTextureData(this.gfxTexture, 0, levels); } public fillTextureMapping(dst: TextureMapping): void {