protected void UpdateColliderBounds() { foreach (var handle in handles) { var handleBounds = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject); UpdateColliderBounds(handle, handleBounds.size); } }
private void CreateHandles(Transform parent, bool drawManipulationTether) { for (int i = 0; i < edgeCenters.Length; ++i) { GameObject midpoint = new GameObject(); midpoint.name = "midpoint_" + i.ToString(); midpoint.transform.position = edgeCenters[i]; midpoint.transform.parent = parent; GameObject midpointVisual; GameObject prefabType = config.HandlePrefab; if (prefabType != null) { midpointVisual = GameObject.Instantiate(prefabType); } else { midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere); GameObject.Destroy(midpointVisual.GetComponent <SphereCollider>()); } // Align handle with its edge assuming that the prefab is initially aligned with the up direction if (edgeAxes[i] == CardinalAxisType.X) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.right); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } else if (edgeAxes[i] == CardinalAxisType.Z) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.forward); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual); float maxDim = Mathf.Max( Mathf.Max(midpointBounds.size.x, midpointBounds.size.y), midpointBounds.size.z); float invScale = config.HandleSize / maxDim; midpointVisual.name = "visuals"; midpointVisual.transform.parent = midpoint.transform; midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale); midpointVisual.transform.localPosition = Vector3.zero; VisualUtils.AddComponentsToAffordance(midpoint, new Bounds(midpointBounds.center * invScale, midpointBounds.size * invScale), config.RotationHandlePrefabColliderType, CursorContextInfo.CursorAction.Rotate, config.ColliderPadding, parent, drawManipulationTether); handles.Add(midpoint.transform); if (config.HandleMaterial != null) { VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial); } } }
private void UpdateColliderType() { foreach (var handle in handles) { // remove old colliders bool shouldCreateNewCollider = false; var oldBoxCollider = handle.GetComponent <BoxCollider>(); if (oldBoxCollider != null && config.RotationHandlePrefabColliderType == HandlePrefabCollider.Sphere) { shouldCreateNewCollider = true; Object.Destroy(oldBoxCollider); } var oldSphereCollider = handle.GetComponent <SphereCollider>(); if (oldSphereCollider != null && config.RotationHandlePrefabColliderType == HandlePrefabCollider.Box) { shouldCreateNewCollider = true; Object.Destroy(oldSphereCollider); } if (shouldCreateNewCollider) { // attach new collider var handleBounds = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject); var invScale = handleBounds.size.x == 0.0f ? 0.0f : config.HandleSize / handleBounds.size.x; Vector3 colliderSizeScaled = handleBounds.size * invScale; Vector3 colliderCenterScaled = handleBounds.center * invScale; if (config.RotationHandlePrefabColliderType == HandlePrefabCollider.Box) { BoxCollider collider = handle.gameObject.AddComponent <BoxCollider>(); collider.size = colliderSizeScaled; collider.center = colliderCenterScaled; collider.size += config.ColliderPadding; } else { SphereCollider sphere = handle.gameObject.AddComponent <SphereCollider>(); sphere.center = colliderCenterScaled; sphere.radius = colliderSizeScaled.x * 0.5f; sphere.radius += VisualUtils.GetMaxComponent(config.ColliderPadding); } } } }
/// <summary> /// Creates the corner visual and returns the bounds of the created visual /// </summary> /// <param name="parent">parent of visual</param> /// <param name="isFlattened">instantiate in flattened mode - slate</param> /// <returns>bounds of the created visual</returns> private Bounds CreateVisual(GameObject parent, bool isFlattened) { // figure out which prefab to instantiate GameObject cornerVisual = null; areHandlesFlattened = isFlattened; GameObject prefabType = isFlattened ? config.HandleSlatePrefab : config.HandlePrefab; if (prefabType == null) { // instantiate default prefab, a cube. Remove the box collider from it cornerVisual = GameObject.CreatePrimitive(PrimitiveType.Cube); cornerVisual.transform.parent = parent.transform; cornerVisual.transform.localPosition = Vector3.zero; GameObject.Destroy(cornerVisual.GetComponent <BoxCollider>()); } else { cornerVisual = GameObject.Instantiate(prefabType, parent.transform); } if (isFlattened) { // Rotate 2D slate handle asset for proper orientation cornerVisual.transform.Rotate(0, 0, -90); } cornerVisual.name = visualsName; // this is the size of the corner visuals var cornerbounds = VisualUtils.GetMaxBounds(cornerVisual); float maxDim = Mathf.Max(Mathf.Max(cornerbounds.size.x, cornerbounds.size.y), cornerbounds.size.z); cornerbounds.size = maxDim * Vector3.one; // we need to multiply by this amount to get to desired scale handle size var invScale = cornerbounds.size.x == 0.0f ? 0.0f : config.HandleSize / cornerbounds.size.x; cornerVisual.transform.localScale = new Vector3(invScale, invScale, invScale); VisualUtils.ApplyMaterialToAllRenderers(cornerVisual, config.HandleMaterial); return(cornerbounds); }
private Bounds CreateVisual(int handleIndex, GameObject parent) { GameObject midpointVisual; GameObject prefabType = config.HandlePrefab; if (prefabType != null) { midpointVisual = Object.Instantiate(prefabType); } else { midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere); Object.Destroy(midpointVisual.GetComponent <SphereCollider>()); } // Align handle with its edge assuming that the prefab is initially aligned with the up direction if (edgeAxes[handleIndex] == CardinalAxisType.X) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.right); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } else if (edgeAxes[handleIndex] == CardinalAxisType.Z) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.forward); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual); float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size); float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim; midpointVisual.name = "visuals"; midpointVisual.transform.parent = parent.transform; midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale); midpointVisual.transform.localPosition = Vector3.zero; if (config.HandleMaterial != null) { VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial); } return(midpointBounds); }
private Bounds CreateVisual(int handleIndex, GameObject parent) { GameObject midpointVisual; GameObject prefabType = config.HandlePrefab; if (prefabType != null) { midpointVisual = Object.Instantiate(prefabType); } else { midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere); if (config.HandlePrefabColliderType != HandlePrefabCollider.Sphere) { Object.Destroy(midpointVisual.GetComponent <SphereCollider>()); } } Quaternion realignment = GetRotationRealignment(handleIndex); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual); float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size); float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim; midpointVisual.name = visualsName; midpointVisual.transform.parent = parent.transform; midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale); midpointVisual.transform.localPosition = Vector3.zero; if (config.HandleMaterial != null) { VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial); } return(midpointBounds); }
internal void Create(ref Vector3[] boundsCorners, Transform parent, bool drawManipulationTether, bool isFlattened) { // create corners for (int i = 0; i < boundsCorners.Length; ++i) { GameObject corner = new GameObject { name = "corner_" + i.ToString() }; corner.transform.parent = parent; corner.transform.localPosition = boundsCorners[i]; GameObject visualsScale = new GameObject(); visualsScale.name = "visualsScale"; visualsScale.transform.parent = corner.transform; visualsScale.transform.localPosition = Vector3.zero; // Compute mirroring scale { Vector3 p = boundsCorners[i]; visualsScale.transform.localScale = new Vector3(Mathf.Sign(p[0]), Mathf.Sign(p[1]), Mathf.Sign(p[2])); } // figure out which prefab to instantiate GameObject cornerVisual = null; GameObject prefabType = isFlattened ? config.HandleSlatePrefab : config.HandlePrefab; if (prefabType == null) { // instantiate default prefab, a cube. Remove the box collider from it cornerVisual = GameObject.CreatePrimitive(PrimitiveType.Cube); cornerVisual.transform.parent = visualsScale.transform; cornerVisual.transform.localPosition = Vector3.zero; GameObject.Destroy(cornerVisual.GetComponent <BoxCollider>()); } else { cornerVisual = GameObject.Instantiate(prefabType, visualsScale.transform); } if (isFlattened) { // Rotate 2D slate handle asset for proper orientation cornerVisual.transform.Rotate(0, 0, -90); } cornerVisual.name = "visuals"; // this is the size of the corner visuals var cornerbounds = VisualUtils.GetMaxBounds(cornerVisual); float maxDim = Mathf.Max(Mathf.Max(cornerbounds.size.x, cornerbounds.size.y), cornerbounds.size.z); cornerbounds.size = maxDim * Vector3.one; // we need to multiply by this amount to get to desired scale handle size var invScale = config.HandleSize / cornerbounds.size.x; cornerVisual.transform.localScale = new Vector3(invScale, invScale, invScale); VisualUtils.ApplyMaterialToAllRenderers(cornerVisual, config.HandleMaterial); VisualUtils.AddComponentsToAffordance(corner, new Bounds(cornerbounds.center * invScale, cornerbounds.size * invScale), RotationHandlePrefabCollider.Box, CursorContextInfo.CursorAction.Scale, config.ColliderPadding, parent, drawManipulationTether); handles.Add(corner.transform); } }