public static void CopyDataPerVertex(this Mesh mesh, RenderChunk chunk) { if (mesh == null) { Debug.Log("CopyDataPerVertex method need some data, mesh == null.."); return; } int b1Cnt = 0, b2Cnt = 0, b3Cnt = 0, b4Cnt = 0; { chunk.vertexCount = mesh.vertexCount; } { DataPerVertex[] dataPerVertex = new DataPerVertex[mesh.vertexCount]; SkinPerVertex[] skinPerVertex = new SkinPerVertex[mesh.vertexCount]; Vector3[] vertices = mesh.vertices; Vector3[] normals = mesh.normals; Vector4[] tangents = mesh.tangents; Vector2[] uv = mesh.uv; BoneWeight[] sourceBoneWeights = mesh.boneWeights; // This code very long time for (int i = 0; i < dataPerVertex.Length; i++) { dataPerVertex[i] = new DataPerVertex() { position = vertices[i], normal = normals[i], tangent = tangents[i], uv = uv[i], }; skinPerVertex[i] = new SkinPerVertex() { weight = new Vector4(sourceBoneWeights[i].weight0, sourceBoneWeights[i].weight1, sourceBoneWeights[i].weight2, sourceBoneWeights[i].weight3), index = new Integer4(sourceBoneWeights[i].boneIndex0, sourceBoneWeights[i].boneIndex1, sourceBoneWeights[i].boneIndex2, sourceBoneWeights[i].boneIndex3), }; if (skinPerVertex[i].weight.y == 0) { b1Cnt++; } else if (skinPerVertex[i].weight.z == 0) { b2Cnt++; } else if (skinPerVertex[i].weight.w == 0) { b3Cnt++; } else { b4Cnt++; } } chunk.boneVertexCount = new Integer4(b1Cnt, b2Cnt, b3Cnt, b4Cnt); chunk.dataPerVertex = dataPerVertex; chunk.skinPerVertex = skinPerVertex; } }
public static unsafe float GetWeightDistance(this SkinPerVertex info1, SkinPerVertex info2) { bool *isValidArray = stackalloc bool[8]; float wholeDistance = 0; bool isCaculationValid = false; for (int i = 0; i < 4; i++) { isValidArray[i] = info1.weight[i] != 0f; isValidArray[i + 4] = info2.weight[i] != 0f; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (isValidArray[i] && isValidArray[j + 4]) { if (info2.index[j] == info1.index[i]) { isCaculationValid = true; } } } } if (!isCaculationValid) { return(float.MaxValue); } for (int i = 0; i < 4; i++) { if (isValidArray[i]) { float weight = info1.weight[i]; for (int j = i; j < 4; j++) { if (isValidArray[j + 4]) { if (info2.index[j] == info1.index[i]) { weight -= info2.weight[j]; isValidArray[j + 4] = false; break; } } } wholeDistance += weight * weight; isValidArray[i] = false; } if (isValidArray[i + 4]) { float weight = info2.weight[i]; for (int j = i; j < 4; j++) { if (isValidArray[j]) { if (info1.index[j] == info2.index[i]) { weight -= info1.weight[j]; isValidArray[j] = false; break; } } } wholeDistance += weight * weight; isValidArray[i + 4] = false; } } return(Mathf.Sqrt(wholeDistance)); }