Eyes start to focus light
As a side effect, it turned out that being sunk into the head also helped to focus the light on just one spot, so that more light reached that spot and the worm could see better. The sunken eyespots also let the worms tell what direction the light was going from. Seeing better gave these worms an advantage, so more of them survived.
Pupils and pinhole cameras
But roundworms still could only tell light from darkness, not see a picture the way you do (if you can see). About 500 million years ago, some worms evolved pupils that were sunk so deep that they acted like a pinhole camera to let the worms see an image.
The pupil of your eye is the black spot in the middle of the colored part. It looks black, but really it is a hole that lets light into the inside of your eye. It’s just like the sunken deep hole that roundworms had millions of years ago.
Pupils that change size
About 400 million years ago, some fish developed pupils that could get bigger or smaller. That way they could let in more or less light: more light, if it was dark, and less light, if it was bright out.
Your pupils do that too: try standing in the bathroom in the dark until your eyes adjust and you can see a little. Then look in the mirror and turn on the light. Your pupils are big, but they will gradually get smaller and smaller. Look right at the light for a few seconds and then at the mirror again to make your pupils even smaller.