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 7d04959
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 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
12 changes: 6 additions & 6 deletions src/OuterWilds/Scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ varying vec2 v_TexCoord0;
#if defined VERT
void mainVS() {
Mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = Mul(t_WorldFromLocalMatrix, vec4(a_Position, 1.0));
mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = t_WorldFromLocalMatrix * vec4(a_Position, 1.0);
vec3 t_LightDirection = normalize(vec3(.2, -1, .5));
vec3 normal = MulNormalMatrix(t_WorldFromLocalMatrix, normalize(a_Normal));
float t_LightIntensityF = dot(-normal, t_LightDirection);
float t_LightIntensityB = dot( normal, t_LightDirection);
gl_Position = Mul(u_ProjectionView, vec4(t_PositionWorld, 1.0));
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
v_TexCoord0 = CalcScaleBias(a_TexCoord0, u_MainTexST);
}
Expand Down Expand Up @@ -127,13 +127,13 @@ uniform sampler2D u_Splat3;
#ifdef VERT
void mainVS() {
Mat4x3 t_WorldFromLocalMatrix = CalcWorldFromLocalMatrix();
vec3 t_PositionWorld = Mul(t_WorldFromLocalMatrix, vec4(a_Position, 1.0));
vec3 t_PositionWorld = t_WorldFromLocalMatrix * vec4(a_Position, 1.0);
vec3 t_LightDirection = normalize(vec3(.2, -1, .5));
vec3 normal = MulNormalMatrix(t_WorldFromLocalMatrix, normalize(a_Normal));
float t_LightIntensityF = dot(-normal, t_LightDirection);
float t_LightIntensityB = dot( normal, t_LightDirection);
gl_Position = Mul(u_ProjectionView, vec4(t_PositionWorld, 1.0));
gl_Position = u_ProjectionView * vec4(t_PositionWorld, 1.0);
v_LightIntensity = vec2(t_LightIntensityF, t_LightIntensityB);
for (int i = 0; i < 6; i++)
Expand Down Expand Up @@ -212,7 +212,7 @@ class OuterWildsMaterialFactory extends UnityMaterialFactory {

public createMaterialInstance(runtime: UnityRuntime, materialData: UnityMaterialData): UnityMaterialInstance {
// TODO(jstpierre): Pull out serialized shader data
console.log(materialData.name, materialData.texturesByName.keys(), materialData.shader);
// console.log(materialData.name, materialData.texturesByName.keys(), materialData.shader);
// const matType = materialData.name.split("_")[0];
// const matNames = Array.from(materialData.texturesByName.keys());
// const existing = this.registry.get(matType);
Expand Down

0 comments on commit 7d04959

Please sign in to comment.