Shia LaBeouf, Megan Fox and Megatron may be the center of publicity on “Transformers: Revenge of the Fallen,” but it’s the supporting cast of Michael Bay’s bombastic sequel that’s the real coup.

The DreamWorks-Paramount sequel, which launches June 24, features four of the five branches of the U.S. armed services in action — an assembly that’s being touted as a first for Hollywood (only the Coast Guard sits this one out).

Hollywood and the military don’t typically march as closely together, and it’s sometimes surprising which films have enjoyed military cooperation — the patriotic “Independence Day” went without assistance while the Army-mocking “Stripes” agreed to heavy rewrites in exchange for full cooperation — but these days showbiz and the military are finding ways to marshal their forces for mutual benefit.

The relationship between the two is often a reflection of the times: Years after Vietnam ended and soon after Iraq’s major battle action concluded, Hollywood pics cast combat and its effects in a harsh light, resulting in less aid and cooperation from the armed forces. Films such as “The Deer Hunter” and “Apocalypse Now” painted a grim view of Vietnam, and in recent years, pics like “In the Valley of Elah” and “Home of the Brave” presented the military as brutal and emotionally scarring.

Now, as a new president moves to extricate forces from Iraq and the “support our troops” refrain remains part of the zeitgeist, “Transformers: Revenge of the Fallen” has drawn heavily on the U.S. military’s machinery, personnel and logistics.

“This is probably the largest joint-military movie ever made,” boasts Army Lt. Col. Greg Bishop, who served in Iraq before being appointed to interface with Hollywood last year. “If you go down the list of military movies, ‘Black Hawk Down’ was just about all Army, ‘Top Gun’ was all Navy, ‘Iron Man’ was predominantly Air Force.”

But in the real world, those branches seldom operate alone. “Soldiers on the ground love to look up in the sky and see fighter jets over their shoulder,” says Bishop, who was excited to support the rare movie of sufficient scope to show all four branches working together.

The pic showcases Marine hovercrafts, Navy subs and nearly every kind of Army helicopter and Air Force plane in service (from the Frisbee-topped E-3 Sentry to a retired SR-71 Blackbird that transforms into the Decepticon character Jetfire) — all coordinated through special arrangement with the Dept. of Defense.

Any film or TV project can apply for U.S. military support, but not every one gets the D.O.D.’s seal of approval. There are considerations of both logistics and tone.

“It’s really about the script and what is being requested of us,” says Pentagon film liaison Phil Strub, who has served as the key contact with the entertainment industry for two decades.

Geographic factors are one of the main reasons Strub must pass on dozens of requests his office gets a year. Because Steven Spielberg shot “Saving Private Ryan” in France and the U.K., the Pentagon was unable to offer assets to the production. HBO’s “Band of Brothers” had to find equipment and extras elsewhere for the same reason.

Though he also filmed in Egypt and Jordan, “Transformers” director Michael Bay drew the U.S. military’s help by shooting on bases in California, Arizona and New Mexico, with some of them doubling for the Middle East. And because the White Sands Missile Range is an active Army testing facility, professionals operating Bradley Fighting Vehicles could fire live tank rounds into the set.

Orson Welles famously said that making movies was “the best electric train set a boy ever had,” and as much as Hollywood loves playing with the military’s multimillion-dollar toys, the military seems to like playing in Hollywood, too.

In the “Transformers” sequel, each branch of the military got an opportunity to show off its equipment, conduct and philosophy.

“I suspect most American citizens could never accurately describe what it’s like to be a soldier in today’s Army. They get their perception of the Army through the media, so our job is to educate the American people on who we are,” Bishop says. “At the end of the day, they are the stockholders.”

And there’s nothing like a Michael Bay movie to make servicemen swell with pride, civilians think about enlisting and rival despots quake in their boots. “Recruiting and deterrence are secondary goals, but they’re certainly there,” concedes Capt. Bryon McGarry, deputy director of the Air Force’s public affairs office.

Each branch of the U.S. military maintains a satellite office in Los Angeles to liaise with the entertainment industry, on everything from giant-robot action movies to documentaries to series such as “NCIS” or “Army Wives” (the Army even lent a hand when a soldier appeared on “America’s Got Talent”).

Hollywood has every incentive to seek the military’s blessing.

A film like “Transformers” gets much of the access, expertise and equipment for a fraction of what it would cost to arrange through private sources, with the production on the hook only for those expenses the government encounters as a direct consequence of supporting the film (such as transporting all that megabucks equipment to the set from the nearest military base).

“We had a lot of hardware in this movie,” says “Transformers” producer Ian Bryce. “You have to budget it as if you’re going to pay for everything, because you don’t know what you’re going to get for free. Certainly the fuel costs for aircrafts are extremely high.”

The production, however, doesn’t pay location fees or military personnel salaries though servicemen can take time off to serve as extras — a plus since they know how to carry a gun, wear a uniform and follow orders.

“It is an interesting thing for military men and women to be on a set, even if it is boring. Most of these folks will never have that opportunity in their whole lives,” Strub says.

With a bit of creativity from the commanders involved, some maneuvers have actually been designated as training exercises and offered at no cost to the filmmakers. McGarry remembers a day at White Sands when a formation of six F-16s popped flares over the set, simulating a low-level, air-to-ground attack. “The flyover was very much the type of training the Air National Guard out of Kirtland AFB does every day. Only that day, Michael Bay and his cameras had a front-row seat to the air power show,” he says.

Given the extent of the logistics required to mount such cooperation, the process of tapping military resources has to begin at the script stage. In Bay’s case, he relies on GSGI president Harry Humphries, a retired Navy SEAL turned Hollywood consultant and Bay team member since “The Rock.” Before reaching out to Strub at the Pentagon, Humphries vets scripts for realism and plausibility — even if those aspects are stretched a bit in Hollywood’s tentpoles.

“In ‘Transformers,’ we’re fighting alien robots, so realism is obviously out the window,” says the Army’s Bishop. And yet, if the military were ever to face such a threat of that scale, “this is how we’d do it,” he says.

Military consultants offer notes on everything from jargon to the portrayal of their respective branches. While Strub insists they are not censors, the Pentagon can ask for changes to a script and has been known to refuse cooperation on some projects.

In general, genre films get more creative leeway than dramatic or historical re-creations (controversial details in the Cuban Missile Crisis drama “Thirteen Days” were a dealbreaker for the DOD).

They also balk at other factors: Alien-invasion blockbuster “Independence Day” was rejected because the military fumbled the defense of Earth, only to have an alcoholic cropduster save the day.

On the other hand, Steven Spielberg’s “War of the Worlds” went about E.T. defense the right way, depicting a positive esprit de corps among soldiers and marines overpowered by the Tripods.

“That big battle scene at the end was going to be different,” Strub says. “We just wanted the case made that the Marines understood that they were not going to prevail, but they were nobly sacrificing so the civilians in that valley could escape.”

Military brass bristle at depictions that suggest cover-ups or conspiracies in which characters must call the news media because they face indifference from within, but it’s OK to have fallible characters who make poor decisions. “We are absolutely not afraid of showing those realities, provided that the system works and there’s a consequence to that action,” Bishop says.

The Army lent its support to the indie “The Dry Land,” with America Ferrera, about an Iraq vet who suffers the consequences of Post-Traumatic Stress Disorder. “We really felt that one of our biggest challenges dealing with troops with PTSD is getting them to get help,” says Bishop.

And some depictions seem to get under the military’s skin more than others. Much of Strub’s job amounts to correcting misconceptions propagated by storytellers who subvert accuracy for dramatic effect.

“There’s an enduring stereotype of the loner hero who must succeed by disobeying orders, going outside the system and doing things his or her way because the rules are stupid,” Strub says. “That’s one of the things we point out. If you’ve got somebody who’s a disciplinary problem, that’s probably the last person you’d want to assign to a high-profile White House job.”

By contrast, the “Transformers” sequel embodies the military philosophy that teamwork is essential to success. It depicts a joint effort between not only the U.S. armed forces but also military from Jordan, Egypt and the U.K. (though the U.S. flags are always largest).

“It was very much a U.S.-centric military operation last time, so I think Mike’s idea was for the movie to be more global this time around,” Bryce says.

Even though Oscar’s first best picture winner, “Wings,” was made with extensive involvement from the War Dept., Strub remembers a time when it looked like Hollywood might not need the military going forward.

“I was here when CGI really came into its own,” he says. “I figured with filmmakers able to use that technology to create their own military equipment, we would probably be seeing a diminishment of our involvement other than story, research assistance and that kind of thing.”

But filmmakers (and gadget freaks) like Bay still insist on the real thing. And close contact with genuine military professionals has augmented his respect for the armed forces (the logistical similarities between film and military operations certainly aren’t lost on either side).

“Michael initially had a typically Hollywood attitude toward how things should look, but he’s learned a lot in 10 years,” Humphries says. “I would say ‘Pearl Harbor’ was his turning point.”