Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Vertex Paint

Discussion in 'Editor & General Support' started by coin-god, Mar 28, 2010.

  1. coin-god

    coin-god

    Joined:
    Jan 21, 2010
    Posts:
    325
    Is it possible to use a Mesh that has a Blend material with 2 textures applyed?

    In wich I used vertex Paint to mix them?
    (Pretty much like what you do in unity terrain system)

    Im trying to get it in unity, but it only shows one of the textures. (So does the material)

    Im using MAX.
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Yes, it's pssobile, but you need the right shader for it, and Unity doesn't ship with one. Nor do I see one on the wiki.

    If you're just using the alpha channel of the vertex colors, then the simplest thing you can use is this:

    Code (csharp):
    1. Shader "Blend 2 by Vertex Alpha" {
    2.  
    3. Properties
    4. {
    5.     _MainTex ("Texture 1  (vertex A  = white)", 2D) = ""
    6.     _Texture2 ("Texture 2  (vertex A = black)", 2D) = ""
    7. }
    8.  
    9. SubShader
    10. {
    11.     BindChannels
    12.     {
    13.         Bind "vertex", vertex
    14.         Bind "color", color
    15.         Bind "texcoord", texcoord
    16.     }
    17.    
    18.     Pass
    19.     {
    20.         SetTexture [_MainTex]
    21.         SetTexture [_Texture2] {combine previous lerp(primary) texture}
    22.     }
    23. }
    24.  
    25.  
    26. }
     
  3. coin-god

    coin-god

    Joined:
    Jan 21, 2010
    Posts:
    325
    Tested it, but it didnt work. I have no experience on writing shaders, but i placed it right(i think). :(

    Only diference is that now it dosnt support shadows.


    Maybe I forgot to say that im using a gradient to mix the colors:



    This is what it should look like:
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Don't have a clue what that means, and I don't have Max. But it's as simple as this: that shader mixes two textures based on the alpha channel of the vertex colors.
     
  5. coin-god

    coin-god

    Joined:
    Jan 21, 2010
    Posts:
    325
    So, why do you think its not working? Should I modifie the material in MAX?
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Materials don't transfer between Max and Unity. If you selected this shader for your material, dragged two textures onto its variable slots, and have vertex alpha that isn't all white or black, it will just work.

    From your screenshots, it doesn't even look to me like you have vertex blending happening in Max. And I can't tell if you selected two different textures for the material in Unity.
     
  7. coin-god

    coin-god

    Joined:
    Jan 21, 2010
    Posts:
    325
    Well, here I show you how the 2 textures are blended in the model:



    One in white, the other in Black. (An in unity I used your shader and applyed both textures)

    What I dont understand is what you mean by "vertex alpha".

    I should add, im using map channel 3 for the vertex paint modifier.
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Vertex colors are RGBA. If you're using the RGB channels instead of the A channel for blending two textures (which I don't recommend), then you'd need this shader instead:

    Code (csharp):
    1. Shader "Blend 2 by Vertex RGB" {
    2.  
    3. Properties
    4. {
    5.     _MainTex ("Texture 1  (white vertices)", 2D) = ""
    6.     _Texture2 ("Texture 2  (black vertices)", 2D) = ""
    7. }
    8.  
    9. SubShader
    10. {
    11.     BindChannels
    12.     {
    13.         Bind "vertex", vertex
    14.         Bind "color", color
    15.         Bind "texcoord", texcoord
    16.     }
    17.    
    18.     Pass
    19.     {
    20.         SetTexture [_MainTex] {Combine texture * primary}
    21.         SetTexture [_Texture2] {Combine texture * one - primary + previous}
    22.     }
    23. }
    24.  
    25.  
    26. }
    Note that this won't work on as much hardware as the previous shader will.

    I don't know what that means, exactly, but if it's like Blender, then you can have multiple vertex paint layers. In that case, Unity uses the first one.
     
  9. MattFS

    MattFS

    Joined:
    Jul 14, 2009
    Posts:
    219
    well - perhaps export it with that data in vertex color instead of map3! :)
     
  10. phixell.p

    phixell.p

    Joined:
    Jan 14, 2011
    Posts:
    13
    Hi Jessy,
    I tried out the first version of the shader code you posted and it worked a treat.
    I'm wondering how hard it would be to mix 4 textures. Color one, and bump one for vertex zero alpha and color two and bump two for vertex full alpha.
     
  11. phixell.p

    phixell.p

    Joined:
    Jan 14, 2011
    Posts:
    13
    Shader "Custom/Diffuse_Bump_Vertex_Blend" {
    Properties {
    _MainTex ("Texture (vertex A = white)", 2D) = "" {}
    _BumpMap ("Bumpmap (vertex A = white)", 2D) = "bump" {}
    _Texture2 ("Texture (vertex A = black)", 2D) = "" {}
    _BumpMap2 ("Bumpmap (vertex A = black)", 2D) = "bump" {}
    }
    SubShader {
    BindChannels
    {
    Bind "vertex", vertex
    Bind "color", color
    Bind "texcoord", texcoord
    }

    Pass
    {
    SetTexture [_MainTex]
    SetTexture [_Texture2] {combine previous lerp(primary) texture}
    SetTexture [_BumpMap]
    SetTexture [_BumpMap2] {combine previous lerp(primary) texture}
    }
    Tags { "RenderType" = "Opaque" }
    CGPROGRAM
    #pragma surface surf Lambert
    struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
    float2 uv_Texture2;
    float2 uv_BumpMap2;
    };
    sampler2D _MainTex;
    sampler2D _BumpMap;
    sampler2D _Texture2;
    sampler2D _BumpMap2;
    void surf (Input IN, inout SurfaceOutput o) {
    o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
    o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    o.Albedo = tex2D (_Texture2, IN.uv_Texture2).rgb;
    o.Normal = UnpackNormal (tex2D (_BumpMap2, IN.uv_BumpMap2));
    }
    ENDCG

    }
    Fallback "Diffuse"
    }
     
  12. Rook3D

    Rook3D

    Joined:
    Sep 27, 2011
    Posts:
    19
    Sorry to re-necro this thread, but I'm trying to get Vertex painting to work using the above shaders.

    I've posted a question on Unity Answers about this : Texture blending using Vertex coloring with a Blender model - which I think has come down to these shaders.

    ( Nb: I figured out a rough Blender Vertex color Alpha value export if anyone is interested in that )

    I've tried both shaders RGBA RGB above, but they produce the same results:



    It seems as though only the very first color pixel is being taken from the textures to colorize the object ( the vertex blending seems to be working properly though ).

    I'm wondering if there is a particular texture import setting I (probably) don't know about?
     
  13. khellstr

    khellstr

    Joined:
    Feb 16, 2012
    Posts:
    72
    Hi

    I got that that shader which uses alpha to work somehow, Thanks for it.

    This seems to be old thread so anyone knows which hardware restrictions there are?

    I was thinking that I could use 4 different textures if I use all three RGB channels. If vertex color is black, then use some base texture(or textures: diffuse, spec, bump), and then add different textures depending on RGB values ( red for some textures, green for some and blue for some).

    Anyone knows if there is shader like that already made, or do I have to spend next 8 hours to learn how to make one?
     
unityunity