Partial transparency/alpha in mesh?

Hi all, I’m trying to simulate a 2d spotlight underwater. The water is a semi transparent mesh which gets less transparent the deeper you go. The water mesh is drawn over the ground mesh, and everything else that is underwater. The spotlight is a triangular mesh and it’s two end points are completely transparent to simulate falloff.

I tried drawing the spotlight with additive blending but I guess it only works with bitmaps, I’d like to avoid drawing the mesh to a bitmap every frame as it can completely fill the screen.

Is there any way to set the transparency of a mesh (acording to the spotlight position and alpha values), or even better somehow draw it with additive blending? Any other suggestions to simulate lighting?

Thanks

Additive blending does work with meshes. Could you post your code?

blendMode(ADDITIVE)
myMesh:draw()
blendMode(NORMAL)

One thing to remember with transparency and additive drawing, is that black (RGB 0) renders as transparent, because you’re adding 0 to what’s already there. Alpha then is in a sense redundant with additive drawing (though it is applied, I believe as + R * A, + G * A, + B * A). So, instead of having your spotlight mesh fade to alpha zero, experiment with having it fade from a bright colour (near white) to black, with additive blending.

Also, with additive blending, draw order is important. Draw the spotlight after everything else that you want the spotlight to brighten.

Ah yes, upon closer inspection I noticed it did actualy work but the result wasn’t at all what I expected. I’m not sure what is wrong exactly, I’m going to experiment some more with the transparency of the water. Thanks!

You could use a shader to calculate distance from the spotlight and adjust transparency accordingly

@Kirl Not sure if this is what you’re after. Move your finger around the screen to move the cone.

supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)

function setup()
    dx,dy=0,0
    m=mesh()
    for z=1,255 do
        m:addRect(WIDTH/2,900-z*2,z*2,2)
        m:setRectColor(z,color(255,255,255,z))
    end
    m:addRect(WIDTH/2,-110,WIDTH*2,1000)
    m:setRectColor(256,color(255,255,255,255))
    m:addRect(-312,550,1100,3000,math.rad(-18.5))
    m:setRectColor(257,color(255,255,255,255))
    m:addRect(1080,550,1100,3000,math.rad(18.5))
    m:setRectColor(258,color(255,255,255,255))    
end

function draw()
    sprite("Cargo Bot:Startup Screen",WIDTH/2,HEIGHT/2)
    pushMatrix()
    translate(dx,dy)
    m:draw()
    fill(0)
    ellipse(384,900,20)
    popMatrix()
end

function touched(t)
    dx=dx+t.deltaX
    dy=dy+t.deltaY
    if dx<-380 then
        dx=-380
    elseif dx>380 then
        dx=380
    end
    if dy>450 then
        dy=450
    elseif dy<-800 then
        dy=-800
    end    
end

Thanks dave, I’m going to see how that looks in my setup! :slight_smile: