I'm trying to create a shader at runtime from a text file (a string) in an editor script. Here's my current method : Code (csharp): Material mat = new Material(shaderText); myPlane.renderer.sharedMaterial = mat; Now, this gives me the generic "Parse error: syntax error at line CGPROGRAM" and my material has no shaders assigned. However, when I copy the content of shaderText in a shader file, the shader works properly. Has it something to do with shader compilation? Does anyone have a clue? Thanks for your help.
Yeah, the runtime functionality of creating shaders is quite limited; you can't have CGPROGRAM for example. So that leaves only fixed function shaders. It's like that because we really don't want to include 50+ megabytes of shader compilation libraries into the actual game build. Shaders are meant to be imported in the editor as assets, at which point you can reference them from runtime code.
Two years later and I'm posting here .... (Runtime shaders still not here) Surely it should be the builders choice wether or not to include the minuscule 50+ Megabytes? At the very least, if unity isn't going to allow us to include the compilation process, give us a way to compile it ourselves and send the shader without the need to patch. Some developers and platforms force you to download the whole app again which could mean gigs of data. One could easily argue that runtime shaders are a must in programs that are dynamic content driven. There are many practical usage case situations in which one does not know beforehand which technique will be used to generate an effect in the look or feel of a scene where it is not feasible to patch for that shader . User and procedurally generated content are good areas where users could gain tremendously from the ability. Even web browsers have runtime shaders these days! Glsl has basically become the standard for this in internet browsers and now in vr internet browser.
This is not an opinion for this thread, just an honest question: for what do you think are dynamically compiled shaders needed? I just can't think of anything
To use the example from above, vr content. Unity is sold as a vr dev platform. Look at janusvr , used to be called firebox. You can't build that in unity. Well, you can build most of it except for the shader support. Another area dynamic shaders would be awesome is in app purchases. Being able to send new shaders without needing to push out an update to the whole user base on all the platforms would save a whole lot of cost from bandwidth and vetting.
emmmmm, thank you for your reply. But can be more specific? I have no idea how to dynamic load a compiled shader, cause i can not found relative interface to do it.