Given that the first three “Shrek” movies have made more than $2 billion, Friday’s “Shrek Forever After,” seemed as inevitable as the green ogre’s next fart joke.

Yet if back in 1996 you asked DreamWorks — founded by Stephen Spielberg, Jeffery Katzenberg and David Geffen — what the studio thought of the grouchy green giant, the answer would be bleak. You would hear that “Shrek” was the “ugly stepchild” of the studio’s animation wing, a movie that would never see the light of day, let alone an Academy Award.

Before its 2001 release, DreamWorks considered “Shrek” a low-budget boondoggle, a project to which animators were banished once they failed on other projects, according to the new book, “The Men Who Would be King: An Almost Epic Tale of Moguls, Movies and a Company Called DreamWorks.”

“It was known as the Gulag,” one animator told author Nicole Laporte. “If you failed on ‘Prince of Egypt’ [a DreamWorks movie that later flopped], you were sent to the dungeons to work on ‘Shrek.’ ”

Employees called it being Shreked.

The project bore few traces of what it would become. “ ‘Shrek’ was essentially the story of the ugliest guy in the world who meets the ugliest woman in the world — they get together and have the ugliest children,” said an animator who stormed out of the story pitch in disgust.

Based on a 30-page children’s book by William Steig, its budget was capped at $20 million by Katzenberg, who also limited the number of characters to 17 and insisted on hiring recent college grads. Working out of a hideous warehouse complex in Glendale, Calif., they were nicknamed “Propellerheads,” doomed to toil at motion-capture animation, widely considered inferior to traditional 2-D drawing techniques. Laporte tells The Post it all added up to “degenerate status” at DreamWorks.

A revolving door of directors, writers, animators and actors didn’t help, either. The original screenwriters were replaced by Joe Stillman (“Beavis and Butthead Do America”) and Roger Schulman (“Balto”). Director Henry Selick (“The Nightmare Before Christmas”) gave “Shrek” a shot, but was replaced by two first-timers, Andrew Adamson and Vicky Jenson.

Turns out the movie’s star wouldn’t last long, either. “Saturday Night Live” alum Chris Farley, hot off the low-budget hits “Tommy Boy” and “Black Sheep,” had been tapped to play the ogre.

“I found [Farley’s] wild energy exhausting but really funny,” said animator Tom Sito. “He was constantly flushed, bouncing off the walls, sweating heavily and looking like he was about to burst out of his clothes.”

Farley’s physicality soon seeped into Shrek’s. “Chris was a huge influence. He was the perfect role model,” said Ken Harsha, another animator. He started to look like “Chris as an ogre,” with a big nose, “funky ears,” and “Tommy Boy-type hair.”

But when Katzenberg saw a one-minute test, he was appalled. The characters looked unrealistic, more like the early Simpsons than the refined Disney characters with which they were competing. Production was stopped and the crew of 40 cut loose. DreamWorks had blown millions of dollars with nothing to show.

Some 18 months later, at the tail end of 1997, Farley was dead, thanks to a mix of cocaine, morphine and heart disease. “They had a moment of ‘Do we scrap the project?’ ” Laporte says.

Instead, Katzenberg shifted production to a computer generated imagery shop in Northern California, giving Shrek a more lifelike feel. More importantly, he landed Mike Myers, fresh off of “Austin Powers,” as the lead. The studio also signed Eddie Murphy to play the irreverent, obnoxious Donkey character.

“You’d pitch him a sequence and you’d show him the pages, and he’d read it very quietly, just kind of to himself. And then he’d step in front of the microphone and just — bam! — instantly, it’s Donkey,” director Adamson said of Murphy. “He’d come up with stuff we’d never even imagined. He’d take a single-beat joke and turn it into a three-beat joke.”

Cameron Diaz, yet to break out in “There’s Something About Mary,” was cast as Princess Fiona. The three actors were paid $350,000 each, a relatively modest sum.

Even with a talented cast in place, the movie progressed in fits and starts. To match Myers’ style, Shrek shed some of his Tommy Boy looks and took on bushier eyebrows and a large, square-shaped face.

After recording dialog in his native Canadian accent, Myers insisted on changing to “the Scottish accent of somebody who’s lived in Canada for 20 years.” It was an impulse that came with a huge price tag.

After much deliberation, Katzenberg “choked out a yes” and green-lighted what may be the best-spent $4 million in Hollywood history. Shrek as we know him was born.

Still, filmmakers were anxious when “Shrek” screened at the Cannes film festival, just days before its May 16 release in the US. “Here we are, sitting in tuxedos and evening gowns, wearing borrowed jewels, and everyone’s watching Shrek take a poot in the water,” said co-director Jenson.

The audience didn’t know what to make of the madcap send-up of Disney fairy tales. Nobody laughed. At least not at first.

“For the first 10 minutes — nothing,” Katzenberg recalled. “My heart was thumping, my forehead was breaking out in a sweat. I said to myself, ‘They’re going to burn the place down.’ ” But by the time Shrek and Fiona celebrated their swamp wedding, the insiders at Cannes had been won over. “Shrek” got a standing ovation and went on to gross $42 million its first weekend — Dreamworks’ biggest take ever.

Ultimately, the film generated $484 million in worldwide sales and received the first-ever Oscar for Best Animated Feature. For the sequel, Katzenberg ponied up $10 million to each of the actors for a week’s worth of voice work. A $919 million worldwide take surpassed “Finding Nemo” as the highest grossing animated film of all time.

“‘Shrek’ completely came out from behind,” Laporte says. “It stumbled lifelessly along for years and years and years. Everything that could have possibly gone wrong, did. No one expected it to do as well as it did.”