DALLAS — The heavily armed sniper who gunned down police officers in downtown Dallas, leaving five of them dead, specifically set out to kill as many white officers as he could, officials said Friday. He was a military veteran who had served in Afghanistan, and he kept an arsenal in his home that included bomb-making materials.

The gunman turned a demonstration against fatal police shootings this week of black men in Minnesota and Louisiana from a peaceful march focused on violence committed by officers into a scene of chaos and bloodshed aimed against them.

The shooting was the kind of retaliatory violence that people have feared through two years of protests around the country against deaths in police custody, forcing yet another wrenching shift in debates over race and criminal justice that had already deeply divided the nation.