Search Unity

Question Converting shader graphs to editable, usable shader code?

Discussion in 'Shader Graph' started by SgerbwdGwyn, Jul 11, 2019.

  1. SgerbwdGwyn

    SgerbwdGwyn

    Joined:
    Dec 2, 2013
    Posts:
    25
    I'm trying to make a gradient-lighting/banded-lighting shader that actually uses per-pixel lighting to determine which "band" of illumination to fall into (pretty much just cel shading). I have old shader code from another engine that simply rounds the illumination value to the nearest band, but obviously this is only doable directly in the shader after the lighting is calculated.

    Is there a way I can compile my shader graph to a usable, editable shader of some kind?
     
  2. alexandral_unity

    alexandral_unity

    Unity Technologies

    Joined:
    Jun 18, 2018
    Posts:
    163
    On the master node, you can right click and select "Show Generated Code" and save the output to a regular shader file. The format will be different than usual shader lab shaders so I recommend checking the documentation for whichever render pipeline you're using.
     
  3. SgerbwdGwyn

    SgerbwdGwyn

    Joined:
    Dec 2, 2013
    Posts:
    25
    Thanks, I'll try this
     
  4. jtjones27

    jtjones27

    Joined:
    Mar 16, 2021
    Posts:
    1
    sorry to drag up an old threat, but is it possible to do this in reverse? I.e. import shader code into shader graph?
     
  5. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    No, unfortunately not (unless a 3rd party plugin exists that I'm unaware of). However, if parts of you shader are hard/annoying to replicate in shader graph, you could consider putting parts of it in custom function nodes.
     
  6. BigGameCompany

    BigGameCompany

    Joined:
    Sep 29, 2016
    Posts:
    112
    How do we do this in Unity 2021? The master node has been replaced by the master stack which doesn't have the same right click options. I selected the shader in the project panel and clicked on "View Generated Shader" in the inspector but the generated code has errors.

    Thanks
     
    Genebris and raul3d like this.
  7. Genebris

    Genebris

    Joined:
    Mar 18, 2013
    Posts:
    144
    I copied generated shader into another project and it fails to compile. Is there any workaround?
    I need to create a simple shader for Unity 2019 where Shader Graph package is very old.
     
  8. GameDeveloper1111

    GameDeveloper1111

    Joined:
    Jul 24, 2020
    Posts:
    100
    What worked for me in Unity 2020.3.14f1 and URP 10.5.1:

    upload_2022-4-5_20-20-42.png

    Then copy the generated code to a blank .shader file, so your changes don't get overwritten by subsequent code generation.

    This didn't create any errors for me. It also allowed me to insert a custom pass with custom inspector-exposed properties.
     
    bb8_1, ShadowAngel, mandisaw and 5 others like this.
  9. Magnilum

    Magnilum

    Joined:
    Jul 1, 2019
    Posts:
    241
    Hi, I am reviving this topic cause I have a question.

    I am making a tool for unity and I would like to use shaders. I have made a shader graph of course, it needs ShaderGraph package to work. If someone install my package, does it mean the user has to install the package to use the tool of the shader is written normally in a shader file?
     
    ghostboots likes this.
  10. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    78
    One thing to remember, you still need shader graph installed to use the generated shader. Otherwise it creates errors.
     
    Magnilum likes this.