Phenotypic plasticity in response to environmental heterogeneity may be performance enhancing, or may simply result from an intrinsic instability in morphology during development. Although patterns of morphological change are well documented for numerous taxa, in many cases it remains unclear whether such plasticity confers a fitness advantage. Reef-building corals are an ideal model system in which to investigate this question. Here we develop a three-dimensional geometric model to predict light harvesting by colonies with complex morphologies, and combine this model with an experimentally calibrated photosynthesis model to predict energy acquisition. This allows us to assess the adaptive significance of phenotypic plasticity by predicting the optimal morphology across an environmental gradient and comparing the extent to which observed morphological variation conforms to the predicted optima. Our results provide the first evidence that phenotypic plasticity in foliose corals optimizes photosynthetic energy acquisition. However, the optimal colony morphology is more constrained at the boundaries of the resource niche. At intermediate resource levels, flexibility in physiology allows multiple colony morphologies to have nearly equal fitness. Our results highlight the importance of phenotypic plasticity on multiple scales. Long-term changes in the overall shape of whole colonies is important at niche boundaries where conditions are consistently more stressful, whereas localised, short-term physiological variation within colonies is important in intermediate and less predictable habitats where a rapid and reversible response to environmental fluctuations is beneficial.