private void determinePresence(Transform root, Vector4 plane, out Dictionary <string, Transform> transformByName, out Dictionary <string, bool> frontPresence, out Dictionary <string, bool> backPresence) { List <Transform> allChildren = new List <Transform>(); concatenateHierarchy(root, allChildren); Vector3[] positions = new Vector3[allChildren.Count]; for (int i = 0; i < positions.Length; i++) { positions[i] = allChildren[i].position; } Matrix4x4 worldToLocal = root.worldToLocalMatrix; for (int i = 0; i < positions.Length; i++) { positions[i] = worldToLocal.MultiplyPoint3x4(positions[i]); } PlaneTriResult[] ptr = new PlaneTriResult[positions.Length]; for (int i = 0; i < positions.Length; i++) { ptr[i] = MuffinSliceCommon.getSidePlane(ref positions[i], ref plane); } transformByName = new Dictionary <string, Transform>(); frontPresence = new Dictionary <string, bool>(); backPresence = new Dictionary <string, bool>(); bool duplicateNameWarning = false; for (int i = 0; i < ptr.Length; i++) { Transform t = allChildren[i]; string key = t.name; if (transformByName.ContainsKey(key)) { duplicateNameWarning = true; } transformByName[key] = t; frontPresence[key] = ptr[i] == PlaneTriResult.PTR_FRONT; backPresence[key] = ptr[i] == PlaneTriResult.PTR_BACK; } if (duplicateNameWarning) { Debug.LogWarning("Sliceable has children with non-unique names. Behaviour is undefined!"); } }
public GameObject[] severByJoint(GameObject go, string jointName, float rootTipProgression, Vector3?planeNormal) { rootTipProgression = Mathf.Clamp01(rootTipProgression); //These here are in local space because they're only used to copy to the resultant meshes; they're not used //to transform the vertices. We expect a world-space slice input. Hackable hackable = null; { Hackable[] hackables = go.GetComponentsInChildren <Hackable>(); if (hackables.Length > 0) { if (hackables.Length > 1) { Debug.LogWarning("Limb Hacker found multiple slice configurations on object '" + go.name + "' in scene '" + Application.loadedLevelName + "'! Behavior is undefined."); } hackable = hackables[0]; } } //We need information about which BONES are getting severed. var allBones = LimbHacker.FindBonesInTree(go); var childTransformByName = new Dictionary <string, Transform>(); var parentKeyByKey = new Dictionary <string, string>(); foreach (Transform t in GetConcatenatedHierarchy(go.transform)) { childTransformByName[t.name] = t; Transform parent = t.parent; if (t == go.transform) { parent = null; } parentKeyByKey[t.name] = parent == null ? null : parent.name; } var severedByChildName = new Dictionary <string, bool>(); { foreach (string childName in childTransformByName.Keys) { severedByChildName[childName] = childName == jointName; } bool changesMade; do { changesMade = false; foreach (string childKey in childTransformByName.Keys) { bool severed = severedByChildName[childKey]; if (severed) { continue; } string parentKey = parentKeyByKey[childKey]; bool parentSevered; if (severedByChildName.TryGetValue(parentKey, out parentSevered) == false) { continue; } if (parentSevered) { severedByChildName[childKey] = true; changesMade = true; } } }while(changesMade); } GameObject frontObject, backObject; { var bonePresenceFront = new Dictionary <string, bool>(); var bonePresenceBack = new Dictionary <string, bool>(); foreach (KeyValuePair <string, bool> kvp in severedByChildName) { bonePresenceFront[kvp.Key] = kvp.Value; bonePresenceBack[kvp.Key] = !kvp.Value; } createResultObjects(go, hackable, childTransformByName, bonePresenceFront, bonePresenceBack, out frontObject, out backObject); } var skinnedMeshRenderers = go.GetComponentsInChildren <SkinnedMeshRenderer>(true); foreach (var smr in skinnedMeshRenderers) { var m = smr.sharedMesh; LoadSkinnedMeshRendererIntoCache(smr, true); var severedByBoneIndex = new Dictionary <int, bool>(); var mandatoryByBoneIndex = new bool[smr.bones.Length]; string severedJointKey = jointName; Dictionary <string, int> boneIndexByName = new Dictionary <string, int>(); List <string> orderedBoneNames = new List <string>(); foreach (Transform bone in smr.bones) { boneIndexByName[bone.name] = orderedBoneNames.Count; orderedBoneNames.Add(bone.name); } for (int boneIndex = 0; boneIndex < orderedBoneNames.Count; boneIndex++) { string boneName = orderedBoneNames[boneIndex]; severedByBoneIndex[boneIndex] = severedByChildName[boneName]; } Vector4 plane = Vector4.zero; bool willSliceThisMesh = boneIndexByName.ContainsKey(severedJointKey); if (willSliceThisMesh) { //We need to create a slice plane in local space. We're going to do that by using the bind poses //from the SEVERED limb, its PARENT and its CHILDREN to create a position and normal. Matrix4x4[] orderedBindPoses = smr.sharedMesh.bindposes; int severedJointIndex = boneIndexByName[severedJointKey]; Matrix4x4 severedJointMatrix = orderedBindPoses[severedJointIndex].inverse; Matrix4x4 severedJointParentMatrix = Matrix4x4.identity; if (parentKeyByKey.ContainsKey(severedJointKey)) { string severedJointParentKey = parentKeyByKey[severedJointKey]; if (boneIndexByName.ContainsKey(severedJointParentKey)) { int severedJointParentIndex = boneIndexByName[severedJointParentKey]; severedJointParentMatrix = orderedBindPoses[severedJointParentIndex].inverse; } } VectorAccumulator meanChildPosition = new VectorAccumulator(); for (int i = 0; i < boneIndexByName.Count; i++) { mandatoryByBoneIndex[i] = false; } if (parentKeyByKey.ContainsKey(severedJointKey)) { string parentKey = parentKeyByKey[severedJointKey]; if (boneIndexByName.ContainsKey(parentKey)) { mandatoryByBoneIndex[boneIndexByName[parentKey]] = true; } } if (rootTipProgression > 0f) { mandatoryByBoneIndex[boneIndexByName[jointName]] = true; List <string> childKeys = new List <string>(); foreach (KeyValuePair <string, string> kvp in parentKeyByKey) { if (kvp.Value == severedJointKey) { childKeys.Add(kvp.Key); } } List <int> childIndices = new List <int>(); foreach (string key in childKeys) { int childIndex; if (boneIndexByName.TryGetValue(key, out childIndex)) { childIndices.Add(childIndex); } } foreach (int index in childIndices) { Matrix4x4 childMatrix = orderedBindPoses[index].inverse; Vector3 childPosition = childMatrix.MultiplyPoint3x4(Vector3.zero); meanChildPosition.addFigure(childPosition); } } Vector3 position0 = severedJointParentMatrix.MultiplyPoint3x4(Vector3.zero); Vector3 position1 = severedJointMatrix.MultiplyPoint3x4(Vector3.zero); Vector3 position2 = meanChildPosition.mean; Vector3 deltaParent = position0 - position1; Vector3 deltaChildren = position1 - position2; Vector3 position = Vector3.Lerp(position1, position2, rootTipProgression); Vector3 normalFromParentToChild = -Vector3.Lerp(deltaParent, deltaChildren, rootTipProgression).normalized; if (planeNormal.HasValue) { Matrix4x4 fromWorldToLocalSpaceOfBone = smr.bones[severedJointIndex].worldToLocalMatrix; Vector3 v = planeNormal.Value; v = fromWorldToLocalSpaceOfBone.MultiplyVector(v); v = severedJointMatrix.MultiplyVector(v); v.Normalize(); if (Vector3.Dot(v, normalFromParentToChild) < 0f) { v = -v; } v = MuffinSliceCommon.clampNormalToBicone(v, normalFromParentToChild, 30f); planeNormal = v; } else { planeNormal = normalFromParentToChild; } plane = (Vector4)planeNormal.Value; plane.w = -(plane.x * position.x + plane.y * position.y + plane.z * position.z); } //We're going to create two new tentative meshes which contain ALL original vertices in order, //plus room for new vertices. Not all of these copied vertices will be addressed, but copying them //over eliminates the need to remove doubles and do an On^2 search. int submeshCount = c.indices.Length; TurboList <int>[] _frontIndices = new TurboList <int> [submeshCount]; TurboList <int>[] _backIndices = new TurboList <int> [submeshCount]; PlaneTriResult[] sidePlanes = new PlaneTriResult[c.vertices.Count]; { BoneWeight[] weights = c.weights.array; Vector3[] vertices = c.vertices.array; int count = c.vertices.Count; bool[] whollySeveredByVertexIndex = new bool[count]; bool[] severableByVertexIndex = new bool[count]; bool[] mandatoryByVertexIndex = new bool[count]; const float minimumWeightForRelevance = 0.1f; for (int i = 0; i < severableByVertexIndex.Length; i++) { BoneWeight weight = weights[i]; bool whollySevered = true; bool severable = false; bool mandatory = false; int[] indices = { weight.boneIndex0, weight.boneIndex1, weight.boneIndex2, weight.boneIndex3 }; float[] scalarWeights = { weight.weight0, weight.weight1, weight.weight2, weight.weight3 }; for (int j = 0; j < 4; j++) { if (scalarWeights[j] > minimumWeightForRelevance) { int index = indices[j]; bool _severable = severedByBoneIndex[index]; bool _mandatory = mandatoryByBoneIndex[index]; whollySevered &= _severable; severable |= _severable; mandatory |= _mandatory; } } whollySeveredByVertexIndex[i] = whollySevered; severableByVertexIndex[i] = severable; mandatoryByVertexIndex[i] = mandatory; } for (int i = 0; i < sidePlanes.Length; i++) { if (willSliceThisMesh && mandatoryByVertexIndex[i]) { sidePlanes[i] = MuffinSliceCommon.getSidePlane(ref vertices[i], ref plane); } else if (whollySeveredByVertexIndex[i]) { sidePlanes[i] = PlaneTriResult.PTR_FRONT; } else if (willSliceThisMesh && severableByVertexIndex[i]) { sidePlanes[i] = MuffinSliceCommon.getSidePlane(ref vertices[i], ref plane); } else { sidePlanes[i] = PlaneTriResult.PTR_BACK; } } } TurboList <int> frontInfill = null; TurboList <int> backInfill = null; for (int j = 0; j < submeshCount; j++) { int initialCapacityIndices = Mathf.RoundToInt((float)c.indices[j].Length * factorOfSafetyIndices); _frontIndices[j] = new TurboList <int>(initialCapacityIndices); _backIndices[j] = new TurboList <int>(initialCapacityIndices); if (hackable.infillMaterial != null && c.mats[j] == hackable.infillMaterial) { frontInfill = _frontIndices[j]; backInfill = _backIndices[j]; } } if (hackable.infillMaterial != null && frontInfill == null) { frontInfill = new TurboList <int>(1024); backInfill = new TurboList <int>(1024); } for (int j = 0; j < submeshCount; j++) { int initialCapacityIndices = Mathf.RoundToInt((float)c.indices[j].Length * factorOfSafetyIndices); int[] _indices = c.indices[j]; TurboList <int> frontIndices = _frontIndices[j]; TurboList <int> backIndices = _backIndices[j]; TurboList <int> splitPending = new TurboList <int>(initialCapacityIndices); int[] indices = new int[3]; for (int i = 0; i < _indices.Length;) { indices[0] = _indices[i++]; indices[1] = _indices[i++]; indices[2] = _indices[i++]; // compute the side of the plane each vertex is on PlaneTriResult r1 = sidePlanes[indices[0]]; PlaneTriResult r2 = sidePlanes[indices[1]]; PlaneTriResult r3 = sidePlanes[indices[2]]; if (r1 == r2 && r1 == r3) // if all three vertices are on the same side of the plane. { if (r1 == PlaneTriResult.PTR_FRONT) // if all three are in front of the plane, then copy to the 'front' output triangle. { frontIndices.AddArray(indices); } else { backIndices.AddArray(indices); } } else if (willSliceThisMesh) { splitPending.AddArray(indices); } } if (willSliceThisMesh) { splitTrianglesLH(plane, c.vertices.array, sidePlanes, splitPending.ToArray(), c, frontIndices, backIndices, hackable.infillMode, frontInfill, backInfill); } } if (hackable.infillMaterial != null) { bool alreadyPresent = System.Array.IndexOf <Material>(c.mats, hackable.infillMaterial) >= 0; if (!alreadyPresent) { int oldLength = c.mats.Length, newLength = c.mats.Length + 1; Material[] newMats = new Material[newLength]; System.Array.Copy(c.mats, newMats, oldLength); newMats[newLength - 1] = hackable.infillMaterial; c.mats = newMats; TurboList <int>[] indexArray; indexArray = new TurboList <int> [newLength]; System.Array.Copy(_backIndices, indexArray, oldLength); indexArray[newLength - 1] = backInfill; _backIndices = indexArray; indexArray = new TurboList <int> [newLength]; System.Array.Copy(_frontIndices, indexArray, oldLength); indexArray[newLength - 1] = frontInfill; _frontIndices = indexArray; submeshCount++; } } Vector3[] geoSubsetOne, geoSubsetTwo; Vector3[] normalsSubsetOne, normalsSubsetTwo; Vector2[] uvSubsetOne, uvSubsetTwo; BoneWeight[] weightSubsetOne, weightSubsetTwo; int[][] indexSubsetOne, indexSubsetTwo; indexSubsetOne = new int[submeshCount][]; indexSubsetTwo = new int[submeshCount][]; targetSubsetOne.Clear(); targetSubsetTwo.Clear(); int transferTableMaximumKey = c.vertices.Count; int[] transferTableOne = new int[transferTableMaximumKey]; int[] transferTableTwo = new int[transferTableMaximumKey]; for (int i = 0; i < transferTableOne.Length; i++) { transferTableOne[i] = -1; } for (int i = 0; i < transferTableTwo.Length; i++) { transferTableTwo[i] = -1; } for (int i = 0; i < submeshCount; i++) { perfectSubsetRD(_frontIndices[i], c.vertices, c.normals, c.UVs, c.weights, out indexSubsetOne[i], targetSubsetOne, ref transferTableOne); } for (int i = 0; i < submeshCount; i++) { perfectSubsetRD(_backIndices[i], c.vertices, c.normals, c.UVs, c.weights, out indexSubsetTwo[i], targetSubsetTwo, ref transferTableTwo); } //Note that we do not explicitly call recalculate bounds because (as per the manual) this is implicit in an //assignment to vertices whenever the vertex count changes from zero to non-zero. Mesh frontMesh = new Mesh(); Mesh backMesh = new Mesh(); var frontSMR = GetSkinnedMeshRendererWithName(frontObject, smr.name); var backSMR = GetSkinnedMeshRendererWithName(backObject, smr.name); if (targetSubsetOne.vertices.Count > 0) { frontSMR.materials = c.mats; frontSMR.sharedMesh = frontMesh; frontMesh.vertices = targetSubsetOne.vertices.ToArray(); frontMesh.normals = targetSubsetOne.normals.ToArray(); frontMesh.uv = targetSubsetOne.UVs.ToArray(); frontMesh.boneWeights = targetSubsetOne.weights.ToArray(); frontMesh.subMeshCount = submeshCount; frontMesh.bindposes = m.bindposes; for (int i = 0; i < submeshCount; i++) { frontMesh.SetTriangles(indexSubsetOne[i], i); } } else { GameObject.DestroyImmediate(frontSMR); } if (targetSubsetTwo.vertices.Count > 0) { backSMR.materials = c.mats; backSMR.sharedMesh = backMesh; backMesh.vertices = targetSubsetTwo.vertices.ToArray(); backMesh.normals = targetSubsetTwo.normals.ToArray(); backMesh.uv = targetSubsetTwo.UVs.ToArray(); backMesh.boneWeights = targetSubsetTwo.weights.ToArray(); backMesh.subMeshCount = submeshCount; backMesh.bindposes = m.bindposes; for (int i = 0; i < submeshCount; i++) { backMesh.SetTriangles(indexSubsetTwo[i], i); } } else { GameObject.DestroyImmediate(backSMR); } } var results = new GameObject[] { frontObject, backObject }; hackable.handleSlice(results); return(results); }