public void SetSegment(int segment) { _cacheList.Clear(); bool change = false; for (int i = 1; i <= _lineSphereColliders.Count; i++) { int count = GetLineCount(i * 100); if (count == 0) { continue; } int value = Mathf.Abs(segment - count); bool add = segment > count; for (int index = 0; index < value; index++) { LineSphereCollider lsc = GetLineSphere(i * 100); if (add) { InsertLineSphereCollider(lsc); } else { RemoveLineSpereCollider(lsc); } change = true; } } if (change) { ResetPointToHypodispersion(); } }
private void SortLine() { _lineSphereColliders.Sort((left, right) => { if (left.lineIndex > right.lineIndex) { return(1); } else if (left.lineIndex < right.lineIndex) { return(-1); } else { return(0); } }); int index = 0; int preIndex = 1; for (int i = 0; i < _lineSphereColliders.Count; i++) { LineSphereCollider lsc = _lineSphereColliders[i]; if (preIndex != lsc.lineIndex / 100) { index = 0; preIndex++; } lsc.lineIndex = preIndex * 100 + index; index++; } }
public bool RemoveLineSpereCollider(LineSphereCollider inlsc) { SortLine(); int count = GetLineCount(inlsc.lineIndex); for (int i = 0; i < _lineSphereColliders.Count; i++) { LineSphereCollider lsc = _lineSphereColliders[i]; if (lsc == inlsc) { if (count <= 1) { return(false); } int a = inlsc.lineIndex / 100 * 100; bool first = (inlsc.lineIndex % 100 == 0); bool last = (inlsc.lineIndex == (a + count - 1)); if (first) { LineSphereCollider lscNext = _lineSphereColliders[i + 1]; inlsc._endBone = lscNext._endBone; inlsc.WorldB = lscNext.WorldB; inlsc.WorldRadiusB = lscNext.WorldRadiusB; inlsc.EditorBFactor = lscNext.EditorBFactor; inlsc.scaleBFactor = lscNext.scaleBFactor; _lineSphereColliders.Remove(lscNext); GameObject.DestroyImmediate(lscNext); } else if (last) { LineSphereCollider lscPre = _lineSphereColliders[i - 1]; inlsc._startBone = lscPre._startBone; inlsc.WorldA = lscPre.WorldA; inlsc.WorldRadiusA = lscPre.WorldRadiusA; inlsc.EditorAFactor = lscPre.EditorAFactor; inlsc.scaleAFactor = lscPre.scaleAFactor; _lineSphereColliders.Remove(lscPre); GameObject.DestroyImmediate(lscPre); } else { LineSphereCollider lscPre = _lineSphereColliders[i - 1]; LineSphereCollider lscNext = _lineSphereColliders[i + 1]; lscNext._startBone = lscPre._endBone; lscNext.WorldA = lscPre.WorldB; lscNext.EditorAFactor = lscPre.EditorBFactor; lscNext.scaleAFactor = lscPre.scaleBFactor; lscNext.WorldRadiusA = lscPre.WorldRadiusB; _lineSphereColliders.Remove(inlsc); GameObject.DestroyImmediate(inlsc); } SortLine(); return(true); } } return(false); }
public void InitRootColliderManagerWhenCreate() { LineSphereCollider[] collider = GetComponents <LineSphereCollider>(); for (int i = 0; i < collider.Length; i++) { LineSphereCollider cd = collider[i]; cd.lineIndex = (i + 1) * 100; _lineSphereColliders.Add(cd); } }
public void SetWorldPosWithHandler(LineSphereCollider lsc, Vector3 newAPos, float newARadius, Vector3 newBPos, float newBRadius, bool relevance = true) { if (lsc == null) { return; } int count = GetLineCount(lsc.lineIndex); lsc.WorldA = newAPos; lsc.WorldRadiusA = newARadius; lsc.WorldB = newBPos; lsc.WorldRadiusB = newBRadius; for (int i = 0; i < _lineSphereColliders.Count; i++) { if (_lineSphereColliders[i] == lsc) { if (count <= 1) { break; } int a = lsc.lineIndex / 100 * 100; bool first = (lsc.lineIndex % 100 == 0); bool last = (lsc.lineIndex == (a + count - 1)); if (first) { LineSphereCollider lscNext = _lineSphereColliders[i + 1]; lscNext.WorldA = newBPos; lscNext.WorldRadiusA = newBRadius; } else if (last) { LineSphereCollider lscPre = _lineSphereColliders[i - 1]; lscPre.WorldB = newAPos; lscPre.WorldRadiusB = newARadius; } else { LineSphereCollider lscPre = _lineSphereColliders[i - 1]; LineSphereCollider lscNext = _lineSphereColliders[i + 1]; lscPre.WorldB = newAPos; lscPre.WorldRadiusB = newARadius; lscNext.WorldA = newBPos; lscNext.WorldRadiusA = newBRadius; } } } }
public static void CopyLineSphereColliderWithSameHierarchy(GameObject src, GameObject des) { if (src == null || des == null) { Debug.LogError("_src == null || _des == null"); return; } else { LineSphereCollider[] scripts = src.GetComponentsInChildren <LineSphereCollider>(true); for (int i = 0; i < scripts.Length; i++) { Transform trs = scripts[i].transform; string path = AnimationUtility.CalculateTransformPath(trs, src.transform); Transform desTrs = des.transform.Find(path); if (desTrs == null) { Debug.LogError("Fail: src and des dismatch in Hierarchy..."); return; } RootColliderManager smanager = desTrs.GetComponent <RootColliderManager>(); if (smanager == null) { smanager = desTrs.gameObject.AddComponent <RootColliderManager>(); } LineSphereCollider sp = desTrs.gameObject.AddComponent <LineSphereCollider>(); LineSphereCollider s1 = scripts[i]; sp.A = s1.A; sp.B = s1.B; sp.RadiusA = s1.RadiusA; sp.RadiusB = s1.RadiusB; sp.scaleAFactor = s1.scaleAFactor; sp.scaleBFactor = s1.scaleBFactor; sp.EditorAFactor = s1.EditorAFactor; sp.EditorBFactor = s1.EditorBFactor; sp.lineIndex = s1.lineIndex; sp._enableCollider = s1._enableCollider; sp._startBone = s1._startBone; sp._endBone = s1._endBone; smanager._lineSphereColliders.Add(sp); } } }
private void GetLineSphere(int index, ref List <LineSphereCollider> list) { if (list == null) { return; } list.Clear(); SortLine(); _cacheList.Clear(); for (int i = 0; i < _lineSphereColliders.Count; i++) { LineSphereCollider lsc = _lineSphereColliders[i]; if ((index / 100) == (lsc.lineIndex / 100)) { list.Add(lsc); } } }
public void InsertLineSphereCollider(LineSphereCollider cd) { SortLine(); bool insertOver = false; for (int i = 0; i < _lineSphereColliders.Count; i++) { LineSphereCollider lsc = _lineSphereColliders[i]; if (lsc == cd) { Vector3 center = Vector3.Lerp(lsc.WorldA, lsc.WorldB, 0.5f); LineSphereCollider newlsc = this.gameObject.AddComponent <LineSphereCollider>(); newlsc.WorldA = center; newlsc.WorldRadiusA = lsc.WorldRadiusA; newlsc.EditorAFactor = lsc.EditorAFactor; newlsc.scaleAFactor = lsc.scaleAFactor; newlsc.WorldRadiusB = lsc.WorldRadiusB; newlsc.WorldB = lsc.WorldB; newlsc.EditorBFactor = lsc.EditorBFactor; newlsc.scaleBFactor = lsc.scaleBFactor; newlsc._endBone = lsc._endBone; lsc.WorldB = newlsc.WorldA; lsc.WorldRadiusB = newlsc.WorldRadiusB; lsc.EditorBFactor = newlsc.EditorBFactor; lsc.scaleBFactor = newlsc.scaleBFactor; lsc._endBone = newlsc._startBone; newlsc.lineIndex = lsc.lineIndex + 1; _lineSphereColliders.Insert(i + 1, newlsc); i++; insertOver = true; } else if (insertOver) { lsc.lineIndex++; } } SortLine(); }
private void CalculatePointToHypodispersion(List <LineSphereCollider> list) { // ReCalculate Position int cacheCount = _cacheList.Count; if (cacheCount > 1) { LineSphereCollider start = _cacheList[0]; LineSphereCollider end = _cacheList[cacheCount - 1]; Vector3 startPos = start.WorldA; Vector3 endPos = end.WorldB; float radius = Mathf.Min(start.RadiusA, end.RadiusB); float d = 1f / (cacheCount); for (int cIndex = 0; cIndex < cacheCount; cIndex++) { LineSphereCollider tlsc = _cacheList[cIndex]; tlsc.WorldA = Vector3.Lerp(startPos, endPos, cIndex * d); tlsc.WorldB = Vector3.Lerp(startPos, endPos, (cIndex + 1) * d); tlsc.RadiusA = radius; tlsc.RadiusB = radius + 0.001f; } } }
private void ResetPointToHypodispersion() { SortLine(); int preIndex = 1; _cacheList.Clear(); for (int i = 0; i < _lineSphereColliders.Count; i++) { LineSphereCollider lsc = _lineSphereColliders[i]; if (preIndex != lsc.lineIndex / 100) { // ReCalculate Position CalculatePointToHypodispersion(_cacheList); preIndex++; _cacheList.Clear(); } _cacheList.Add(lsc); } if (_cacheList.Count > 0) { CalculatePointToHypodispersion(_cacheList); } }
public static void CreateJoints(Transform root, HumanBodyBoneReferenceData r, bool isReal) { // Hip // Spine // Chest // Shoulder // Arm // Leg // Clear Colliders LineSphereCollider[] preColliders = root.gameObject.GetComponentsInChildren <LineSphereCollider>(true); for (int i = 0; i < preColliders.Length; i++) { GameObject.DestroyImmediate(preColliders[i]); } RootColliderManager[] mgs = root.gameObject.GetComponentsInChildren <RootColliderManager>(true); for (int i = 0; i < mgs.Length; i++) { GameObject.DestroyImmediate(mgs[i]); } Transform trsLeftUpperArm = r._dicBones[(int)HumanBodyBones.LeftUpperArm]; Transform trsRightUpperArm = r._dicBones[(int)HumanBodyBones.RightUpperArm]; Transform trsLeftLowerArm = r._dicBones[(int)HumanBodyBones.LeftLowerArm]; Transform trsLeftUpperLeg = r._dicBones[(int)HumanBodyBones.LeftUpperLeg]; Transform trsLeftLowerLeg = r._dicBones[(int)HumanBodyBones.LeftLowerLeg]; float upperArmDis = Vector3.Distance(trsLeftUpperArm.position, trsLeftLowerArm.position); float legDis = Vector3.Distance(trsLeftUpperLeg.position, trsLeftLowerLeg.position); Vector3 v2 = trsLeftUpperArm.position - trsRightUpperArm.position; float torsoWidth = v2.magnitude; // check Optional JointConfig bool hasChest = r.CheckBone(HumanBodyBones.Chest); bool hasShoulder = r.CheckBone(HumanBodyBones.LeftShoulder); bool hasNeck = r.CheckBone(HumanBodyBones.Neck); _cacheList.Clear(); if (!hasChest) { if (hasShoulder) { _cacheList.Add(optionalSpineWithOutChestJoint); } else { _cacheList.Add(optionalSpineWithOutCheckShoulderJoint); } if (!hasNeck) { _cacheList.Add(optionaSpineWithOutNeck); } } else { if (!hasShoulder) { _cacheList.Add(optionalChestWithOutShoulderJoint); } if (!hasNeck) { _cacheList.Add(optionalCheckWithOutNeck); } } _cacheList.AddRange(_listJointConfigs007); for (int i = 0; i < _cacheList.Count; i++) { JointConfig jc = _cacheList[i]; HumanBodyBones rootBone = jc._rootBone; if (!r._dicBones.ContainsKey((int)rootBone) || (r._dicBones[(int)rootBone] == null)) { continue; } List <HumanBodyBones> jointBones = jc._bones; Transform rootTrs = r._dicBones[(int)rootBone]; bool hasCollider = false; for (int boneIndex = 0; boneIndex < jointBones.Count; boneIndex++) { HumanBodyBones bone = jointBones[boneIndex]; if (!r._dicBones.ContainsKey((int)jointBones[boneIndex]) || (r._dicBones[(int)jointBones[boneIndex]]) == null) { continue; } Transform boneTrs = r._dicBones[(int)jointBones[boneIndex]]; LineSphereCollider collider = rootTrs.gameObject.AddComponent <LineSphereCollider>(); collider._startBone = rootBone; collider._endBone = jointBones[boneIndex]; collider.WorldA = rootTrs.position; collider.WorldB = boneTrs.position; float a = GetRadius(upperArmDis, legDis, torsoWidth, rootBone); float b = GetRadius(upperArmDis, legDis, torsoWidth, bone); collider.RadiusA = a; collider.RadiusB = b; hasCollider = true; } if (hasCollider) { RootColliderManager mg = rootTrs.gameObject.AddComponent <RootColliderManager>(); mg.InitRootColliderManagerWhenCreate(); } } // Debug.Log("## Create Joints Point collider over ##"); }
public static void CopyLineSphereColliderWithAvatar(GameObject src, GameObject des) { if (src == null || des == null) { Debug.LogError("_src == null || _des == null"); return; } Animator animatorSrc = src.GetComponent <Animator>(); Animator animatorDes = des.GetComponent <Animator>(); if (animatorSrc == null || animatorDes == null) { Debug.LogError("animator src == null || animator src == null"); return; } int boneCount = (int)HumanBodyBones.LastBone - 1; for (int i = 0; i < boneCount; i++) { HumanBodyBones bone = (HumanBodyBones)i; Transform trs01 = animatorSrc.GetBoneTransform(bone); Transform trsDes = animatorDes.GetBoneTransform(bone); if (trs01 == null) { continue; } if (trs01 != null && trsDes == null) { Debug.LogError("Two humanoid does have the same bone please Check"); return; } LineSphereCollider[] colliders = trs01.GetComponents <LineSphereCollider>(); RootColliderManager smanager = trsDes.GetComponent <RootColliderManager>(); if (smanager == null) { smanager = trsDes.gameObject.AddComponent <RootColliderManager>(); } for (int index = 0; index < colliders.Length; index++) { LineSphereCollider sp = trsDes.gameObject.AddComponent <LineSphereCollider>(); LineSphereCollider s1 = colliders[index]; sp.A = s1.A; sp.B = s1.B; sp.RadiusA = s1.RadiusA; sp.RadiusB = s1.RadiusB; sp.scaleAFactor = s1.scaleAFactor; sp.scaleBFactor = s1.scaleBFactor; sp.EditorAFactor = s1.EditorAFactor; sp.EditorBFactor = s1.EditorBFactor; sp.lineIndex = s1.lineIndex; sp._enableCollider = s1._enableCollider; sp._startBone = s1._startBone; sp._endBone = s1._endBone; smanager._lineSphereColliders.Add(sp); } } }