U 3.0 surface shader + vertex shader mixing

How do I mix surface and vertex shader so as to grab the vertex color and pipe it in the surface shader ?

I’m getting error when I have vertex and surface shader in the same CG block

Moving to ShaderLab section.

You use one of predefined inputs; one of which is per-vertex color (all in the docs, but I agree that there should be more examples).

Typing out of the top of my head (could be wrong):

Shader "Diffuse with Vertex Colors" {
Properties {
	_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 200

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;

struct Input {
	float2 uv_MainTex;
	float4 color : COLOR; //< HERE!
};

void surf (Input IN, inout SurfaceOutput o) {
	half4 c = tex2D(_MainTex, IN.uv_MainTex);
	c *= IN.color; //< HERE!
	o.Albedo = c.rgb;
	o.Alpha = c.a;
}
ENDCG
}
Fallback "VertexLit"
}

You can’t “just” mix a surface shader and your own vertex shader; because a vertex pixel shader is already generated from your surface shader. No easy way to “mix in” your own shader into that.

Thanks, that was easy !

re: vertex shader unable to coexist with surface shader - I understand, although you could add a base.vertex mechanism within the surface shader no ? Or is it currently possible to do everything a vertex shader does inside a surface shader? like say move vertices ?

Re: doc
Yes I cannot have enough examples, to see the main combinations but what would help is in-text linking to reference pages, similar to the scripting doc (but IMO, no need to be as thorough since most of the language is cG)

By the way, do you set VS a certain way to write shaders ?

Probably not “everything”, but I tried to make them handle the cases I and some folks who provided feedback though about.

Yes, moving vertices is possible.

What is “VS”? Visual Studio? Personally I just use whatever text editor is at hand.

What’s the syntax to do that, give me an example of surface shader that does something cool on vertices.

Any of the tree/grass shaders in builtin shaders.

Quick example (might not compile, typing out of my head):

Shader "Extrude Vertices Along Normals" {
  SubShader {
    Tags { "RenderType" = "Opaque" }
    CGPROGRAM
    #pragma surface surf Lambert vertex:vert
    struct Input {
        float4 color : COLOR;
    };
    void vert (inout appdata_full v) {
        v.vertex.xyz += v.normal;
    }
    void surf (Input IN, inout SurfaceOutput o) {
        o.Albedo = 1;
    }
    ENDCG
  }
  Fallback "Diffuse"
}

This does not do anything interesting, just makes a white object with vertices pushed along object normals. But it says “vertex modification function is ‘vert’”, and does the modification in that function.

Edit - here’s a shader a picture to illustrate this: x.com

Thanks that’s useful, and Fun - got stung by bees?

Note on the shader doc regarding this :

I found the reference to vertex example, it took some digging because shaders have two names file and menu - better use the shader file name when making reference to external examples.
Also where is the function TerrainAnimateTree ?

so it’s possible to mix surface and vertex functions inside a shader isn’t it ?

“mix” is a loaded verb. You gotta realize that every surface shader internally generates its vertex shader and fragment shader programs. So if you want to add some customization to the vertex shader program, Surface Shaders let you write a function for that (and link it in via the vertex:myfunc syntax), which the Surface Shader’s generated vertex program will then call.

ok nice, is there a unity doc about that ?

nevermind i found it (Unity - Manual: Writing Surface Shaders)

If you like to investigate the internals, you can also add a #pragma debug to any Surface Shader, then if you click “open compiled shader” in Inspector you can see the intermediate Cg code with the generated vertex and fragment programs, good to see sometimes how and where custom funcs (such as vertex:myvert and custom lighting models) as well as the main “surf” code tie in the full shader programs that Unity sends to the GPU to run.

good tip thanks

FYI, in Unity 4.5 to see generated surface shader code you don’t need to add #pragmas. Just press “show generated code” in the shader inspector.

Hello, I need a shader suitable for a low poly game i can’t really describe the shader’s properties cause i don’t really know much about shaders, I am generating my own mesh and coloring it by the mesh’s vertices so i need a mesh the also supports vertex coloring if anybody can answer the challenege and provide me with such shader i’ll be very grateful! :slight_smile:

the shader should look something like this: