Search Unity

Creating shader at runtime

Discussion in 'Scripting' started by jesta, Sep 25, 2012.

  1. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    294
    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):
    1. Material mat = new Material(shaderText);
    2. 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.
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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.
     
    SamFernGamer4k likes this.
  3. africanrhino

    africanrhino

    Joined:
    Aug 15, 2013
    Posts:
    11
    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.
     
  4. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    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
     
  5. africanrhino

    africanrhino

    Joined:
    Aug 15, 2013
    Posts:
    11
    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.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Just because you cant think of a need, doesnt mean there isnt.

    I've used them.
     
    SamFernGamer4k likes this.
  7. godbian

    godbian

    Joined:
    Aug 3, 2020
    Posts:
    4
    I see. So, is it possible to download some compiled shader from dlc and load it in runtime?
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,799
    Start a new thread, the Unity person you're asking this from no longer works at Unity.
     
  9. godbian

    godbian

    Joined:
    Aug 3, 2020
    Posts:
    4
    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.
     
  10. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,799
    Go to the appropriate forum and click "Post New Thread" and describe your issue.