2017-08-30 113 views
0

我想構建一個多邊形工具WebGL項目。該項目在編輯器工作正常,但着色器給人建設時,即語法錯誤:着色器語法錯誤 - Unity WebGL

Shader error in 'Unlit/polygon': 'Value' : syntax error syntax error at line 28 (on gles) 

Compiling Vertex program 
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA 

符合「28」的存在:

int PointNum =0; 

我比較沒有經驗與OpenGL和首次置業計劃,但是我一直在研究Unity Shader手冊和ShaderLab資源,考慮可能會出現什麼問題 - 不幸的是沒有成功。您可以在翻譯後的評論中找到下面的着色器代碼(source link)。

我在想也許我沒有正確分配值?請讓我知道,如果你發現錯誤!任何幫助或想法嘗試非常感謝:)請讓我知道,如果你需要任何其他文件/信息!

Polygon.shader」 文件:

Shader "Unlit/polygon" 
{ 
Properties 
{ 
    //Define basic properties can be set from inside the editor variable 
    // _MainTex ("Texture", 2D) = "white" {} 
} 

CGINCLUDE 
// Upgrade NOTE: excluded shader from DX11 because it uses wrong array syntax (type[size] name) 
#pragma exclude_renderers d3d11 
     //Incoming vertices function from the application data structure definitions 
     struct appdata 
     { 
      float4 vertex : POSITION; 
      float2 uv : TEXCOORD0; 
     }; 
     //Incoming segment from a vertex function from the data structure definitions 
     struct v2f 
     { 
      float2 uv : TEXCOORD0; 
      float4 vertex : SV_POSITION; 
     }; 
     //Define mapping variables 
     sampler2D _MainTex; 
     // float4 _MainTex_ST; 

     //Define variables for communicating with the script 
     vector Value[6]; 
     int PointNum = 0; 

     //Function that calculates the distance between two points 
     float Dis(float4 v1,float4 v2) 
     { 
      return sqrt(pow((v1.x-v2.x),2)+pow((v1.y-v2.y),2)); 
     } 

     //Draw line segments 
     bool DrawLineSegment(float4 p1, float4 p2, float lineWidth,v2f i) 
     { 
      float4 center = float4((p1.x+p2.x)/2,(p1.y+p2.y)/2,0,0); 
      //Calculate distance between point and line 
      float d = abs((p2.y-p1.y)*i.vertex.x + (p1.x - p2.x)*i.vertex.y +p2.x*p1.y -p2.y*p1.x)/sqrt(pow(p2.y-p1.y,2) + pow(p1.x-p2.x,2)); 
      //When less than or equal to half the line width, which belongs to the linear range, return true 
      float lineLength = sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2)); 
      if(d<=lineWidth/2 && Dis(i.vertex,center)<lineLength/2) 
      { 
       return true; 
      } 
      return false; 
     } 

     //To draw a polygon, this limits the number of vertices is not more than 6. You can change. 
     bool pnpoly(int nvert, float4 vert[6], float testx, float testy) 
     { 

      int i, j; 
      bool c=false; 
      float vertx[6]; 
      float verty[6]; 

      for(int n=0;n<nvert;n++) 
      { 
       vertx[n] = vert[n].x; 
       verty[n] = vert[n].y; 
      } 
      for (i = 0, j = nvert-1; i < nvert; j = i++) { 
      if (((verty[i]>testy) != (verty[j]>testy)) && (testx < (vertx[j]-vertx[i]) * (testy-verty[i])/(verty[j]-verty[i]) + vertx[i])) 
       c = !c; 
      } 
      return c; 
     } 

     v2f vert (appdata v) 
     { 
      v2f o; 
      //Object vertices from model space to the camera cut space, or you can use the shorthand way: 
      //o.vertex = UnityObjectToClipPos(v.vertex); 
      o.vertex = mul(UNITY_MATRIX_MVP,v.vertex); 
      //2D UV coordinate transformation can also use shorthand methods 
      //o.uv = TRANSFORM_TEX(v.uv, _MainTex); 
      //o.uv = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw; 
      return o; 
     }    
     fixed4 frag (v2f i) : SV_Target 
     { 

      //Draw a polygon vertex 
      for(int j=0;j<PointNum;j++) 
      { 
       if(Dis(i.vertex, Value[j])<3) 
       { 
        return fixed4(1,0,0,0.5); 
       } 
      } 
      //Draws the edges of the polygon 
      for(int k=0;k<PointNum;k++) 
      { 
       if(k==PointNum-1) 
       { 
        if(DrawLineSegment(Value[k],Value[0],2,i)) 
        { 
         return fixed4(1,1,0,0.5); 
        } 
       } 
       else 
       { 
        if(DrawLineSegment(Value[k],Value[k+1],2,i)) 
        { 
         return fixed4(1,1,0,0.5); 
        } 
       } 

      } 
      //Within the filled polygon 
      if(pnpoly(PointNum, Value,i.vertex.x ,i.vertex.y)) 
      { 
       return fixed4(0,1,0,0.3); 
      } 
      return fixed4(0,0,0,0); 
      //fixed4 col = tex2D(_MainTex, i.uv); 
      //return col; 
     } 
ENDCG 

SubShader 
{ 
    Tags { "RenderType"="Opaque" } 
    LOD 100 
    Pass 
    { 
     //Select Alpha blend mode 
     Blend SrcAlpha OneMinusSrcAlpha 
     //In the CGPROGRAM block of code to write your own processes 
     CGPROGRAM 
     //Defined segment function entry and vertex function respectively, Vert and Frag 
     #pragma vertex vert 
     #pragma fragment frag 
     //Contains the basic files, there are some macro definitions and basic functions 
     #include "UnityCG.cginc"    

     ENDCG 
    } 
} 
} 

編譯後熄滅,polygon.shader」 文件:

// Compiled shader for WebGL 
    ////////////////////////////////////////////////////////////////////////// 
    // 
    // NOTE: This is *not* a valid shader file, the contents are provided just 
    // for information and for debugging purposes only. 
    // 
    ////////////////////////////////////////////////////////////////////////// 
    // Skipping shader variants that would not be included into build of current scene. 

    Shader "Unlit/polygon" { 
    SubShader { 
    LOD 100 
    Tags { "RenderType"="Opaque" } 
    Pass { 
    Tags { "RenderType"="Opaque" } 
    Blend SrcAlpha OneMinusSrcAlpha 
    ////////////////////////////////// 
    //        // 
    //  Compiled programs  // 
    //        // 
    ////////////////////////////////// 
    ////////////////////////////////////////////////////// 
    No keywords set in this variant. 
    -- Vertex shader for "gles": 
    // Compile errors generating this shader. 

    -- Fragment shader for "gles": 
    Shader Disassembly: 
    // All GLSL source is contained within the vertex program 

    -- Vertex shader for "gles3": 
    Shader Disassembly: 
    #ifdef VERTEX 
    #version 300 es 

    uniform  vec4 hlslcc_mtx4x4glstate_matrix_mvp[4]; 
    in highp vec4 in_POSITION0; 
    vec4 u_xlat0; 
    void main() 
    { 
    u_xlat0 = in_POSITION0.yyyy * hlslcc_mtx4x4glstate_matrix_mvp[1]; 
    u_xlat0 = hlslcc_mtx4x4glstate_matrix_mvp[0] * in_POSITION0.xxxx + u_xlat0; 
    u_xlat0 = hlslcc_mtx4x4glstate_matrix_mvp[2] * in_POSITION0.zzzz + u_xlat0; 
    gl_Position = hlslcc_mtx4x4glstate_matrix_mvp[3] * in_POSITION0.wwww + u_xlat0; 
    return; 
    } 

    #endif 
    #ifdef FRAGMENT 
    #version 300 es 

    precision highp int; 
    uniform  vec4 Value[6]; 
    uniform  int PointNum; 
    layout(location = 0) out lowp vec4 SV_Target0; 
    int u_xlati0; 
    vec2 u_xlat1; 
    ivec2 u_xlati1; 
    bool u_xlatb1; 
    float u_xlat2; 
    vec2 u_xlat3; 
    int u_xlati3; 
    bool u_xlatb3; 
    vec2 u_xlat4; 
    float u_xlat5; 
    vec2 u_xlat6; 
    int u_xlati6; 
    bool u_xlatb6; 
    vec2 u_xlat7; 
    float u_xlat9; 
    int u_xlati9; 
    bool u_xlatb9; 
    float u_xlat10; 
    int u_xlati10; 
    vec4 TempArray0[6]; 
    vec4 TempArray1[6]; 
    void main() 
    { 
    for(int u_xlati_loop_1 = 0 ; u_xlati_loop_1<PointNum ; u_xlati_loop_1++) 
    { 
      u_xlat3.xy = gl_FragCoord.xy + (-Value[u_xlati_loop_1].xy); 
      u_xlat3.xy = u_xlat3.xy * u_xlat3.xy; 
      u_xlat3.x = u_xlat3.y + u_xlat3.x; 
      u_xlat3.x = sqrt(u_xlat3.x); 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb3 = !!(u_xlat3.x<3.0); 
    #else 
      u_xlatb3 = u_xlat3.x<3.0; 
    #endif 
      if(u_xlatb3){ 
      SV_Target0 = vec4(1.0, 0.0, 0.0, 0.5); 
      return; 
      //ENDIF 
      } 
    } 
    u_xlati0 = PointNum + int(0xFFFFFFFFu); 
    for(int u_xlati_loop_2 = 0 ; u_xlati_loop_2<PointNum ; u_xlati_loop_2++) 
    { 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb6 = !!(u_xlati0==u_xlati_loop_2); 
    #else 
      u_xlatb6 = u_xlati0==u_xlati_loop_2; 
    #endif 
      if(u_xlatb6){ 
      u_xlat6.xy = Value[0].xy + Value[u_xlati_loop_2].xy; 
      u_xlat1.x = (-Value[u_xlati_loop_2].y) + Value[0].y; 
      u_xlat4.xy = (-Value[0].xy) + Value[u_xlati_loop_2].xy; 
      u_xlat10 = u_xlat4.x * gl_FragCoord.y; 
      u_xlat10 = u_xlat1.x * gl_FragCoord.x + u_xlat10; 
      u_xlat10 = Value[0].x * Value[u_xlati_loop_2].y + u_xlat10; 
      u_xlat10 = (-Value[0].y) * Value[u_xlati_loop_2].x + u_xlat10; 
      u_xlat4.xy = u_xlat4.xy * u_xlat4.xy; 
      u_xlat1.x = u_xlat1.x * u_xlat1.x + u_xlat4.x; 
      u_xlat1.x = sqrt(u_xlat1.x); 
      u_xlat1.x = abs(u_xlat10)/u_xlat1.x; 
      u_xlat4.x = u_xlat4.y + u_xlat4.x; 
      u_xlat4.x = sqrt(u_xlat4.x); 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb1 = !!(1.0>=u_xlat1.x); 
    #else 
      u_xlatb1 = 1.0>=u_xlat1.x; 
    #endif 
      u_xlat6.xy = (-u_xlat6.xy) * vec2(0.5, 0.5) + gl_FragCoord.xy; 
      u_xlat6.xy = u_xlat6.xy * u_xlat6.xy; 
      u_xlat6.x = u_xlat6.y + u_xlat6.x; 
      u_xlat6.x = sqrt(u_xlat6.x); 
      u_xlat9 = u_xlat4.x * 0.5; 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb6 = !!(u_xlat6.x<u_xlat9); 
    #else 
      u_xlatb6 = u_xlat6.x<u_xlat9; 
    #endif 
      u_xlatb6 = u_xlatb6 && u_xlatb1; 
      if(u_xlatb6){ 
        SV_Target0 = vec4(1.0, 1.0, 0.0, 0.5); 
        return; 
      //ENDIF 
      } 
      } else { 
      u_xlati6 = u_xlati_loop_2 + 1; 
      u_xlat1.xy = Value[u_xlati6].xy + Value[u_xlati_loop_2].xy; 
      u_xlat9 = (-Value[u_xlati_loop_2].y) + Value[u_xlati6].y; 
      u_xlat7.xy = (-Value[u_xlati6].xy) + Value[u_xlati_loop_2].xy; 
      u_xlat2 = u_xlat7.x * gl_FragCoord.y; 
      u_xlat2 = u_xlat9 * gl_FragCoord.x + u_xlat2; 
      u_xlat2 = Value[u_xlati6].x * Value[u_xlati_loop_2].y + u_xlat2; 
      u_xlat6.x = (-Value[u_xlati6].y) * Value[u_xlati_loop_2].x + u_xlat2; 
      u_xlat7.xy = u_xlat7.xy * u_xlat7.xy; 
      u_xlat9 = u_xlat9 * u_xlat9 + u_xlat7.x; 
      u_xlat9 = sqrt(u_xlat9); 
      u_xlat6.x = abs(u_xlat6.x)/u_xlat9; 
      u_xlat9 = u_xlat7.y + u_xlat7.x; 
      u_xlat9 = sqrt(u_xlat9); 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb6 = !!(1.0>=u_xlat6.x); 
    #else 
      u_xlatb6 = 1.0>=u_xlat6.x; 
    #endif 
      u_xlat1.xy = (-u_xlat1.xy) * vec2(0.5, 0.5) + gl_FragCoord.xy; 
      u_xlat1.xy = u_xlat1.xy * u_xlat1.xy; 
      u_xlat1.x = u_xlat1.y + u_xlat1.x; 
      u_xlat1.x = sqrt(u_xlat1.x); 
      u_xlat9 = u_xlat9 * 0.5; 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb9 = !!(u_xlat1.x<u_xlat9); 
    #else 
      u_xlatb9 = u_xlat1.x<u_xlat9; 
    #endif 
      u_xlatb6 = u_xlatb9 && u_xlatb6; 
      if(u_xlatb6){ 
        SV_Target0 = vec4(1.0, 1.0, 0.0, 0.5); 
        return; 
      //ENDIF 
      } 
      //ENDIF 
      } 
    } 
    for(int u_xlati_loop_3 = 0 ; u_xlati_loop_3<PointNum ; u_xlati_loop_3++) 
    { 
      TempArray0[u_xlati_loop_3].x = Value[u_xlati_loop_3].x; 
      TempArray1[u_xlati_loop_3].x = Value[u_xlati_loop_3].y; 
    } 
    u_xlati1.y = 0; 
    u_xlati1.x = u_xlati0; 
    u_xlati3 = 0; 
    while(true){ 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb6 = !!(u_xlati1.y>=PointNum); 
    #else 
      u_xlatb6 = u_xlati1.y>=PointNum; 
    #endif 
      if(u_xlatb6){break;} 
      u_xlat6.x = TempArray1[u_xlati1.y].x; 
    #ifdef UNITY_ADRENO_ES3 
      { bool cond = gl_FragCoord.y<u_xlat6.x; u_xlati9 = int(!!cond ? 0xFFFFFFFFu : uint(0u)); } 
    #else 
      u_xlati9 = int((gl_FragCoord.y<u_xlat6.x) ? 0xFFFFFFFFu : uint(0u)); 
    #endif 
      u_xlat7.x = TempArray1[u_xlati1.x].x; 
    #ifdef UNITY_ADRENO_ES3 
      { bool cond = gl_FragCoord.y<u_xlat7.x; u_xlati10 = int(!!cond ? 0xFFFFFFFFu : uint(0u)); } 
    #else 
      u_xlati10 = int((gl_FragCoord.y<u_xlat7.x) ? 0xFFFFFFFFu : uint(0u)); 
    #endif 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb9 = !!(u_xlati9!=u_xlati10); 
    #else 
      u_xlatb9 = u_xlati9!=u_xlati10; 
    #endif 
      u_xlat10 = TempArray0[u_xlati1.x].x; 
      u_xlat2 = TempArray0[u_xlati1.y].x; 
      u_xlat10 = u_xlat10 + (-u_xlat2); 
      u_xlat5 = (-u_xlat6.x) + gl_FragCoord.y; 
      u_xlat10 = u_xlat10 * u_xlat5; 
      u_xlat6.x = (-u_xlat6.x) + u_xlat7.x; 
      u_xlat6.x = u_xlat10/u_xlat6.x; 
      u_xlat6.x = u_xlat2 + u_xlat6.x; 
    #ifdef UNITY_ADRENO_ES3 
      u_xlatb6 = !!(gl_FragCoord.x<u_xlat6.x); 
    #else 
      u_xlatb6 = gl_FragCoord.x<u_xlat6.x; 
    #endif 
      u_xlatb6 = u_xlatb6 && u_xlatb9; 
      u_xlati9 = ~u_xlati3; 
      u_xlati3 = (u_xlatb6) ? u_xlati9 : u_xlati3; 
      u_xlati1.x = u_xlati1.y + 1; 
      u_xlati1.xy = u_xlati1.yx; 
    } 
    if(u_xlati3 != 0) { 
      SV_Target0 = vec4(0.0, 1.0, 0.0, 0.300000012); 
      return; 
    //ENDIF 
    } 
    SV_Target0 = vec4(0.0, 0.0, 0.0, 0.0); 
    return; 
    } 

    #endif 


    -- Fragment shader for "gles3": 
    Shader Disassembly: 
    // All GLSL source is contained within the vertex program 

    } 
    } 
    } 

回答

0

嘗試使用float4代替vector

// Upgrade NOTE: excluded shader from DX11 because it uses wrong array syntax (type[size] name) 
// #pragma exclude_renderers d3d11 
... 
uniform float4 Value[6]; 

這會被編譯爲uniform highp vec4 Value[6];

+0

輝煌,這曾經魅力!謝謝 :) –