One can legislate public morality, in the sense of criminalising harmful or anti-social actions. However, legislating personal morality is more difficult. Sumptuary legislation has always been a source of resentment and a cause of criminal behaviour - Prohibition in America was the proof absolute of this.
Religion, organised religion, was the way in which rulers tried to control personal behaviour. Priests were and are the original Thought Police. So while they did not necessarily sit down and invent the gods or God, they certainly decided what the deities approved and disapproved of.
This is why legislation to enforce tolerance and equal treatment of mintority groups has and will continue to fail in uprooting racism, sexism , homophobia etc. It is similar in intent and practice to sumptuary legislation and produces similar results
If we coud come up with a list or code of morals and ethics, and embed them into child-rearing and education in the way religion has been, we would do better. But it would take time and meet opposition.