1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Standard shader with vertex colors

Discussion in 'Shaders' started by defaxer, Apr 5, 2015.

  1. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hello, everyone!
    I needed to use vertex colors in materials for my current project, with flexibility of Unity 5 standard shader.
    So here it is in action: Standard shader modified to support vertex colors of your models.
    Latest version (0.91) with additive pass (multiple lights supported)
    Version for Unity 5.4.0 (0.92) with vertex alpha in shadows support

    [​IMG]

    vertex color intensity support

    [​IMG]

    vertex alpha in shadows support

    [​IMG]
     

    Attached Files:

    Last edited: Aug 11, 2016
  2. Boredman1234

    Boredman1234

    Joined:
    Sep 17, 2013
    Posts:
    13
    This is exactly what I was looking for! Thank you
     
    Last edited: Apr 6, 2015
  3. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    You are welcome to use it any way you want
     
  4. Boredman1234

    Boredman1234

    Joined:
    Sep 17, 2013
    Posts:
    13
    Awesome! Thanks again.
     
  5. Botanika

    Botanika

    Joined:
    Nov 2, 2013
    Posts:
    59
    Thank you very much !
     
  6. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hi everyone!
    Here is an updated version with vertex color intensity and vertex color automatic disabling (just standard shader style)
    [​IMG]
     

    Attached Files:

    luminance likes this.
  7. luminance

    luminance

    Joined:
    Nov 21, 2012
    Posts:
    11
    This is exactly what I was just looking for to work with sprites. Thanks so much!
     
  8. Fuzzy_Slippers

    Fuzzy_Slippers

    Joined:
    Dec 21, 2012
    Posts:
    450
    Any ideas on adding per vertex fading like the fade rendering mode but controlled by the vertex alpha? I can't figure out where in the shader the SurfaceOutput alpha is being set.
     
  9. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    It's not a surface shader, thats why it has no SurfaceOutput. I've modified shader to support transparency, but I need to figure out proper shadow rendering for transparent areas. And for some reason vertex alpha is not working with Cutout rendering mode.
     

    Attached Files:

    Fuzzy_Slippers likes this.
  10. speakr

    speakr

    Joined:
    May 22, 2013
    Posts:
    2
    So, I'm pretty new to custom shaders and very new to vertex colors. I'm wondering if someone can explain what I may be doing wrong here? I'm modelling in 3DS Max and exporting an FBX with a Vertex Color Map to use in Unity 5.
    When I import the model into Unity and apply the vertex color shader, the colors are all smeared instead of clean. Any ideas as to why this is happening?

    Capture.PNG Capture2.png
     
  11. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    In Max you need to detach faces with different colors to separate elements (Note: elements not objects).
     
  12. Fuzzy_Slippers

    Fuzzy_Slippers

    Joined:
    Dec 21, 2012
    Posts:
    450
    Thanks for this. I'll play around with it and see if I can figure out the shadows issue.
     
  13. plolley

    plolley

    Joined:
    Dec 19, 2014
    Posts:
    6
    Thanks for this shader, it's working great for me in the Unity player. The transparency doesn't seem to be working on Android. Can you think of any reason why?
     
  14. speakr

    speakr

    Joined:
    May 22, 2013
    Posts:
    2
    Oh man thank you so much for helping me figure this out!
     
  15. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    I'm not sure and I don't have decent device to test it right now.
     
  16. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    About this question: Combine Lights and Clamp intesity.

    I thought of using a vertex color shader to create ambient light on the object, and change color, clamp intensity through its vertex color.

    Would it be possible with this shader? I tried the following code does not work:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VertexColor : MonoBehaviour {
    5.  
    6.     public Color color;
    7.  
    8.     void Start () {
    9.  
    10.         Mesh mesh = Instantiate<Mesh>(GetComponent<MeshFilter>().sharedMesh);
    11.         for(int i=0;i<mesh.colors.Length;i++) mesh.colors[i] = color;
    12.         GetComponent<MeshFilter>().sharedMesh = mesh;
    13.    
    14.     }
    15.  
    16. }
    Another question, other usage could be creating fog of war, but would need to be transparent shader. This would be possible?

    Thanks.
     
  17. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Use vertex color modification like that:

    Code (CSharp):
    1.  
    2. void Start(){
    3.         Mesh mesh = Instantiate<Mesh>(GetComponent<MeshFilter>().sharedMesh);
    4.         Color[] colors = mesh.colors;
    5.         for (int i = 0; i < colors.Length; i++)
    6.             colors[i] = color;
    7.         mesh.colors = colors;
    8.         GetComponent<MeshFilter>().sharedMesh = mesh;
    9.     }
    10.  
    Yes it can be transparent
     
  18. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    No I get it to work on my computer.

    Does not change the color.

    Only change the intensity, if Vertex Color factor is modified in the material.

    Adding this test project, if the problem is not the script (I have installed Unity 5.0.1f1 Personal Edition, the graphics card is something old, Nvidia GeForce 9500GS)
     

    Attached Files:

    Last edited: Apr 27, 2015
  19. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    That's because unity primitives don't have vertex colors by default (uninitialized arrays) so you first need to create it.
    Note that if you use imported model with vertex colors this script will overwrite those colors.
     

    Attached Files:

  20. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Yes, that was the problem.
     
  21. Julinoleum

    Julinoleum

    Joined:
    Aug 31, 2011
    Posts:
    32
    I was hoping it would work on iOS but it didnt when I tried. Is it normal?
     
  22. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    I'm not sure, but it seems to me you device does not support Shader Model 3.0.
    It turns out shader isn't working with SM 2.
     
  23. SilverStorm

    SilverStorm

    Joined:
    Aug 25, 2011
    Posts:
    354
    Thanks a Lot defaxer, grab this package and try it out with your shader and you will get amazing results! I really wanna be greedy and keep this to myself but its not the right thing to do :

    Vertexdirt: https://www.assetstore.unity3d.com/en/#!/content/21015

    It allows AO baking to vertex colors WHILE using the GI in Unity allowing for perhaps the most fastest performance possible and since it bakes AO to vertex colors it will perform better on mobile devices compared to lightmaps i believe.

    This is an example of it. Its amazing!
     

    Attached Files:

  24. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,583
    Nice. Bookmarked :cool:
     
  25. MingJ

    MingJ

    Joined:
    May 13, 2015
    Posts:
    11
    Hi !

    First thing: good work, it helped me a lot.
    There's something I want to ask you: where does your 'UnityVC.cginc' file come from? Did you write it all or just modified an existent script?
    I'd like to know the best way to merge a custom shader into the Standard shader, like you did, but I'm new to shaders and I couldn't find the link between your file and the originals.

    If you could help me to understand it better I would really appreciate it!

    Thanks
     
  26. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    "UnityVC.cginc" is a file which includes all structs and functions that "StandardVC.shader" uses.
    UnityVC based on UnityStandardInput.cginc file, I've modified it to support vertex colors.

    AFAIK you can replace standart shader with you own version if its name is "Standard" and file is in "Resources" folder. I know people are replacing terrain shaders that way
     
  27. MingJ

    MingJ

    Joined:
    May 13, 2015
    Posts:
    11
    Thanks, good to know!

    Good job again on this shader.
     
  28. elamhut

    elamhut

    Joined:
    Sep 13, 2013
    Posts:
    17
    Hello!

    Thank you for the shader! I'm super happy I've found a Vertex Color shader but I'm sad that I'm not able to make it work.

    When using unity 4 we used to have greyscale textures that we colored using Vertex Colored Multiplied on top of the Difuse, I tried just applying your shader on top of the texture but it's not showing any result here, could you give me a hand?

    I'm using 5.0.2f1

    (The wall in the back its what's selected) http://puu.sh/hZzC6/74a7ef84a7.jpg
     
  29. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Are you using Dx9 or Dx11? Shader currently does not work with Shader model 2.0 Maybe this is the case?
     
  30. elamhut

    elamhut

    Joined:
    Sep 13, 2013
    Posts:
    17
    Even when in DX11 mode it doesn't work, what's the latest version of the Shader I should download? Maybe I got it wrong?

    Just to check I went to maya and all vertex colors are there, but still not in untiy
     
  31. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    In the first post updated version link is the one you need, note that intensity must be greater than zero. Also there is a "debug sphere" in a package use it to check if vertex color is working
     
  32. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hello, everyone! Just added Standard(Specular) shader version for you to try. Included an example scene (to see if shaders are working for you), which must look like a picture below.
    ExampleScene.png
     

    Attached Files:

  33. Fuzzy_Slippers

    Fuzzy_Slippers

    Joined:
    Dec 21, 2012
    Posts:
    450
    I've noticed it doesn't work with deferred rendering. I haven't tried it out in a build yet but in the editor DX11 deferred anything with either shader renders as grey with no color. Forward it works fine. Anyone else get that?
     
  34. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Yeah, It's was not correctly blending (if you have textures applied to your material) in deferred rendering, I've fixed it but with kind of tricky solution. To make it correct way I have to rewrite standard shader almost entirely :(
    Anyway if everything goes well, I will update it next week with that deferred fix, SM2.0 support and shadows with vertex alpha.
     
  35. HeadTrip

    HeadTrip

    Joined:
    Apr 1, 2013
    Posts:
    418
    This looks so great, but I need deferred support too... thank you sooooo much! :)
     
  36. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Ok, here is a new updated version with deferred fix and SM 2.0 working (on my machine at least)
    VC_deferred_fix.png
     

    Attached Files:

    Malbers, rocket5tim and HeadTrip like this.
  37. HeadTrip

    HeadTrip

    Joined:
    Apr 1, 2013
    Posts:
    418
    THANKS!! that is so very cool of you. trying it tonight...
     
  38. HeadTrip

    HeadTrip

    Joined:
    Apr 1, 2013
    Posts:
    418
    It works perfectly... Thank you so much! :D
     
  39. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    90
    This is very useful. I don't have the time or energy right now to learn the mechanics of ShaderLab.
     
  40. shapeupstudio

    shapeupstudio

    Joined:
    May 17, 2013
    Posts:
    3
    Hi defaxer,
    Thanx for this shader... I am 3d artist and right now m R&D on one of my project using your VC shader. I am facing one issue that when I open my project in 32bit unity 5 then shader dose not calculate VC. Its working awesome in 64bt unity 5.. its 32 bit issue or any other setting i have to change.
     
  41. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    I need to take a look, never actually tried it on 32bits
    Update: Just installed Unity 5.1.2 32bit and shader is working fine, no errors
     
    Last edited: Jul 20, 2015
  42. shapeupstudio

    shapeupstudio

    Joined:
    May 17, 2013
    Posts:
    3
    ok thanx defaxer,
    I have to check other options.. because when i move my project to other pc.. like 32bit. its turn to white. Do you have any idea about any setting where I have to check?
     
  43. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Are there any messages in console? How inspector looks like when shader is selected? any errors?
     
  44. shapeupstudio

    shapeupstudio

    Joined:
    May 17, 2013
    Posts:
    3
    Hay defaxer,
    I get the point where that issue come from.. ur shader is based one GPU so that if I open that project in on graphics card pc then its turn in to white. even I test that shader in different mobile also. Its work perfect in OPO and nexus but its turn white in android one and lower device.
     
  45. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    I have not tested it on mobile devices, so it's not predictable there, and I don't think it will be working on lowest devices Unity supports. On PC I didn't test it on 32bit systems, but i don't think the problem is bits, mo likely it's a GPU problem.
     
  46. massimoatcatnicdotit

    massimoatcatnicdotit

    Joined:
    Jan 14, 2014
    Posts:
    1
    Incredibly helpful shader! thanks a lot.
    Two questions:

    1) What's the use of "vertex color" in the standard specular (vertex color) shader? I have tried to change it's value but I cannot see any noticeable effect
    2) Any plan to support a "strength" parameter? 0 means no vertex color, 1 means full vertex color, any value in between 0,1 means a weighted blend.

    Thank you a lot!
     
  47. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hey thanks for pointing that out. Actually, Vertex Color parameter does almost exactly what you say, but for some reason it does not work. I'll fix that
     
    Last edited: Jul 22, 2015
  48. crudeMe

    crudeMe

    Joined:
    Jul 8, 2015
    Posts:
    66
    Hi, defaxer!

    Thx for this shader! But it seems I cant tame it.
    I set the material color to white and setting vertex color to any other. Material seems dim comparing to standard non-vertex shader of the same color. I'm using linear forward.
    Is there something I'm doing wrong?
     
  49. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    diffuse color and vertex color in this shader behave a little bit different. So you can't mimic diffuse color with vertex color.
     
  50. crudeMe

    crudeMe

    Joined:
    Jul 8, 2015
    Posts:
    66
    Bummer! In u4 we've used same material for lots of objects, colouring them with verticies. Thanks for answer anyway!)