Vertex Colored shader problem

Well I’m using this vertex colored shader for models in my map editor, not all shaders work, it must be a vertex colored one.

Here’s the one I use:

Shader "RSShader" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_SpecColor ("Spec Color", Color) = (1,1,1,1)
		_Emission ("Emmisive Color", Color) = (0,0,0,0)
		_Shininess ("Shininess", Range (0.01, 1)) = 0.7
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}

	SubShader {
		Pass {
  
			Material {

				Emission [_Emission]    
			}
			Cull Off
			ZTest LEqual
			ColorMaterial AmbientAndDiffuse
			Lighting On
			SetTexture [_MainTex] {
				Combine texture * primary, texture * primary
			}
			SetTexture [_MainTex] {
				constantColor [_Color]
				Combine previous * constant DOUBLE, previous * constant
			} 
		}
	}

	Fallback "VertexLit", 1
}

But the problem is, that it kinda mixes up the colors… Here’s a quick pic to show you what I mean:

You see, the colors are slightly mixed up, not as it should be. Here’s how it’s supposed to be like (without the texture):

I’m quite sure that the problem is with my shader, although who knows… Maybe some shader-wizzard could prove me wrong or could help me out if the problem is indeed in my shader? :slight_smile:

Thanks

the problem is your model.
the model looks correct but your triangulation is incorrect or you didn’t ensure that the normals are correct.

you might have looked at it in the modeller before doing the triangulation but that by definition is worthless as modellers are incapable to render / visualize such models even remotely correct as long as they can use quads or even ngons
so ensure to triangulate them in the modeller to fix errors there instead of hoping that the exporter won’t break it, cause it will :slight_smile:

The issue is in exportation, but I don’t see what it has to do with triangulation. It just looks to me like you’re sharing vertices, where you need separate ones.

The shader also needs some work:

First, I don’t see a reason why you need emmisive color and a another color. Do you have some rationale for this?

Also, are you actually doing anything with alpha in another shader? You’re defining alpha in several places but not using it in the shader itself.

  • _SpecColor and _Shininess are useless; remove them.
  • You don’t need to explicitly define the alpha calculation when it is the same as RGB – take out the second texture * primary.
  • ZTest LEqual is the default. Remove this clutter.

Here’s what I figure your shader actually needs to be:

Shader "RSShader" {

Properties {
	_Color ("Emmisive Color", Color) = (1,1,1)
	_MainTex ("Texture", 2D) = ""
}

SubShader {
	Cull Off
	
	Lighting On
	Material {Emission [_Emission]}
	ColorMaterial AmbientAndDiffuse
	
	Pass {
		SetTexture[_MainTex] {Combine texture * primary Double}
	}
}

}

Hey guys, thanks for the answers!
Then it’s not modeller or exporter that is the problem, but my custom model loader script. These models are custom format and I got a script to load them at runtime, much like the runtime obj loader.
Then it must be something wrong with the loader… hm…

edit:
well aparently I’m shading the models by vertices, but the game where this model is from and my map editor is for, use models that share vertices and the coloring/shading is done by faces so this problem doesn’t occur in game, where as I’m doing vertex shading and it messes up. Any ideas how to do the shading by face or any other way to get over this problem? :S

Things are always interpolated between vertices. The only thing you can try is to disable the interpolation as that will apply the values of the first vertex(?) to the whole face. When using directx .fx files, you have to set ShadeMode = Flat to do this. I don´t know what it is called in shaderlab actually… And it may also doesn´t provide you the desired result.

It simply doesn’t exist. The only way to do this in Unity is via using the vertices/triangles you actually need; it has to be in the mesh itself.

http://forum.unity3d.com/threads/61625-How-to-get-Flat-(non-gouraud)-shading

Is there a way to avoid creating the shared vertices when generating the mesh in unity, or something like that so I wouldn’t get this problem with the mixed colors?

I don’t know what you mean. However your mesh is created, in order to have hard edges for lighting (split normals) / vertex colors, or uv seams, you need two vertices in the same place. Here’s a project I made which may illustrate the concept for you:

http://forum.unity3d.com/threads/58536-reveal-a-texture?p=375739&viewfull=1#post375739