Search code examples
glslwebglgl-matrix

wrong operand types - no operation '*' exists that takes a left-hand operand of type 'uniform lowp samplerCube'


I wanna add dir and amb light to the cubeMap sampler. Any sugestion?

FS

   // shader for opengles native 'uniform samplerCube' CubeMap canvas2d textures.
    precision mediump float;
    precision highp float;

    varying vec3 vLightWeighting;

    uniform float textureSamplerAmount[1];
    int MixOperandString = ${mixOperand};
    // The CubeMap texture.
    uniform samplerCube u_texture;
    varying vec3 v_normal;
    varying vec3 v_normal_cubemap;

    void main(void) {
      gl_FragColor = textureCube(u_texture * vLightWeighting, v_normal_cubemap);
    }

VS

 attribute vec3 aVertexPosition;
  attribute vec3 aVertexNormal;

  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;
  uniform mat3 uNMatrix;

  varying vec3 v_normal;
  varying vec3 v_normal_cubemap;

  // lights
  uniform vec3 uAmbientColor;
  uniform vec3 uLightingDirection;
  uniform vec3 uDirectionalColor;
  uniform bool uUseLighting;
  varying vec2 vTextureCoord;
  varying vec3 vLightWeighting;

  void main(void) {

    v_normal = mat3(uNMatrix) * aVertexNormal;
    v_normal_cubemap = normalize(aVertexPosition.xyz);
    gl_Position   = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);

    // lights
    // vTextureCoord = aTextureCoord;

    if (!uUseLighting) {
      vLightWeighting = vec3(1.0, 1.0, 1.0);
    }
    else {
      vec3 transformedNormal          = uNMatrix * aVertexNormal;
      float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
      vLightWeighting                 = uAmbientColor + uDirectionalColor * directionalLightWeighting;
    }

Error log:

app.js:833 Shader Program compile failed:ERROR: 0:16: '' : Invalid operation for variables with an opaque type ERROR: 0:16: '' : wrong operand types - no operation '*' exists that takes a left-hand operand of type 'uniform lowp samplerCube' and a right operand of type 'varying highp 3-component vector of float' (or there is no acceptable conversion)


Solution

  • u_texture is a sampler. You can't multiply a sampler by anything. However, you can multiply the color returned by the texture lookup with vLightWeighting:

    void main(void) {
        vec4 lightMapColor = textureCube(u_texture, v_normal_cubemap);
        gl_FragColor = vec4(lightMapColor.rgb * vLightWeighting, lightMapColor.a);
    }