private void CreateSkeletonMesh() { NullMeshFile meshFile = new NullMeshFile(NullWorkingFlag.WF_SKELETON_MESHPIECE); bool hadNormals = mVertexNormals.Length > 0; bool hadTangents = mVertexTangents.Length > 0; bool hadColors = mVertexColors.Length > 0; bool hadUv = mVertexuvs.Length > 0; NullMeshObject meshObject = meshFile.AppendSkinObject(NullPrimitiveType.MOT_INDEXED_PRIMITIVES, mVertexTriangles.Length, mVertexPoses.Length, true, true, true); NullUVGroup uvGroup = hadUv ? meshObject.GetOrCreateUVGroup(UVType.UVT_DEFAULT, mVertexPoses.Length) : null; int vertexCount = mVertexPoses.Length; for (int i = 0; i < vertexCount; ++i) { meshObject.SetVertex(i, mVertexPoses[i]); if (hadColors) { meshObject.SetVertexColor(i, Convert(mVertexColors[i])); } if (hadTangents) { meshObject.SetTangent(i, mVertexTangents[i]); } if (hadNormals) { meshObject.SetNormal(i, mVertexNormals[i]); } if (hadUv) { uvGroup.SetUV(i, mVertexuvs[i]); } } for (int i = 0; i < mVertexTriangles.Length; i += 3) { int i1 = mVertexTriangles[i]; int i2 = mVertexTriangles[i + 1]; int i3 = mVertexTriangles[i + 2]; meshObject.SetTriangleIndex(i, new Vector3Int(i1, i2, i3)); } // 骨骼数 NullNodeTree rootNode = meshFile.GetNodeTree(); ConvertNodeTree(mRootBone, rootNode, mBindposes, mBones); // 权重绑定 NullSkeletonBinding skeletonBinding = meshFile.GetSkeletonBinding(); int pieceCount = 1; // 对应子模型,现在只限制一个子模型 skeletonBinding.SetSkeletonName(mMesh.name); skeletonBinding.SetSkeletonBindingCount(pieceCount); for (int i = 0; i < pieceCount; ++i) { NullSkeletonPiece skeletonPiece = skeletonBinding[i]; skeletonPiece.SetPieceHandle(i); ConvertSkeletonPiece(skeletonPiece, mBoneweights); } string fileName = OutPutDir + "/" + Target.name + ".hxs"; using (NullMemoryStream stream = NullMemoryStream.WriteToFile(fileName)) { meshFile.SaveToStream(stream); } }
protected bool BuildTangentForMeshObjects(NullMeshObjects meshObjects, float thresHold, bool forceSmooth = false) { if (meshObjects == null || meshObjects.GetMeshObjectCount() == 0) { return(false); } int triangleCount = meshObjects.GetTriangleCount(); if (triangleCount > 0) { //preparing data List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <Vector3> tangents = Make <Vector3>(triangleCount * 3); List <Vector3> binormals = Make <Vector3>(triangleCount * 3); List <byte> smoothGroups = Make <byte>(triangleCount); int realTriangleCount = 0; for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++) { NullMeshObject meshObject = meshObjects[i]; if (meshObject.GetUVGroups().Count == 0 || meshObject.GetNormalData().Count == 0) { continue; } int count = meshObject.GetTriangleCount(); vertices.AddRange(meshObject.GetVertexData()); byte smoothGroup = meshObject.GetSmoothGroup(); if (forceSmooth && smoothGroup == 0) { smoothGroup = 1; } Set(smoothGroups, smoothGroup); NullUVGroup uvGroup = meshObject.GetUVGroup(UVType.UVT_NORMAL_MAP); if (uvGroup == null) { uvGroup = meshObject.GetUVGroup(UVType.UVT_DEFAULT); } uvs.AddRange(uvGroup.GetUVData()); realTriangleCount += count; } //do face-smoothing ComputeTengents(thresHold, vertices, realTriangleCount, smoothGroups, uvs, tangents, binormals); //update mesh objects normals for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++) { NullMeshObject meshObject = meshObjects[i]; if (meshObject.GetUVGroups().Count == 0 || meshObject.GetNormalData().Count == 0) { continue; } meshObject.BuildTangentArray(); int count = meshObject.GetTriangleCount(); for (int j = 0; j < count; j++) { //set tangent meshObject.SetTangent(j * 3 + 0, tangents[j * 3]); meshObject.SetTangent(j * 3 + 1, tangents[j * 3 + 1]); meshObject.SetTangent(j * 3 + 2, tangents[j * 3 + 2]); //set binormal meshObject.SetBinormal(j * 3 + 0, binormals[j * 3]); meshObject.SetBinormal(j * 3 + 1, binormals[j * 3 + 1]); meshObject.SetBinormal(j * 3 + 2, binormals[j * 3 + 2]); } } //delete temp buffer vertices.Clear(); tangents.Clear(); binormals.Clear(); uvs.Clear(); smoothGroups.Clear(); } return(true); }