This is the general shape of a pyramid stacked using squares. For example, the first layer would have 1 sphere, the second layer would have 4, and each layer would have $n^2$ spheres. I want to know a general formula of the height with $n$ layers expressed with $r$ as a function of $h_n(r)$.
I've made to $n=2$ so far, but I can't figure out the height beyond $2$ or even the general form.
My progress:
Obviously, we know that the height if it's 1 layer would be $2r$, or $h_1(r)=2r$.
It is also relatively easy if we have $2$ layers.
We can construct a pyramid by connecting the $5$ centers. Using Pythagorean theorem, we can know that the middle portion is $\sqrt{2}r$. Therefore, the total height would be $h_2(r)=r(2+\sqrt{2})$
I start to struggle with $3$ layers. Please help me solving for that as well as a general form. Hopefully this version is more detailed and well-written question.

Using a model with $r=1$ for simplicity, let $A$ be the center of the upper circle and $B,C,D,E$ be the centers of the underlying four circles. These points represent a pyramid with $|AB|=|AC|=|AD|=|AE|=|BC|=|CD|=|DE|=|EB|=2$.
The base $BCDE$ of the pyramid is a $2\times2$ square, all the distances from $B,C,D,E$ to the center of the square are equal to $\sqrt2$, and obviously, the height $|AO|=\sqrt2$. Accounting for the radii of the balls on the top and the bottom, the height of two layers of spheres in such an arrangement must be $|AO|+2=2+\sqrt2$. Adding one layer below, we just need to add $\sqrt2$, so the total height of the pyramid of 14 balls is $2+2\sqrt2$, and for the balls of radius $r$ it would be $2(1+\sqrt2)\,r$.
Edit
The total height of the pyramid of $n$ layers of balls with $n\times n$ balls on the bottom layer would then be
\begin{align} h_n(r) &= (2+\sqrt2 (n-1))\,r ,\\ h_1&=2 ,\\ h_2&=(2+\sqrt2)\,r ,\\ h_3&=(2+2\sqrt2)\,r ,\\ h_4&=(2+3\sqrt2)\,r ,\\ h_5&=(2+4\sqrt2)\,r ,\\ &\cdots \end{align}
This is a diagonal cross-section of the 14-balls pyramid: