static bool GetBoundWithChildren( Transform parent, ref Bounds pBound, ref bool initBound ) { Bounds bound = new Bounds(); bool didOne = false; if( parent.gameObject.renderer != null ) { bound = parent.gameObject.renderer.bounds; if( initBound ) { pBound.Encapsulate( bound.min ); pBound.Encapsulate( bound.max ); } else { pBound.min = new Vector3( bound.min.x, bound.min.y, bound.min.z ); pBound.max = new Vector3( bound.max.x, bound.max.y, bound.max.z ); initBound = true; } didOne = true; } foreach( Transform child in parent ) { if( GetBoundWithChildren( child, ref pBound, ref initBound )) didOne = true; } return didOne; }
public static Bounds GetPartColliderBoundsInBasis(this Part part, Matrix4x4 worldToBasisMatrix, int excessiveVerts = 2500) { Transform[] transforms = part.FindModelComponents<Transform>(); Bounds bounds = new Bounds(); for (int i = 0; i < transforms.Length; i++) { Transform t = transforms[i]; MeshCollider mc = t.GetComponent<MeshCollider>(); Mesh m; Matrix4x4 matrix = worldToBasisMatrix * t.localToWorldMatrix; if (mc == null) { BoxCollider bc = t.GetComponent<BoxCollider>(); if (bc != null) { bounds.Encapsulate(matrix.MultiplyPoint3x4(bc.bounds.min)); bounds.Encapsulate(matrix.MultiplyPoint3x4(bc.bounds.max)); } continue; } else m = mc.sharedMesh; if (m == null) continue; bounds.Encapsulate(matrix.MultiplyPoint3x4(m.bounds.min)); bounds.Encapsulate(matrix.MultiplyPoint3x4(m.bounds.max)); } return bounds; }
/// <summary> /// Gets the rendering bounds of the transform. /// </summary> /// <param name="transform">The game object to get the bounding box for.</param> /// <param name="pBound">The bounding box reference that will </param> /// <param name="encapsulate">Used to determine if the first bounding box to be /// calculated should be encapsulated into the <see cref="pBound"/> argument.</param> /// <returns>Returns true if at least one bounding box was calculated.</returns> public static bool GetBoundWithChildren(Transform transform, ref Bounds pBound, ref bool encapsulate) { var didOne = false; // get 'this' bound if (transform.gameObject.renderer != null) { var bound = transform.gameObject.renderer.bounds; if (encapsulate) { pBound.Encapsulate(bound.min); pBound.Encapsulate(bound.max); } else { pBound.min = bound.min; pBound.max = bound.max; encapsulate = true; } didOne = true; } // union with bound(s) of any/all children foreach (Transform child in transform) { if (GetBoundWithChildren(child, ref pBound, ref encapsulate)) { didOne = true; } } return didOne; }
public static void DrawSkeleton(Transform reference, Dictionary<Transform, bool> actualBones, AvatarSetupTool.BoneWrapper[] bones) { if ((Object) reference == (Object) null || actualBones == null) return; AvatarSkeletonDrawer.sPoseError = false; Bounds bounds = new Bounds(); Renderer[] componentsInChildren = reference.root.GetComponentsInChildren<Renderer>(); if (componentsInChildren != null) { foreach (Renderer renderer in componentsInChildren) { bounds.Encapsulate(renderer.bounds.min); bounds.Encapsulate(renderer.bounds.max); } } Quaternion orientation = Quaternion.identity; if (bones != null) orientation = AvatarSetupTool.AvatarComputeOrientation(bones); AvatarSkeletonDrawer.DrawSkeletonSubTree(actualBones, bones, orientation, reference, bounds); Camera current = Camera.current; if (!AvatarSkeletonDrawer.sPoseError || !((Object) current != (Object) null)) return; GUIStyle style = new GUIStyle(GUI.skin.label); style.normal.textColor = Color.red; style.wordWrap = false; style.alignment = TextAnchor.MiddleLeft; style.fontSize = 20; GUIContent content = new GUIContent("Character is not in T pose"); Rect rect = GUILayoutUtility.GetRect(content, style); rect.x = 30f; rect.y = 30f; Handles.BeginGUI(); GUI.Label(rect, content, style); Handles.EndGUI(); }
private void AddBreastColliders() { if ((this.middleSpine != null) && (this.pelvis != null)) { Bounds bounds = this.Clip(this.GetBreastBounds(this.pelvis), this.pelvis, this.middleSpine, false); BoxCollider collider = this.pelvis.gameObject.AddComponent<BoxCollider>(); collider.center = bounds.center; collider.size = bounds.size; bounds = this.Clip(this.GetBreastBounds(this.middleSpine), this.middleSpine, this.middleSpine, true); collider = this.middleSpine.gameObject.AddComponent<BoxCollider>(); collider.center = bounds.center; collider.size = bounds.size; } else { Bounds bounds2 = new Bounds(); bounds2.Encapsulate(this.pelvis.InverseTransformPoint(this.leftHips.position)); bounds2.Encapsulate(this.pelvis.InverseTransformPoint(this.rightHips.position)); bounds2.Encapsulate(this.pelvis.InverseTransformPoint(this.leftArm.position)); bounds2.Encapsulate(this.pelvis.InverseTransformPoint(this.rightArm.position)); Vector3 size = bounds2.size; size[SmallestComponent(bounds2.size)] = size[LargestComponent(bounds2.size)] / 2f; BoxCollider collider2 = this.pelvis.gameObject.AddComponent<BoxCollider>(); collider2.center = bounds2.center; collider2.size = size; } }
void EditPageBounds() { Page t = target as Page; Vector3 pos = t.transform.position; Vector3[] verts = new Vector3[4]; verts[0] = new Vector3(pos.x + t.pageBounds.min.x, pos.y + t.pageBounds.min.y, 0); verts[1] = new Vector3(pos.x + t.pageBounds.min.x, pos.y + t.pageBounds.max.y, 0); verts[2] = new Vector3(pos.x + t.pageBounds.max.x, pos.y + t.pageBounds.max.y, 0); verts[3] = new Vector3(pos.x + t.pageBounds.max.x, pos.y + t.pageBounds.min.y, 0); Handles.DrawSolidRectangleWithOutline(verts, new Color(1,1,1,0.2f), new Color(0,0,0,1)); for(int i = 0; i < 4; ++i) { Vector3 vert = verts[i]; Vector3 newPos = Handles.FreeMoveHandle(vert, Quaternion.identity, HandleUtility.GetHandleSize(pos) * 0.1f, Vector3.zero, Handles.CubeCap); newPos.z = 0; verts[i] = newPos; if (vert != newPos) { switch(i) { case 0: verts[1].x = newPos.x; verts[3].y = newPos.y; break; case 1: verts[0].x = newPos.x; verts[2].y = newPos.y; break; case 2: verts[3].x = newPos.x; verts[1].y = newPos.y; break; case 3: verts[2].x = newPos.x; verts[0].y = newPos.y; break; } break; } } Bounds newBounds = new Bounds(verts[0], Vector3.zero); newBounds.Encapsulate(verts[1]); newBounds.Encapsulate(verts[2]); newBounds.Encapsulate(verts[3]); t.transform.position = newBounds.center; newBounds.center = Vector3.zero; t.pageBounds = newBounds; }
public void Voxelize (Transform root) { var meshFilters = root.GetComponentsInChildren<MeshFilter>(); var objectBounds = new List<BoundHierarchy>(); foreach (var filter in meshFilters) { var mesh = filter.sharedMesh; var vertices = mesh.vertices; var tris = mesh.triangles; var triangleBounds = new List<BoundHierarchy>(); for(int i = 0; i < tris.Length; i += 3) { var vert1 = vertices[tris[i + 0]]; var vert2 = vertices[tris[i + 1]]; var vert3 = vertices[tris[i + 2]]; vert1 = filter.transform.TransformPoint(vert1); vert2 = filter.transform.TransformPoint(vert2); vert3 = filter.transform.TransformPoint(vert3); var u = vert2 - vert3; var v = vert3 - vert1; var triNormal = Vector3.Cross(u, v); triNormal = triNormal.normalized; var triBounds = new Bounds(vert1, Vector3.zero); triBounds.Encapsulate(vert2); triBounds.Encapsulate(vert3); var tri = new Triangle { vertA = vert1, vertB = vert2, vertC = vert3, normal = triNormal, bound = triBounds, }; triangleBounds.Add(new BoundHierarchy() { bound = triBounds, subBounds = null, triList = tri }); } objectBounds.Add(new BoundHierarchy() { bound = filter.GetComponent<Renderer>().bounds, subBounds = triangleBounds.ToArray() }); } var rootNode = new BoundHierarchy() { bound = _bounds, subBounds = objectBounds.ToArray() }; GenerateBlockArray (); GenerateVoxelData (rootNode); }
public static Bounds GetBounds(this GameObject obj) { var renderers = obj.GetComponentsInChildren<Renderer>(); var bounds = new Bounds(renderers.Length > 0 ? renderers.First().bounds.center : obj.transform.position, Vector3.zero); renderers.ToList().ForEach(r => bounds.Encapsulate(r.bounds)); obj.GetComponentsInChildren<Collider>().ForEach( c => bounds.Encapsulate(new Bounds(obj.transform.TransformPoint(c.bounds.center), obj.transform.TransformPoint(c.bounds.size)))); return bounds; }
// -------------- private Rect GetMapBoundingBox() { Matrix4x4 mat = this.GetMapTargetMatrix(); Bounds box = new Bounds( mat.MultiplyPoint3x4(new Vector3(0, 0, 0)), Vector3.zero); box.Encapsulate(mat.MultiplyPoint3x4(new Vector3(this.mapWidth, 0, 0))); box.Encapsulate(mat.MultiplyPoint3x4(new Vector3(0, this.mapHeight, 0))); box.Encapsulate(mat.MultiplyPoint3x4(new Vector3(this.mapWidth, this.mapHeight, 0))); return new Rect(box.min.x, box.min.y, box.size.x, box.size.y); }
public static Bounds GetBounds(List<Vector3> vals) { Bounds b = new Bounds(Vector3.zero, Vector3.zero); if ( vals != null && vals.Count > 0 ) { b.Encapsulate(vals[0]); for ( int i = 1; i < vals.Count; i++ ) b.Encapsulate(vals[i]); } return b; }
//============================ Bounds Functions ============================\ // Creates bounds that encapsulate of the two Bounds passed in. public static Bounds BoundsUnion( Bounds b0, Bounds b1 ) { // If the size of one of the bounds is Vector3.zero, ignore that one if ( b0.size==Vector3.zero && b1.size!=Vector3.zero ) { return( b1 ); } else if ( b0.size!=Vector3.zero && b1.size==Vector3.zero ) { return( b0 ); } else if ( b0.size==Vector3.zero && b1.size==Vector3.zero ) { return( b0 ); } // Stretch b0 to include the b1.min and b1.max b0.Encapsulate(b1.min); b0.Encapsulate(b1.max); return( b0 ); }
public static Bounds BoundsColliders(GameObject obj) { var bounds = new Bounds(obj.transform.position, Vector3.zero); var colliders = obj.GetComponentsInChildren<Collider2D>(); foreach(var c in colliders) { var blocal =BoundsOf(c); var t = c.transform; var max = t.TransformPoint(blocal.max); bounds.Encapsulate(max); var min = t.TransformPoint(blocal.min); bounds.Encapsulate(min); } return bounds; }
public Bounds GetBoundsRelativeTo( Transform t ) { Vector3 min = t.InverseTransformPoint( worldScreenBounds.min ); Vector3 max = t.InverseTransformPoint( worldScreenBounds.max ); Bounds b = new Bounds( min, Vector3.zero ); b.Encapsulate( max ); return b; }
public static Bounds CalculateAbsoluteWidgetBounds(Transform trans) { UIWidget[] componentsInChildren = trans.GetComponentsInChildren<UIWidget>(); if (componentsInChildren.Length == 0) { return new Bounds(trans.position, Vector3.zero); } Vector3 rhs = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 vector2 = new Vector3(float.MinValue, float.MinValue, float.MinValue); int index = 0; int length = componentsInChildren.Length; while (index < length) { UIWidget widget = componentsInChildren[index]; if (widget.enabled) { Vector3[] worldCorners = widget.worldCorners; for (int i = 0; i < 4; i++) { vector2 = Vector3.Max(worldCorners[i], vector2); rhs = Vector3.Min(worldCorners[i], rhs); } } index++; } Bounds bounds = new Bounds(rhs, Vector3.zero); bounds.Encapsulate(vector2); return bounds; }
public static Bounds GetMeshBounds(this GameObject obj) { var filters = obj.GetComponentsInChildren<MeshFilter>(); var bounds = new Bounds(filters.Length > 0 ? filters.First().mesh.bounds.center : Vector3.zero, Vector3.zero); filters.ToList().ForEach(r => bounds.Encapsulate(r.mesh.bounds)); return bounds; }
public static Bounds GetEncapsulatingLocalBounds(this GameObject go) { Bounds bounds = new Bounds (Vector3.zero, Vector3.zero); MeshFilter mf = go.GetComponent<MeshFilter> (); if (mf != null && mf.sharedMesh != null) bounds = mf.sharedMesh.bounds; else if (go.renderer is SkinnedMeshRenderer) bounds = ((SkinnedMeshRenderer)go.renderer).localBounds; foreach (MeshFilter m in go.GetComponentsInChildren<MeshFilter> ()) bounds.Encapsulate (m.sharedMesh.bounds); foreach (SkinnedMeshRenderer m in go.GetComponentsInChildren<SkinnedMeshRenderer> ()) bounds.Encapsulate (m.localBounds); return bounds; }
void Awake() { propBounds = new Bounds( transform.position, Vector3.zero ); foreach( var c in GetComponents<Collider>() ) { propBounds.Encapsulate( c.bounds ); } }
static void onReseatPosition() { Transform parent = Selection.activeGameObject.transform; Vector3 postion = parent.position; Quaternion rotation = parent.rotation; Vector3 scale = parent.localScale; parent.position = Vector3.zero; parent.rotation = Quaternion.Euler(Vector3.zero); parent.localScale = Vector3.one; Vector3 center = Vector3.zero; Renderer[] renders = parent.GetComponentsInChildren<Renderer>(); foreach (Renderer child in renders){ center += child.bounds.center; } center /= parent.GetComponentsInChildren<Transform>().Length; Bounds bounds = new Bounds(center,Vector3.zero); foreach (Renderer child in renders){ bounds.Encapsulate(child.bounds); } parent.position = postion; parent.rotation = rotation; parent.localScale = scale; foreach(Transform t in parent){ t.position = t.position - bounds.center; } parent.transform.position = bounds.center + parent.position; }
public static Bounds getChildBounds(this Transform t) { //To handle exeption when a object has no children and no sprite Renderer if (t.childCount == 0) { SpriteRenderer r = t.gameObject.GetComponent<SpriteRenderer>(); if (r) return r.bounds; else return new Bounds(); } Bounds bounds; // First find a center for your bounds. Vector3 center = Vector3.zero; foreach (Transform child in t.transform) { center += child.gameObject.GetComponent<SpriteRenderer>().bounds.center; } center /= t.transform.childCount; //center is average center of children //Now you have a center, calculate the bounds by creating a zero sized 'Bounds', bounds = new Bounds(center, Vector3.zero); foreach (Transform child in t.transform) { bounds.Encapsulate(child.gameObject.GetComponent<SpriteRenderer>().bounds); } return bounds; }
public static Bounds CalculateRelativeRectTransformBounds(Transform root, Transform child) { RectTransform[] componentsInChildren = child.GetComponentsInChildren<RectTransform>(false); if (componentsInChildren.Length > 0) { Vector3 rhs = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 vector2 = new Vector3(float.MinValue, float.MinValue, float.MinValue); Matrix4x4 worldToLocalMatrix = root.worldToLocalMatrix; int index = 0; int length = componentsInChildren.Length; while (index < length) { componentsInChildren[index].GetWorldCorners(s_Corners); for (int i = 0; i < 4; i++) { Vector3 lhs = worldToLocalMatrix.MultiplyPoint3x4(s_Corners[i]); rhs = Vector3.Min(lhs, rhs); vector2 = Vector3.Max(lhs, vector2); } index++; } Bounds bounds = new Bounds(rhs, Vector3.zero); bounds.Encapsulate(vector2); return bounds; } return new Bounds(Vector3.zero, Vector3.zero); }
void OnEnable() { var flags = BindingFlags.Static | BindingFlags.NonPublic; var propInfo = typeof(Camera).GetProperty ("PreviewCullingLayer", flags); int previewLayer = (int)propInfo.GetValue (null, new object[0]); previewRenderUtility = new PreviewRenderUtility (true); previewRenderUtility.m_CameraFieldOfView = 30f; previewRenderUtility.m_Camera.cullingMask = 1 << previewLayer; var component = (Component)target; previewObject = Instantiate (component.gameObject); previewObject.hideFlags = HideFlags.HideAndDontSave; previewObject.layer = previewLayer; foreach (Transform transform in previewObject.transform) { transform.gameObject.layer = previewLayer; } Bounds bounds = new Bounds (component.transform.position, Vector3.zero); foreach (var renderer in previewObject.GetComponentsInChildren<Renderer>()) { bounds.Encapsulate (renderer.bounds); } centerPosition = bounds.center; previewObject.SetActive (false); RotatePreviewObject (new Vector2 (-120, 20)); }
public static Bounds calculateWorldBounds(GameObject go) { if (null == go) { return new Bounds(Vector3.zero, Vector3.zero); } Vector2 min = new Vector2(float.MaxValue, float.MaxValue); Vector2 max = new Vector2(float.MinValue, float.MinValue); Vector3 v; RectTransform[] rts = go.GetComponentsInChildren<RectTransform>(); if (rts.Length == 0) return new Bounds(go.transform.position, Vector3.zero); for (int i = 0, imax = rts.Length; i < imax; ++i) { RectTransform t = rts[i]; if (!t.gameObject.activeSelf) { continue; } Vector3[] corners = new Vector3[4]; t.GetWorldCorners(corners); for (int j = 0; j < 4; ++j) { v = corners[j]; if (v.x > max.x) max.x = v.x; if (v.y > max.y) max.y = v.y; if (v.x < min.x) min.x = v.x; if (v.y < min.y) min.y = v.y; } } Bounds b = new Bounds(min, Vector3.zero); b.Encapsulate(max); return b; }
public static float MaxBoundsExtent(Transform obj, bool includeEffects) { // get the maximum bounds extent of object, including all child renderers, // but excluding particles and trails, for FOV zooming effect. var renderers = obj.GetComponentsInChildren<Renderer>(); Bounds bounds = new Bounds(); bool initBounds = false; foreach (Renderer r in renderers) { if (!((r is TrailRenderer) || (r is ParticleRenderer) || (r is ParticleSystemRenderer))) { if (!initBounds) { initBounds = true; bounds = r.bounds; } else { bounds.Encapsulate(r.bounds); } } } float max = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z); return max; }
static public int Encapsulate(IntPtr l) { try{ if (matchType(l, 2, typeof(UnityEngine.Vector3))) { UnityEngine.Bounds self = (UnityEngine.Bounds)checkSelf(l); UnityEngine.Vector3 a1; checkType(l, 2, out a1); self.Encapsulate(a1); setBack(l, self); return(0); } else if (matchType(l, 2, typeof(UnityEngine.Bounds))) { UnityEngine.Bounds self = (UnityEngine.Bounds)checkSelf(l); UnityEngine.Bounds a1; checkType(l, 2, out a1); self.Encapsulate(a1); setBack(l, self); return(0); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public void CalculateBounds() { selectionBounds = new Bounds(transform.position, Vector3.zero); foreach(Renderer r in GetComponentsInChildren< Renderer >()) { selectionBounds.Encapsulate(r.bounds); } }
/// <summary> /// Revalidate internal bounds cache. No reposition for new bounds! /// </summary> public void ValidateBounds() { var bounds = new Bounds (); gameObject.GetComponentsInChildren (false, _widgetsBuf); Transform tr; for (int i = _widgetsBuf.Count - 1; i >= 0; i--) { tr = _widgetsBuf[i].transform; bounds.Encapsulate (tr.TransformPoint (new Vector3 (-_widgetsBuf[i].Width * 0.5f, -_widgetsBuf[i].Height * 0.5f, 0f))); bounds.Encapsulate (tr.TransformPoint (new Vector3 (_widgetsBuf[i].Width * 0.5f, _widgetsBuf[i].Height * 0.5f, 0f))); } _widgetsBuf.Clear (); _min = transform.InverseTransformPoint (bounds.min); _max = transform.InverseTransformPoint (bounds.max); _size = _max - _min; }
public static UnityEngine.Bounds SharedVisualSpace(List <BoundingBox> bounds) { var center = Vector3.zero; var totalCount = 0f; foreach (var bound in bounds) { foreach (Renderer child in bound.GetComponentsInChildren <Renderer>()) { if (child.GetComponent <BoundsHighlightSatellite>() == null) { center += child.bounds.center; totalCount++; } } } center /= Mathf.Max(totalCount, 1f); var extents = new UnityEngine.Bounds { center = center }; foreach (var bound in bounds) { foreach (Renderer child in bound.GetComponentsInChildren <Renderer>()) { if (child.GetComponent <BoundsHighlightSatellite>() == null) { extents.Encapsulate(child.bounds); } } } return(extents); }
private void PositionHighlight() { var center = Vector3.zero; var totalCount = 0f; foreach (Renderer child in GetComponentsInChildren <Renderer>()) { if (child.GetComponent <BoundsHighlightSatellite>() == null) { center += child.bounds.center; totalCount++; } } center /= Mathf.Max(totalCount, 1f); var extents = new UnityEngine.Bounds(); extents.center = center; foreach (Renderer child in GetComponentsInChildren <Renderer>()) { if (child.GetComponent <BoundsHighlightSatellite>() == null) { extents.Encapsulate(child.bounds); } } HighlightBox.transform.rotation = Quaternion.identity; HighlightBox.transform.position = extents.center; HighlightBox.transform.SetWorldScale(extents.size + 2f * _HighlightPadding); }
static void FitToChildren() { foreach (GameObject rootGameObject in Selection.gameObjects) { if (!(rootGameObject.GetComponent<Collider>() is BoxCollider)) continue; bool hasBounds = false; Bounds bounds = new Bounds(Vector3.zero, Vector3.zero); for (int i = 0; i < rootGameObject.transform.childCount; ++i) { Renderer childRenderer = rootGameObject.transform.GetChild(i).GetComponent<Renderer>(); if (childRenderer != null) { if (hasBounds) { bounds.Encapsulate(childRenderer.bounds); } else { bounds = childRenderer.bounds; hasBounds = true; } } } BoxCollider collider = (BoxCollider)rootGameObject.GetComponent<Collider>(); collider.center = bounds.center - rootGameObject.transform.position; collider.size = bounds.size; } }
public static List<Bounds> GetMeshTriangleBoundsList(Mesh mesh) { List<Bounds> ret = new List<Bounds>(); MeshTopology meshTopo = mesh.GetTopology(0); int stride = 3; if (meshTopo == MeshTopology.Triangles) stride = 3; else if (meshTopo == MeshTopology.Quads) stride = 4; else return ret; for (int subMesh = 0; subMesh < mesh.subMeshCount; ++subMesh) { var indices = mesh.GetIndices(subMesh); var vertices = mesh.vertices; for (int i = 0; i + stride <= indices.Length; i += stride) { Bounds bounds = new Bounds(vertices[indices[i]], Vector3.zero); for (int j = 1; j < stride; ++j) { bounds.Encapsulate(vertices[indices[i + j]]); } ret.Add(bounds); } } return ret; }
void CreateGrid() { GameObject floorTiles = new GameObject("Floor Tiles"); // An object to store the tiles in the Hiearchy. Just for tidyness ;). floorTiles.AddComponent<LevelInfo>(); // This should be done manually for the designed floors. Bounds levelBounds = new Bounds(); int nrTiles = this.transform.childCount; for (int i = 0; i < nrTiles; i++) { Transform child = transform.GetChild(i); if (child.GetComponent<Renderer>()) levelBounds.Encapsulate(child.GetComponent<Renderer>().bounds); //TODO fixa så att det även funkar med cubeworld utan en tom box i dem // else if (child.GetComponentInChildren<Renderer>()) // levelBounds.Encapsulate(child.GetComponent<Renderer>().bounds); } Vector3 size = levelBounds.size; _gridManager.CreateGrid((int)size.x + 1, (int)size.z + 1); for (int i = 0; i < nrTiles; i++) { BaseTile tile = transform.GetChild(i).GetComponent<BaseTile>(); tile.Init(_gridManager); _gridManager.AddTile(tile); } Object[] allUnits = Object.FindObjectsOfType<BaseUnit>(); for (int i = 0; i < allUnits.Length; i++) { BaseUnit bu = allUnits[i] as BaseUnit; bu.Init(_gridManager); BaseTile tile = _gridManager.GetTile(bu.transform.position); DebugAux.Assert(tile != null, "Can't have a unit placed on a non-tile " + bu); BaseTile.TeleportTo(bu, null, tile); } }
private void Start() { if (this.template != null) { int num = 0; Bounds bounds = new Bounds(); for (int i = 0; i < this.maxRows; i++) { for (int j = 0; j < this.maxColumns; j++) { GameObject obj2 = NGUITools.AddChild(base.gameObject, this.template); obj2.transform.localPosition = new Vector3(this.padding + ((j + 0.5f) * this.spacing), -this.padding - ((i + 0.5f) * this.spacing), 0f); UIStorageSlot component = obj2.GetComponent<UIStorageSlot>(); if (component != null) { component.storage = this; component.slot = num; } bounds.Encapsulate(new Vector3((this.padding * 2f) + ((j + 1) * this.spacing), (-this.padding * 2f) - ((i + 1) * this.spacing), 0f)); if (++num >= this.maxItemCount) { if (this.background != null) { this.background.transform.localScale = bounds.size; } return; } } } if (this.background != null) { this.background.transform.localScale = bounds.size; } } }
public static UnityEngine.Bounds CalculateBounds(List <MeshCollider> meshColliders) { if (meshColliders.Count == 0) { return(new UnityEngine.Bounds()); } UnityEngine.Bounds bounds = meshColliders[0].bounds; for (int i = 1; i < meshColliders.Count; i++) { bounds.Encapsulate(meshColliders[i].bounds); } return(bounds); }
static bool Bounds_Encapsulate__Bounds(JSVCall vc, int argc) { int len = argc; if (len == 1) { UnityEngine.Bounds arg0 = (UnityEngine.Bounds)JSMgr.datax.getObject((int)JSApi.GetType.Arg); UnityEngine.Bounds argThis = (UnityEngine.Bounds)vc.csObj; argThis.Encapsulate(arg0); JSMgr.changeJSObj(vc.jsObjID, argThis); } return(true); }
public override IEnumerator ReceivePayload(VisualPayload payload) { var combinedBounds = new UnityEngine.Bounds(payload.VisualData.Bound.transform.position, Vector3.zero); foreach (var rend in payload.VisualData.Bound.GetComponentsInChildren <Renderer>()) { combinedBounds.Encapsulate(rend.bounds); } ScaleTarget.SetValue(combinedBounds.size, payload.Data); var iterator = Router.TransmitAll(payload); while (iterator.MoveNext()) { yield return(null); } }
private void AddNavMeshObstalceForRobot() { var bounds = new UE.Bounds(); var renderers = transform.GetComponentsInChildren <UE.Renderer>(); for (var i = 0; i < renderers.Length; i++) { bounds.Encapsulate(renderers[i].bounds.size); } var navMeshObstacle = gameObject.AddComponent <UEAI.NavMeshObstacle>(); navMeshObstacle.carving = true; navMeshObstacle.carveOnlyStationary = false; navMeshObstacle.carvingMoveThreshold = CarvingMoveThreshold; navMeshObstacle.carvingTimeToStationary = CarvingTimeToStationary; navMeshObstacle.size = transform.rotation * bounds.size; }
public static UnityEngine.Bounds Calculate <TSource>(GameObject gameObject, Func <TSource, UnityEngine.Bounds> extractor) { var value = new UnityEngine.Bounds(gameObject.transform.position, Vector3.zero); var components = Dependancy.GetAll <TSource>(gameObject); for (int i = 0; i < components.Count; i++) { if (i == 0) { value = extractor(components[i]); } else { value.Encapsulate(extractor(components[i])); } } value.center = gameObject.transform.InverseTransformPoint(value.center); return(value); }
private BoundingBox ConstructChildBound(IEnumerable <BoundingBox> bounds, BoundChildTypes childMethod) { ClearBounds(); if (!bounds.Any()) { return(BoundingBox.ConstructBoundingBox("Child Bound")); } var firstBound = bounds.First(); var sharedSpace = BoundingBox.SharedVisualSpace(bounds.ToList()); switch (childMethod) { case BoundChildTypes.CenterOfFirst: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(Vector3.zero) , firstBound.transform.rotation)); case BoundChildTypes.OriginOfFirst: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(Vector3.one * -.5f), firstBound.transform.rotation)); case BoundChildTypes.CenterOfMinusX: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(new Vector3(-.5f, 0f, 0f)), firstBound.transform.rotation)); case BoundChildTypes.CenterOfPlusX: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(new Vector3(+.5f, 0f, 0f)), firstBound.transform.rotation)); case BoundChildTypes.CenterOfMinusY: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(new Vector3(0f, -.5f, 0f)), firstBound.transform.rotation)); case BoundChildTypes.CenterOfPlusY: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(new Vector3(0f, +.5f, 0f)), firstBound.transform.rotation)); case BoundChildTypes.CenterOfMinusZ: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(new Vector3(0f, 0f, -.5f)), firstBound.transform.rotation)); case BoundChildTypes.CenterOfPlusZ: return(ConstructMinimalBound( firstBound.VisualSpace.LocalToWorldSpace(new Vector3(0f, 0f, +.5f)), firstBound.transform.rotation)); case BoundChildTypes.CenterOfAll: // note: this does not inherit rotation from any of the related bounds. Use average? var center = Vector3.zero; var totalCount = 0f; foreach (var bound in bounds) { center += bound.Centerpoint; totalCount++; } center /= Mathf.Max(totalCount, 1f); var extents = new UnityEngine.Bounds(); extents.center = center; foreach (var bound in bounds) { extents.Encapsulate(bound.Centerpoint); } return(BoundingBox.ConstructBoundingBox ("Child Bound", extents.center, Quaternion.identity, .01f * Vector3.one)); case BoundChildTypes.CenterOfAllPlusX: return(ConstructMinimalBound( sharedSpace.center + new Vector3(.5f, 0, 0) * sharedSpace.size.x, Quaternion.identity)); case BoundChildTypes.CenterOfAllMinusX: return(ConstructMinimalBound( sharedSpace.center + new Vector3(-.5f, 0, 0) * sharedSpace.size.x, Quaternion.identity)); case BoundChildTypes.CenterOfAllPlusY: return(ConstructMinimalBound( sharedSpace.center + new Vector3(0, .5f, 0) * sharedSpace.size.y, Quaternion.identity)); case BoundChildTypes.CenterOfAllMinusY: return(ConstructMinimalBound( sharedSpace.center + new Vector3(0, -.5f, 0) * sharedSpace.size.y, Quaternion.identity)); case BoundChildTypes.CenterOfAllPlusZ: return(ConstructMinimalBound( sharedSpace.center + new Vector3(0, 0, .5f) * sharedSpace.size.z, Quaternion.identity)); case BoundChildTypes.CenterOfAllMinusZ: return(ConstructMinimalBound( sharedSpace.center + new Vector3(0, 0, -.5f) * sharedSpace.size.z, Quaternion.identity)); default: throw new Exception("Invalid child bounding operation!"); } }
// Use this for initialization void Start() { alarm = GameObject.Find("Scene").GetComponents <AudioSource> () [3]; if (eventDirectory.Length > 0) { string[] files = System.IO.Directory.GetFiles(eventDirectory); GameObject[] sources = GameObject.FindGameObjectsWithTag("NeutrinoSource"); int numSources = sources.Length; foreach (string file in files) { if (file.EndsWith(".txt")) { EventVis e = new EventVis(); e.eventData = new List <EventData>(); e.fileName = file; Debug.Log(file); StreamReader sr = new StreamReader(e.fileName, Encoding.Default); string s = sr.ReadLine(); int lineCount = 0; while (s != null) { if (lineCount < 2) { string[] data = s.Split(' '); float x = (float)double.Parse(data[0]); float y = (float)double.Parse(data[1]); float z = (float)double.Parse(data[2]); if (lineCount < 1) { //Debug.LogWarning ("START SET"); e.startPos = new Vector3(x * 0.3048f, y * 0.3048f, z * 0.3048f); } else { //Debug.LogWarning ("END SET"); e.endPos = new Vector3(x * 0.3048f, y * 0.3048f, z * 0.3048f); } } else { string[] data = s.Split('\t'); if (data.Length != 7) { data = s.Split(' '); } EventData d; d.str = IntParseFast(data[0]) - 1; d.dom = IntParseFast(data[1]) - 1; d.pos.x = (float)double.Parse(data[2]); d.pos.y = BELOW_ICE + (float)double.Parse(data[4]); d.pos.z = (float)double.Parse(data[3]); d.charge = (float)double.Parse(data[5]); d.time = (float)double.Parse(data[6]); e.eventData.Add(d); } lineCount++; s = sr.ReadLine(); } e.eventData.Sort((s1, s2) => s1.time.CompareTo(s2.time)); if (sources.Length > 0) { e.eventSource = sources[UnityEngine.Random.Range(0, numSources - 1)]; } events.Add(e); } } } if (newEventFile.Length > 0) { StreamReader sr = new StreamReader(newEventFile, Encoding.Default); string s = sr.ReadLine(); bool first = true; GameObject[] sources = GameObject.FindGameObjectsWithTag("NeutrinoSource"); int numSources = sources.Length; List <EventData> ed = new List <EventData>(); float lastTheta = 0.0f; float lastPhi = 0.0f; while (s != null) { if (s[0] != '#') { string[] data = s.Split(' '); if (data.Length == 2) { //theta, phi lastTheta = (float)double.Parse(data[0]); lastPhi = (float)double.Parse(data[1]); //todo - derive a start and end point from theta and phi } else if (data.Length == 7) { //string, dom, x, y, z, time, charge EventData d; d.dom = IntParseFast(data[0]) - 1; d.str = IntParseFast(data[1]) - 1; d.pos.x = (float)double.Parse(data[2]); d.pos.y = BELOW_ICE + (float)double.Parse(data[4]); d.pos.z = (float)double.Parse(data[3]); d.charge = (float)double.Parse(data[6]); d.time = (float)double.Parse(data[5]); ed.Add(d); } } else { if (s[1] == 'e') { //new event... //store old event... if (!first) { EventVis e = new EventVis(); e.eventData = new List <EventData>(ed); e.theta = lastTheta; e.phi = lastPhi; //todo - derive a start and end point from ed, theta, phi float thetaDeg = Mathf.Rad2Deg * e.theta; float phiDeg = Mathf.Rad2Deg * e.phi; Vector3 dir = SphericalToCartesian(1.0f, phiDeg, thetaDeg); Vector3 avgPos = UnityEngine.Vector3.zero; for (int i = 0; i < ed.Count; ++i) { avgPos += ed[i].pos; } avgPos /= (float)ed.Count; UnityEngine.Bounds b = new UnityEngine.Bounds(avgPos, new Vector3(1.0f, 1.0f, 1.0f)); for (int i = 0; i < ed.Count; ++i) { b.Encapsulate(ed[i].pos); } e.startPos = avgPos + dir * b.extents.magnitude; e.endPos = avgPos - dir * b.extents.magnitude; e.eventData.Sort((s1, s2) => s1.time.CompareTo(s2.time)); if (sources.Length > 0) { e.eventSource = sources[UnityEngine.Random.Range(0, numSources - 1)]; } events.Add(e); ed.Clear(); } first = false; } } s = sr.ReadLine(); } } if (events.Count > 0) { Debug.Log(events.Count); eventsPlaying = new EventPlayback[events.Count]; for (int e = 0; e < events.Count; ++e) { eventsPlaying[e].isPlaying = false; eventsPlaying[e].isDetected = false; eventsPlaying[e].eventIndex = -1; eventsPlaying[e].eventStartTime = 0.0f; eventsPlaying[e].eventEndTime = 0.0f; eventsPlaying[e].playStartTime = 0.0f; eventsPlaying[e].advancedIndex = false; eventsPlaying[e].newPlayTime = 0.0f; //eventsPlaying [e].ActivatedDoms = new List<DomState> (); } } }
public static Bounds GetBounds(Vector3[] vals) { Bounds b = new Bounds(Vector3.zero, Vector3.zero); if ( vals != null && vals.Length > 0 ) { b.Encapsulate(vals[0]); for ( int i = 1; i < vals.Length; i++ ) b.Encapsulate(vals[i]); } return b; }