public static void Capture(DisplayObject target, RenderTexture texture, Vector2 offset)
{
CheckMain();
Matrix4x4 matrix = target.cachedTransform.localToWorldMatrix;
float unitsPerPixel = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude;
Vector3 forward;
forward.x = matrix.m02;
forward.y = matrix.m12;
forward.z = matrix.m22;
Vector3 upwards;
upwards.x = matrix.m01;
upwards.y = matrix.m11;
upwards.z = matrix.m21;
float halfHeight = (float)texture.height / 2;
Camera camera = _main.cachedCamera;
camera.targetTexture = texture;
camera.orthographicSize = halfHeight * unitsPerPixel;
_main.cachedTransform.localPosition = target.cachedTransform.TransformPoint(halfHeight * camera.aspect - offset.x, -halfHeight + offset.y, 0);
_main.cachedTransform.localRotation = Quaternion.LookRotation(forward, upwards);
int oldLayer = 0;
if (target.graphics != null)
{
oldLayer = target.graphics.gameObject.layer;
target.graphics.gameObject.layer = CaptureCamera.layer;
}
if (target is Container)
{
oldLayer = ((Container)target).numChildren > 0 ? ((Container)target).GetChildAt(0).layer : CaptureCamera.hiddenLayer;
((Container)target).SetChildrenLayer(CaptureCamera.layer);
}
RenderTexture old = RenderTexture.active;
RenderTexture.active = texture;
GL.Clear(true, true, Color.clear);
camera.Render();
RenderTexture.active = old;
if (target.graphics != null)
target.graphics.gameObject.layer = oldLayer;
if (target is Container)
((Container)target).SetChildrenLayer(oldLayer);
}