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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple shader problem?

Discussion in 'Shaders' started by mikesawicki64, Apr 2, 2015.

  1. mikesawicki64

    mikesawicki64

    Joined:
    May 12, 2014
    Posts:
    33
    I'm getting this error

    Shader error in 'Custom/Specular Rim Light': incorrect number of arguments to numeric-type constructor at line 58 (on d3d11)

    based off of this code

    float3 viewDirection = normalize(float3(float4(_WorldSpaceCameraPos.xyz, 1.0) - i.posWorld).xyz);

    I've been writing and re-writing these simple shaders so I can learn, but I have no idea why this shader won't compile. Can anyone shed light? here's the whole shader

    Code (CSharp):
    1. Shader "Custom/Specular Rim Light"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1.0,1.0,1.0,1.0)
    6.         _SpecColor ("Spec Color", Color) = (1.0,1.0,1.0,1.0)
    7.         _Shininess ("Shininess", float) = 10
    8.         _RimColor ("Rim Color", Color) = (1.0,1.0,1.0,1.0)
    9.         _RimPower ("Rim Power", float) = 5
    10.     }
    11.    
    12.     SubShader
    13.     {
    14.         Pass
    15.         {
    16.             Tags{"LightMode" = "ForwardBase"}
    17.            
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.            
    22.             uniform float4 _Color;
    23.             uniform float4 _SpecColor;
    24.             uniform float _Shininess;
    25.             uniform float4 _RimColor;
    26.             uniform float _RimPower;
    27.            
    28.             uniform float4 _LightColor0;
    29.            
    30.             struct vertexInput
    31.             {
    32.                 float4 vertex : POSITION;
    33.                 float3 normal : NORMAL;
    34.             };
    35.            
    36.             struct vertexOutput
    37.             {
    38.                 float4 pos : SV_POSITION;
    39.                 float4 posWorld : TEXCOORD0;
    40.                 float3 normalDir : TEXCOORD1;
    41.             };
    42.            
    43.             vertexOutput vert(vertexInput v)
    44.             {
    45.                 vertexOutput o;
    46.                
    47.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    48.                 o.posWorld = mul(_Object2World, v.vertex);
    49.                 o.normalDir = normalize(mul(float4(v.normal, 0.0), _World2Object).xyz);
    50.                
    51.                 return o;
    52.             }
    53.            
    54.             float4 frag(vertexOutput i) : COLOR
    55.             {
    56.                 float3 normalDirection = i.normalDir;
    57.                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    58.                 float3 viewDirection = normalize(float3(float4(_WorldSpaceCameraPos.xyz, 1.0) - i.posWorld).xyz);
    59.                 float atten = 1.0;
    60.                
    61.                 float rim = 1 - saturate(dot(lightDirection, normalDirection));
    62.                 float3 rimLighting = atten * _LightColor0.xyz * _RimColor.rgb * saturate(dot(lightDirection, normalDirection)) * pow(rim, _RimPower);
    63.                
    64.            
    65.            
    66.                 return _Color;
    67.             }
    68.            
    69.            
    70.             ENDCG
    71.         }
    72.     }
    73. }
     
  2. mikesawicki64

    mikesawicki64

    Joined:
    May 12, 2014
    Posts:
    33
    Internet gods can play their jokes - as soon as I post it becomes solved, but I still don't understand why the above code doesn't compile, if anyone can explain it that would be greatly appreciated. Sry for the wasted thread

    I used this instead
    float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);

    which I thought would have been the same as line 58 up above?
     
    Last edited: Apr 2, 2015
  3. MuseGames

    MuseGames

    Joined:
    Mar 19, 2008
    Posts:
    98
    Your original code tries to construct a float3 by passing it a float4:

    Code (csharp):
    1.  
    2. float3( float4(_WorldSpaceCameraPos.xyz, 1.0) - i.posWorld )
    3.  
    i.posWorld is declared as a float4, subtracting it from the float4 you explicitly create makes another float4.