public ViveSR_StaticColliderInfo[] GetColliderWithPropsAndCondition(uint[] props, ColliderCondition condition, Vector3 testPos = new Vector3()) { if (condition == ColliderCondition.NONE) { return(GetAllColliderHasProps(props)); } ViveSR_StaticColliderInfo info = null; if (condition == ColliderCondition.LARGEST) { info = GetLargestCollider(props); } else if (condition == ColliderCondition.CLOSEST) { info = GetClosestColliderWithProps(testPos, props); } else if (condition == ColliderCondition.FURTHEST) { info = GetFurthestColliderWithProps(testPos, props); } _tempList.Clear(); if (info) { _tempList.Add(info); } return(_tempList.ToArray()); }
private void _AddColliderInfo(ViveSR_StaticColliderInfo info) { if (!allColliders.Contains(info)) { allColliders.Add(info); } numClds = allColliders.Count; }
public void SetCorrespondingColliderOfType(ColliderShapeType type, ViveSR_StaticColliderInfo info) { if (type == ColliderShapeType.CONVEX_SHAPE) { _corresConvexCld = info; } else if (type == ColliderShapeType.BOUND_RECT_SHAPE) { _corresBoundingRectCld = info; } else if (type == ColliderShapeType.MESH_SHAPE) { _corresMeshCld = info; } }
public ViveSR_StaticColliderInfo GetLargestCollider(uint[] props) { _GetAllColliderHasProps_Internal(props); // get filtered info in tempList int found_id = -1; float max_Area = float.MinValue; for (int i = 0; i < _tempList.Count; i++) { ViveSR_StaticColliderInfo info = _tempList[i]; if (info.ApproxArea > max_Area) { max_Area = info.ApproxArea; found_id = i; } } return((found_id == -1) ? null : _tempList[found_id]); }
public void DrawAllExtractedPlacerLocations(ViveSR_StaticColliderInfo[] infoArray) { for (int infoIdx = 0; infoIdx < infoArray.Length; ++infoIdx) { //Vector3[] raycastPositions; Quaternion outRot; ViveSR_StaticColliderInfo info = infoArray[infoIdx]; info.GetColliderUsableLocations(placerIntervalW, placerIntervalH, placerUpShift, placer_locations, out outRot); for (int i = 0; i < placer_locations.Count; i++) { GameObject placer = GameObject.Instantiate(_defaultPlacer); placer.GetComponent <MeshRenderer>().enabled = true; placer.transform.localScale = new Vector3(placerShowScale, placerShowScale, placerShowScale); placer.transform.position = placer_locations[i]; placer.transform.rotation = outRot; placer_list.Add(placer); } } }
public ViveSR_StaticColliderInfo GetFurthestColliderWithProps(Vector3 testPos, uint[] props) { _GetAllColliderHasProps_Internal(props); // get filtered info in tempList int found_id = -1; float max_dist = float.MinValue; for (int i = 0; i < _tempList.Count; i++) { ViveSR_StaticColliderInfo info = _tempList[i]; float dist = Vector3.Distance(info.GetComponent <MeshRenderer>().bounds.center, testPos); if (dist > max_dist) { max_dist = dist; found_id = i; } } return((found_id == -1) ? null : _tempList[found_id]); }
public void GetColliderUsableLocationsWithRightAxis(float intervalW, float intervalH, float surf_shift, List <Vector3> outLocations, out Quaternion rotation, ref Vector3 rightVec) { outLocations.Clear(); rotation = new Quaternion(); ViveSR_StaticColliderInfo bb_cldInfo = GetCorrespondingColliderOfType(ColliderShapeType.BOUND_RECT_SHAPE); if (bb_cldInfo != null) { // Get collider axes Vector3 bb_center = bb_cldInfo.GetComponent <MeshRenderer>().bounds.center; Vector3 up = bb_cldInfo.GroupNormal; Vector3 forward = Vector3.Cross(rightVec, up); forward.Normalize(); rightVec = Vector3.Cross(up, forward); rightVec.Normalize(); float bb_width = bb_cldInfo.RectWidth; float bb_height = bb_cldInfo.RectHeight; // return rotation rotation.SetLookRotation(forward, up); // Calculate new range new_height = new_width = Mathf.Sqrt(Mathf.Pow(bb_height, 2) + Mathf.Pow(bb_width, 2)); // Check if collider exists in each position by Raycast for (float j = -new_height / 2 + intervalH / 2; j <= new_height / 2; j += intervalH) { for (float i = -new_width / 2 + intervalW / 2; i <= new_width / 2; i += intervalW) { Vector3 pos = bb_center + rightVec * i + forward * j; Vector3 pos_raycast = pos + up * 0.1f; RaycastHit hit; Physics.Raycast(pos_raycast, -up, out hit); if (hit.collider != null && hit.collider.gameObject.name == this.gameObject.name) { outLocations.Add(pos + up * surf_shift); } } } } }
// get colliders within customized height range public ViveSR_StaticColliderInfo[] GetColliderByHeightRange(ColliderShapeType shapeType, float lowestHeight, float highestHeight) { _tempList.Clear(); if (lowestHeight <= highestHeight) { for (int i = 0; i < allColliders.Count; i++) { ViveSR_StaticColliderInfo info = allColliders[i]; if (info.CheckHasAllBit((uint)shapeType)) { Vector3 center = info.GetComponent <MeshRenderer>().bounds.center; if (center.y >= lowestHeight && center.y <= highestHeight) { _tempList.Add(info); } } } } return(_tempList.ToArray()); }
// data pre-proc static public bool ProcessDataAndGenColliderInfo(GameObject go) { // organize and category collider type bool hasCollider = false; MeshFilter[] mFilters = go.GetComponentsInChildren <MeshFilter>(); int numRnds = mFilters.Length; for (int id = 0; id < numRnds; ++id) { ViveSR_StaticColliderInfo cldInfo = mFilters[id].gameObject.AddComponent <ViveSR_StaticColliderInfo>(); SceneUnderstandingObjectType semantic_type = ViveSR_SceneUnderstanding.GetSemanticTypeFromObjName(go.name); cldInfo.SemanticType = semantic_type; if (semantic_type != SceneUnderstandingObjectType.NONE) { string s_idx = go.name.Replace(ViveSR_SceneUnderstanding.SemanticTypeToString(semantic_type) + "_", "").Replace("_cld", ""); cldInfo.SceneObjectID = int.Parse(s_idx); } string meshName = mFilters[id].name; string newName = ""; bool thisIsCLD = false; if (meshName.Contains("PlaneConvexCollider")) { newName = "PlaneConvexCollider"; cldInfo.SetBit((int)ColliderShapeType.CONVEX_SHAPE); thisIsCLD = true; } else if (meshName.Contains("PlaneBBCollider")) { newName = "PlaneBBCollider"; cldInfo.SetBit((int)ColliderShapeType.BOUND_RECT_SHAPE); thisIsCLD = true; } else if (meshName.Contains("PlaneMeshCollider")) { newName = "PlaneMeshCollider"; cldInfo.SetBit((int)ColliderShapeType.MESH_SHAPE); thisIsCLD = true; } if (meshName.Contains("Horizontal")) { cldInfo.SetBit((int)PlaneOrientation.HORIZONTAL); } else if (meshName.Contains("Vertical")) { cldInfo.SetBit((int)PlaneOrientation.VERTICAL); } else { cldInfo.SetBit((int)PlaneOrientation.OBLIQUE); } hasCollider = (hasCollider || thisIsCLD); if (!thisIsCLD) { Component.DestroyImmediate(cldInfo); } else { // parse area int areaStringStartIdx = meshName.LastIndexOf("Area_"); if (areaStringStartIdx != -1) { areaStringStartIdx = areaStringStartIdx + 5; string curString = meshName.Substring(areaStringStartIdx); int areaStringEndIdx = curString.IndexOf("_"); cldInfo.ApproxArea = float.Parse(curString.Substring(0, areaStringEndIdx)); } else { cldInfo.SetBit((int)PlaneOrientation.FRAGMENT); } // parse normal int normalStringStartIdx = meshName.LastIndexOf("Normal_"); if (normalStringStartIdx != -1) { normalStringStartIdx = normalStringStartIdx + 7; string curString = meshName.Substring(normalStringStartIdx); int normalXEndIdx = curString.IndexOf("_"); cldInfo.GroupNormal.x = float.Parse(curString.Substring(0, normalXEndIdx)); curString = curString.Substring(normalXEndIdx + 1); int normalYEndIdx = curString.IndexOf("_"); cldInfo.GroupNormal.y = float.Parse(curString.Substring(0, normalYEndIdx)); curString = curString.Substring(normalYEndIdx + 1); int normalZEndIdx = curString.IndexOf("_"); cldInfo.GroupNormal.z = float.Parse(curString.Substring(0, normalZEndIdx)); } // parse right axis int rightStringStartIdx = meshName.LastIndexOf("Right_"); if (rightStringStartIdx != -1) { rightStringStartIdx = rightStringStartIdx + 6; string curString = meshName.Substring(rightStringStartIdx); int rightXEndIdx = curString.IndexOf("_"); cldInfo.RectRightAxis.x = float.Parse(curString.Substring(0, rightXEndIdx)); curString = curString.Substring(rightXEndIdx + 1); int rightYEndIdx = curString.IndexOf("_"); cldInfo.RectRightAxis.y = float.Parse(curString.Substring(0, rightYEndIdx)); curString = curString.Substring(rightYEndIdx + 1); int rightZEndIdx = curString.IndexOf("_"); cldInfo.RectRightAxis.z = float.Parse(curString.Substring(0, rightZEndIdx)); } // parse width height int whStringStartIdx = meshName.LastIndexOf("WH_"); if (whStringStartIdx != -1) { whStringStartIdx = whStringStartIdx + 3; string curString = meshName.Substring(whStringStartIdx); int widthEndIdx = curString.IndexOf("_"); cldInfo.RectWidth = float.Parse(curString.Substring(0, widthEndIdx)); curString = curString.Substring(widthEndIdx + 1); int heightEndIdx = curString.IndexOf("_"); cldInfo.RectHeight = float.Parse(curString.Substring(0, heightEndIdx)); } // parse id int idxStringStartIdx = meshName.LastIndexOf("_"); cldInfo.PlaneID = (int.Parse(meshName.Substring(idxStringStartIdx + 1))); newName = newName + "_" + meshName.Substring(idxStringStartIdx + 1); mFilters[id].gameObject.name = newName; } } return(hasCollider); }
public void OrganizeHierarchy() { ViveSR_StaticColliderInfo[] infos = GetComponentsInChildren <ViveSR_StaticColliderInfo>(true); int len = infos.Length; // init list with length * null List <ViveSR_StaticColliderInfo> brInfoList = new List <ViveSR_StaticColliderInfo>(len); List <ViveSR_StaticColliderInfo> cInfoList = new List <ViveSR_StaticColliderInfo>(len); List <ViveSR_StaticColliderInfo> mInfoList = new List <ViveSR_StaticColliderInfo>(len); brInfoList.AddRange(Enumerable.Repeat((ViveSR_StaticColliderInfo)null, len)); cInfoList.AddRange(Enumerable.Repeat((ViveSR_StaticColliderInfo)null, len)); mInfoList.AddRange(Enumerable.Repeat((ViveSR_StaticColliderInfo)null, len)); GameObject meshCldGroup = new GameObject("PlaneMeshColliderGroup"); { meshCldGroup.transform.SetParent(transform); GameObject HorizontalGroup = new GameObject("Horizontal"); GameObject VerticalGroup = new GameObject("Vertical"); GameObject ObliqueGroup = new GameObject("Oblique"); GameObject FragmentGroup = new GameObject("Fragment"); { HorizontalGroup.transform.SetParent(meshCldGroup.transform); VerticalGroup.transform.SetParent(meshCldGroup.transform); ObliqueGroup.transform.SetParent(meshCldGroup.transform); FragmentGroup.transform.SetParent(meshCldGroup.transform); } } meshCldGroup.SetActive(true); GameObject convexCldGroup = new GameObject("PlaneConvexColliderGroup"); { convexCldGroup.transform.SetParent(transform); GameObject HorizontalGroup = new GameObject("Horizontal"); GameObject VerticalGroup = new GameObject("Vertical"); GameObject ObliqueGroup = new GameObject("Oblique"); { HorizontalGroup.transform.SetParent(convexCldGroup.transform); VerticalGroup.transform.SetParent(convexCldGroup.transform); ObliqueGroup.transform.SetParent(convexCldGroup.transform); } } convexCldGroup.SetActive(false); GameObject bbCldGroup = new GameObject("PlaneBoundingRectColliderGroup"); { bbCldGroup.transform.SetParent(transform); GameObject HorizontalGroup = new GameObject("Horizontal"); GameObject VerticalGroup = new GameObject("Vertical"); GameObject ObliqueGroup = new GameObject("Oblique"); { HorizontalGroup.transform.SetParent(bbCldGroup.transform); VerticalGroup.transform.SetParent(bbCldGroup.transform); ObliqueGroup.transform.SetParent(bbCldGroup.transform); } } bbCldGroup.SetActive(false); for (int i = 0; i < len; ++i) { Transform parent = transform; ViveSR_StaticColliderInfo cldInfo = infos[i]; if (cldInfo.CheckHasAllBit((uint)ColliderShapeType.MESH_SHAPE)) { parent = parent.Find("PlaneMeshColliderGroup"); cldInfo.SetCorrespondingColliderOfType(ColliderShapeType.MESH_SHAPE, cldInfo); mInfoList[cldInfo.PlaneID] = cldInfo; } else if (cldInfo.CheckHasAllBit((uint)ColliderShapeType.CONVEX_SHAPE)) { parent = parent.Find("PlaneConvexColliderGroup"); cldInfo.SetCorrespondingColliderOfType(ColliderShapeType.CONVEX_SHAPE, cldInfo); cInfoList[cldInfo.PlaneID] = cldInfo; } else if (cldInfo.CheckHasAllBit((uint)ColliderShapeType.BOUND_RECT_SHAPE)) { parent = parent.Find("PlaneBoundingRectColliderGroup"); cldInfo.SetCorrespondingColliderOfType(ColliderShapeType.BOUND_RECT_SHAPE, cldInfo); brInfoList[cldInfo.PlaneID] = cldInfo; } if (cldInfo.CheckHasAllBit((uint)PlaneOrientation.HORIZONTAL)) { parent = parent.Find("Horizontal"); } else if (cldInfo.CheckHasAllBit((uint)PlaneOrientation.VERTICAL)) { parent = parent.Find("Vertical"); } else if (cldInfo.CheckHasAllBit((uint)PlaneOrientation.OBLIQUE)) { parent = parent.Find("Oblique"); } else { parent = parent.Find("Fragment"); // this should only appear in PlaneMesh } cldInfo.transform.SetParent(parent, true); cldInfo.gameObject.AddComponent <MeshCollider>(); MeshRenderer rnd = cldInfo.gameObject.GetComponent <MeshRenderer>(); if (rnd) { Material wireframe = new Material(Shader.Find("ViveSR/Wireframe")); wireframe.SetFloat("_ZTest", 0); wireframe.SetFloat("_Thickness", 0); rnd.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; rnd.receiveShadows = false; rnd.sharedMaterial = wireframe; rnd.enabled = false; } } // get collider of other groups _LinkColliderInfo(ColliderShapeType.BOUND_RECT_SHAPE, brInfoList, new List <ViveSR_StaticColliderInfo>[] { cInfoList, mInfoList }); _LinkColliderInfo(ColliderShapeType.CONVEX_SHAPE, cInfoList, new List <ViveSR_StaticColliderInfo>[] { brInfoList, mInfoList }); _LinkColliderInfo(ColliderShapeType.MESH_SHAPE, mInfoList, new List <ViveSR_StaticColliderInfo>[] { brInfoList, cInfoList }); }