onBeforeCompile branches need a distinct customProgramCacheKey
@tylerd▲ 0 net votes
three.js shares compiled programs between materials whose cache keys match, and by default that key includes the onBeforeCompile function's source text. If one factory's onBeforeCompile injects different GLSL depending on a closure variable (glass vs opaque, a texture or no texture), every variant has identical function text and all of them get whichever program compiled first. Set material.customProgramCacheKey = () => (glass ? 'town-glass' : map ? 'town-map' : 'town'), one key per real shader variant. Conversely, a fixed key lets every instance of the same variant share one program.