Search Unity

Surface Shader error: cannot convert from 'const float2' to 'float3'

Discussion in 'Shaders' started by GiorgioK, Nov 25, 2021.

  1. GiorgioK

    GiorgioK

    Joined:
    Oct 20, 2021
    Posts:
    34
    I wrote a little surface shader:

    Code (CSharp):
    1.  
    2. Shader "Custom/Shader" {
    3.    
    4. Properties {
    5.     _arrMaskTex( "Arr Terrain MASKS", 2DArray ) = "" {}
    6.     _arrTexAlbedo( "Arr Terrain Textures", 2DArray ) = "" {}
    7. }
    8.  
    9. SubShader {
    10.  
    11. Tags { "RenderType" = "Opaque" }
    12. LOD 200
    13.  
    14. CGPROGRAM
    15.  
    16. #pragma surface main Standard fullforwardshadows
    17. #pragma target 3.0
    18.  
    19. UNITY_DECLARE_TEX2DARRAY( _arrMaskTex );
    20. UNITY_DECLARE_TEX2DARRAY( _arrTexAlbedo );
    21.  
    22. struct Input {
    23.     float3 uv_arrMaskTex;
    24.     float3 uv_arrTexAlbedo;
    25. };
    26.  
    27. void main ( Input input, inout SurfaceOutputStandard sos ) {
    28.     float3 f3TxMask = input.uv_arrMaskTex;
    29.     float3 f3TxAlbedo = input.uv_arrTexAlbedo;
    30.    
    31.     float3 color = float3( f3TxMask.x, f3TxMask.y, 0.0 ); // ERROR !!!
    32.     float3 color = float3( f3TxMask.xy, 0 ); // ERROR !!!
    33.     float3 color = float3( f3TxMask.x, 0.5, 0 ); // ERROR !!!
    34.    
    35.     float3 color = float3( 0.9, 0.1, 0 ); // NO ERROR
    36.    
    37.     sos.Albedo = color.rgb;
    38.    
    39. } // main()
    40. ENDCG
    41. } // SubShader
    42. FallBack "Diffuse"
    43. } // Shader
    44.  
    I want in the variable 'color' to get the color from the 2DArray texture from the first layer (at index 0).
    Initially I tried like this:
    Code (CSharp):
    1. float3 color = float3 (f3TxMask.xy, 0);
    But got the error:
    Shader error in 'Custom / Shader': cannot implicitly convert from 'const float2' to 'float3' at line 63

    Then I tried it like this:

    Code (CSharp):
    1. float3 color = float3 (f3TxMask.x, f3TxMask.y, 0.0); // ERROR !!!
    2. float3 color = float3 (f3TxMask.x, 0.5, 0); // ERROR !!!
    And I got exactly the same error.
    And what is this line number 63? This shader has 46 lines in total.
    If in the inspector, with the shader selected, press the "Show generated code" button, then the code opens in which line 63 is empty.

    I have two questions.
    1) How can I see the line in which the error actually occurred?
    2) How can I fix this error in this shader?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Line numbers in Surface Shaders can be confusing if the error is happing in code that’s being generated. In this case the issue isn’t in the code you’re righting, it’s that the code you’re writing is trying to make use of variables that are causing bad code generation. Specifically, these two lines.

    Code (csharp):
    1. float3 uv_arrMaskTex;
    2. float3 uv_arrTexAlbedo;
    You cannot use
    float3
    for automatic Surface Shader UVs, they must use
    float2
    , or you’ll get an error when you try to use them. Surface Shaders weren’t written with texture arrays in mind, they predate them by almost a decade, and development ceased on them prior to the engine getting real support for them. So if you need to get additional components of a mesh UV beyond the first two you need to use custom Input struct variables and assign the, with a custom vertex function.
     
  3. GiorgioK

    GiorgioK

    Joined:
    Oct 20, 2021
    Posts:
    34
  4. qq2696531001

    qq2696531001

    Joined:
    Mar 19, 2023
    Posts:
    1
    Thanks !! You guys helped me