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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Surface shader's vertex input struct (appdata) needs to have a 'float4 color' member.

Discussion in 'Shaders' started by markashburner, Jun 23, 2018.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Would anyone be able to help me out with this error I get in the console for this shader?

    Surface shader's vertex input struct (appdata) needs to have a 'float4 color' member.

    I am trying to add tessellation to this shader.

    The console error obviously has something to do with my vertex input struct(appdata).

    How do I add the float4 color member the struct appdata below?

    Code (CSharp):
    1. struct appdata {
    2.     float4 vertex : POSITION;
    3.     float4 tangent : TANGENT;
    4.     float3 normal : NORMAL;
    5.     float2 texcoord : TEXCOORD0;
    6.     float2 texcoord1 : TEXCOORD1;
    7.     float2 texcoord2 : TEXCOORD2;
    8. };
    Here is my entire shader.

    Code (CSharp):
    1. Shader "PlanetaryTerrain/Planet Bumped Specular (displacement)" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.     _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
    6.     _Parallax ("Height", Range (0.0, 1.0)) = 0.5
    7.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    8.     _BumpMap ("Normalmap", 2D) = "bump" {}
    9.     _ParallaxMap ("Heightmap (A)", 2D) = "black" {}
    10.  
    11.     _EdgeLength ("Edge length", Range(3,50)) = 10
    12.  
    13.     _Tex1 ("Texture 0 (Beach)", 2D) = "white" {}
    14.     _Nor1 ("Normal", 2D) = "bump" {}
    15.     _Color1 ("Color", Color) = (1,1,1,1)
    16.     _Glossiness1 ("Smoothness", Range(0,1)) = 0.5
    17.     _Metallic1 ("Metallic", Range(0,1)) = 0.0
    18.     _TexScale1 ("Texture Scale", Float) = 1
    19.        
    20.     _Tex2 ("Texture 1", 2D) = "white" {}
    21.     _Nor2 ("Normal", 2D) = "bump" {}
    22.     _Color2 ("Color", Color) = (1,1,1,1)
    23.     _Glossiness2 ("Smoothness", Range(0,1)) = 0.5
    24.     _Metallic2 ("Metallic", Range(0,1)) = 0.0
    25.     _TexScale2 ("Texture Scale", Float) = 1
    26.  
    27.     _Tex3 ("Texture 2", 2D) = "white" {}
    28.     _Nor3 ("Normal", 2D) = "bump" {}
    29.     _Color3 ("Color", Color) = (1,1,1,1)
    30.     _Glossiness3 ("Smoothness", Range(0,1)) = 0.5
    31.     _Metallic3 ("Metallic", Range(0,1)) = 0.0
    32.     _TexScale3 ("Texture Scale", Float) = 1
    33.  
    34.     _Tex4 ("Texture 3", 2D) = "white" {}
    35.     _Nor4 ("Normal", 2D) = "bump" {}
    36.     _Color4 ("Color 4", Color) = (1,1,1,1)
    37.     _Glossiness4 ("Smoothness", Range(0,1)) = 0.5
    38.     _Metallic4 ("Metallic", Range(0,1)) = 0.0
    39.     _TexScale4 ("Texture Scale", Float) = 1
    40.  
    41.     _Tex5 ("Texture 4", 2D) = "white" {}
    42.     _Nor5 ("Normal", 2D) = "bump" {}
    43.     _Color5 ("Color", Color) = (1,1,1,1)
    44.     _Glossiness5 ("Smoothness", Range(0,1)) = 0.5
    45.     _Metallic5 ("Metallic", Range(0,1)) = 0.0
    46.     _TexScale5 ("Texture Scale", Float) = 1
    47.  
    48.     _Tex6 ("Texture 5 (Mountains)", 2D) = "white" {}
    49.     _Nor6 ("Normal", 2D) = "bump" {}
    50.     _Color6 ("Color", Color) = (1,1,1,1)
    51.     _Glossiness6 ("Smoothness", Range(0,1)) = 0.5
    52.     _Metallic6 ("Metallic", Range(0,1)) = 0.0
    53.     _TexScale6 ("Texture Scale", Float) = 1
    54. }
    55. SubShader {
    56.     Tags { "RenderType"="Opaque" }
    57.     LOD 800
    58.    
    59. CGPROGRAM
    60. #pragma surface surf BlinnPhong addshadow vertex:disp tessellate:tessEdge
    61. #include "Tessellation.cginc"
    62.  
    63.         sampler2D _Tex1;
    64.         sampler2D _Tex2;
    65.         sampler2D _Tex3;
    66.         sampler2D _Tex4;
    67.         sampler2D _Tex5;
    68.         sampler2D _Tex6;
    69.  
    70.         sampler2D _Nor1;
    71.         sampler2D _Nor2;
    72.         sampler2D _Nor3;
    73.         sampler2D _Nor4;
    74.         sampler2D _Nor5;
    75.         sampler2D _Nor6;
    76.  
    77.         float _TexScale1;
    78.         float _TexScale2;
    79.         float _TexScale3;
    80.         float _TexScale4;
    81.         float _TexScale5;
    82.         float _TexScale6;
    83.  
    84. struct appdata {
    85.     float4 vertex : POSITION;
    86.     float4 tangent : TANGENT;
    87.     float3 normal : NORMAL;
    88.     float2 texcoord : TEXCOORD0;
    89.     float2 texcoord1 : TEXCOORD1;
    90.     float2 texcoord2 : TEXCOORD2;
    91. };
    92.  
    93.  
    94. float _EdgeLength;
    95. float _Parallax;
    96.  
    97. float4 tessEdge (appdata v0, appdata v1, appdata v2)
    98. {
    99.     return UnityEdgeLengthBasedTessCull (v0.vertex, v1.vertex, v2.vertex, _EdgeLength, _Parallax * 1.5f);
    100. }
    101.  
    102. sampler2D _ParallaxMap;
    103.  
    104. void disp (inout appdata v)
    105. {
    106.     float d = tex2Dlod(_ParallaxMap, float4(v.texcoord.xy,0,0)).a * _Parallax;
    107.     v.vertex.xyz += v.normal * d;
    108. }
    109.  
    110. sampler2D _MainTex;
    111. sampler2D _BumpMap;
    112. fixed4 _Color;
    113. half _Shininess;
    114.  
    115. struct Input {
    116.     float2 uv_MainTex;
    117.     float2 uv_BumpMap;
    118.     float2 uv_Tex1;
    119.     float2 uv4_Tex2;
    120.     float4 color: COLOR;
    121. };
    122.  
    123.         half _Glossiness1;
    124.         half _Metallic1;
    125.         fixed4 _Color1;
    126.  
    127.         half _Glossiness2;
    128.         half _Metallic2;
    129.         fixed4 _Color2;
    130.  
    131.         half _Glossiness3;
    132.         half _Metallic3;
    133.         fixed4 _Color3;
    134.  
    135.         half _Glossiness4;
    136.         half _Metallic4;
    137.         fixed4 _Color4;
    138.  
    139.         half _Glossiness5;
    140.         half _Metallic5;
    141.         fixed4 _Color5;
    142.  
    143.         half _Glossiness6;
    144.         half _Metallic6;
    145.         fixed4 _Color6;
    146.  
    147. void surf (Input IN, inout SurfaceOutput o) {
    148.  
    149.             fixed4 c1 = tex2D (_Tex1, IN.uv_Tex1 * _TexScale1) * _Color1;
    150.             fixed4 c2 = tex2D (_Tex2, IN.uv_Tex1 * _TexScale2) * _Color2;
    151.             fixed4 c3 = tex2D (_Tex3, IN.uv_Tex1 * _TexScale3) * _Color3;
    152.             fixed4 c4 = tex2D (_Tex4, IN.uv_Tex1 * _TexScale4) * _Color4;
    153.             fixed4 c5 = tex2D (_Tex5, IN.uv_Tex1 * _TexScale5) * _Color5;
    154.             fixed4 c6 = tex2D (_Tex6, IN.uv_Tex1 * _TexScale6) * _Color6;
    155.  
    156.    
    157.     o.Albedo = IN.color.r * c1 + IN.color.g * c2 + IN.color.b * c3 + IN.color.a * c4 + IN.uv4_Tex2.x * c5 + IN.uv4_Tex2.y * c6;
    158.     o.Gloss = IN.color.r * _Glossiness1 + IN.color.g * _Glossiness2 + IN.color.b * _Glossiness3 + IN.color.a * _Glossiness4 + IN.uv4_Tex2.x * _Glossiness5 + IN.uv4_Tex2.y * _Glossiness6;
    159.     //o.Alpha = tex.a * _Color.a;
    160.     //o.Specular = _Shininess;
    161.     o.Normal = UnpackNormal(tex2D(_Nor1, IN.uv_Tex1 * _TexScale1)) * IN.color.r + UnpackNormal(tex2D(_Nor2, IN.uv_Tex1 * _TexScale2)) * IN.color.g
    162.             + UnpackNormal(tex2D(_Nor3, IN.uv_Tex1 * _TexScale3)) * IN.color.b + UnpackNormal(tex2D(_Nor4, IN.uv_Tex1 * _TexScale4)) * IN.color.a
    163.             + UnpackNormal(tex2D(_Nor5, IN.uv_Tex1 * _TexScale5)) * IN.uv4_Tex2.x + UnpackNormal(tex2D(_Nor6, IN.uv_Tex1 * _TexScale6)) * IN.uv4_Tex2.y;
    164. }
    165. ENDCG
    166. }
    167.  
    168. FallBack "Bumped Specular"
    169. }
     
  2. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    372
    By adding a float4 color to the appdata struct, doesn't that mean just:


    Code (CSharp):
    1.     struct appdata {
    2.         float4 vertex : POSITION;
    3.         float4 tangent : TANGENT;
    4.         float3 normal : NORMAL;
    5.         float2 texcoord : TEXCOORD0;
    6.         float2 texcoord1 : TEXCOORD1;
    7.         float2 texcoord2 : TEXCOORD2;
    8.         float4 color : COLOR;
    9.     };
    10.  
    Or does that give an error or not work still?
     
  3. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Yes I have tried that before and it gives me this error:

    Shader error in 'PlanetaryTerrain/Planet Bumped Specular (displacement)': invalid subscript 'texcoord3' at line 329 (on d3d11)

    Compiling Vertex program with DIRECTIONAL
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_FULL_HDR
     
  4. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Ok I have managed to solve this issue by adding
    float2 texcoord3 : TEXCOORD3; and float4 color : COLOR;


    struct appdata {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD0;
    float2 texcoord1 : TEXCOORD1;
    float2 texcoord2 : TEXCOORD2;
    float2 texcoord3 : TEXCOORD3;
    float4 color : COLOR;
    };

    However the colour is not coming out correctly because it seems to be very yellowish when the grass is mean't to be green?

    Anyone know how I can get the color to be correct?
     

    Attached Files:

  5. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    372
    Oh okay I see.. sorry xD I'm glad you fixed that. Now about the color not seeming to come through right, I would check your line where you set o.Albedo. Perhaps just to debug it, try using o.Albedo = IN.color; to make sure it looks correct. It might be in one of your C# scripts setting the vertex colors or something?

    Or if you also test with just one of the six textures at a time, like o.Albedo = c1; and see if that looks okay. That may help you find where the problem is coming from
     
  6. mdsaeid753

    mdsaeid753

    Joined:
    Aug 11, 2022
    Posts:
    3
    You can use :- float4 color : TEXCOORD4;
    TEXCOORD's can be used for float3 and float4 also;