The teenage gunman who opened fire on a California garlic festival posted hate-filled messages against mixed-race people and said he was “angry” just before his rage boiled over into bloodshed, it emerged Monday.

“Why are you doing this?” one unidentified attendee of the annual Gilroy Garlic Festival gathering could be heard yelling seconds after Santino William Legan, 19, opened fire with his AK-47-style rifle at around 5:40 p.m. Sunday.

“Because I’m really angry,” came the response, according to survivor Jack van Breen, whose rock band, Tin Man, had just launched into an encore in the final hours of the three-day festival when Legan opened fire.

Moments after the chilling exchange, Legan was shot dead by cops, putting an end to a rampage that killed three people — including a 6-year-old boy and 13-year-old girl — and wounded a dozen more.

With Legan no longer around to lift the veil on what fueled his rampage, local and federal investigators were still searching for a motive Monday.

That includes a review of body-camera footage and extensive interviews with Legan’s friends and family — who are well known in Gilroy, the quaint Northern California town of 50,000 people, where he lived.

Legan’s apparent Instagram account provided just a glimpse into his hatred.

“Ayyy garlic festival time,” read the caption on one Instagram photo of the event posted shortly before the attack by a since-deleted account bearing Legan’s name. “Come get wasted on overpriced s–t.”

A caption on a second photo posted a short time later heaped praise upon an obscure 19th-century manifesto embraced by white supremacists for its vile views.

“Read Might is Right by Ragnar Redbeard,” urges the caption, before the writer injects some bigotry of his own: “Why overcrowd towns and pave more open space to make room for hordes of mestizos and Silicon Valley white tw-ts?”

“Mestizo” is a slur against people of mixed racial heritage, often white and Hispanic.

That caption was attached to a photo of Smokey Bear holding a sign with a macabre double entendre: “Fire danger high today!”

Also under the microscope are Legan’s moves in the weeks leading up to his murderous outburst.

Although he was raised in Gilroy — which is about 80 miles southeast of San Francisco — family neighbor Jan Dickson told reporters that she hadn’t seen him for at least a year.

Another neighbor told San Jose’s Mercury News that she exchanged pleasantries with Legan outside the home just last week.

“You don’t know what’s going through the minds of people,” said Elia Scettrini, recounting the outwardly friendly interaction.

Gilroy Police Chief Scot Smithee confirmed that investigators believe Legan had been staying for some time with relatives in Nevada, where he legally purchased the rapid-fire assault weapon on July 9.

FBI agents searched a triplex in unincorporated Walker Lake, Nev. — a town with a population of 275 that’s just over the California border — believed to have been used by Legan in the days before the shooting, according to the local district attorney’s office.

In Gilroy, SWAT officers raided Legan’s family’s two-story home late Sunday, augmented by a growing army of investigators that labored through the night.

Authorities at the home — less than a mile from the grounds where the Garlic Festival is held — were seen Monday hauling out several paper bags apparently containing evidence, and also scouring a dust-coated car parked outside.

A 2017 profile in the Gilroy Dispatch describes how Legan and his three brothers were coached in boxing by their fitness-nut dad, Tom Legan Jr., out of a home gym.

One brother, Rosino Legan, was good enough to compete under a national spotlight, fighting in the 2014 USA Boxing National Championship, according to the Dispatch.

Their grandfather, Tom Legan Sr. — a West Point-educated US Army captain who served as a nuclear weapons officer along the 38th parallel separating North and South Korea — was a prominent figure in local politics, according to his 2018 obituary.

He served two terms as the supervisor for Santa Clara County, of which Gilroy is a part, but lost a re-election bid in 1988 as he successfully fended off allegations that he molested a young female relative.

One neighbor on the block expressed relief Monday that he and his family likely dodged a fateful trip to the Garlic Festival because they instead opted to take in a baseball game.

“Otherwise, we most likely would’ve been there. It hurts,” Andrew Sanchez, 19, told The Mercury News. “It makes everyone sick.”

Other neighbors almost uniformly described both the family and Legan himself as cordial and keeping to themselves.

Speaking early Monday at an unrelated White House ceremony, President Trump blasted Legan not as the quiet son of a connected family raised on a well-kept street, but as the destroyer of American lives.

“While families were spending time together at a local festival, a wicked murderer opened fire and killed three innocent citizens, including a young child,” Trump said.

Smithee, the Gilroy top cop, preferred that Legan be lost to time altogether.

“I say that name with some hesitation,” Smithee said during a press briefing, shortly after publicly identifying the killer for the first time. “I don’t believe someone like this deserves notoriety.”

With Wires