Search Unity

'UnityObjectToClipPos': cannot implicitly convert from 'float4x4' to 'float4'?

Discussion in 'Shaders' started by markashburner, Mar 19, 2018.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    Hi I see this error comes up a lot, especially when Unity updated to 5.6

    Is there a way to get around this syntax error?

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Planet/GrassGeometry"
    4. {
    5.      Properties
    6.      {
    7.          _Length ("Cantidad", Float) = 10
    8.          _Width ("Ancho", Float) = 1
    9.          _Gravity ("Gravedad", Float) = 0
    10.          _Steps ("Pasos", Int) = 20
    11.          _shadercolor ("supersimple color", Color) = (1,1,1,1)
    12.          _ObjectSpaceLightPos("Some Vector", Vector) = (0,0,0,0)
    13.      }
    14.      SubShader
    15.      {
    16.          Tags { "RenderType"="Opaque" }
    17.          Pass
    18.          {
    19.              CGPROGRAM
    20.              #pragma vertex vert
    21.              #pragma geometry geom
    22.              #pragma fragment frag
    23.              #include "UnityCG.cginc"
    24.              //Structura de entrada al vertex
    25.              struct appdata
    26.              {
    27.                  float4 vertex : POSITION;
    28.                  float3 normal : NORMAL;
    29.              };
    30.              //Structura de entra al geometry
    31.              struct geomInput
    32.              {
    33.                  float4 pos : POSITION;
    34.                  float3 normal: NORMAL;
    35.              };
    36.              //Structura de entrada al fragment
    37.              struct v2f
    38.              {
    39.                  float4 vertex : SV_POSITION;
    40.                  float3 normal: NORMAL;
    41.              };
    42.              //Vertex shader
    43.              geomInput vert (appdata v)
    44.              {
    45.                  geomInput o;
    46.                  //o.pos = v.vertex;
    47.                  o.pos = UnityObjectToClipPos(v.vertex);
    48.                  o.normal = v.normal;
    49.                  return o;
    50.              }
    51.              float _Length;
    52.              float _Width;
    53.              float _Gravity;
    54.              float _Steps;
    55.              //Geometry shader
    56.              [maxvertexcount(80)]
    57.              void geom(triangle geomInput i[3], inout TriangleStream<v2f> stream)
    58.              {
    59.                  float4 P1 = i[0].pos;
    60.                  float4 P2 = i[1].pos;
    61.                  float4 P3 = i[2].pos;
    62.                  float3 N1 = i[0].normal;
    63.                  float3 N2 = i[1].normal;
    64.                  float3 N3 = i[2].normal;
    65.                  float4 P = (P1 + P2 + P3) / 3.0;
    66.                  float3 N = (N1 + N2 + N3) / 3.0;
    67.                  float4 T = float4 (normalize((P2.xyz-P1.xyz)), 0.0);
    68.                  for( int i = 0; i < _Steps; i++ )
    69.                  {
    70.    
    71.                      float t0 = (float)i / _Steps;
    72.                      float t1 = (float)(i+1) / _Steps;
    73.  
    74.                      float4 p0 = normalize(float4(N, 0) - (float4(0, _Length * t0, 0, 0) * _Gravity * t0)) * (_Length * t0);
    75.                      float4 p1 = normalize(float4(N, 0) - (float4(0, _Length * t1, 0, 0) * _Gravity * t1)) * (_Length * t1);
    76.  
    77.                      float4 w0 = T * lerp(_Width, 0, t0);
    78.                      float4 w1 = T * lerp(_Width, 0, t1);
    79.                      v2f ot;
    80.                      float4x4 vp = UnityObjectToClipPos(unity_WorldToObject);
    81.                      ot.vertex = UnityObjectToClipPos((p0 - w0) + P);
    82.                      //ot.vertex = mul(UNITY_MATRIX_MVP, p0 - w0);
    83.                      ot.normal = cross(T, p0);
    84.                      stream.Append(ot);
    85.                      ot.vertex = UnityObjectToClipPos((p0 - w0) + P);
    86.                      //ot.vertex = mul(UNITY_MATRIX_MVP, p0 + w0);
    87.                      ot.normal = cross(T, p0);
    88.                      stream.Append(ot);
    89.                      ot.vertex = UnityObjectToClipPos((p0 - w0) + P);
    90.                      //ot.vertex = mul(UNITY_MATRIX_MVP, p1 - w1);
    91.                      ot.normal = cross(T, p1);
    92.                      stream.Append(ot);
    93.                      ot.vertex = UnityObjectToClipPos((p0 - w0) + P);
    94.                      //ot.vertex = mul(UNITY_MATRIX_MVP, p1 + w1);
    95.                      ot.normal = cross(T, p1);
    96.                      stream.Append(ot);
    97.                  }      
    98.              }
    99.              //Fragment shader
    100.              float4 _shadercolor;
    101.              float4 _ObjectSpaceLightPos; //position vector of light source
    102.              fixed4 frag (v2f i) : SV_Target
    103.              {
    104.                  float3 lightdirWRTvertes=((i.vertex)-(_ObjectSpaceLightPos));
    105.                  float3 worldnormal = normalize(i.normal);
    106.                  float3 lightdir = normalize(lightdirWRTvertes);
    107.                  float3 lambert = saturate(dot(lightdir,worldnormal));
    108.                  fixed4 col;
    109.                  col.rgb = lambert*_shadercolor;
    110.                          col.a=1;//a for transperency or alpha.
    111.                  return col;
    112.              }
    113.              ENDCG
    114.          }
    115.      }
    116. }
    It is this line of code here:

    Code (CSharp):
    1. float4x4 vp = UnityObjectToClipPos(unity_WorldToObject);
    Shader error in 'Planet/GrassGeometry': 'UnityObjectToClipPos': cannot implicitly convert from 'float4x4' to 'float4' at line 90 (on d3d11)

    I have searched the Unity forums everywhere on google and cannot come up with a solution?
     
  2. kripto289

    kripto289

    Joined:
    Feb 21, 2013
    Posts:
    505
    UnityObjectToClipPos return float4 variable, not a float4x4.
    Anyway, this method "float4x4 vp = UnityObjectToClipPos(unity_WorldToObject);" not used in the code and you can remove it. =/