Search Unity

The simplest shader

Discussion in 'Shaders' started by Omar Rojo, May 4, 2007.

  1. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Some one can tell me why the Simplest Vertex and Fragment program con the CG Tutorial doesn't work with Unity ?

    I keep getting errors like:

    - Undefined channel 'POSITION'
    - 'POSITION' is not visible in the profile
    - etc.

    The shader i'm trying is:

    Code (csharp):
    1. Shader "New Shader" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,0.5)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Pass {
    8.         CGPROGRAM
    9.         // profiles arbfp1
    10.         // vertex vert
    11.         // fragment frag
    12.        
    13.         struct VOut {
    14.             float4 position : POSITION;
    15.             float4 color : COLOR;
    16.         };
    17.        
    18.         struct FOut {
    19.             float4 color : COLOR;
    20.         };
    21.        
    22.         VOut vert (float2 position : POSITION)
    23.         {
    24.             VOut output;
    25.            
    26.             output.position = float4 (position, 0, 1);
    27.             output.color = float4 (0, 1, 0, 1);
    28.            
    29.             return output;
    30.         }
    31.        
    32.         FOut frag (float4 color : COLOR)
    33.         {
    34.             FOut output;
    35.            
    36.             output.color = color;
    37.            
    38.             return output;
    39.         }
    40.        
    41.         ENDCG      
    42.         }
    43.     }
    44. }
    Thanks in advance, i will check for it tomorrow morning.

    Omar Rojo
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    On quick scan, one problem is that it's missing:
    Code (csharp):
    1. #include "UnityCG.cginc"
    before the struct VOut { line. There could be more problems, but hopefully this helps. Might want to start with the tutorials in the ShaderLab documentation instead/also.

    -Jon
     
  3. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    I should depend on the UnityCG library ? I don't wan't to, that is why i omitted, and i have already read an re-read the ShaderLab documentation.

    I want my shaders to be 100% mine. I'll check it though.

    Omar Rojo
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Currently vertex shader inputs are not based on semantics; instead they must use predefined names (vertex, color, normal, texcoord, texcoord1, tangent).

    So in your case you should change vertex shader prototype to:
    Code (csharp):
    1. VOut vert (float2 vertex)
    and later in the shader use "vertex" as the variable name:
    Code (csharp):
    1. output.position = float4 (vertex, 0, 1);
    Using UnityCG.cginc is nothing magic, it just brings some parameters that are exposed by the engine into the shaders, and has some commonly used functions. You can always check out the contents of that file in Unity.app/Contents/CGIncludes/UnityCG.cginc
     
  5. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Is a Unity limitation not to specify semantics for the vertex program's input ? because, according to the book, there should be semantics in CG.

    I have already show the sphere with the shader without using UnityCG.cginc :D

    Thanks both, im learning so much :D, expect to hear more from me :oops:

    Omar Rojo
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Vertex shader input semantics are there only for a single reason: so that the engine would know what data to pass where. If the engine can figure that out by some other means, it's fine as well.

    Currently Unity figures that out based on the names (not semantics). In the future this will be changed, so you can either use names (like it's done now), or use whatever names you like and pass Unity-understandable semantics.

    If semantics are not indicated, Cg compiler assigns them automatically "somehow". For OpenGL, all vertex components get mapped to "generic vertex attributes", and that's fine.
     
  7. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    Yes Aras, I understand perfectly, and i can assume how it can automagically pass the data too (based on the data types and the order of the parameters can be a type), as long as it's logical.

    It's just i want to learn CG as it should be according to the book, but i understand perfectly what you mean.

    Omar Rojo