Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Change alpha value of the Standard Shader

Discussion in 'Shaders' started by Hellium, Jul 4, 2017.

  1. Hellium

    Hellium

    Joined:
    Sep 25, 2014
    Posts:
    43
    I want to edit the Standard shader provided by Unity in order to change the alpha value of a fragment (pixel) according to given parameters I pass to the shader.

    For now, thanks to this link, I have been able to :

    1. Retrieve the standard shader,
    2. Retrieve a custom `Core.cginc` I included in the `DEFERRED` pass of the custom shader,
    3. Create a custom Editor script to handle the Shader and my custom parameters,
    4. Selecte `Fade` as the rendering mode of the shader to handle transparency, using the custom editor script,
    5. Create a custom file replacing UnityStandardInput.cginc and changed the references in the Core.cginc file.

    But now, I can't seem to figure where to change the alpha value of the fragment. I've tried a ton of possibilities in the Core.cginc file. Changing the color and the map of the Albedo works, but I can't find where the final alpha value is set.

    I've browsed the files in the CGIncludes folder in the builtin shaders of Unity.
    • UnityStandardCore.cginc : Already tried many possibilities (including the returned value of Alpha declared in UnityStandardInput.cginc
    • CustomInput.cginc : Alpha function, no effect

    Since the files are huge, I don't feel it will be useful to copy-paste them here.

    Thanks for your help in advance.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Is there a reason you don't just change the alpha of the material's color?
     
  3. Hellium

    Hellium

    Joined:
    Sep 25, 2014
    Posts:
    43
    Yes, as indicated in my first sentence, I want to change the alpha value of a fragment according to custom parameters I will pass to the shader. I don't want to change the alpha value of the whole material, but the alpha value of fragments I will choose.

    In fact, the alpha value will be determined according to the world position of the vertex.
     
  4. Hellium

    Hellium

    Joined:
    Sep 25, 2014
    Posts:
    43
    I have been able to solve it myself. In the end, you will need at least 6 files :
    • Shader file itself
    • Core.cginc
    • CoreForward.cginc
    • CoreForwardSimple.cginc
    • Input.cginc
    • Meta.cginc

    In all these files, replace all the references to the UnityStandardXXX by your custom files. Don't miss any !

    Then, in the Core.cginc file, I've edited the FragmentSetup function to change the value of the alpha variable :

    Code (CSharp):
    1. half alpha = Alpha(i_tex.xy) ;
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You also could use a surface shader if you don't need all of the features of the Standard shader (like being able to change the render mode).