Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Shader inheritance

Discussion in 'Shaders' started by Eduard, Feb 8, 2015.

  1. Eduard

    Eduard

    Joined:
    Nov 24, 2012
    Posts:
    10
    Hello,

    is it possible to derive a new shader from an existing shader by just replacing some vertex programs? Some sort of inheritance?

    Or do I have to duplicate the entire shader (Unity 5 Standard Shader) and replace all vertex programs accordingly?

    I already read the documentation, but couldn't find anything related to inheritance.


    Thank you for your help in advance

    Greetings Eduard
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Shaders don't really have inheritance in the OO sense. You have to think of it as C, not C++/C#.

    You can use includes to reuse code in multiple shaders. Unity does that and you can add your own includes.
     
  3. IC_

    IC_

    Joined:
    Jan 27, 2016
    Posts:
    61
    Why not just make a regular syntax like cg and make unity-related features (properties, blend, cull, pass) as macros then we could easily override Standard shader with stencil culling support just with few lines of code
    Code (CSharp):
    1.  
    2. // our stencil code
    3. Properties() {
    4.    Color2("Prop Name", fixed4(1,1,1,1));
    5. }
    6. Stencil(Ref=1, Comp=always, Op=Replace);
    7.  
    8. Pass() {
    9.   // do custom pass
    10. }
    11.  
    12. #include "StandardShader.cginc" // properties block gets merged into the existing one at the top and we get sort of inheritance
    13.  
    And we easy get a standard shader with custom prepass or something like that

    stencil is just an example, because right now I need to reimplement/passthrough standard shader to realize a feature. There are lots of another usecases of such "inheritance"