Search Unity

Standard shader with vertex colors

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

  1. atpkewl

    atpkewl

    Joined:
    Jun 30, 2015
    Posts:
    17
    Hi @defaxer thank you for sharing the Shader. I'm using the Standard (Vertex Color). It works on Android device beautifully, however doesn't work on iOS. The object doesn't get drawn at all... Do you know why this is the case ?

    I'm using exactly the same codes & settings for both Android and iOS.
    Android: Nexus 5
    iOS : iPhone 6s (latest iOS)

    Thank you
     
  2. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Never got reports about that. I haven't tested it on iOS (since I don't have one), so I can't tell you what is the reason.
     
  3. killa247

    killa247

    Joined:
    May 7, 2013
    Posts:
    25
    Hey! Great shader only I'm having some trouble with lighting, if i have more than one directional light the second light removes the vertex color and replaces it with grey/blue/green whatever color the light is, it only seems to color the dominant directional light. see picture for example.
     

    Attached Files:

    • qwe.jpg
      qwe.jpg
      File size:
      59.4 KB
      Views:
      1,142
  4. Lichty

    Lichty

    Joined:
    Apr 26, 2016
    Posts:
    2
    I keep getting this error:


    It only happens when multiple meshes are using the same material. For some reason it only happens with the trunks and never the leaves. I'm new to shaders and have no idea how to begin debugging this. Ideas?
     
  5. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Absolutely have no idea. Whats wrong with a third trunk? Why it's not changing? Are you using fog or LODs maybe? Is blue material or geometry color? What lights do you use?
     
  6. Lichty

    Lichty

    Joined:
    Apr 26, 2016
    Posts:
    2
    They change color based on the angle you look at them. I'm not using fog or LODs and blue isn't used anywhere in the game. I'm using a single directional light (yellowish color).
    The part that really confuses me is that only the trunks change color. The trunks and the leaves are generated by the same scripts. They just have different parameters. After they're generated, the 'generator' drops any reference to the tree and the trees have no scripts of their own, that's why I'm assuming it's a shader issue.
     
  7. megisto

    megisto

    Joined:
    Dec 25, 2009
    Posts:
    127
    Hello, Defaxer.
    Great shader. I am working on an asset for the assets store: modular models for urban enviroment. I am using vertex colored models. Can i include this shader in the asset as default shader for the models?

    Thank you
     
  8. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Yes you can :) Just keep a link somewhere to this thread so people could get an updated version or write a feedback here
     
  9. megisto

    megisto

    Joined:
    Dec 25, 2009
    Posts:
    127
    Thanks a lot! Ook :)
     
  10. FatWednesday

    FatWednesday

    Joined:
    Jul 4, 2012
    Posts:
    50
    Anyone managed to get a fix running this on iOS metal devices? We are trying to use this shader in a project at the moment, unfortunately on metal devices it crashes on boot in our project, failing on a GetShader call somewhere in unity. The example scene runs without crashing but fails with an error "Unknown type name 'FragmentCommonData' "

    It would be great if anyone has a solution, if not we'll just have to find an alternative.
     
  11. iainr86

    iainr86

    Joined:
    Apr 27, 2016
    Posts:
    2
    Hi there, thanks so much for offering this, it is exactly what I need for a project I'm working on, however, I can't get it to work with my assets. The sample scene works fine, but when I import my own model I can't get the vertex colours to be displayed at all. The .obj files open fine in MeshLab with the vertex colouring working fine. Is it something to do with the format of the obj files themselves, they've been converted from .ply, or is it down to my import settings in Unity?

    I've attached an example .obj file, could some kind person take a look for me and see if they can work out what I'm doing wrong?
     

    Attached Files:

  12. VOTRUBEC

    VOTRUBEC

    Joined:
    Dec 17, 2014
    Posts:
    106
    First up, just want to say a big Thank You to defaxer for the effort you put into the shader!

    But...

    I've just now come across the same "blue" error. My mesh seemed to be just picking up the blue component of the colour. But when I started playing around with the scale, for example, when I scaled the mesh to -1 in the x, the colours were displayed correctly.

    I modified the mesh to use Color32 for the vertex colours. That then made the whole mesh turn black, until again I scaled the x to minus values, at which point the mesh colours came back, though the mesh was inverted.

    Does this seem to give you any indication as to what's going wrong?
     
  13. killa247

    killa247

    Joined:
    May 7, 2013
    Posts:
    25
    I had this same issue, there is nothing wrong with the shader, its your objects, the normal directions need to be calculated correctly, there is an option in most 3d software to average normals, or correct normal direction, even in unity there is an option within the import options to calculate normals, please tell me if this worked for you :).
     
  14. killa247

    killa247

    Joined:
    May 7, 2013
    Posts:
    25
    Also still nothing about the multiple lighting issue?
     
  15. VOTRUBEC

    VOTRUBEC

    Joined:
    Dec 17, 2014
    Posts:
    106
    Hi Killa, while I agree that there's probably something wrong with my objects, if the normals were incorrectly aligned, I wouldn't be able to see the faces at all, unless I looked at the object faces from "behind". The scene is made up of about 100 small meshes (~20 triangles each). Each mesh is created through script, and the last step for each is a call to Mesh.RecalculateNormals(). They're all a shade of dark blue. But when I stitch the objects together into one mesh, the vertex colours are displayed correctly! I haven't exhausted all methods for scripting meshes from triangle points, but I'm close. I've even called the Mesh.Optimize() method, to no effect. My next step will be to calculate the normals for each vertex by hand; I just hope it doesn't blow out the execution time...
    Thanks for your input though. And I really should move this line of question out of here. In a desperate attempt, I changed the shader, and noticed the same "blue" error. So it would seem it's not solely related to this shader.
     
  16. andSol

    andSol

    Joined:
    May 8, 2016
    Posts:
    22
    @defaxer Many thanks for sharing this! Very helpful. Would you care explain a bit what you did change over the Standard shader? I was comparing both files and it seems that the only differences are in a bunch of #pragma and #include calls. I've been trying to understand why just that changes the usual standard output, by couldn't.

    Don't get me wrong: the shader is working perfectly. I am just trying to learn from what you've done! Thanks a lot
     
  17. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Didn't have time to look into the shader yet, but the most work will likely be done in UnityStandardShaderCore.cginc or something similar. The standard shader itself IS only a bunch of pragmas and includes, because there's a lot of duplicate code going on for each pass so it has all been lifted to include files.
    In the core include file, you'll find the vertex and fragment shaders for both forward and deferred pass. I'd imagine he added a Color passed from the vertex to the fragment program, being the vertex color, which is then multiplied with the albedo in the fragment program (everthing for both forward and deferred).
    That's only theory of ourse but that's the basics steps required for this. He probably wrapped his changes in a pragma check and added #shaderfeatures in the apropriate passes in the actual standardshader.
    Hope that helps:)
     
  18. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    I am making meshes in code and the Dynamic Batcher was turning them black.
    The first GameObject was ok, but then they all turn black after the second is added.
    Adding this to each GO fixed it.
    Of course after 3 hours of trying everything else :)


    Well, I thought I fixed it, but I was making new instances.
    I hate those things. So I still have the problem of Dynamic Batching making things black.
     
    Last edited: May 29, 2016
  19. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    The shader seems to work just fine without setting that Keyword.
    I was never setting it before and it displayed vertex colors.
     
  20. andSol

    andSol

    Joined:
    May 8, 2016
    Posts:
    22
    Super helped! Thanks a lot, I will take a deeper look into the include files that were added/changed.
     
  21. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Sorry, dropped out of this thread a little, seems like it's living it's own life.

    @andSol yeap that's exactly like @Seneral said, everything is going on in include files.

    As long as you're using StandardShaderVCGUI.cs file (custom editor for this shader) from Editor folder, it adds "_VERTEXCOLOR" automatically.

    Sorry not for now as I'm pretty busy with another project. But I think it's not that hard to do anyway.
     
  22. JermyAker

    JermyAker

    Joined:
    Aug 4, 2014
    Posts:
    17
    I was having a similar issue with objects turning black. I turned off Dynamic Batching (PlayerSettings -> Other Settings -> Dynamic Batching) and it now works perfectly!
     
    Pixelaborate likes this.
  23. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    Yup I noticed that too.
    But I would rather use the dynamic batching than be forced to combine everything on my own.
    I do combine a lot of things already. I was hoping a few small things could be left for batching.
     
  24. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    Solved the black texture problem
    Ok, after working on a different part of my project I came back to this with a new idea.
    Because I didnt need UVs I didnt have any on the mesh, we use vert color.
    The dynamic batcher made them black when viewed from some angles.
    So I tried adding UVs just set to 0f,0f and it worked!

    Unfortunately the useless UVs count against the batching limits.
     
    Last edited: Jun 16, 2016
  25. Pixelaborate

    Pixelaborate

    Joined:
    Jan 18, 2014
    Posts:
    4
    To anybody having the blue texture problem:

    After a bit of searching I found out this was actually a dynamic batching issue that can be fixed by going to
    File > Build Settings > Player Settings... > Other Settings > And turning OFF dynamic batching

    Hopefully this works for you like it did for me :) cheers!

    EDIT: whoops seemed to have missed a post saying the exact same thing above me!
     
  26. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    So Additional lights dont pick up vertex color, because the Add pass is sent to the Unity standard shader that doesnt handle vert colors. It took me 3 hours to figure that out.

    I then spent the next 2 hours writing some new code to make it work the way we want.
    I then spent the next 5 hours chasing compiler errors on code I never even touched.
    After 10 hours total, I still havent gotten it to compile, so I have no idea if my changes work. :(
     
  27. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    How easy would it be to update the Vertex shaders to work as an Instancing shaders?
     
  28. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Hello everyone!
    Just added Additive parr for shader, now it suppose to work correctly with two or more lights. So go on, test and post some feedback!

    That's why it's not a preferred way to rewrite a shader, you'll have to copy/paste more and more code from Unity, or just rewrite it from scratch.

    Never looked at how instancing shader work, but now I want to have a look ;)
     

    Attached Files:

    Alverik, JamesArndt and Tzan like this.
  29. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    Just tried it out and it works great!
    Thanks so much!

    Before:
    The trees are using the old style Unity shader, pre 5.0,
    1 point light



    After:
    2 point lights 1 spot, same intensity and height off ground.
    The sphere in the back is white with high metallic, (but nothing to reflect).


    Thanks again!
     
    defaxer likes this.
  30. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Hi, everyone! Defaxer, thanks for these shaders, they're great and I've been using them a lot! But I've just updated to unity 5.4 and now the UnityVC shader stopped working :(. I'm getting these errors:

    Shader error in 'Standard Specular (Vertex Color)': invalid subscript 'posWorld' 'FragmentSetup': no matching 5 parameter function at Assets/UnityVC/UnityVC.cginc(130) (on d3d11)

    Shader error in 'Standard (Vertex Color)': invalid subscript 'posWorld' 'FragmentSetup': no matching 5 parameter function at Assets/UnityVC/UnityVC.cginc(130) (on d3d11)


    Anyone know how to fix this? I must admit I'm terribly ignorant at shader programming, so I feel somewhat helpless (plus, half of the objects in all my scenes are using UnityVC)...

    Anyway, any advice will be greatly appreciated!
     
  31. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Hi! I took a look at Unity 5.4 Standard shader code and it's changed, seems critically for my shader version, so I think this version needs adaptation for new Unity
     
    GilCat and Alverik like this.
  32. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    It would be really nice to have this working on Unity 5.4.0
    Thanks
     
    Alverik likes this.
  33. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Finally had time to take a look at shader code, started rewrite and then decided to import it to 5.4.0... and it gives me NO ERRORS! Yes, Standard 5.4 shader code is different and imported shader does not look same as in 5.0 but it works. Can someone confirm that?
    VC54.png
    Looks like specular is not working (or working other way) but thats fixable. Anyway i'll convert it for 5.4 and post later on progress.
     
  34. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Ok, Here is a 5.4.0 Unity shader version with an extra bonus: now shader in Transparent or Fade rendering mode support vertex alpha in shadows!
    VC54Alpha.png
     

    Attached Files:

    amasinton, elias_t, GilCat and 4 others like this.
  35. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Thanks!
     
  36. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    Thanks defaxer!

    I know they wont do it because they don't really care about the vertex color users, but Unity should grab this and support it in future versions.
     
    Last edited: Aug 12, 2016
    Alverik likes this.
  37. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    Thanks for the update.
    I get no errors when the target platform is PC/Mac or WebGL but if i switch to iOS or Android it do get the old errors:
    Code (CSharp):
    1. Shader error in 'Standard (Vertex Color)': invalid subscript 'posWorld' at Assets/UnityVC/UnityVC.cginc(299) (on d3d11)
    2.  
    3. Compiling Vertex program with DIRECTIONAL SHADOWS_SCREEN LIGHTMAP_OFF DYNAMICLIGHTMAP_OFF _EMISSION _VERTEXCOLOR
    4. Platform defines: UNITY_NO_SCREENSPACE_SHADOWS UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF2 UNITY_HARDWARE_TIER3
    5.  
    Seems like an editor only problem since when i build it to iOS and Android it works. But on iOS i have to disable Metal Graphic API since it crashes on that.
     
    Alverik likes this.
  38. stickadtroja

    stickadtroja

    Joined:
    Oct 25, 2015
    Posts:
    33
    nice shader, its exactly what i need, but i cant get it to work. some of the StandardShaderVCGUI generates errors, and the shaders cant find some file. they turn up purple.

    i tried a earlier version back in the thread and that works though! my editor is up to date btw.
     
  39. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    It's probably because the last version Defaxer uploaded is for Unity 5.4, which has some changes which break backwards compatibility (basically the standard shader is now different).
     
  40. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    I cant tell what's wrong, cause I'm not at my computer right now but, can you post error message text? That will definitely help.
    There is a "simple" Standard shader version, dont know which platform It's for, but maybe I should look at this... Have a feeling that version should work with Metal
     
  41. stickadtroja

    stickadtroja

    Joined:
    Oct 25, 2015
    Posts:
    33
  42. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    Are you by any chance using Unity 5.3? If so - you may have downloaded the 5.4 version of the shader.
    To my knowledge instancing is a new 5.4 feature that does not exist in 5.3. Thus the message about the missing UnityInstancing.cginc.
     
    Alverik likes this.
  43. stickadtroja

    stickadtroja

    Joined:
    Oct 25, 2015
    Posts:
    33
    ah yeah i was using 5.3. weirdly it wouldnt update when i checked "updates". works fine now
     
  44. traunay

    traunay

    Joined:
    Oct 24, 2013
    Posts:
    16
    Hi,
    I've updated my unity to 5.4 and also updated the shader to the latest but I still get the error

    Shader error in 'Standard Specular (Vertex Color)': invalid subscript 'posWorld' at Assets/UnityVC/UnityVC.cginc(299) (on d3d11)

    Compiling Vertex program with DIRECTIONAL SHADOWS_SCREEN LIGHTMAP_OFF DYNAMICLIGHTMAP_OFF _EMISSION _VERTEXCOLOR _NORMALMAP _SPECGLOSSMAP
    Platform defines: UNITY_NO_SCREENSPACE_SHADOWS UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF2 UNITY_HARDWARE_TIER3

    Been trying to figure out what's going on for the last couple of hours but can't seem to make it work. Can someone show me what the UnityVC.cginc on line 299 supposed to be (I want to diff/compare to see if mine is actually the latest)?

    Thanks
     
  45. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    I've seen that too. But he did mention it in a previous post. The Specular version is still not working properly, but the one based on metalness should be. If you have the option, you could use that one in the meantime.
     
  46. traunay

    traunay

    Joined:
    Oct 24, 2013
    Posts:
    16
    well, it's not what shader is working or not but any shader is pretty much not working right now cause that error in that file won't allow unity to compile.

    Has anyone has package where it doesn't produce shader compile error in 5.4?
     
  47. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    It should say "Unity Standard Vertex Color Shader Lib v0.92 (for Unity 5.4.0)" at the top of the file. I believe you are building for Android or iOS? Try switching to PC or WebGL and see if it fixes the error
     
  48. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Umm, you know, you should be able to just look for the offending shader file and take it out of the project folder, if it's the only file giving an error that should fix the problem without affecting any else... I'm away from my PC but I think the metalness and specular versions should be different shader files (at least, that's usually how it goes)
     
  49. stickadtroja

    stickadtroja

    Joined:
    Oct 25, 2015
    Posts:
    33
    any chance the vertex colours could affect the emissive texture aswell? :)
     
  50. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    That's feasible, but it makes shader less multipurpose. Anyway, I'll think about that.