Skip to content

Instantly share code, notes, and snippets.

@jhorikawa
Created January 15, 2018 18:39
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jhorikawa/7a236ae0b801bf2aca2d8a3038b9bf40 to your computer and use it in GitHub Desktop.
Save jhorikawa/7a236ae0b801bf2aca2d8a3038b9bf40 to your computer and use it in GitHub Desktop.
Unity Shader to visualize vertex color.
Shader "Custom/VertexColor" {
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert
#pragma target 3.0
struct Input {
float4 vertColor;
};
void vert(inout appdata_full v, out Input o){
UNITY_INITIALIZE_OUTPUT(Input, o);
o.vertColor = v.color;
}
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = IN.vertColor.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
@mikedolan03
Copy link

I'm going to figure out how to add some transparency to your shader. Thanks for sharing!

@arminak1992
Copy link

arminak1992 commented Apr 2, 2019

I'm going to figure out how to add some transparency to your shader. Thanks for sharing!

hey, did u manage to do it? I see that u added o.Alpha = .5; but its not working for me, in my case, im trying to stream rgba from grasshopper and I have issues with alpha and saturation.

@Narcha
Copy link

Narcha commented Apr 9, 2019

Thanks a bunch, this is exactly what i needed.

@manuelrossner
Copy link

@jhorikawa Still works great for Unity 2019.2.
Do you have an idea how to use it with the Lightweight-Renderpipeline?

@Hamu72
Copy link

Hamu72 commented Jun 9, 2020

hi jhorikawa

Thank you for sharing this shader code! I'm not a programmer just a gfx guy :)

I made a test. I changed your code in line 20:
o.Emissive = IN.vertColor.rgb;
because I want only vertex color lit on the scene without any lamp. (vertex color with ambient support)

But I have a "small" problem in Unity.
image
Left is the original mesh in Maya without lighting/shading - vertex color as "emissive"
The mesh's edges are smooth (each vertex has 1 vertex normal).

When I importing the mesh with vertex color and give this shader to them the colors

  1. changed to yellow (no lights in the scene)
  2. looks like there is some kind of shading and looks like the vertex normals changed

I don't know how can I fix this.
Do you have any idea?

@Hamu72
Copy link

Hamu72 commented Jun 9, 2020

Ok!

I solved the bug. I tweaked Maya baking and fixed the bug!
Your shader working well!!!
image
(The gamma is different)

@fakhirali
Copy link

Lifesaver🙌🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment