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

Modify unity 5 standard shader

Discussion in 'Shaders' started by Elzean, Mar 24, 2015.

  1. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    I would like to add 2 textures to the standard shader, 1 new Albedo/diffuse and 1 grayscale texture to use inside a lerp. In shader forge it looks like this :




    I downloaded the shader archive from Unity download page to check, but too much stuff i don't understand inside and i couldn't find a way to merged the code from Shader forge with it (the part with the lerp).


    Would be nice if someone could help me with this :/
     
  2. unity_webqam

    unity_webqam

    Joined:
    Mar 16, 2015
    Posts:
    1
    I tried with a substance using 2 bitmap input and plug it to the standard shader. It works but the transition is too slow, its too much to rebuild the final texture unless i make an extremely low resolution texture.


    is this something hard to add inside the standard shader ? (obviously for me it is)
    It doesn't seems too much code to add seems like shader forge only use a few lines for this part, i just have no idea where to put it in the unity standard shader.

    Also i'm only using the diffuse so i dont need this to work for normal, spec etc...

    Edit: sorry used wrong account, this is also Elzean...
     
    Last edited: Mar 25, 2015
  3. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    so this part should be what i need to add :
    Code (CSharp):
    1. float4 frag(VertexOutput i) : COLOR {
    2. float4 _transition_var = tex2D(_transition,TRANSFORM_TEX(i.uv0, _transition));
    3.             float node_7586_if_leA = step(_Transition,_transition_var.rgb);
    4.             float node_7586_if_leB = step(_transition_var.rgb,_Transition);
    5.             float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    6.             float4 _MainTex2_var = tex2D(_MainTex2,TRANSFORM_TEX(i.uv0, _MainTex2));
    7.             float3 node_7586 = lerp((node_7586_if_leA*_MainTex_var.rgb)+(node_7586_if_leB*_MainTex2_var.rgb),_MainTex2_var.rgb,node_7586_if_leA*node_7586_if_leB);
    8.             float3 diffuseColor = node_7586;
    9. }
    and also this :

    Code (CSharp):
    1. float4 frag(VertexOutput i) : SV_Target {
    2. /// Vectors:
    3.             float4 _transition_var = tex2D(_transition,TRANSFORM_TEX(i.uv0, _transition));
    4.             float node_7586_if_leA = step(_Transition,_transition_var.rgb);
    5.             float node_7586_if_leB = step(_transition_var.rgb,_Transition);
    6.             float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    7.             float4 _MainTex2_var = tex2D(_MainTex2,TRANSFORM_TEX(i.uv0, _MainTex2));
    8.             float3 node_7586 = lerp((node_7586_if_leA*_MainTex_var.rgb)+(node_7586_if_leB*_MainTex2_var.rgb),_MainTex2_var.rgb,node_7586_if_leA*node_7586_if_leB);
    9.             float3 diffColor = node_7586;
    10.             o.Albedo = diffColor;
    11.        
    12.             return UnityMetaFragment( o );
    13.         }
    This is from sharder forge, i'm not really sure this is enough or not.

    I made a copy of the standard shader and i suppose i should somehow add this around line 143 before
    #include "UnityStandardCore.cginc"

    I probably say lots of stupid stuff here so i'm really hoping some shader guru to help XD
     
  4. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    i still have problems with this, any suggestion would be very much welcome :(
     
  5. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    Is there a particular reason why you want to modify the Standard Shader? What you want to achieve could be done through a simple shader : as long as you write the right info into each channel (Albedo, Metalness etc...) the lighting will be the same.

    I think Shader Forge was updated for Unity 5 PBR so maybe you could simply use it, redo your blending and plug the output into the Albedo channel. Then you just use that shader on your material that need that blending.
     
  6. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    I tried with shader forge and got it working on PC, the effect was nice but the rest was looking a lot worse than the standard shader. I tried with more complexe ones but still didnt look as good.

    Also none of my test with shader forge worked on mobile even with unlit and force shader model 2.0, not only it did not work on mobile i also couldnt get the shadows to work with the unlit shader.

    The standard shader is optimize during compilation to get rid of whatever i dont use, and it looks very good on mobile with shadows and the performance are good, so i'd would have like to just add that small part to the standard shader instead.
     
  7. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    Hum I see your point. Still not sure it's the right way to do it, but sure is a possible way. Let's try it and see if it's what you want. Follow me, it's a bit of a convoluted way!

    Disclaimer, not really have time to test it, so that's all just trying to pieces thing together by reading the shaders...

    Copy from the Standard Shader package you downloaded inside your assets folder :

    Standard.shader
    UnityStandardInput.cginc (found in the CGInclude folder)

    - Rename Standard to CustomStandard. Open it.
    - On top, change the name ("Standard") to ("CustomStandard")
    - In Properties, add, under "_MainTex :

    Code (CSharp):
    1. _MainTex2("MainTex2", 2D) = "white" {}
    2. _TransitionTex("TransitionTex", 2D) = "white" {}
    3. _Transition("Transition", Range (0.0, 1.0)) = 0.0
    4.  
    - Remove at the very bottom "CustomEditor "StandardShaderGUI"" (you'll lose the nicely formatted material inspector, sorry, but it's the easiest way, instead of writing a new Custom Inspector to include your params...)


    Now, open UnityStandardInput

    - under "float4 _MainTex_ST;" add

    Code (CSharp):
    1. sampler2D    _MainTex2;
    2. sampler2D    _TransitionTex;
    3. float        _Transition;
    - find the function called "half3 Albedo(float4 texcoords)" inside that file, and replace the line

    Code (CSharp):
    1. half3 albedo = _Color.rgb * tex2D (_MainTex, texcoords.xy).rgb;
    by


    Code (CSharp):
    1. half3 tex1 = tex2D (_MainTex, texcoords.xy).rgb;
    2. half3 tex2 = tex2D (_MainTex2, texcoords.xy).rgb;
    3. half weight = tex2D(_TransitionTex, texcoords.xy).r;
    4.  
    5. //step will give 0 if weight is < than _Transition, 1 if >
    6. half t = step(_Transition, weight);
    7.  
    8. //since t is either 0 or 1, then lerp return either tex1 or tex2
    9. half3 albedo = _Color.rgb * lerp(tex1, tex2, t);

    Once that done, you should have a CustomShader in your shader dropdown on your materials, that should do exactly what your Shaderforge was doing. No idea how that will interact with all the internal of Unity though...
     
  8. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    659
  9. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584

    So i test your code and also created a copy of the standardGUI and added the new stuff. At first it showed black on mobile too. I had to add the shader file in "Edit>Project settings> graphics" inside the "Always include shaders".

    Now it works on mobile with lightmap, dynamic shadows and it looks nice :)

    Thanks!
     
  10. julianwitte

    julianwitte

    Joined:
    Oct 3, 2012
    Posts:
    12
    mcroswell likes this.
  11. saarwii

    saarwii

    Joined:
    Mar 7, 2014
    Posts:
    24
  12. mcroswell

    mcroswell

    Joined:
    Jan 6, 2010
    Posts:
    79
  13. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Tried this out all i get is a black mesh no matter what textures i try:( Did this stop working or did i do it wrong I restarted twice and no luck
     
    Last edited: May 6, 2016
  14. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Bah and now the standard shader no longer works is just white............
     
  15. StaffanEk

    StaffanEk

    Joined:
    Jul 13, 2012
    Posts:
    380
    Any word from Unity on how to actually fully utilize their standard shader? Seems kinda goofy that changing the standard shader is so hidden.
     
  16. ReemOnspec2020

    ReemOnspec2020

    Joined:
    Dec 16, 2020
    Posts:
    2
    @UnityGuillaume hello I have the same problem right now i want to add two shaders one to rotate the texture in the z-axis and the other one to add alpha to make it transparent to control the alpha so each shader worked so nice alone but I can't mix them together , if any one can help me