Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Standard shader with vertex colors

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

  1. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    425
    Isn't there a legacy vertex color shader in unity or at least an easy way to use vertex colors fromsurface shaders in hte pbr workflow?
     
  2. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    There is! In fact I eventually use it more then this shader. It's almost like an "Old" surface shader but with StandardSurfaceOutput instead of SurfaceOutput

    Code (CSharp):
    1. void surf (Input IN, inout SurfaceOutputStandard o)
    2. {
    3.     fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    4.     o.Albedo = c.rgb * IN.color;
    5.     // Metallic and smoothness come from slider variables
    6.     o.Metallic = _Metallic;
    7.     o.Smoothness = _Glossiness;
    8.     o.Alpha = c.a;
    9. }
    the only problem is you have to write Normal mapping, Parallax etc yourself, while this shader has it built in
     
  3. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    425
    Thankyou defaxer, so in theory I can just create a default Unity standard surface shader, add this line:
    o.Albedo= c.rgb*IN.color;​
    And I'm good to go, so why you had to make you shader in vs/fs? doesn't Unity's default tamplate shader cover that already?
    And what about a built-in vertex lit shaders, I cant find one that shows my vertex color (FBX imported from ZBrush using their new FBX Import/Export plugin).
    Thank you for your help:)
     
  4. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    In StandardSurfaceOutput shader case you'll loose universality of Standard shader. You'll have to write support for Detail, Normal, Specular maps use yourself. Plus you'll need to write CustomEditor for your new Shader to automaticaly enable/disable use of those maps.
    AFAIK none of Unity builtin shaders except Particle and Sprite support vertex color
     
  5. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    425
    Man you're right, I also lose emission, occlusion, secondary maps and so on. anyone has a link to a surface shader that mimics Unity's built-in standard shader. I checked the one shipped with Unity but it's 300+ lines of vs/fs and ShaderLab.
     
  6. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    425
    Code (csharp):
    1. Shader "Custom/Damned Shader" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.      
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         #pragma surface surf Standard fullforwardshadows
    15.  
    16.         // Use shader model 3.0 target, to get nicer looking lighting
    17.         #pragma target 3.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.             float4 vcolor : COLOR;
    24.         };
    25.  
    26.         half _Glossiness;
    27.         half _Metallic;
    28.         fixed4 _Color;
    29.  
    30.         void surf (Input IN, inout SurfaceOutputStandard o) {
    31.             // Albedo comes from a texture tinted by color
    32.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    33.             o.Albedo = c.rgb * IN.vcolor;
    34.             // Metallic and smoothness come from slider variables
    35.             o.Metallic = _Metallic;
    36.             o.Smoothness = _Glossiness;
    37.             o.Alpha = c.a;
    38.         }
    39.         ENDCG
    40.     }
    41.     FallBack "Diffuse"
    42. }
    I got:
    Code (csharp):
    1. Shader error in 'Custom/Damned Shader': Bad characters in source file at line 33 (on )
     
  7. crudeMe

    crudeMe

    Joined:
    Jul 8, 2015
    Posts:
    76
    I guess you've already found out mistype. But, just in case )
     
  8. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    425
    I still can't find what wrong.
     
  9. crudeMe

    crudeMe

    Joined:
    Jul 8, 2015
    Posts:
    76
    i''m terribly sorry, i've misread your code.
     
  10. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    536
    Try renaming vcolor to color to see if this fixes the error. I have a feeling that this might be a requirement though it is not documented.
     
  11. rocket5tim

    rocket5tim

    Joined:
    May 19, 2009
    Posts:
    218
    Exactly what I needed! Thanks defaxer!
     
  12. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    425
    It was color to begin with, I actually thought it maybe a conflict so I changed it to vcolor.
     
  13. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    81
    Hello! Love the Vertex shader w/fade, super useful for polygon effects work. But I'm using the shader in my project at the moment and I'm getting the red error message,

    Shader error in 'Standard Specular (Vertex Color)': failed to open source file: 'UnityVC.cginc' at line 89 (on d3d11)

    Compiling Vertex program with DIRECTIONAL SHADOWS_SCREEN LIGHTMAP_OFF DIRLIGHTMAP_OFF DYNAMICLIGHTMAP_OFF _NORMALMAP _VERTEXCOLOR _SPECGLOSSMAP
    Any idea what could be causing this? I've moved the UnityVC folder to my own shaders folder in the project, but the file UnityVC.cginc is in the same folder as the StandardSpecularVC shader.

    Many thanks!

    --Rev
     
  14. C_S

    C_S

    Joined:
    Aug 24, 2015
    Posts:
    4
    Hi,
    I have a problem using this shader.
    If i just drag a material to the debug sphere and choose the shader it works perfect and colors the sphere.
    But if I run my script (within i create a mesh) and apply the same material to the mesh, it stays single colored.

    Is there anything else I have to do ?
    I want to color the mesh equal to the higth component of the vertex points.
     
  15. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Moving shader files around a project folder doesn't give me that error. Are you sure all shader files
    StandardSpecularVC.shader, StandardVC.shader and UnityVC.cginc are in the same folder?

    Is mesh you generate already have vertex color? By default meshes generated in Unity don't have any vertex color.
     
  16. C_S

    C_S

    Joined:
    Aug 24, 2015
    Posts:
    4
  17. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Shader uses Shader keywords to enable vertex color rendering. So in your code you need to add
    Code (CSharp):
    1.  
    2. rend.material.EnableKeyword("_VERTEXCOLOR");
    3.  
     
  18. C_S

    C_S

    Joined:
    Aug 24, 2015
    Posts:
    4
    I added the line, but it is still not working.
     
  19. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    You can try to apply an existing material (created in editor, not on runtime) and see if it works on your mesh.
     
  20. timmyen

    timmyen

    Joined:
    Dec 31, 2013
    Posts:
    3
    Hello defaxer, thank you so much for your shader, it is everything I wanted! However I'm encountering an issue with alpha sorting on my personal project. I'm using your shader for a waterfall with vert alpha to blend edges, however it's sorting behind the Water4 shader that comes with the standard package (in fact, almost anything with alpha is sorting behind it...). The only way for the waterfall to sort above the Water4 is to use "cutout", but I lose the vertex alpha. Is there a solution for this?

    See: http://i.imgur.com/MdCRjw9.jpg
     
  21. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    901
    Seems the particle sprites just intersect the water4 mesh, the first guess I'd use is to add depth write offset in the particle shader or to use soft particles in the render settings
     
  22. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Never used Water4 shader, so I'm not sure if this will work. But try switching inspector to debug mod and set custom RenderQueue for particle and waterfall material (something like 3010 or more).

    DebugMaterial.jpg
     
  23. timmyen

    timmyen

    Joined:
    Dec 31, 2013
    Posts:
    3
    OMG finally! For some reason the render queue for Water4 was set to 90000 by default. I've been banging my head on this for two days and now I can finally move on. THANKS GUYS! :)
     
  24. eransh

    eransh

    Joined:
    Oct 18, 2014
    Posts:
    1
    Hi defaxer,
    I'm using your awesome shader (standard VC) with a material setting of "fade" rendering mode.
    I want all my vertices colors to start with aplha 0.
    After some time i want to change part of the vertices to have a full alpha color.
    How can i achieve this? it doesn't seem to work
    At first, all of my mesh vertices have alpha 0 color.
    Then, I'm setting some vertices color to full aplha in code, like the examples above, but they stay transparent (as long as my initial material color setting has albedo with allpha 0)

    If my initial setting has a color with alpha > 0 it works, but it's not what i want.
    Thanks!

    upload_2015-11-16_15-6-9.png
     
  25. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    901
    Maybe the alpha values get multiplied with the specified alpha from the color, in this case you'd want to set the color alpha to one. Seems to be the case, as the color itself also gets multiplied with texture color;)
     
  26. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    286
    Awesome work. Thank you for it.
     
  27. zsombor

    zsombor

    Joined:
    Feb 21, 2013
    Posts:
    1
    Does anybody have problems with this using the Metal API? Using Unity 5.2.3f1, Xcode stops with this message:

    program_source:3:10: error: unknown type name 'FragmentCommonData'

    constant FragmentCommonData _xlat_mtl_const1 = {float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), 0.0, 0.0, float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), 0.0};
     
  28. hsallander

    hsallander

    Joined:
    Dec 19, 2013
    Posts:
    12
    I have the same problem but no solution, I'm unable to use this shader together with iOS Metal. Has anyone else been able to solve this?
     
  29. kfirmon

    kfirmon

    Joined:
    Oct 4, 2015
    Posts:
    34

    Hi. Forgive me for the stupid question. I really want to use this tool and have no knowledge whatsoever about shaders (still didn't get to that part in Unity).
    Can you tell me how to use this ? how do I assign a color to a vertex?

    Thanks a lot
     
  30. Finward-Studios

    Finward-Studios

    Joined:
    Sep 27, 2015
    Posts:
    25
    This is great. Is it possible to modify the shader to use for example vertex illumation as a mask to blend with another standard material. Add double slots for all the maps and vertex illumination would drive which maps to use. Thanks
     
  31. caponeart

    caponeart

    Joined:
    Jul 9, 2013
    Posts:
    21
    Hi, this shader looks great but I just can't get it to work. My mesh just shows as White? I deleted the normal, metalic and albedo maps. I just want flat VC.

    [​IMG]

    This is how it looks in my 3D app;

    [​IMG]
     
    Last edited: Jan 19, 2016
  32. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    No. This can't be done. If only unity could blend materials by mask, same way unreal does it. But I have no idea how to achieve this.

    Seems like you are using deferred rendering. If so are you using latest shader version? It should look same in forward and deferred now.
     
  33. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    901
    It can be done, there are implementations like UBER which support this (or even RTP if you need terrain like behaviour with splatmaps). Don't know exact technique behind this, though:(
     
  34. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Standard shader like this uses all available data input slots:
    Position,
    Color,
    Normal,
    Tangent,
    TexCoord(0-4) so I dont have a place to squeeze more texture maps. If only I knew how to bypass that...
     
  35. caponeart

    caponeart

    Joined:
    Jul 9, 2013
    Posts:
    21
    How can I find out what version it is? Doesn't say in the visual studio script from what I can see?

    [​IMG]
     
  36. Graham-B

    Graham-B

    Joined:
    Feb 27, 2013
    Posts:
    161
    Thanks so much for sharing this awesome shader! The updated version you linked in the first post doesn't appear to have the strength slider any longer. Is it accessed elsewhere now?
     
  37. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Oh, it must be broken for a while now :) I'll try to update shader as soon as I can
     
  38. Graham-B

    Graham-B

    Joined:
    Feb 27, 2013
    Posts:
    161
    Looking forward to it, thanks defaxer.
     
  39. nauroman

    nauroman

    Joined:
    Nov 1, 2014
    Posts:
    1
    Hello defaxer,
    Thanks for your great shader :)

    I'm doing a "minecraft" game. Is it possible to do a shader where vertex color simulates lights (emission is albedo texture color * vertex color) ?
     
    Last edited: Feb 27, 2016
  40. Pangeli70

    Pangeli70

    Joined:
    Mar 2, 2016
    Posts:
    2
    Hello all. I'm interested in a voxel shader too. The solution should be to pack the illumination amount inside the alpha channel of the vertex color, but I'm not good in CG programming at the moment. I can help in beta testing or with a donation :).
     
  41. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hi! That can be done easy in A Quick Way:
    1. open UnityVC.cginc in UnityVC folder
    2. If using Forward rendering find a function half4 fragForwardBase_VC
    3. If using Deferred rendering find void fragDeferred_VC
    4. In corresponding function change line c.rgb += Emission(i.tex.xy); to c.rgb += i.color.a * s.diffColor;
    5. That should work
    and a Slow Way, after I'll get there and fix an old bug with intensity, I will think about MultipurposeVertexColorStandardShader later, as there can be a lot of variations on how to handle vertex color in shader.

    PS. I'm using colors to animate vegetation, or flags. Crytek were using colors to alter character body constitution in Crysis.
     
  42. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    536
    So many uses for vertex colors out there. No wonder you are getting so many requests for each and every little thing that can be done :)
    Good job so far.
     
  43. Estaxo

    Estaxo

    Joined:
    Feb 7, 2016
    Posts:
    3
    Hello! Your vertex shader is great and is definitely what I was looking for, but unfortunately it seems not to work with duplicated object (or objects sharing the same mesh). Is there something wrong on my side or is it a "global" issue? Could you help me with that? Here is how it looks: once duplicated, both o bject and its copy turn black

    [​IMG] [​IMG]
     
    Last edited: Mar 7, 2016
  44. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hello everyone! Just updated shader with new version (0.9).
    Thanks for pointing that out! From now on shader has versions.

    Fixed vertex color intensity feature.
    From now intensity works differently,e.g. if it is lower than 1, vertex color lerps with base material color. Like so:
    VC_intensity.png
    while old version just darkened material.
    I couldn't replicate that bug with new version. So you can try that version out and see if it's fixed.

    P.S. In next version vertex alpha will affect shadow transparency.
     

    Attached Files:

    Seneral likes this.
  45. Estaxo

    Estaxo

    Joined:
    Feb 7, 2016
    Posts:
    3
    Thanks! It seems to be fixed. I've encountered another problem: vertex colors work only with directional lights. Point and spotlights show models in grey color only.
     
  46. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Just tried it with both spot and point lights and it's working (try it on debug sphere). But there is other prob: in forward and deferred rendering materials interact different with these kind of lights.
     
  47. Estaxo

    Estaxo

    Joined:
    Feb 7, 2016
    Posts:
    3
    Unfortunately in my case it is not working even on debug sphere :( Notice that I've disabled skybox and ambient light. If I re-enable those, the point light seems to "decolorize" debug spheres (see last screenshot). I've checked it on another hardware, the problem is still there.
    Screenshot 10-03-16 003.png Screenshot 10-03-16 002.png Screenshot 10-03-16 004.png Screenshot 10-03-16 005.png
     
  48. PimTheLord

    PimTheLord

    Joined:
    Mar 23, 2016
    Posts:
    1
    Hey there,

    Any chance outlines are going to be supported? Or how can i do it myself if you are not going to do it?
     
  49. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    133
    Hi! No I was not planning outlines support. Can't tell If it's possible to squeze into my shader without sacrificing some advanced features like parallax or anything else. And I'm not aware of any suchx standard shader solution, only legacy
     
  50. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    736
    RenderingMode Fade= Vertex colors black and white, are both treated the same and those parts of the material become transparent, leaving only the other vertex colors showing the albedo map..

    RenderingMode Transparent = Black is transparent, while the white areas are just more whiter...

    This is with Vertex Color at 1.0

    So I'm wondering is there a way to get transparency(where vertices are painted black) +vertex colors overlayed, while not effecting the original albedo map brightness (for areas painted white)? Currently the closest I can get to what I was wanting is using RenderingMode Transparent ...its just the white vertex colored areas are the closet to having the original albedo texture color (only its alot brighter/whiter, changing Vertex Color down to 0.0 gets the original color, but then no transparency or other vertex colors are shown) .... any way to get that?

    Anyway was just playing with it, never bothered with the vertex coloring stuff before.