Here's the story behind the shocking ending of 'Glass' (spoilers!)

Warning: This post and video contain major spoilers for the ending of Glass.

Audiences waited 19 years to witness the return of David Dunn and Elijah Price (Bruce Willis and Samuel L. Jackson), the hero-and-villain pair at the center of M. Night Shyamalan’s Unbreakable, a savvy deconstruction of comic book movies that hit theaters before comic book movies dominated the box office. Now with his long-awaited sequel, Glass, the director ventures into territory that most Marvel- and DC-derived superhero movies have feared to tread: killing off the bad guy and the good guy. Sure, Avengers: Infinity War dusted half the Marvel Cinematic Universe, but we already know that most of those heroes are coming back.

As Shyamalan himself indicates, there are no surprise resurrections in store for Glass‘s trio of superpowered casualties: David, Elijah and Kevin Wendell Crumb, aka the Horde (James McAvoy), who perish in the movie’s deliberately operatic climax. “I had it in my notes that I thought it should be a tragic opera,” he confesses to Yahoo Entertainment. “I didn’t know whether I’d be able to pull it off!” (Watch our video interview above.)

Specifically, the director wasn’t convinced whether he’d be able to create a tragic ending that could also incorporate a note of hope … one that didn’t involve a last-minute reprieve for any of the three main characters. Having spent much of the movie locked in a psychiatric hospital under the care of Dr. Ellie Staple (Sarah Paulson), Elijah, David and the Horde break free and seem poised to reveal themselves to the larger world via a good ol’ fashioned superhero/supervillain brawl.

But the good doctor has other plans; it turns out she’s part of a secret society — identifiable only by a telltale cloverleaf tattoo — that’s dedicated to keeping those with great powers from feeling the great responsibility to disturb the natural order of things. That requires her to oversee the executions of all three men, a grim mission that Paulson didn’t take lightly. “I had been such a fan of Unbreakable, and I didn’t want to be part of any character’s demise,” she tells us. “I loved them all so much, so it was a funny acting challenge to push all my feelings all the way over so I could do my job.”

For his part, Jackson says he didn’t feel especially emotional while filming Price’s death scene. “I die too much,” he says, laughing. “I don’t take any of it personally — it’s all fun.” Instead, he saw the logic in Elijah’s endgame. “It’s closure. I think Elijah is not above going on a kamikaze run to prove his point, and he doesn’t care how many people he takes down with him.”

In contrast, Anya Taylor-Joy had to hold back tears while filming McAvoy’s emotional death scene. The duo originally starred opposite each other in Split, which introduced Crumb and Taylor-Joy’s Casey, the girl who somehow cuts through Kevin’s mental clutter to get to his soul. “It was so painful for the two of us to experience that,” the actress remembers. “There was a very real sense of a chapter closing, and to have that end was special.”

To provide extra closure, McAvoy and Willis filmed their death scenes in their final days of shooting. “That was Bruce’s last shot,” confirms Spencer Treat Clark, who reprises his Unbreakable role as David’s son, Joseph. “It felt like this catharsis. We didn’t rehearse it, and it felt appropriate having this relationship with Bruce offscreen as well. That chapter of Glass was coming to a close.”

So where’s the silver lining in this dark cloud of an ending? Keep your eyes on Joseph and Casey, as well as Elijah’s doting mother (Charlayne Woodard). Ever the forward-thinking mastermind, Price ensured that the institution’s security cameras would record the David vs. the Horde throwdown, along with their deaths at the hands of the cloverleaf cult. That video is then uploaded for the world to see via their three loved ones, who are now a kind of super-team of their own … albeit without any superpowers.

It’s a final beat that Shyamalan credits not to a comic book but rather to the 1975 classic One Flew Over the Cuckoo’s Nest. That Best Picture winner ends with Jack Nicholson’s iconic rabble-rouser Randle McMurphy lobotomized, which forces Will Sampson’s silent Chief to make a daring escape. “The main character loses the battle and yet wins at the same time, because a side character takes up the journey and continues on,” the director says. “One could even argue that the last moments of One Flew Over the Cuckoo’s Nest is a superhero emerging. Maybe when I saw it as a kid, that’s what I was feeling.”

Glass is playing in theaters now.

Read more from Yahoo Entertainment: