Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

U 3.0 surface shader + vertex shader mixing

Discussion in 'Shaders' started by llavigne, Jul 13, 2010.

  1. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    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

     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,325
    Moving to ShaderLab section.
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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):

    Code (csharp):
    1. Shader "Diffuse with Vertex Colors" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", 2D) = "white" {}
    4. }
    5. SubShader {
    6.     Tags { "RenderType"="Opaque" }
    7.     LOD 200
    8.  
    9. CGPROGRAM
    10. #pragma surface surf Lambert
    11.  
    12. sampler2D _MainTex;
    13.  
    14. struct Input {
    15.     float2 uv_MainTex;
    16.     float4 color : COLOR; //< HERE!
    17. };
    18.  
    19. void surf (Input IN, inout SurfaceOutput o) {
    20.     half4 c = tex2D(_MainTex, IN.uv_MainTex);
    21.     c *= IN.color; //< HERE!
    22.     o.Albedo = c.rgb;
    23.     o.Alpha = c.a;
    24. }
    25. ENDCG
    26. }
    27. Fallback "VertexLit"
    28. }
    29.  
    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.
     
  4. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    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 ?
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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.
     
  6. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    What's the syntax to do that, give me an example of surface shader that does something cool on vertices.
     
  7. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Any of the tree/grass shaders in builtin shaders.

    Quick example (might not compile, typing out of my head):
    Code (csharp):
    1.  
    2. Shader "Extrude Vertices Along Normals" {
    3.   SubShader {
    4.     Tags { "RenderType" = "Opaque" }
    5.     CGPROGRAM
    6.     #pragma surface surf Lambert vertex:vert
    7.     struct Input {
    8.         float4 color : COLOR;
    9.     };
    10.     void vert (inout appdata_full v) {
    11.         v.vertex.xyz += v.normal;
    12.     }
    13.     void surf (Input IN, inout SurfaceOutput o) {
    14.         o.Albedo = 1;
    15.     }
    16.     ENDCG
    17.   }
    18.   Fallback "Diffuse"
    19. }
    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: http://twitter.com/aras_p/status/18598257688
     
  8. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    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 ?
     
  9. Mike99

    Mike99

    Joined:
    Dec 11, 2011
    Posts:
    155
    so it's possible to mix surface and vertex functions inside a shader isn't it ?
     
  10. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    "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.
     
  11. Mike99

    Mike99

    Joined:
    Dec 11, 2011
    Posts:
    155
    Last edited: Jun 28, 2014
  12. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    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.
     
  13. Mike99

    Mike99

    Joined:
    Dec 11, 2011
    Posts:
    155
    good tip thanks
     
  14. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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.
     
  15. Orr10c

    Orr10c

    Joined:
    Sep 11, 2016
    Posts:
    45
    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! :)

    the shader should look something like this:
     

    Attached Files:

unityunity