Browse Source

"Fixed" coordinate conversion from Quake to Unity, by swizzling Y and Z coordinates during vector conversion. There seems to be a lot of confusion and disagreement about Quake's coordinate system, but this setup is simple and looks correct both in-game and in-editor, and the world isn't mirrored anymore either.

console
Nico de Poel 5 years ago
parent
commit
211c64a879
  1. 7
      Assets/Scripts/Data/QExtensions.cs
  2. 2
      Assets/Scripts/Data/QModel.cs
  3. 13
      Assets/Scripts/Modules/AliasModel.cs
  4. 10
      Assets/Scripts/Modules/BrushModel.cs
  5. 2
      Assets/Scripts/Modules/RenderModule.cs

7
Assets/Scripts/Data/QExtensions.cs

@ -30,7 +30,7 @@ public static class QExtensions
return result;
}
public static Vector3 ToVector2(this QVec2 vec)
public static Vector2 ToVector2(this QVec2 vec)
{
return new Vector2(vec.x, vec.y);
}
@ -45,4 +45,9 @@ public static class QExtensions
byte[] v = triVertex.v;
return new Vector3(v[0], v[1], v[2]);
}
public static Vector3 ToUnity(this Vector3 vec)
{
return new Vector3(vec.x, vec.z, vec.y);
}
}

2
Assets/Scripts/Data/QModel.cs

@ -259,6 +259,7 @@ public struct QSurface
public bool cachedDLight;
public IntPtr samples; // Pointer to byte
// This is safe as the Quake engine guarantees this pointer is never null (see: Mod_LoadFaces)
public QTexInfo TextureInfo => Marshal.PtrToStructure<QTexInfo>(texInfo);
public IEnumerable<QGLPolyVert[]> GetPolygons()
@ -311,5 +312,6 @@ public struct QTexInfo
public IntPtr texture; // Pointer to texture_t
public int flags;
// This is safe as the Quake engine guarantees this pointer is never null (see: Mod_LoadTexinfo)
public QTexture Texture => Marshal.PtrToStructure<QTexture>(texture);
}

13
Assets/Scripts/Modules/AliasModel.cs

@ -220,8 +220,8 @@ public class AliasModel
for (int i = 0; i < numVerts; ++i)
{
vertices[i] = Vector3.Scale(triVerts[i].ToVector3(), scale) + origin;
normals[i] = QLightNormals.Get(triVerts[i].lightNormalIndex);
vertices[i] = (Vector3.Scale(triVerts[i].ToVector3(), scale) + origin).ToUnity();
normals[i] = QLightNormals.Get(triVerts[i].lightNormalIndex).ToUnity();
}
}
@ -232,10 +232,9 @@ public class AliasModel
for (int i = 0; i < numTris; ++i)
{
// Quake triangles are wound clockwise, so we reverse them here
indices[i * 3 + 0] = (ushort)triangles[i].vertIndex[2];
indices[i * 3 + 0] = (ushort)triangles[i].vertIndex[0];
indices[i * 3 + 1] = (ushort)triangles[i].vertIndex[1];
indices[i * 3 + 2] = (ushort)triangles[i].vertIndex[0];
indices[i * 3 + 2] = (ushort)triangles[i].vertIndex[2];
}
}
@ -295,9 +294,9 @@ public class AliasModel
for (int vertIdx = 0; vertIdx < header.numVerts; ++vertIdx)
{
Vector3 vert = Vector3.Scale(poseVerts[vertIdx].ToVector3(), scale) + origin;
Vector3 vert = (Vector3.Scale(poseVerts[vertIdx].ToVector3(), scale) + origin).ToUnity();
deltaVertices[vertIdx] = vert - baseVertices[vertIdx];
deltaNormals[vertIdx] = QLightNormals.Get(poseVerts[vertIdx].lightNormalIndex) - baseNormals[vertIdx];
deltaNormals[vertIdx] = QLightNormals.Get(poseVerts[vertIdx].lightNormalIndex).ToUnity() - baseNormals[vertIdx];
}
mesh.AddBlendShapeFrame(animName, (float)index / numPoses, deltaVertices, deltaNormals, null);

10
Assets/Scripts/Modules/BrushModel.cs

@ -30,17 +30,17 @@ public class BrushModel
for (int vertIdx = 0; vertIdx < polyVerts.Length; ++vertIdx)
{
vertices.Add(polyVerts[vertIdx].position.ToVector3());
vertices.Add(polyVerts[vertIdx].position.ToVector3().ToUnity());
textureUVs.Add(polyVerts[vertIdx].textureUV.ToVector2());
lightmapUVs.Add(polyVerts[vertIdx].lightmapUV.ToVector2());
}
// Reconstruct triangle fan (in reverse order)
// Reconstruct triangle fan
for (ushort index = 2; index < polyVerts.Length; ++index)
{
indices.Add(index);
indices.Add((ushort)(index - 1));
indices.Add(0);
indices.Add((ushort)(index - 1));
indices.Add(index);
}
Mesh mesh = new Mesh();
@ -78,6 +78,6 @@ public class BrushModel
mr.reflectionProbeUsage = ReflectionProbeUsage.Off;
}
go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.Euler(-90, 90, 0));
go.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
}
}

2
Assets/Scripts/Modules/RenderModule.cs

@ -58,7 +58,7 @@ public partial class RenderModule
aliasModels.Add(aliasModel);
var go = new GameObject(modelName);
go.transform.SetPositionAndRotation(new Vector3(xPos, 0, zPos), Quaternion.Euler(-90, 90, 0));
go.transform.SetPositionAndRotation(new Vector3(xPos, 0, zPos), Quaternion.identity);
aliasModel.Animate(0, out Mesh mesh, out float blendWeight);

Loading…
Cancel
Save