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.
128 lines
4.3 KiB
128 lines
4.3 KiB
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
public partial class RenderModule
|
|
{
|
|
private readonly UniQuake uq;
|
|
private readonly List<AliasModel> aliasModels = new List<AliasModel>();
|
|
private readonly Dictionary<uint, Texture2D> textures = new Dictionary<uint, Texture2D>();
|
|
|
|
public RenderModule(UniQuake uniQuake)
|
|
{
|
|
uq = uniQuake;
|
|
BuildCallbacks();
|
|
}
|
|
|
|
public override void Destroy()
|
|
{
|
|
base.Destroy();
|
|
|
|
foreach (var aliasModel in aliasModels)
|
|
{
|
|
// aliasModel.Dispose(); // TODO: reactivate when done testing in editor
|
|
}
|
|
|
|
aliasModels.Clear();
|
|
|
|
foreach (var texture in textures.Values)
|
|
{
|
|
// Object.Destroy(texture); // TODO: reactivate when done testing in editor
|
|
}
|
|
|
|
textures.Clear();
|
|
}
|
|
|
|
private float xPos = -8f;
|
|
|
|
private int UploadAliasModel(string name, QAliasHeader header, QAliasFrameType frameType,
|
|
QTriVertex[][] poseVertices, QTriangle[] triangles, QSTVert[] stVertices,
|
|
QGLTexture[][] glTextures, QGLTexture[][] fbTextures)
|
|
{
|
|
var sb = new System.Text.StringBuilder();
|
|
foreach (var frame in header.frames)
|
|
{
|
|
sb.AppendLine($"- {frame.name} ({frame.numPoses} poses)");
|
|
}
|
|
|
|
Debug.Log($"Alias model '{name}' (frame type {frameType}) with {header.numVerts} vertices, {header.numTriangles} triangles, {header.numFrames} frame(s), {header.numPoses} pose(s):\n{sb}");
|
|
|
|
string modelName = System.IO.Path.GetFileNameWithoutExtension(name);
|
|
AliasModel aliasModel = new AliasModel(modelName, frameType);
|
|
aliasModel.ImportMeshData(header, poseVertices, triangles, stVertices);
|
|
aliasModels.Add(aliasModel);
|
|
|
|
var go = new GameObject(modelName);
|
|
go.transform.SetPositionAndRotation(new Vector3(xPos, 0, 0), Quaternion.Euler(-90, 90, 0));
|
|
|
|
aliasModel.Animate(0, out Mesh mesh, out float blendWeight);
|
|
|
|
var material = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
|
|
uint texNum = glTextures[0][0].texNum;
|
|
if (texNum > 0 && textures.ContainsKey(texNum))
|
|
{
|
|
material.mainTexture = textures[texNum];
|
|
}
|
|
|
|
if (header.numPoses > 1)
|
|
{
|
|
var mr = go.AddComponent<SkinnedMeshRenderer>();
|
|
mr.material = material;
|
|
mr.sharedMesh = mesh;
|
|
mr.shadowCastingMode = ShadowCastingMode.Off;
|
|
mr.receiveShadows = false;
|
|
mr.lightProbeUsage = LightProbeUsage.Off;
|
|
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
|
|
|
|
var animator = go.AddComponent<AliasModelAnimator>();
|
|
animator.aliasModel = aliasModel;
|
|
}
|
|
else
|
|
{
|
|
var mf = go.AddComponent<MeshFilter>();
|
|
mf.sharedMesh = mesh;
|
|
var mr = go.AddComponent<MeshRenderer>();
|
|
mr.material = material;
|
|
mr.shadowCastingMode = ShadowCastingMode.Off;
|
|
mr.receiveShadows = false;
|
|
mr.lightProbeUsage = LightProbeUsage.Off;
|
|
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
|
|
}
|
|
|
|
xPos += 0.5f;
|
|
return 1;
|
|
}
|
|
|
|
private int UploadBrushModel(QModel model)
|
|
{
|
|
if (model.type != QModelType.Brush)
|
|
return -1;
|
|
|
|
Debug.Log($"Brush model '{model.name}' with {model.numLeafs} leafs, {model.numVertices} vertices, {model.numSurfaces} surfaces");
|
|
return 1;
|
|
}
|
|
|
|
private uint nextTexNum = 0x10001;
|
|
|
|
private bool UploadTexture(QGLTexture texture, byte[] data, ref uint texNum)
|
|
{
|
|
Debug.Log($"Texture '{texture.name}' with dimensions {texture.width}x{texture.height}, data size = {data.Length} bytes");
|
|
|
|
if (texNum == 0)
|
|
{
|
|
// Assign a new texture number
|
|
while (textures.ContainsKey(nextTexNum))
|
|
++nextTexNum;
|
|
|
|
texNum = nextTexNum++;
|
|
}
|
|
|
|
var tex = new Texture2D((int)texture.width, (int)texture.height, TextureFormat.RGBA32, texture.flags.HasFlag(QTexPrefs.Mipmap));
|
|
tex.name = texture.name;
|
|
tex.SetPixelData(data, 0);
|
|
tex.Apply();
|
|
|
|
textures[texNum] = tex;
|
|
return true;
|
|
}
|
|
}
|