Search Unity

GLSL Help

Discussion in 'Shaders' started by DevDuFF, Mar 13, 2015.

  1. DevDuFF

    DevDuFF

    Joined:
    Jan 3, 2013
    Posts:
    17
    I've found an extreme lack of documentation regarding GLSL shaders in Unity.
    I'm trying to debug a distortion shader which will be attached to the camera via a call to:
    _camera.SetReplacementShader(shaderTest, "RenderType");
    where shaderTest is a public Shader set in the editor.

    Can anybody tell me what's wrong with this shader? I've gotten many obscure errors that I'm having difficulty making sense of. The current error I'm facing is:
    "Shader error in 'ShaderTest6': GLSL Error in Fragment Shader: 0(50) : error C0204: version directive must be first statement and may not be repeated at line 50"
    The thing is, I can move that version directive anywhere that would appear to be "first" and it still throws the error. One line before GLSLPROGRAM and it won't compile. One line after and it says it needs to be first.

    Code (CSharp):
    1. Shader "ShaderTest6" { // defines the name of the shader
    2.    SubShader { // Unity chooses the subshader that fits the GPU best
    3.    Tags { "RenderType"="Opaque"}
    4.       Pass { // some shaders require multiple passes
    5.          GLSLPROGRAM // here begins the part in Unity's GLSL
    6.          #ifdef VERTEX // here begins the vertex shader
    7.          #extension GL_ARB_gpu_shader5 : enable
    8.  
    9.          out INFO{
    10.          vec2 texCoords;
    11.          float k1_red;
    12.          float k1_green;
    13.          float k1_blue;
    14.          vec2 fullscr_center;
    15.          vec2 left_center;
    16.          vec2 right_center;
    17.          } vs_out;
    18.  
    19.          uniform float k1_red;
    20.          uniform float k1_green;
    21.          uniform float k1_blue;
    22.          uniform vec2 fullscr_center;
    23.          uniform vec2 left_center;
    24.          uniform vec2 right_center;
    25.  
    26.          void main() // all vertex shaders define a main() function
    27.          {
    28.             const vec2 texCoords[4] = vec2[4](vec2(0.0, 1.0),
    29.             vec2(0.0, 0.0),
    30.             vec2(1.0, 0.0),
    31.             vec2(1.0, 1.0));
    32.  
    33.             gl_Position = ftransform();
    34.             gl_TexCoord[0] = gl_MultiTexCoord0;
    35.             vs_out.texCoords = texCoords[gl_VertexID];
    36.             vs_out.k1_red = k1_red;
    37.             vs_out.k1_green = k1_green;
    38.             vs_out.k1_blue = k1_blue;
    39.             vs_out.fullscr_center = fullscr_center;
    40.             vs_out.left_center = left_center;
    41.             vs_out.right_center = right_center;
    42.          }
    43.          #endif // here ends the definition of the vertex shader
    44.          #ifdef FRAGMENT // here begins the fragment shader
    45.          #version 430
    46.  
    47.          in INFO{
    48.          vec2 texCoords;
    49.          float k1_red;
    50.          float k1_green;
    51.          float k1_blue;
    52.          vec2 fullscr_center;
    53.          vec2 left_center;
    54.          vec2 right_center;
    55.          } fs_in;
    56.  
    57.          out vec4 color;
    58.  
    59.          layout(binding = 0) uniform sampler2D s;
    60.          vec2 Distort(vec2 p, float k1)
    61.          {
    62.             float r2 = p.x * p.x + p.y * p.y;
    63.             float r = sqrt(r2);
    64.             float newRadius = (1 + k1*r*r);
    65.             p.x = p.x * newRadius;
    66.             p.y = p.y * newRadius;
    67.             return p;
    68.          }
    69.          void main() // all fragment shaders define a main() function
    70.          {
    71.             vec2 uv_red, uv_green, uv_blue;
    72.             vec4 color_red, color_green, color_blue;
    73.             vec2 sectorOrigin;
    74.  
    75.             sectorOrigin = fs_in.fullscr_center.xy;
    76.  
    77.             uv_red = Distort(fs_in.texCoords-sectorOrigin, fs_in.k1_red) + sectorOrigin;
    78.             uv_green = Distort(fs_in.texCoords-sectorOrigin, fs_in.k1_green) + sectorOrigin;
    79.             uv_blue = Distort(fs_in.texCoords-sectorOrigin, fs_in.k1_blue) + sectorOrigin;
    80.  
    81.             color_red = texture2D(s, uv_red);
    82.             color_green = texture2D(s, uv_green);
    83.             color_blue = texture2D(s, uv_blue);
    84.  
    85.             if( ((uv_red.x > 0) && (uv_red.x < 1) && (uv_red.y > 0) && (uv_red.y < 1)))
    86.             {
    87.                 color = vec4(color_red.x, color_green.y, color_blue.z, 1.0);
    88.             }
    89.             else
    90.             {
    91.                 color = vec4(1,0,0,1); //red
    92.             }
    93.          }
    94.          #endif // here ends the definition of the fragment shader
    95.          ENDGLSL // here ends the part in GLSL
    96.       }
    97.    }
    98. }
    Thanks.
     
    xieyun2017 and kebrus like this.
  2. xieyun2017

    xieyun2017

    Joined:
    Apr 14, 2017
    Posts:
    9
    I'v ran into the same problem. Have you solved it?