public static Bounds CalculateLocalBounds(GameObject ob) { // from https://forum.unity3d.com/threads/calculating-a-bound-of-a-grouped-model.101121/ Quaternion currentRotation = ob.transform.rotation; ob.transform.rotation = Quaternion.Euler(0f, 0f, 0f); Bounds bounds = new Bounds(ob.transform.position, Vector3.zero); foreach (Renderer renderer in ob.GetComponentsInChildren <Renderer>()) { if (renderer.gameObject.GetComponent <MText>() != null || renderer.gameObject.transform.parent.GetComponent <MText>() != null) { continue; // don't include text elements in bounds calculation } //Debug.LogFormat("Encapsulating bounds {0} {1} {2}", renderer.gameObject.name, renderer.bounds.center, renderer.bounds.extents); bounds.Encapsulate(renderer.bounds); } Vector3 localCenter = bounds.center - ob.transform.position; //Debug.LogFormat("Bounds before inverse scale: {0}", bounds); bounds.center = Misc.InverseScale(localCenter, ob.transform.localScale); bounds.extents = Misc.InverseScale(bounds.extents, ob.transform.localScale); //Debug.Log("The local bounds of this model is " + bounds); ob.transform.rotation = currentRotation; return(bounds); }
public Vector3 convertFromDataToPixelSpace(Vector3 dataCoord, bool doOffset = false) { Vector3 dataSpan = new Vector3(XLim[1] - XLim[0], YLim[1] - YLim[0], ZLim[1] - ZLim[0]); // right now, this setting of local scale doesn't account for any camera rotation //TODO: compensate localScale and change transform.rotation to match matlab's camera angle // for now, assume X axis and Y axis, combined with DataAspectRatio define relative scaling // (this assumption is almost certainly incorrect, but proceed anyways) var dataWidth = (XLim[1] - XLim[0]); //TODO: based on camera angle, form critical dimension from weighted combination of directions // for now, just use x axis // max size available is only constrained in X/Y (actually, based on camera angle) var USz = USize; if (USz[2] == 0) { USz[2] = Mathf.Infinity; } var axisSz = dataSpan; axisSz = Misc.InverseScale(axisSz, Misc.ArrayToVec3(DataAspectRatio)); var scale = Misc.VecMin(Misc.InverseScale(USz, axisSz)); axisSz = axisSz * scale; if (doOffset) { Vector3 origin = new Vector3(XLim[0], YLim[0], ZLim[0]); dataCoord = dataCoord - origin; } Vector3 pixelCoord = Vector3.Scale(Misc.InverseScale(dataCoord, dataSpan), axisSz); if (doOffset) { // add offset for axis to be centered inside figure USz = USize; pixelCoord = pixelCoord + USz / 2 - axisSz / 2; pixelCoord.Scale(new Vector3(1, 1, -1)); // correct for Unity's left-handed coordinate system } return(pixelCoord); }