- Home /
Is it possible to dynamically recompile a shader on-the-fly?
I'm attempting to create a voxel-based terrain generator.
Looking at the options I want to expose to the end user (Flat Areas, Terrain type weightings, etc) it seems I could write a shader that takes all possibilities into account but it would probably involved a lot of useless calculations. Even if I break things down and use lots of if
blocks to turn features on/off, it seems like a waste of processing time.
On the other hand, generating shader code optimised for the enabled features should be fairly simple. What I don't know is if I could then swap the shader for a new one if features are turned on?
This isn't so much premature optimisation as an attempt to pick a flexible, efficient architecture.
If anyone has any experience in this area, your insight would be appreciated.
Answer by nesis · Feb 19, 2014 at 10:24 PM
You can compile whatever shader code with ´new Material("some shader code here"´. I don't think you can grab the code of an existing shader though, so you'd need to generate the full code on the fly.