Log In  

Cart #34411 | 2016-12-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
15

Here's a toy I have been playing around with. Perhaps somebody can use these functions for fancy shaded 3D rendering or something like that.

I have a few functions that draw "RGB" pixels to the screen using pattern dither that are approximated using the rgb values of the pico8 palette.

color_bayer_plot_8(x,y,r,g,b) -- writes a given r,g,b at the x,y (r,g,b are from 0 to 1)

fast_rgb_create() -- pre-renders patterns using a 4X4 bayer dither representing 8 levels of R and B and 16 levels of G.
fast_rgb(x,y,r,g,b) -- writes a given r,g,b at the x,y (r,g,b are from 0 to 1)

function solid_trifill( x1,y1,x2,y2,x3,y3, r0,g0,b0,r1,g1,b1,r2,g2,b2)
Renders a triangle with the three corners colored at rgb and shades smoothly between them. Right now this uses the slower color_bayer_plot_8.

If you move the cursor around with the arrow keys you can see the triangles. The vertices can be grabbed with the "z" key and the color of the vertices can be changed with the "x" key.

Originally I had been thinking about making a gradient image compression algorithm, but it seemed less interesting after getting half-way through.

Here's a spinning R G B triangle for fun.

Cart #34413 | 2016-12-29 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
15

P#34412 2016-12-29 00:25 ( Edited 2016-12-29 15:42)

:: Scathe

You never cease to amaze me, dude.

P#34418 2016-12-29 03:54 ( Edited 2016-12-29 08:54)

Brilliant! Thank you for doing this.

suggestion: you can save a lot of tokens by directly importing the look-up table generated by your cart (attached below) into the spritesheet, then eliminating every method except for fast_rgb().

Or if you'd rather trade tokens for keeping your spritesheet intact, you could replace fast_rgb_create() and fast_rgb() with these modified functions, which precalculate and pack the LUT into 8kb of RAM instead:

rbitmasks={{shr,12},
          {shr,8},
          {shr,4},
          {shr,0},
          {shl,4},
          {shl,8},
          {shl,12},
          {shl,16}}
wbitmasks={{shl,12},
          {shl,8},
          {shl,4},
          {shl,0},
          {shr,4},
          {shr,8},
          {shr,12},
          {shr,16}}       

bayer_lut={}
function    fast_rgb_create()
    local sx=0
    local sy=0

    local bx=0
    local by=0

 local bit,mask,addr

    --init lut
    for j=0,127 do
     for i=1,16 do
      bayer_lut[j*16+i]=0
     end
    end

    for g=0,15 do
        for r=0,7 do
            for b=0,7 do            
                for i=0,3 do
                    for j=0,3 do
                        x=(g%4)*32+(r%8)*4+i
                        y=flr(g/4)*32+(b%8)*4+j
                        c=color_bayer_8( x, y,r/7,g/15,b/7)
                        bit=x%8+1
                        x=flr(x/8)
                        addr=y*16+x+1
                        mask=wbitmasks[bit]
                        bayer_lut[addr]+=mask[1](c,mask[2])
                        --sset(x,y,c)
                    end
                end
                sx+=4

            end
            sx=0
            sy+=4
        end
    sx=0
    sy=0
    end
end

function    fast_rgb(x,y,r,g,b)

        local x=flr(x)
        local y=flr(y)

  local bit,mask

        local g=flr(g*15)
        local r=flr(r*7)
        local b=flr(b*7)

        local sx=(g%4)*32+(r%8)*4+x%4
        local sy=flr(g/4)*32+(b%8)*4+y%4
     bit=sx%8+1
        sx=flr(sx/8)
        mask=rbitmasks[bit]
        pset(x,y,band(mask[1](bayer_lut[sy*16+sx+1],mask[2]),0xf))
end

P#34430 2016-12-29 10:42 ( Edited 2016-12-30 00:25)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-10-25 19:44 | 0.018s | 2097k | Q:33