//public SourceTexture texture; public FaceMesh() { relativePosition = Vector3.zero; relativeRotation = Vector3.zero; meshData = new MeshHelpers.MeshData(); textureFlag = texflags.SURF_NODRAW; }
private void ReadFaceMeshes(MDLParser mdl, VVDParser vvd, VTXParser vtx, BSPParser bspParser, VPKParser vpkParser) { int textureIndex = 0; if (mdl.bodyParts.Length == vtx.bodyParts.Length) { for (int bodyPartIndex = 0; bodyPartIndex < mdl.bodyParts.Length; bodyPartIndex++) { for (int modelIndex = 0; modelIndex < mdl.bodyParts[bodyPartIndex].models.Length; modelIndex++) { //int currentPosition = 0; for (int meshIndex = 0; meshIndex < mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes.Length; meshIndex++) { int rootLodIndex = mdl.header1.rootLod; //int rootLodIndex = 0; //int rootLodCount = 1; //if (mdl.header1.numAllowedRootLods == 0) // rootLodCount = vtx.header.numLODs; FaceMesh currentFace = new FaceMesh(); int verticesStartIndex = mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexIndexStart; int vertexCount = 0; //int vertexCount = mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexCount; //int vertexCount = mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexData.lodVertexCount[rootLodIndex]; //int vertexCount = 0; //for (int i = 0; i <= rootLodIndex; i++) // vertexCount += mdl.bodyParts[bodyPartIndex].models[modelIndex].theMeshes[meshIndex].vertexData.lodVertexCount[i]; int trianglesCount = 0; for (int stripGroupIndex = 0; stripGroupIndex < vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].stripGroupCount; stripGroupIndex++) { var currentStripGroup = vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].theVtxStripGroups[stripGroupIndex]; for (int stripIndex = 0; stripIndex < currentStripGroup.theVtxStrips.Length; stripIndex++) { var currentStrip = currentStripGroup.theVtxStrips[stripIndex]; if (((StripHeaderFlags_t)currentStrip.flags & StripHeaderFlags_t.STRIP_IS_TRILIST) > 0) { trianglesCount += currentStrip.indexCount; } } } //List<int> triangles = new List<int>(); int[] triangles = new int[trianglesCount]; int trianglesIndex = 0; //for (int countUpLodIndex = 0; countUpLodIndex <= rootLodIndex; countUpLodIndex++) //{ for (int stripGroupIndex = 0; stripGroupIndex < vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].stripGroupCount; stripGroupIndex++) { //var currentStripGroup = vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].theVtxStripGroups[0]; var currentStripGroup = vtx.bodyParts[bodyPartIndex].theVtxModels[modelIndex].theVtxModelLods[rootLodIndex].theVtxMeshes[meshIndex].theVtxStripGroups[stripGroupIndex]; //int trianglesCount = currentStripGroup.theVtxIndices.Length; //int[] triangles = new int[trianglesCount]; for (int stripIndex = 0; stripIndex < currentStripGroup.theVtxStrips.Length; stripIndex++) { var currentStrip = currentStripGroup.theVtxStrips[stripIndex]; if (((StripHeaderFlags_t)currentStrip.flags & StripHeaderFlags_t.STRIP_IS_TRILIST) > 0) { for (int indexIndex = 0; indexIndex < currentStrip.indexCount; indexIndex += 3) { int vertexIndexA = verticesStartIndex + currentStripGroup.theVtxVertices[currentStripGroup.theVtxIndices[indexIndex + currentStrip.indexMeshIndex]].originalMeshVertexIndex; int vertexIndexB = verticesStartIndex + currentStripGroup.theVtxVertices[currentStripGroup.theVtxIndices[indexIndex + currentStrip.indexMeshIndex + 2]].originalMeshVertexIndex; int vertexIndexC = verticesStartIndex + currentStripGroup.theVtxVertices[currentStripGroup.theVtxIndices[indexIndex + currentStrip.indexMeshIndex + 1]].originalMeshVertexIndex; vertexCount = Mathf.Max(vertexCount, vertexIndexA, vertexIndexB, vertexIndexC); //if (vertexIndexA < vertices.Length && vertexIndexB < vertices.Length && vertexIndexC < vertices.Length) //{ triangles[trianglesIndex++] = vertexIndexA; triangles[trianglesIndex++] = vertexIndexB; triangles[trianglesIndex++] = vertexIndexC; //triangles.Add(vertexIndexA); //triangles.Add(vertexIndexB); //triangles.Add(vertexIndexC); //} } } } } //} vertexCount += 1; //vertexCount = vvd.vertices.Length; //vertexCount = vvd.header.numLODVertices[rootLodIndex]; Vector3[] vertices = new Vector3[vertexCount]; Vector3[] normals = new Vector3[vertexCount]; Vector2[] uv = new Vector2[vertexCount]; for (int verticesIndex = 0; verticesIndex < vertices.Length; verticesIndex++) { vertices[verticesIndex] = vvd.vertices[verticesIndex].m_vecPosition; normals[verticesIndex] = vvd.vertices[verticesIndex].m_vecNormal; uv[verticesIndex] = vvd.vertices[verticesIndex].m_vecTexCoord; } Debug.Assert(triangles.Length % 3 == 0, "SourceModel: Triangles not a multiple of three for " + modelPath); if (mdl.header1.includemodel_count > 0) { Debug.LogWarning("SourceModel: Include model count greater than zero (" + mdl.header1.includemodel_count + ", " + mdl.header1.includemodel_index + ") for " + modelPath); } if (vvd.header.numFixups > 0) { Debug.LogWarning("SourceModel: " + vvd.header.numFixups + " fixups found for " + modelPath); } MeshHelpers.MeshData meshData; //var outcome = MeshHelpers.GenerateConvexHull(vertices, out meshData, 0.2); //if (outcome != MIConvexHull.ConvexHullCreationResultOutcome.Success) // Debug.LogError("SourceModel: Convex hull error " + outcome + " for " + modelPath); if (decimationPercent > 0) { meshData = MeshHelpers.DecimateByTriangleCount(vertices, triangles, normals, 1 - decimationPercent); meshData.uv = new Vector2[meshData.vertices.Length]; System.Array.Copy(uv, meshData.uv, meshData.vertices.Length); } else { meshData = new MeshHelpers.MeshData(); meshData.vertices = vertices; meshData.triangles = triangles; meshData.normals = normals; meshData.uv = uv; } currentFace.meshData = meshData; string textureName = ""; string texturePath = mdl.texturePaths[0].Replace("\\", "/").ToLower(); if (textureIndex < mdl.textures.Length) { textureName = mdl.textures[textureIndex].name.Replace("\\", "/").ToLower(); } //textureName = mdl.textures[textureIndex].name.Replace("\\", "/").ToLower(); if (textureName.IndexOf(texturePath) > -1) { texturePath = ""; } string textureLocation = texturePath + textureName; //if (modelTextures != null && textureIndex < modelTextures.Length) //Should not have this line // textureLocation = modelTextures[textureIndex]?.location; currentFace.faceName = textureLocation; currentFace.material = VMTData.GrabVMT(bspParser, vpkParser, textureLocation); //if (!excludeTextures) // currentFace.texture = SourceTexture.GrabTexture(bspParser, vpkParser, textureLocation); faces.Add(currentFace); textureIndex++; } } } } else { Debug.LogError("SourceModel: MDL and VTX body part count doesn't match (" + modelPath + ")"); } }
public MeshHelpers.MeshData MakeFace(BSPParser bspParser, dface_t face) { #region Get all vertices of face List <Vector3> surfaceVertices = new List <Vector3>(); List <Vector3> originalVertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); for (int i = 0; i < face.numedges; i++) { ushort[] currentEdge = bspParser.edges[Mathf.Abs(bspParser.surfedges[face.firstedge + i])].v; Vector3 point1 = bspParser.vertices[currentEdge[0]], point2 = bspParser.vertices[currentEdge[1]]; Vector3 planeNormal = bspParser.planes[face.planenum].normal; point1 = new Vector3(point1.x, point1.y, point1.z); point2 = new Vector3(point2.x, point2.y, point2.z); if (bspParser.surfedges[face.firstedge + i] >= 0) { if (surfaceVertices.IndexOf(point1) < 0) { surfaceVertices.Add(point1); normals.Add(planeNormal); } originalVertices.Add(point1); if (surfaceVertices.IndexOf(point2) < 0) { surfaceVertices.Add(point2); normals.Add(planeNormal); } originalVertices.Add(point2); } else { if (surfaceVertices.IndexOf(point2) < 0) { surfaceVertices.Add(point2); normals.Add(planeNormal); } originalVertices.Add(point2); if (surfaceVertices.IndexOf(point1) < 0) { surfaceVertices.Add(point1); normals.Add(planeNormal); } originalVertices.Add(point1); } } #endregion #region Apply Displacement if (face.dispinfo > -1) { ddispinfo_t disp = bspParser.dispInfo[face.dispinfo]; int power = Mathf.RoundToInt(Mathf.Pow(2, disp.power)); List <Vector3> dispVertices = new List <Vector3>(); Vector3 startingPosition = surfaceVertices[0]; Vector3 topCorner = surfaceVertices[1], topRightCorner = surfaceVertices[2], rightCorner = surfaceVertices[3]; #region Setting Orientation Vector3 dispStartingVertex = disp.startPosition; if (Vector3.Distance(dispStartingVertex, topCorner) < 0.01f) { Vector3 tempCorner = startingPosition; startingPosition = topCorner; topCorner = topRightCorner; topRightCorner = rightCorner; rightCorner = tempCorner; } else if (Vector3.Distance(dispStartingVertex, rightCorner) < 0.01f) { Vector3 tempCorner = startingPosition; startingPosition = rightCorner; rightCorner = topRightCorner; topRightCorner = topCorner; topCorner = tempCorner; } else if (Vector3.Distance(dispStartingVertex, topRightCorner) < 0.01f) { Vector3 tempCorner = startingPosition; startingPosition = topRightCorner; topRightCorner = tempCorner; tempCorner = rightCorner; rightCorner = topCorner; topCorner = tempCorner; } #endregion int orderNum = 0; #region Method 13 (The one and only two) Vector3 leftSide = (topCorner - startingPosition), rightSide = (topRightCorner - rightCorner); float leftSideLineSegmentationDistance = leftSide.magnitude / power, rightSideLineSegmentationDistance = rightSide.magnitude / power; for (int line = 0; line < (power + 1); line++) { for (int point = 0; point < (power + 1); point++) { Vector3 leftPoint = (leftSide.normalized * line * leftSideLineSegmentationDistance) + startingPosition; Vector3 rightPoint = (rightSide.normalized * line * rightSideLineSegmentationDistance) + rightCorner; Vector3 currentLine = rightPoint - leftPoint; Vector3 pointDirection = currentLine.normalized; float pointSideSegmentationDistance = currentLine.magnitude / power; Vector3 pointA = leftPoint + (pointDirection * pointSideSegmentationDistance * point); Vector3 dispDirectionA = bspParser.dispVerts[disp.DispVertStart + orderNum].vec; dispVertices.Add(pointA + (dispDirectionA * bspParser.dispVerts[disp.DispVertStart + orderNum].dist)); orderNum++; } } #endregion surfaceVertices = dispVertices; } #endregion #region Triangulate List <int> triangleIndices = new List <int>(); if (face.dispinfo > -1) { ddispinfo_t disp = bspParser.dispInfo[face.dispinfo]; int power = Mathf.RoundToInt(Mathf.Pow(2, disp.power)); #region Method 12 Triangulation for (int row = 0; row < power; row++) { for (int col = 0; col < power; col++) { int currentLine = row * (power + 1); int nextLineStart = (row + 1) * (power + 1); triangleIndices.Add(currentLine + col); triangleIndices.Add(currentLine + col + 1); triangleIndices.Add(nextLineStart + col); triangleIndices.Add(currentLine + col + 1); triangleIndices.Add(nextLineStart + col + 1); triangleIndices.Add(nextLineStart + col); } } #endregion } else { for (int i = 0; i < (originalVertices.Count / 2) - 0; i++) { int firstOrigIndex = i * 2, secondOrigIndex = (i * 2) + 1, thirdOrigIndex = 0; int firstIndex = surfaceVertices.IndexOf(originalVertices[firstOrigIndex]); int secondIndex = surfaceVertices.IndexOf(originalVertices[secondOrigIndex]); int thirdIndex = surfaceVertices.IndexOf(originalVertices[thirdOrigIndex]); triangleIndices.Add(thirdIndex); triangleIndices.Add(secondIndex); triangleIndices.Add(firstIndex); } } #endregion #region Map UV Points Vector3 s = Vector3.zero, t = Vector3.zero; float xOffset = 0, yOffset = 0; try { s = new Vector3(bspParser.texInfo[face.texinfo].textureVecs[0][0], bspParser.texInfo[face.texinfo].textureVecs[0][1], bspParser.texInfo[face.texinfo].textureVecs[0][2]); t = new Vector3(bspParser.texInfo[face.texinfo].textureVecs[1][0], bspParser.texInfo[face.texinfo].textureVecs[1][1], bspParser.texInfo[face.texinfo].textureVecs[1][2]); xOffset = bspParser.texInfo[face.texinfo].textureVecs[0][3]; yOffset = bspParser.texInfo[face.texinfo].textureVecs[1][3]; } catch (Exception e) { Debug.LogError(e); } Vector2[] uvPoints = new Vector2[surfaceVertices.Count]; int textureWidth = 0, textureHeight = 0; try { textureWidth = bspParser.texData[bspParser.texInfo[face.texinfo].texdata].width; textureHeight = bspParser.texData[bspParser.texInfo[face.texinfo].texdata].height; } catch (Exception e) { Debug.LogError(e); } for (int i = 0; i < uvPoints.Length; i++) { uvPoints[i] = new Vector2((Vector3.Dot(surfaceVertices[i], s) + xOffset) / textureWidth, (textureHeight - (Vector3.Dot(surfaceVertices[i], t) + yOffset)) / textureHeight); } #endregion #region Organize Mesh Data MeshHelpers.MeshData meshData = new MeshHelpers.MeshData(); meshData.vertices = surfaceVertices.ToArray(); meshData.triangles = triangleIndices.ToArray(); meshData.normals = normals.ToArray(); meshData.uv = uvPoints; #endregion #region Clear References surfaceVertices.Clear(); surfaceVertices = null; originalVertices.Clear(); originalVertices = null; normals.Clear(); normals = null; triangleIndices.Clear(); triangleIndices = null; uvPoints = null; #endregion return(meshData); }