Search Unity

Vertex Color Shader Texture Blending Problem

Discussion in 'Shaders' started by AcornBringer, Nov 25, 2019.

  1. AcornBringer

    AcornBringer

    Joined:
    Mar 22, 2015
    Posts:
    70
    Hey there!

    I created a simple vertex color shader inside Shader Forge based off the example they have on the wiki page here: Acedikmo/Shaderforge Wiki: Texture Splatting

    Mine is essentially the same only I'm using just 4 textures and I added a color property for each of them:



    It seems to work but I'm noticing some weird behavior where two colors are supposed to blend. Instead of blending between the two textures, it looks like another texture fades in instead. Below you can see my vertex colors in Maya and then below that the material applied to the same mesh in Unity.



    Where I circled is where it should be blending from blue to green or blue to red but somehow, the leaf texture shows up instead.

    If someone could let me know what's going on here, I would really appreciate it!

    Thanks :)
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The vertex colors shouldn't blend from one to another, ie: one vertex shouldn't be RGB (0, 255, 0), and the next be RGB (255, 0, 0). Half way between those two vertices the interpolated vertex color is going to be RGB (127, 127, 0), which if you're using black to mean the base texture will mean whatever the base texture is will show through.

    If you're going to pack 4 layers into 3 channels of the vertex color, make sure you go from RGB (0, 255, 0), to RGB (255, 255, 0), and assuming your red vertex color is the top layer, fade out the green only "inside" the fully red areas. Alternatively you can use a slightly more complex setup where you only fade to the base layer in areas where the total sum of all 3 vertex color channels is < 1.

    Code (csharp):
    1. float blendNormalization = max(1, dot(vertexColor.xyz, float3(1,1,1)); // dot product is a quick way to add all 3
    2. float3 blend = vertexColor.xyz / blendNormalization;
    edit: opps, this is wrong
     
    Last edited: Nov 26, 2019
  3. AcornBringer

    AcornBringer

    Joined:
    Mar 22, 2015
    Posts:
    70
    I think I understand; lerping between the colors doesn't really work the way I expected and I'm not getting a true blend of color but instead it's dipping into a darker tone while transitioning, giving me the extra texture.

    I'm only going off the example inside that wiki page so I don't need to use 4 layers. If I only use 3 (RGB) layers, would that prevent this odd blending from happening and allow me to put which ever color next to which ever? I'm still trying to work this out in shader forge at the moment based off your explanation so any more explanation in the mean time would be great!
     
  4. AcornBringer

    AcornBringer

    Joined:
    Mar 22, 2015
    Posts:
    70
    Okay looks like this works! Just using three this time and adding them together instead of doing the lerp.



    Thanks for all the explanation and help :)
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Kinda? It's not blending from color to color via a hue transition if that's what you mean. It's not going green (0,255,0) => yellow (255,255,0) => red (255,0,0). The term lerp stands for "linear interpolation", which means each RGB component of the color is changing linearly.
    upload_2019-11-26_9-12-35.png
    The half way point between them is 50% of each. By removing your background layer texture it's no longer as obvious that this is the case, but it's still happening.

    What you want is something more like this:
    upload_2019-11-26_9-17-39.png
    Which I realize my above code doesn't actually do properly. A simply multiply by 2 and clamp between 0 and 1 of the vertex color before usage will do that just fine. That will change the shape of the transitions a little.