Shader uniform 'x' does not exist.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Shader uniform 'x' does not exist.

Post by veethree »

So i get that this is a common error, If a uniform doesn't contribute to the output of the shader, It gets optimized out. But as far as i can tell the uniform in this case is totally contributing. But since i'm getting the error clearly im wrong.

Here's the shader in question:

Code: Select all

varying vec4 worldPosition;
varying vec4 viewPosition;
varying vec4 screenPosition;
varying vec3 vertexNormal;
varying vec4 vertexColor;

#define MAX_LIGHTS 16

struct lightSource {
    vec3 position;
    vec3 color;
};

extern lightSource lights[MAX_LIGHTS];

float specularStrength = 0.5;
vec3 ambient = vec3(0.1, 0.1, 0.1);

vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
    vec3 fragPosition = vec3(worldPosition[0], worldPosition[1], worldPosition[2]);
    vec3 viewPos = vec3(viewPosition[0], viewPosition[1], viewPosition[2]);
    vec3 norm = normalize(vertexNormal);
    vec3 viewDirection = normalize(viewPos - fragPosition);
    vec3 diffuse = ambient;
    vec3 specular = ambient;

    for (int i = 0; i < MAX_LIGHTS; i++) {
        lightSource l = lights[i];
        vec3 lightDirection = normalize(l.position - fragPosition);
        vec3 reflectDirection = reflect(-lightDirection, norm);
        float spec = pow(max(dot(viewDirection, reflectDirection), 0.0), 32.0);
        specular += specularStrength * spec * l.color;
        float diff = max(dot(norm, lightDirection), 0.0);
        diffuse += diff * l.color;
    };
    
    diffuse = clamp(diffuse, 0.0, 1.0);
    return vec4(ambient + diffuse + specular, 1.0);
}
I'm sure it has other errors than the unused uniform, But ignore those for now. It complains about the "lights" array.

Here's the rest of the code:

Code: Select all

-- GLOBALS
lg = love.graphics
fs = love.filesystem
kb = love.keyboard
lm = love.mouse
random = math.random
noise = love.math.noise
sin = math.sin
cos = math.cos
f = string.format

function love.load()
    g3d = require("g3d")

    cubes = {}
    count = 16
    radius = 10
    for i=1, count do
        local a = (math.pi * 2) / count * i
        x = radius * cos(a)
        y = radius * sin(a)
        cubes[i] = g3d.newModel("cube.obj", nil, {x, y, 0}, {random(), random(), random()})
    end

    lights = {
        {
            position = {0, 0, 0},
            color = {1, 1, 1}
        },
        {
            position = {10, 0, 0},
            color = {1, 0, 0}
        },
        {
            position = {0, 10, 0},
            color = {0, 0, 1}
        },
    }

    g3d.shader:send("lights", lights)

    time = 0
end

function love.update(dt)
    time = time + dt
    if time > math.pi * 2 then time = 0 end
    g3d.camera.firstPersonMovement(dt)

end

function love.mousemoved(x, y, dx, dy)
    g3d.camera.firstPersonLook(dx, dy)

end

function love.draw()
    for i,v in ipairs(cubes) do
        local ox, oy, oz = unpack(v.translation)
        local rx, ry, rz = unpack(v.rotation)
        v:setTransform({ox, oy, sin(time + i) * 5}, {time, time, time})
        v:draw()
    end
end

function love.keypressed(key)
    if key == "escape" then love.event.push("quit") end
end
EDIT: After tinkering around with a simpler shader, with an array, It's seems clear to me i'm doing something fundamentally wrong here. It seems to me glsl doesn't like arrays very much. Should i be representing the lights in a different way? An image perhaps?
Attachments
brokenshader.love
(16.08 KiB) Downloaded 115 times
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Shader uniform 'x' does not exist.

Post by pgimeno »

Try this:

Code: Select all

    for i = 1, #lights do
       g3d.shader:send("lights[" .. i-1 .. "].position", lights[i].position)
       g3d.shader:send("lights[" .. i-1 .. "].color", lights[i].color)
    end
Don't ask me why it is that way.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Shader uniform 'x' does not exist.

Post by veethree »

pgimeno wrote: Thu Sep 30, 2021 4:09 pm Try this:

Code: Select all

    for i = 1, #lights do
       g3d.shader:send("lights[" .. i-1 .. "].position", lights[i].position)
       g3d.shader:send("lights[" .. i-1 .. "].color", lights[i].color)
    end
Don't ask me why it is that way.
I can't believe how intuitive that is. Thanks a lot!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 28 guests