[SOLVED] Shader gl_FrontFacing parameter


I want to flip the normals in a custom shader but I’ve some trooble making it work. I’ve try some tutorials but from my understanding each engine handle glsl differently. So here is the code.

The fragment shader

varying vec2 vUv0;

uniform sampler2D uDiffuseMap;
varying mat4 matrix_model;
varying vec4 verpos;
varying float effect;

void main(void)
    gl_FragColor = vec4(0.0);
    vec4 color = texture2D(uDiffuseMap, vUv0);
    color = vec4(color.x+ ((verpos.y*0.0009) * (effect - 0.9) * 2.0), color.y+ ((verpos.y*0.0009) * (effect - 0.9) * 2.0),color.z+((verpos.y*0.0009) * (effect - 0.90) * 2.0),1);
    gl_FrontFacing = false;
    gl_FragColor = color;


The Vertex shader

attribute vec3 aPosition;
attribute vec2 aUv0;
varying vec2 texCoord;

uniform mat4 matrix_model;
uniform mat4 matrix_viewProjection;

uniform float scale;
varying float effect;
varying vec2 vUv0;
varying vec4 verpos;

void main(void)
    effect = scale;
    vUv0 = aUv0;
    verpos = vec4(aPosition ,scale);
    gl_Position = matrix_viewProjection * matrix_model * vec4(aPosition ,scale);

[the link I followed] (http://stackoverflow.com/questions/28160756/normals-are-inverted)

I’ve a compilation error on the gl_FontFacing = false;. Is there any other way I can access this function.

I tried to find some documentation about it but couldn’t find one wich actually work on playcanvas. Is it beceause it is on web gl?

that’s a lot of questions and i’m a bit noob in shader programming. If anyone can link me to a documentation that would be great :slight_smile:


gl_FrontFacing is not a function, it’s a boolean value. Here is a description given in the GLSL spec:

The fragment shader has access to the read-only built-in variable gl_FrontFacing whose value is true if
the fragment belongs to a front-facing primitive. One use of this is to emulate two-sided lighting by
selecting one of two colors calculated by the vertex shader.

So the issues seem to be:

  • You are writing to gl_FrontFacing, when it is a read-only value.
  • You are accessing gl_FrontFacing in the vertex shader when you should instead do it in the fragment shader.

thanks for the tip :slight_smile: and the specs.

Found my mistake. I tried to set the variable inside the glsl shader. The variable must be changed in the javascript.

The answer
material.cull = pc.CULLFACE_FRONT;

1 Like