mesh:color()

I guess I’m doing something wrong here, when I do mesh:color(i,color(a,b,c)) then the result is pure white. What am I doing wrong?

Does mesh:color(i, r, g, b) or mesh:color(i, r, g, b, a) work for you?

Not so far as I can tell.

In trying a few more experiments, then the problem seems to be in trying to assign lots of different colours in the same loop. So I can safely assign one or two colours, but when I try to assign a different colour to each vertex then I get white.

Here’s a non-minimal example

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    m = mesh()
    ver = {
        vec3(1,0,0),
        vec3(0,1,0),
        vec3(0,0,1),
        vec3(-1,0,0),
        vec3(0,1,0),
        vec3(0,0,1),
        vec3(1,0,0),
        vec3(0,-1,0),
        vec3(0,0,1),
        vec3(-1,0,0),
        vec3(0,-1,0),
        vec3(0,0,1),
        vec3(1,0,0),
        vec3(0,1,0),
        vec3(0,0,-1),
        vec3(-1,0,0),
        vec3(0,1,0),
        vec3(0,0,-1),
        vec3(1,0,0),
        vec3(0,-1,0),
        vec3(0,0,-1),
        vec3(-1,0,0),
        vec3(0,-1,0),
        vec3(0,0,-1)
    }
    c = {}
    vv = {}
    for k,v in ipairs(ver) do
        table.insert(c,color(128+v.x*127,128+v.y*127,128+v.z*127,255))
        table.insert(vv,v)
    end
    
    m.vertices = vv
    m.colors = c
    for k,v in ipairs(ver) do
        m:color(k,color(128+v.x*127,128+v.y*127,128+v.z*127,255))
        
    end
    parameter("azimuth",0,2*math.pi,math.pi)
    parameter("zenith",0,math.pi,0)
    parameter("size",.1,5,1)
    dx,dy = 0,0
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    --resetMatrix()
    background(40, 40, 50)
    perspective(45,WIDTH/HEIGHT)
    
    local ca = math.cos(azimuth)
    local sa = math.sin(azimuth)
    local cz = math.cos(zenith)
    local sz = math.sin(zenith)
    local mt = 
        matrix(
        size,0,0,0,
        0,size,0,0,
        0,0,size,0,
        0,0,0,1        
        )*
        matrix(
        ca,-sa,0,0,
        sa,ca,0,0,
        0,0,1,0,
        0,0,0,1
        ) * matrix(
        1,0,0,0,
        0,cz,-sz,0,
        0,sz,cz,0,
        0,0,0,1
        ) * 
        Quaternion.Gravity():tomatrix() *
        matrix(
        1,0,0,0,
        0,1,0,0,
        0,0,1,0,
        dx,dy,0,1
        )
        
        
        camera(0,0,10,0,0,0,0,1,0)
        
        local j,k,l
    for i = 0,7 do
        j = 2*(i%2)
        k = 2*(math.floor(i/2)%2)
        l = 2*(math.floor(i/4)%2)
    --resetMatrix()
    modelMatrix(
        matrix(
        1,0,0,0,
        0,1,0,0,
        0,0,1,0,
        j,k,l,1
        ) *
        mt
    )
    m:draw()
    end
end

function touched(touch)
    dx = dx + 2*touch.deltaX/WIDTH
    dy = dy + 2*touch.deltaY/HEIGHT
end

The m:color() part ought to do nothing as it is just setting each vertex to the colour it already had, but it doesn’t. It sets all the colours to white.

Just figured out what this is. It’s my bug.

It looks like mesh.color accepts float arguments — not 0 - 255. This will be changed as soon as possible. For now you can do mesh.color( index, 0.5, 0.5, 0.5, 1 ) for grey, for example.

The returned colour will also be in the 0-1 range.

Yup, that fixes it. I’ve put it in a function so that I can remove it later.

I’m torn on this one. I have a few workarounds for these bugs which, once they get fixed in beta, I’ll no longer need. But I’m quite fond of the game (this is the roller coaster one) and so I’d like to keep it in a version that can be played on 1.3.5. Time to fork the code, I think.

Incidentally, the mesh vertex setting crash seems fixed. I just counted (rather, I got the computer to count) and my track mesh is up to 72,000 vertices. Frame rate still 60fps! But that reminds me, meshes are a bit unforgiving on their input. From time to time I feed a mesh something that isn’t a vec3 by mistake and it crashes Codea. A nice error message would be a bit more friendly, methinks.

I just need to sort out some colours and I’ll have the complete code in under 1000 lines.