Blending with all materials of submeshes

Hi,

The model that I have has a couple of submeshes with different materials.
The shader I have is just a simple color blend but I would like it to blend with the different submeshes materials. What do I need to add to achieve this?

Thanks
Greg

Shader "SimpleBlend" {
   
Properties {
   _Color ("Main Color", Color) = (1,1,1,0.5)
}

SubShader {
   
   ZWrite Off
   Tags { "Queue" = "Transparent" }
   Blend One OneMinusSrcAlpha
   

   Pass {

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#include "UnityCG.cginc"

float4 _Color;

struct v2f {
   V2F_POS_FOG;
};

v2f vert (appdata_base v)
{
   v2f o;
   PositionFog( v.vertex, o.pos, o.fog );
   
   return o;
}

half4 frag () : COLOR
{ 
   return half4(_Color);
}
ENDCG

    }
}
Fallback "Transparent/Diffuse"
}

Could someone PLEASE give me some hint on this?

Thanks
Greg

Can you better explain what you would like? You say “The shader I have is just a simple color blend but I would like it to blend with the different submeshes materials”. Shaders don’t blend materials. What do you mean?

Thanks for the reply.

So here is a screen-shot of the materials that are on the object.
There are 3 different materials on 3 different sub-meshes of the object. When I select this object I add a 4th material with the shader that I mentioned before. The shader is transparent, so I imagined that if I put it as a last material everything that precedes it in the materials array will get shown with the shader on top. But it seems that its only the last material that shows with the shader.
So my question is, how would I go about making all other materials show with the shader?
Or is there a better way of sort of highlighting objects that has sub-meshes and different materials?

Thanks a lot
Greg

I don’t think you can blend a shader across materials. You would probably need to combine them to use the same shader.

Hi,

Ok I understand.
But could you please describe a bit more how to combine multiple materials to share the same shader?

The goal here is to highlight the selected object.

Thanks
Greg

You would combine submesh into eachother using SetTriangles and lower the submesh count.

The material list is not a sequence of materials, but a list of one material per sub-mesh. So technically you cannot simply add a material to have it applied to all those before.

Except it appears in Unity editor that you can arbitrarily add materials, which I was very surprised to discover as it doesn’t make any real sense.

This means if your model has a single mesh, then you can overlay other materials. I guess it must just treat them as separate passes. I’m unsure if this is intentional, but its quite a cool feature.

Of course once you have more than one submesh the ‘overlay’ technique breaks down. From a quick test the additional materials are always applied to the last submesh (i.e last real material).

As for your actual question, you need to either

a. Assign you blending shader to all the materials of the model, though losing their original material.

b. Write a new shader to incorporate the blending. Depending upon your requirements, this could be as simple as adding an alpha property. Alternatively perhaps add it as an additional pass, though you may have to swap shaders/materials still.

c. Combine the sub-meshes, though if they are actual meant to be using different materials, with say different textures applied, then this will force them to all use the same shader. However there should now only be one submesh so using your original idea would work.

d. Duplicate the model, then combine its meshes, thus arriving at ‘c’ above. However now you can apply your blending shader/material to the duplicate and if the shader states are set up correctly will blend over the original model.

1 Like

Thanks a lot Noisecrime for the explications. That is something I didn’t understand before.

Now about your propositions.
The materials on the submeshes have different shaders and textures so I cannot make something that will result in loosing that information.
The only option I have is to add something to them that will keep the original look but will blend and highlight the part I am selecting.

So the method I wanted to use was to create a new material slot and add there the material that holds the transparent shader with a color that would highlight the mesh, but this shader that I am using at the moment shows only with the last material of the array and since this last material is used only on a submesh, only that part get highlighted where the last material is.

I’ve looked into the combine mesh proposition of kingdruid, and I’ve found this (mesh.CombineMesh):
http://forum.unity3d.com/viewtopic.php?t=35239
I don’t know if it is the same as what kingdruid suggested with the set triangles.
The other thing is that the full mesh of the sclera for example contains more than 60k vertices, so as I’ve read somewhere I can’t use combine mesh where that vertex limit is reached.

So at this point, I think the shader option would be the most suitable, but what should I add to the shader that I already have (first post) to get the desired result?

EDIT: I’ve edited this whole post because some things I wrote were confusing.

Okay I think I have an idea.
Could you please tell me if it is something usable?

So lets say the object has 3 sub-meshes with 3 different materials using different shaders and textures.
Lets say Specular with only main color,
Transparent/Specular with a texture and
Bumped Specular with another texture and its bump.

So how about modifying each shader used, to have an additional texture slot. When the object is not selected it would be empty so that would make no effect to the textures already used by the shader. But when it becomes selected it would look up the materials used on the object and put a texture in the slot and the shader would blend this texture according to its alpha with the textures or colors already used in the material, thus giving the desired “highlighted” effect.

Would it work like this?

Thanks a lot
Greg

EDIT: Having re-read this thread I think Noisecrime was talking about a similar solution! So disregard the first sentence of this post please!

It seems like it works like this.
I didn’t use a texture though, adding a simple color and setting its alpha was sufficient.

Thanks a lot
Greg