In case you’re under the impression that the Old Testament is shy about human sexuality, here is a rated R story that is fully cancelable by modern standards.
Leah’s fourth son, Judah had three sons with Shua, a Canaanite, who named them Er, Onan and Shelah. A young woman named Tamar is married the eldest Er. Before they can have kids, Er offends God and God puts him to death.
In Middle Eastern tradition of the time, a childless widow sleeps her with husband’s brother in order to have a son that will receive the first husband’s name and inheritance. It was an important thing for a woman to have children for her security, both socially and financially.
Onan agrees to sleep with her, but does not want her to have a child because he doesn’t want to lose out on his brother’s portion of the inheritance or status. So he employs the pull out method to prevent Tamar from becoming pregnant. God sees what he did and kills him too. God is very much involved in these people’s lives despite them fully ignoring him.
Judah says Shelah is too young, so Tamar will have to remain a widow. Judah is afraid to let his youngest son get involved with her because his older sons have both died. Judah does not recognize that it is not Tamar’s fault, but his own boys’ bad behavior that led to their untimely demise.
Time goes by, and Tamar realizes that she is never going to get a child or her inheritance. While Judah is off sheering sheep, she devised a plan. She puts a veil over her face and sits by the road where Judah comes home. He thinks she is a prostitute and asks to sleep with her, but does not have a goat for payment. She takes his signet and chord as a pledge.
When Judah comes back with a goat, she is gone. He asks around if anyone knows where the cult prostitute went, but they say there is no cult prostitute. What is a cult prostitute, you ask? This is intercourse tied to pagan religious worship, although lacking a temple or altar makes them just regular people trading sex for goats.
Three months later, people notice that Tamar is pregnant and tell Judah that his widowed daughter-in-law has been immoral. He calls her out, she shows him his signet and cord. Judah says, “She is more righteous than I, since I did not give her to my son Shelah.”
Tamar has twins. While she’s giving birth, one puts his hand out and the midwife ties a red thread around it, but the other one comes out first. Tamar names the first born Perez, meaning breaking out, and the one with the thread Zerah, meaning dawn. Tamar is in the lineage of both King David and Jesus.

The thread is to mark the firstborn, and for good luck. 
