public static unsafe float GetSimliarity(this RenderChunk chunk, int vtxIdx1, int vtxIdx2, float kernel) { int sameIndexCount = 0; float *weightArray = stackalloc float[8]; // Find same bone index, and store weights. { Integer4 index1 = chunk.skinPerVertex[vtxIdx1].index, index2 = chunk.skinPerVertex[vtxIdx2].index; Vector4 weight1 = chunk.skinPerVertex[vtxIdx1].weight, weight2 = chunk.skinPerVertex[vtxIdx2].weight; for (int i = 0; i < 4; i++) { if (weight1[i] > 0) { for (int j = 0; j < 4; j++) { if (weight2[j] > 0) { if (index1[i] == index2[j]) { weightArray[sameIndexCount] = weight1[i]; weightArray[sameIndexCount + 1] = weight2[j]; sameIndexCount++; break; } } } } } } // Just calculate similarity with store weights. if (sameIndexCount < 2) { return(0f); } else { float similarity = 0f, diff; for (int i = 0; i < sameIndexCount; i++) { for (int j = i + 1; j < sameIndexCount; j++) { diff = weightArray[i] * weightArray[j + 1] - weightArray[i + 1] * weightArray[j]; similarity += weightArray[i] * weightArray[i + 1] * weightArray[j] * weightArray[j + 1] * Mathf.Exp(-(diff * diff) / (kernel * kernel)); } } return(similarity); } }
public static unsafe float GetSimliarity(this RenderChunk chunk, int vtxIdx1, int vtxIdx21, int vtxIdx22, int vtxIdx23, float kernel) { int threeWeightCount = 0, index; int * threeWeightIndexArray = stackalloc int[12]; float *threeWeightSumedWeightArray = stackalloc float[12]; Integer4 index1 = chunk.skinPerVertex[vtxIdx1].index, index21 = chunk.skinPerVertex[vtxIdx21].index, index22 = chunk.skinPerVertex[vtxIdx22].index, index23 = chunk.skinPerVertex[vtxIdx23].index; Vector4 weight1 = chunk.skinPerVertex[vtxIdx1].weight, weight21 = chunk.skinPerVertex[vtxIdx21].weight, weight22 = chunk.skinPerVertex[vtxIdx22].weight, weight23 = chunk.skinPerVertex[vtxIdx23].weight; // Merge 3 weight data { for (int i = 0; i < 4; i++) { index = -1; for (int j = 0; j < threeWeightCount; j++) { if (threeWeightIndexArray[j] == index21[i]) { index = j; threeWeightSumedWeightArray[index] += weight21[i] / 3; break; } } if (index < 0) { threeWeightIndexArray[threeWeightCount] = index21[i]; threeWeightSumedWeightArray[threeWeightCount] = weight21[i] / 3; threeWeightCount++; } index = -1; for (int j = 0; j < threeWeightCount; j++) { if (threeWeightIndexArray[j] == index22[i]) { index = j; threeWeightSumedWeightArray[index] += weight22[i] / 3; break; } } if (index < 0) { threeWeightIndexArray[threeWeightCount] = index22[i]; threeWeightSumedWeightArray[threeWeightCount] = weight22[i] / 3; threeWeightCount++; } index = -1; for (int j = 0; j < threeWeightCount; j++) { if (threeWeightIndexArray[j] == index23[i]) { index = j; threeWeightSumedWeightArray[index] += weight23[i] / 3; break; } } if (index < 0) { threeWeightIndexArray[threeWeightCount] = index23[i]; threeWeightSumedWeightArray[threeWeightCount] = weight23[i] / 3; threeWeightCount++; } } } int sameIndexCount = 0; float *weightArray = stackalloc float[8]; // Find same bone index, and store weights. { for (int i = 0; i < 4; i++) { if (weight1[i] > 0) { for (int j = 0; j < threeWeightCount; j++) { if (threeWeightSumedWeightArray[j] > 0) { if (index1[i] == threeWeightIndexArray[j]) { weightArray[sameIndexCount] = weight1[i]; weightArray[sameIndexCount + 1] = threeWeightSumedWeightArray[j]; sameIndexCount++; break; } } } } } } // Just calculate similarity with store weights. if (sameIndexCount < 2) { return(0f); } else { float similarity = 0f, diff; for (int i = 0; i < sameIndexCount; i++) { for (int j = i + 1; j < sameIndexCount; j++) { diff = weightArray[i] * weightArray[j + 1] - weightArray[i + 1] * weightArray[j]; similarity += weightArray[i] * weightArray[i + 1] * weightArray[j] * weightArray[j + 1] * Mathf.Exp(-(diff * diff) / (kernel * kernel)); } } return(similarity); } }