dlib/assets.d
2025-11-08 11:58:20 +11:00

502 lines
7.9 KiB
D

module dlib.assets;
import includes;
import dlib.aliases;
import dlib.util;
import dlib.alloc;
import std.file;
import std.stdio;
import std.exception;
File Asset_File;
FileHeader Asset_Header;
AssetInfo[] Asset_Info;
u8[][] Asset_Data;
const u32 FILE_VERSION = 3;
const u32 MODEL_VERSION = 2;
enum AssetType : u32
{
None,
ModelM3D,
Shader,
Texture,
}
alias AT = AssetType;
struct FileHeader
{
u32 magic;
u32 file_version;
u64 asset_count;
u64 asset_info_offset;
}
struct ModelHeader
{
u32 magic;
u32 model_version;
u64 vertex_count;
u64 vertex_offset;
u64 index_count;
u64 index_offset;
u64 material_count;
u64 material_offset;
u64 texture_count;
u64 texture_offset;
}
struct Vertex
{
Vec4 color;
Vec4 tangent;
Vec3 pos;
Vec3 normal;
Vec2 uv;
}
struct ModelData
{
Vertex[] vertices;
u32[] indices;
MaterialData[] materials;
TextureInfo[] textures;
}
enum IllumModel : u32
{
Color = 0,
ColorAmbient,
Highlight,
ReflectionRayTrace,
GlassRayTrace,
FresnelRayTrace,
RefractionRayTrace,
RefractionFresnelRayTrace,
Reflection,
Glass,
ShadowsInvis,
}
enum MatColor : u32
{
Ambient,
Albedo,
Specular,
Transmission,
Emissive,
Max,
}
enum MatFloat : u32
{
SpecularExp,
Alpha,
OpticalDensity,
Roughness,
Metallic,
ClearcoatThickness,
ClearcoatRoughness,
Anisotropy,
Bump,
FaceThickness,
Sheen,
Max,
}
enum MatMap : u32
{
Ambient,
Albedo,
Specular,
SpecularHighlight,
Alpha,
Bump,
Displacement,
Roughness,
Metallic,
Emissive,
Anisotropy,
Normal,
Max,
}
struct MaterialData
{
Vec4[MatColor.max] colors;
string[MatMap.max] maps;
f32[MatFloat.max] props = 0.0;
IllumModel illum;
}
struct TextureInfo
{
string name;
u32 id;
}
struct TextureHeader
{
u64 str_length;
u64 str_offset;
u32 texture_id;
}
struct ModelMeta
{
u64 index_count;
}
struct TexData
{
void* data;
TexMeta meta;
}
struct TexMeta
{
u32 w;
u32 h;
u32 ch;
}
struct AssetInfo
{
u64 hash;
u64 offset;
u64 length;
AssetType type;
}
enum MatProp
{
None,
Ambient,
Albedo,
Specular,
SpecularExp,
Dissolve, // Transparency 1.0 -> opaque
Transparency, // Transparency 0.0 -> opaque
Transmission,
OpticalDensity,
Illumination,
AmbientMap,
AlbedoMap,
SpecularMap,
SpecularHighlightMap,
AlphaMap,
BumpMap,
DisplacementMap,
Stencil,
Roughness,
RoughnessMap,
Metallic,
MetallicMap,
Sheen,
SheenMap,
ClearcoatThickness,
ClearcoatRoughness,
Emissive,
EmissiveMap,
Anisotropy,
AnisotropyMap,
NormalMap,
}
bool Asset_Pack_Opened = false;
MatProp
GetMapProp(u8[] str)
{
string s = ConvToStr!(u8)(str);
switch(s) with(MatProp)
{
case "Ka": return Ambient;
case "Kd": return Albedo;
case "Ks": return Specular;
case "Ns": return SpecularExp;
case "d": return Dissolve;
case "Tr": return Transparency;
case "Tf": return Transmission;
case "Ni": return OpticalDensity;
case "illum": return Illumination;
case "map_Ka": return AmbientMap;
case "map_Kd": return AlbedoMap;
case "map_Ks": return SpecularMap;
case "map_Ns": return SpecularHighlightMap;
case "map_d": return AlphaMap;
case "map_bump":
case "bump": return BumpMap;
case "disp": return DisplacementMap;
case "decal": return Stencil;
case "Pr": return Roughness;
case "map_Pr": return RoughnessMap;
case "Pm": return Metallic;
case "map_Pm": return MetallicMap;
case "Pc": return ClearcoatThickness;
case "Pcr": return ClearcoatRoughness;
case "Ke": return Emissive;
case "map_Ke": return EmissiveMap;
case "aniso": return Anisotropy;
case "anisor": return AnisotropyMap;
case "norm": return NormalMap;
default: return None;
}
}
bool
M3DColor(u32 type)
{
return type == m3dp_Kd ||
type == m3dp_Ka ||
type == m3dp_Ks ||
type == m3dp_Ke ||
type == m3dp_Tf;
}
MatColor
M3DMatColor(u32 type)
{
switch(type) with(MatColor)
{
case m3dp_Kd: return Albedo;
case m3dp_Ka: return Ambient;
case m3dp_Ks: return Specular;
case m3dp_Ke: return Emissive;
case m3dp_Tf: return Transmission;
default: assert(false, "Unknown M3D MatColor");
}
}
bool
M3DFloat(u32 type)
{
return type == m3dp_Ns ||
type == m3dp_Km ||
type == m3dp_d ||
type == m3dp_Pr ||
type == m3dp_Pm ||
type == m3dp_Ps ||
type == m3dp_Ni ||
type == m3dp_Nt;
}
MatFloat
M3DMatFloat(u32 type)
{
switch(type) with(MatFloat)
{
case m3dp_Ns: return SpecularExp;
case m3dp_Km: return Bump;
case m3dp_d: return Alpha;
case m3dp_Pr: return Roughness;
case m3dp_Pm: return Metallic;
case m3dp_Ps: return Sheen;
case m3dp_Ni: return OpticalDensity;
case m3dp_Nt: return FaceThickness;
default: assert(false);
}
}
bool
M3DMap(u32 type)
{
return type == m3dp_map_Kd ||
type == m3dp_map_Ka ||
type == m3dp_map_Ks ||
type == m3dp_map_Ns ||
type == m3dp_map_Ke ||
type == m3dp_map_Tf ||
type == m3dp_map_Km ||
type == m3dp_map_D ||
type == m3dp_map_N ||
type == m3dp_map_Pr ||
type == m3dp_map_Pm ||
type == m3dp_map_Ps ||
type == m3dp_map_Ni ||
type == m3dp_map_Nt;
}
debug
{
void
SetAssetsDir(string dir)
{
assert(ChDir(dir));
}
u8[]
LoadAssetData(Arena* arena, string name)
{
File f;
try
{
f = File(name, "rb");
}
catch (ErrnoException e)
{
assert(false, "Unable to open file");
}
u8[] mem = Alloc!(u8)(arena, f.size());
return f.rawRead(mem);
}
}
else
{
void
OpenAssetPack(string file_path)
{
if(!Asset_Pack_Opened)
{
bool success = true;
// TODO: replace this with something that doesn't throw an exception and figure out if this is the best way to handle thing (probably isnt)
try
{
Asset_File = File(file_path, "rb");
}
catch (ErrnoException e)
{
Logf("OpenAssetPack failure: Unable to open file %s", file_path);
assert(false, "Unable to open asset pack file");
}
FileHeader[1] header_arr;
Asset_File.rawRead(header_arr);
Asset_Header = header_arr[0];
Asset_Info = Alloc!(AssetInfo)(Asset_Header.asset_count);
Asset_Data = Alloc!(u8[])(Asset_Header.asset_count);
assert(Asset_Header.file_version == FILE_VERSION, "OpenAssetPack failure: file version incorrect");
Asset_File.seek(Asset_Header.asset_info_offset);
Asset_File.rawRead(Asset_Info);
}
}
pragma(inline) void
CheckAssetPack()
{
assert(Asset_Pack_Opened);
}
AssetInfo
GetAssetInfo(string name)
{
CheckAssetPack();
u64 hash = Hash(name);
AssetInfo asset_info;
foreach(i, info; Asset_Info)
{
if(info.hash == hash)
{
asset_info = info;
break;
}
}
assert(asset_info.hash != 0, "GetAssetInfo failure: unable to find matching asset");
return asset_info;
}
u8[]
LoadAssetData(Arena* arena, string name)
{
CheckAssetPack();
u64 hash = Hash(name);
u8[] data = null;
foreach(i, info; Asset_Info)
{
if(info.hash == hash)
{
data = Alloc!(u8)(arena, info.length);
Asset_File.seek(info.offset);
Asset_File.rawRead(data);
assert(data != null && data.length == info.length, "LoadAssetData failure: Asset data loaded incorrectly");
break;
}
}
return data;
}
u8[]
LoadAssetData(string name)
{
CheckAssetPack();
u64 hash = Hash(name);
u8[] data = null;
foreach(i, info; Asset_Info)
{
if(info.hash == hash)
{
if(Asset_Data[i].ptr == null)
{
Asset_Data[i] = Alloc!(u8)(info.length);
Asset_File.seek(info.offset);
Asset_File.rawRead(Asset_Data[i]);
assert(Asset_Data[i] != null && Asset_Data[i].length == info.length, "LoadAssetData failure: Asset data loaded incorrectly.");
}
data = Asset_Data[i];
break;
}
}
return data;
}
void
UnloadAssetData(string name)
{
u64 hash = Hash(name);
foreach(i, info; Asset_Info)
{
if(info.hash == hash)
{
if(Asset_Data[i] != null)
{
Free(Asset_Data[i]);
break;
}
}
}
}
}
unittest
{
{
MatProp prop = GetMapProp(cast(u8[])r"Ka");
}
}