The eyes of a fly, squid, or human might seem like enormously complex pieces of engineering — and they are. But looking at the levels of homology within those eyes can help us better understand how such complicated structures evolve. Each step in the evolution of an eye involves slightly more complexity and a slight improvement on the ancestral version of the eye — so each was favored by natural selection.
Imagine an ancient worm-like ancestor of most modern animals, one of the earliest organisms to carry a rudimentary nervous system and a Pax6-like control gene for turning on a light-sensitive protein. Unlike its competitors lacking that critical innovation, this critter could crudely sense and react to light in its environment, allowing it to sense its depth in the ocean and better position itself relative to resources or avoid predators. Although this worm-like ancestor certainly didn’t have anything as marvelous and complex as the eyes sported by many animals today, its light sensitivity nevertheless gave it a distinct advantage over other organisms around at the time. Its descendants inherited that light-sensing genetic machinery.
Some of those descendants happened to evolve more specialized eye tissues (perhaps a lens-like ball of jelly, in addition to photoreceptor cells and pigmented screening tissue). This would have given them more fine-tuned light perception, allowing them to form crude images and perhaps detect potential prey nearby. Again, though no match for the complex eyes of a modern squid, those organisms with several specialized tissues for sensing light had an advantage over other organisms of the day and would have passed that innovation on to their descendants.
Finally, some of those descendants happened to evolve even more complex eye parts (perhaps a lens with special characteristics for forming sharp images and movable parts to control the amount of light entering the eye). Those innovations would have allowed even more accurate perception and likely an increased ability to detect prey and avoid danger. And again, those organisms would have had an advantage over their competitors.
Though the final products of this process, structurally complex eyes, are so integrated that it is hard to imagine how they evolved “in pieces” — looking into the deep history of eyes makes the different stages of this evolution clear.
Read more about the evolution of complex structures.