Robin Williams struggled to remember his lines.

This was unusual for the hyperverbal, Oscar-winning actor, and it hit him hard in Vancouver in 2014 during the filming of “Night at the Museum: Secret of the Tomb,” the third movie in the successful family franchise.

“He was sobbing in my arms at the end of every day. It was horrible. Horrible,” makeup artist Cheri Minns recalled. “I said to his people, ‘I’m a makeup artist. I don’t have the capacity to deal with what’s happening to him.’ ”

Minns suggested to Williams that he return to stand-up to get out of his rut and reclaim some of his lost confidence. But he refused.

“He just cried and said, ‘I can’t, Cheri. I don’t know how anymore. I don’t know how to be funny.’ ”

How could this be the same lovable weirdo who coined the greeting “Nanu nanu” as Mork from Ork; the radio DJ who famously brayed, “GOOOOOOD MORNING, VIETNAM!”; the widower therapist who broke our hearts in “Good Will Hunting”?

The reality — though Williams didn’t know it — was that he was suffering from a pernicious neurodegenerative disease that was in the process of robbing him of his talents, his brain and his very self.

This heartbreaking interaction is recounted in the biography “Robin” (Henry Holt & Co.) by Dave Itzkoff, out this month, which provides new details about the comic great’s final days and the harsh reality of what it’s like to lose a once-in-a-generation mind.

Robin McLaurin Williams, born in Chicago on July 21, 1951, had a privileged but lonely childhood, spending hours playing with toy soldiers in his attic. He attended Juilliard, then headed out West to blow up the Los Angeles and San Francisco comedy scenes.

Longtime friend Billy Crystal described seeing Williams kill it on stage: “It was electric, and we all just sat there and went, ‘Oh, my God, what is this?’ It was like trying to catch a comet with a baseball glove.”

Williams landed the guest role of Mork from Ork on the hit show “Happy Days” in February 1978. The character was so indelible, it led to a spin-off show, “Mork & Mindy,” which, by the following spring in 1979, reached 60 million viewers. Robin Williams was now a household name.

Despite rampant drug and alcohol addiction (he famously said cocaine was “God’s way of telling you you’re making too much money”), Williams easily found big-screen stardom. He earned an Academy Award nomination for his portrayal of the verbose Vietnam radio host in 1987’s “Good Morning, Vietnam.” Other critically acclaimed roles followed, including 1989’s “Dead Poets Society,” 1991’s “The Fisher King,” and 1997’s “Good Will Hunting,” which landed him an Oscar for his portrayal of a caring therapist to Matt Damon’s angsty genius.

‘He was sobbing in my arms at the end of every day. It was horrible. Horrible.’ - Cheri Minns

Williams also piggybacked from one moneymaker to the next as the face of comedy. He voiced the spastic, singing Genie in Disney’s 1992 film “Aladdin” and played a cross-dressing nanny in 1993’s “Mrs. Doubtfire” and a man trapped in a board game in 1995’s “Jumanji.”

Robin Williams could evidently do no wrong. But a series of commercial and critical flops followed — from the maudlin (1998’s “Patch Adams”) to the dark (2002’s “Death to Smoochy”) to the plain unwatchable (2009’s “Old Dogs”). Meanwhile, his long-running issues with drugs and alcohol resurfaced, and after his family staged an intervention, he checked himself into a rehab facility in 2006. In recovery, he met his third wife, Susan Schneider, whom he wed in 2011. (He married first wife Valerie Velardi in 1978, and the two had a son, Zachary, now 35. They divorced in 1988, and the next year, he wed his son’s nanny, Marsha Garces. The two were married for 19 years and had daughter Zelda, 28, and son Cody, 26.)

He was a “stimulus junkie,” whose anxieties came from his work. “The line of work he was in bred anxiety and self-centered concerns. He would always say, ‘You’re only as good as your last performance,’ ” Schneider says in the book.

The magical 1990s success eluded him as he jumped from one low-budget project to the next until CBS announced his return to the small screen in “The Crazy Ones,” which premiered in September 2013. He played Simon Roberts, an aging ad-agency founder, who must cede control of his business to his daughter.

But the magic wasn’t there.

“Williams seems exhausted,” one review read. “So is this show.”

That’s where the trouble began.

Williams began to complain about an array of symptoms: indigestion, trouble urinating, insomnia, loss of his sense of smell and heartburn. A slight tremor cropped up in his left hand, which was attributed to a shoulder injury.

Schneider describes the avalanche of symptoms: “It was like playing whack-a-mole. Which symptom is it this month? I thought, is my husband a hypochondriac? We’re chasing it and there’s no answers, and by now we’d tried everything.”

He dropped weight, his once-booming voice became tremulous, and he stooped. Producers and colleagues noticed the change.

Pam Dawber, Williams’s co-star from “Mork & Mindy,” joined the cast of “The Crazy Ones” to raise ratings. On set, Dawber saw a deeply changed man from the riotous and often inappropriate (she says Williams groped and flashed her on set) man she had worked with decades before.

“I would come home and say to my husband, ‘Something is wrong. He’s flat. He’s lost the spark. I don’t know what it is,’ ” Dawber says in the book.

CBS canceled the show after one season as ratings dropped from a high of 15.5 million to just about 5 million by the finale.

Friends thought Williams was depressed over the cancellation, but other strange behaviors started to emerge that couldn’t be so easily explained. He grew paranoid over a belief that people were stealing from him. On the set of “Night at the Museum,” he suffered a severe panic attack and was placed on antipsychotic medication.

Crystal described seeing his old friend after a four-month absence, finding him frail, skinny and, most disconcerting, “uncharacteristically quiet.”

When they said their goodbyes after dinner, Williams burst into tears.

“What’s the matter?” Crystal asked.

“Oh, I’m just so happy to see you. It’s been too long. You know I love you,” Williams said.

On May 28, 2014, Williams was diagnosed with Parkinson’s disease, a degenerative disorder that impairs motor functioning. Doctors assured him that they had drugs that could control the tremors and that he likely would have another “10 good years.”

Williams kept his diagnosis close to the vest, sharing it only with his children, family members and inner circle.

“I never heard him afraid like that before,” Crystal said. “This was the boldest comedian I ever met — the boldest artist I ever met. But this was just a scared man.”

To Williams, “it was the realization of one of his most deeply felt and lifelong fears,” Itzkoff writes. In a 1991 interview with Playboy during the publicity tour for “The Fisher King,” Robin discussed that latent fear two decades before it would touch him personally.

“There’s that fear — if I felt like I was becoming not just dull but a rock, that I still couldn’t spark, still fire off or talk about things, if I’d start to worry or got too afraid to say something . . . If I stop trying, I’d get afraid,” Williams said.

Two decades after he spoke those words, Williams was clearly afraid.

Williams went on an apology tour, begging for forgiveness (where none was demanded) from his three children for the way he treated them when he was in the throes of a drug addiction. He asked comedian Dana Carvey to forgive him for stealing bits (an allegation that dogged Williams earlier in his career), though Carvey insisted that he hadn’t.

Williams checked himself into the Dan Anderson Renewal Center, a rehab facility in Minnesota, to be “cloistered on a campus where he could receive close supervision, and where he could meditate, do yoga and focus on further 12-step work that, it was hoped, would help him manage his illness.”

But this wasn’t enough — this was not a drug addiction, it was a degenerative brain illness. He didn’t know it then, but there was little to be done.

In an article she later wrote for the journal Neurology titled “The Terrorist Inside My Husband’s Brain,” Schneider described what Williams was like:

“He had a slow, shuffling gait. He hated that he could not find the words he wanted in conversations. He would thrash at night and still had terrible insomnia. At times, he would find himself stuck in a frozen stance, unable to move, and frustrated when he came out of it. He was beginning to have trouble with visual and spatial abilities in the way of judging distance and depth. His loss of basic reasoning just added to his growing confusion.”

Still, sometimes Williams’s light would shine.

Comedian Mark Pitta recalls the shock of seeing Williams at the Throckmorton Theatre in Mill Valley, Calif., in July 2014. “He had a thousand-yard stare going,” he said.

But then in the green room, Pitta brought up service animals and told the story of a comedian whose dog would wake her up when she choked in her sleep. Without missing a beat, Williams said, “Oh, a Heimlich retriever.”

“It got a huge laugh. He just sat there and had a little smile on his face,” Pitta said.

Two weeks later, Williams settled down for the night at his San Francisco Bay Area home. It had been a rough day. Williams had fixated on his collection of vintage wristwatches, which he wanted to relocate for fear that they would be stolen. But that night he seemed calm and even offered his wife a foot massage before retiring to his own room.

“As we always did, we said to each other, ‘Good night, my love,’ ” Schneider recalled. “He seemed like he was doing better, like he was on the path of something,” she later said. “I’m thinking, ‘OK, stuff is working. The medication, he’s getting sleep.’ ”

When Schneider woke the next morning, she noticed the door to Williams’s bedroom was still shut. She told Itzkoff she was relieved he was finally getting some needed sleep.

When Rebecca Erwin Spencer, his 20-year-old assistant, asked how he was doing, Schneider optimistically answered, “I think he’s getting better.”

By 11 a.m., Williams had still not left his room, and Spencer became concerned. She used a paper clip to open his bedroom door and found a grisly scene: Williams had hanged himself with his belt.

While the world mourned the loss of the comedy legend, in the absence of any suicide note, questions surfaced: Had Williams been using drugs again? Was he depressed?

Three months later, the autopsy results came in. His brain had left its own suicide note.

The neuropathologist’s diagnosis was: “diffuse Lewy body dementia.”

The comedian did not have Parkinson’s, he had not fallen off the wagon and he was not severely depressed. It was something even graver: He suffered from an incurable brain disease that occurs when proteins build up in the brain’s nerve cells, impairing its function. It begins with memory problems and physical stiffness and graduates to extreme personality changes, psychiatric symptoms and eventually death.

Lewy body is the second-most common progressive dementia after Alzheimer’s disease. Unlike Alzheimer’s, in which sufferers have issues forming new memories, people with Lewy body dementia can form new memories but have a hard time retrieving them. It’s as if the very essence of Williams was still there — he just could no longer access it.

No one can truly understand what Williams faced during those final days, but Crystal provides some insight into what it must have been like for such a high-functioning mind to unravel so violently.

“I put myself in his place. Think of it this way: The speed at which the comedy came is the speed at which the terrors came,” Crystal said. “And all that they described that can happen with this psychosis, if that’s the right word — the hallucinations, the images, the terror — coming at the speed his comedy came at, maybe even faster, I can’t imagine living like that.”