You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.3 KiB
177 lines
4.3 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GameAssets
|
|
{
|
|
private readonly UniQuake uq;
|
|
|
|
private readonly Dictionary<uint, Texture2D> textures = new Dictionary<uint, Texture2D>();
|
|
private readonly Dictionary<int, Texture2D> lightmaps = new Dictionary<int, Texture2D>();
|
|
|
|
private BrushModel worldModel;
|
|
private readonly List<BrushModel> brushModels = new List<BrushModel>();
|
|
private readonly List<AliasModel> aliasModels = new List<AliasModel>();
|
|
|
|
private uint nextTexNum = 0x10001;
|
|
|
|
public GameAssets(UniQuake uniQuake)
|
|
{
|
|
uq = uniQuake;
|
|
}
|
|
|
|
public uint SetTexture(uint texNum, Texture2D texture)
|
|
{
|
|
if (texNum == 0)
|
|
{
|
|
// Assign a new texture number
|
|
while (textures.ContainsKey(nextTexNum))
|
|
++nextTexNum;
|
|
|
|
texNum = nextTexNum++;
|
|
}
|
|
|
|
if (textures.ContainsKey(texNum))
|
|
{
|
|
Object.Destroy(textures[texNum]);
|
|
textures.Remove(texNum);
|
|
}
|
|
|
|
textures.Add(texNum, texture);
|
|
return texNum;
|
|
}
|
|
|
|
public bool TryGetTexture(uint texNum, out Texture2D texture)
|
|
{
|
|
texture = null;
|
|
return texNum > 0 && textures.TryGetValue(texNum, out texture);
|
|
}
|
|
|
|
public bool TryGetTexture(string texName, out Texture2D texture)
|
|
{
|
|
foreach (var tex in textures.Values)
|
|
{
|
|
if (tex.name == texName)
|
|
{
|
|
texture = tex;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
texture = null;
|
|
return false;
|
|
}
|
|
|
|
public void UploadLightmap(int lightmapNum, int width, int height, byte[] data)
|
|
{
|
|
if (!lightmaps.TryGetValue(lightmapNum, out var lightmap))
|
|
{
|
|
lightmap = new Texture2D(width, height, TextureFormat.RGBA32, true) { name = $"Lightmap_{lightmapNum}" };
|
|
lightmaps.Add(lightmapNum, lightmap);
|
|
}
|
|
|
|
lightmap.SetPixelData(data, 0);
|
|
lightmap.Apply();
|
|
}
|
|
|
|
public bool TryGetLightmap(int lightmapNum, out Texture2D lightmap)
|
|
{
|
|
lightmap = null;
|
|
return lightmapNum >= 0 && lightmaps.TryGetValue(lightmapNum, out lightmap);
|
|
}
|
|
|
|
public void AddAliasModel(AliasModel aliasModel)
|
|
{
|
|
aliasModels.Add(aliasModel);
|
|
}
|
|
|
|
public bool TryGetAliasModel(string name, out AliasModel aliasModel)
|
|
{
|
|
foreach (var model in aliasModels)
|
|
{
|
|
if (model.Name == name)
|
|
{
|
|
aliasModel = model;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
aliasModel = null;
|
|
return false;
|
|
}
|
|
|
|
public void AddBrushModel(BrushModel brushModel)
|
|
{
|
|
brushModels.Add(brushModel);
|
|
}
|
|
|
|
public bool TryGetBrushModel(string name, out BrushModel brushModel)
|
|
{
|
|
foreach (var model in brushModels)
|
|
{
|
|
if (model.Name == name)
|
|
{
|
|
brushModel = model;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
brushModel = null;
|
|
return false;
|
|
}
|
|
|
|
public void SetWorldModel(BrushModel brushModel)
|
|
{
|
|
worldModel?.Dispose();
|
|
worldModel = brushModel;
|
|
}
|
|
|
|
public bool TryGetWorldSubModel(int subModelIndex, out BrushModel.SubModel subModel)
|
|
{
|
|
if (worldModel == null || subModelIndex < 0 || subModelIndex >= worldModel.SubModelCount)
|
|
{
|
|
subModel = null;
|
|
return false;
|
|
}
|
|
|
|
subModel = worldModel.GetSubModel(subModelIndex);
|
|
return true;
|
|
}
|
|
|
|
public void Destroy()
|
|
{
|
|
if (worldModel != null)
|
|
{
|
|
worldModel.Dispose();
|
|
worldModel = null;
|
|
}
|
|
|
|
foreach (var brushModel in brushModels)
|
|
{
|
|
brushModel.Dispose();
|
|
}
|
|
|
|
brushModels.Clear();
|
|
|
|
foreach (var aliasModel in aliasModels)
|
|
{
|
|
aliasModel.Dispose();
|
|
}
|
|
|
|
aliasModels.Clear();
|
|
|
|
foreach (var lightmap in lightmaps.Values)
|
|
{
|
|
Object.Destroy(lightmap);
|
|
}
|
|
|
|
lightmaps.Clear();
|
|
|
|
foreach (var texture in textures.Values)
|
|
{
|
|
Object.Destroy(texture);
|
|
}
|
|
|
|
textures.Clear();
|
|
}
|
|
}
|