Skip to content

Commit

Permalink
use texture2ddecoder for decrunching
Browse files Browse the repository at this point in the history
  • Loading branch information
wgreenberg committed Jan 19, 2025
1 parent a2b09a3 commit 242d210
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
9 changes: 9 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
24 changes: 24 additions & 0 deletions rust/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@ pub fn deflate_raw_decompress(src: &[u8]) -> Vec<u8> {
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<Self, String> {
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<Vec<u8>, String> {
self.handle.unpack_level(data, level_index)
.map_err(|err| err.into())
}
}
28 changes: 15 additions & 13 deletions src/Common/Unity/AssetManager.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 242d210

Please sign in to comment.