If you had a lucky childhood, tucked away in a corner of your closet was a gigantic collection of loose Legos. It was the culmination of dozens of kits and construction sets, collected over the years and dumped into a single bin. Paw through the bits and pieces long enough, and you could create anything: Spaceships. Submarines. Medieval castles. Western ranches.

But soon, hobbyists won't be limited by what's inside those bins. They'll be able to craft their own bricks, thanks to 3D printers that make fabricating those plastic parts as convenient as going to Toys R Us. With such technology, entire structures can now be reverse-engineered, reduced into a pile of components and snapped together in minutes.

You see the danger here for Lego. 3D printing may prove to be one of the biggest tests the company has ever faced. Unlike the rise of PCs and tablets, which merely demanded that Lego invest in new digital products, 3D printing strikes at the heart of Lego's core business. Manufacturing small bits of plastic is, in fact, what 3D printers do best.

Lego doesn't see it that way — as Roar Rude Trangbæk, a company spokesperson, told me, it's a lot harder than it looks to produce high-quality bricks.

That may be true for now. But it rarely pays to bet against improvements in technology — meaning that this 5,000-piece puzzle isn't going away.

Outside the company, Legos and 3D printing are a natural fit for each other — sometimes literally. Designers like Stefanie Mueller have started using Legos as part of their prototypes. For large items, Mueller developed a program called faBrickator, which converts a 3D design image into a homegrown Lego project, complete with instructions. Only the crucial parts, the ones that need constant retooling, are reserved for the 3D printer. According to the German Ph.D student, using a combination of Lego blocks and smaller 3D-printed pieces can cut a 14-hour production process down to 67 minutes.

If you can convert a design file into a Lego set, it's not much of a leap to imagine printing the blocks themselves. There are already 3D-printable bricks that act as adapters between Lego pieces and non-Lego pieces — a project that's raised questions about the limits of Lego's intellectual property rights.

Lego's chief marketing officer, Mads Nipper, seems conscious of 3D printing's potential.

"Is it a threat?" he told the Financial Times. "An opportunity?"

Lego is hardly unfamiliar with the technology; it uses it to test its own new products. In one example, said Trangbæk, the company used 3D printing to develop the Lego versions of Homer Simpson and his family.

But Lego doesn't see rapid prototyping as a replacement for the sophisticated molding process it currently uses to produce 55 billion Lego pieces a year. Plastic granules get heated to as much as 590 degrees Fahrenheit before being injected into molds at high pressure; the result is a building block that snaps together and holds like glue yet can be easily dismantled by young hands.

"If you do it a little bit wrong, the bricks might not stick together, or maybe they'll stick too well and the child won't be able to pull them apart," said Trangbæk.

The implication, of course, is that no 3D printer can hope to match that level of precision or consistency in a single piece, let alone at scale. The current limitations of 3D printing certainly suggest Lego deserves the benefit of the doubt. But replicating the Lego formula is mostly a technical problem, subject to trial-and-error — which is incidentally just the type of problem that rapid prototyping is good at solving. It's only a matter of time before somebody figures it out.

In the short run, 3D printing might have even greater downstream effects for Lego's low-budget imitators. Where the Danish company designs its plastic to flex no more than a thousandth of a millimeter, construction bricks made by other companies tend to be much flimsier. An explosion in rudimentary homemade Legos would pose a much greater threat to those firms first.

Lego also has a major advantage over other competitors in its licensing prowess. The company began producing Star Wars-themed construction kits in 1999, with the release of "Star Wars: The Phantom Menace." Since then, its deals have expanded to cover Spiderman, Batman, Toy Story and the Pirates of the Caribbean franchises, among others.

In a sign of how large Lego has become, Trangbæk told me that as profitable as those franchises might seem, only the Star Wars line made it into the top five best-selling Lego themes last year. The others were all original properties, like Lego City and its toddler-themed line, Duplo.

Lego could try to turn this popularity to its advantage. It could use 3D printing to broaden its distribution reach, allowing people to print their Lego sets themselves after purchasing a digital copy of the instructions, for instance. Or perhaps, in an inverse of what 3D-printed firearms makers have done, Lego could make sets that contain a must-have piece that cannot produced on a 3D printer.

Trangbæk said the company has no intention of doing any of that. The instructions included in Lego sets are just meant to help newcomers get started; the real fun — as most of us have discovered — comes after we've taken everything apart again and start to build original things in interesting new combinations.

"If you have six classic two-by-four bricks, that can be combined in more than 950 million ways," he said. "That's if the bricks are all of the same color. If you combine all the colors, it's even more combinations. That is the strength of what we're putting on the market."