Just junk to pass the time

Here’s a Voxel version of the Sierpinski cube. I was able to go one more level higher than the Craft version above. There’s still a problem with this that I haven’t figured out yet. This will take a lot longer to show the cube.

Updated code below.

@dave1707 - mine crashed on 81, reduced to 72 still crashed.

Edit: @Simeon - tried at 64 but ran into another issue, fed up of crashing as each time have to scroll down dependencies to get to Cameras so long press on run to save and run. Then Codea was tied up not running.

Edit 2: after readIng @dave’s comments realised needed to run with power of 3 size. Ran with 27 was OK. Otherwise sat staring at black screen. Is there a memory issue if not a power of 3? Or is it just hung up.

@Simeon @John Here’s a Sierpinski cube being drawn using voxels. I was able to go up more levels over my Craft version, but there’s a problem. I don’t know voxel stuff that well, so I’m not sure if things are set right. The 81 x 81 x 81 cube works, but the next one up 243 x 243 x 243 doesn’t display the cube. It looks like just one or two slices are drawn and I don’t know why. Are Codea limits being hit.

PS. I just realized that I can’t display more than 128 cubes high. Changing code to handle that still doesn’t fix the display.

displayMode(FULLSCREEN)

function setup()
    
     ss=81           -- cube size, powers of 3 only (3, 9, 27, 81, 243)
     --ss=243         -- doesn't draw correctly    
    
    fill(255)
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
    scene = craft.scene() 
    v=scene.camera:add(OrbitViewer,vec3(ss,ss,ss), ss*3, 0, 6000)    
    v.rx=-10
    v.ry=-30
    scene.voxels.blocks:addAssetPack("Blocks")
    grass = scene.voxels.blocks:new("Table")
    grass.setTexture(ALL, "Blocks:Table") 
    scene.voxels:fill("Table")                                 
    scene.voxels.visibleRadius=90
    scene.voxels:resize(vec3(60,1,60))          
    scene.voxels.coordinates = vec3(ss,ss,ss) 
    count=0
    cube(0,0,0,ss)
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()
    text("Cube size "..ss.."x"..ss.."x"..ss.." = "..(ss^3).." cubes",WIDTH/2,HEIGHT-25)
    text("Number of cubes drawn "..count,WIDTH/2,HEIGHT-50)
end

function cube(x,y,z,size)
    local s=size/3
    for x1=-s,s,s do
        for y1=-s,s,s do
            for z1=-s,s,s do
                if (y1~=0 or z1~=0) and (x1~=0 or z1~=0) and (x1~=0 or y1~=0) then
                    if size~=3 then
                        cube(x1+x,y1+y,z1+z,s)
                    else
                        count=count+1                    
                        scene.voxels:block((x1+x+ss),(y1+y+ss),(z1+z+ss))
                    end
                end
            end
        end
    end
end

@Bri_G It has to be a power of 3. If It’s some other value then it’s just not going to work. It not a matter of memory, just the way the calculations work. I’ll have to try running the above code on my iPad Pro with 128GB memory.

I ran the above code on my iPad Pro 128GB. Size 81 ran OK and size 243 ran but it didn’t show the cube, but just a slice. So I don’t think memory is the problem on the 243 run.

@dave1707 yes on the much simpler ifs … but even so it feels like there should be an even simpler [clever] formulation, but i don’t see it.

voxel one is nice. i moved the fill outside the loop. thanks!

@RonJeffries Thanks for the comment about the scene.voxel:fill. I knew there was something I wanted to do but I couldn’t remember which program or what it was that I wanted to fix. That was it, now I can stop trying to remember what I wanted to do. I moved it to setup() and I’ll fix the above code later.

@Simeon @dave1707 - latest update above from Dave crashes on mine at 27, but OK on 9. Tried to change the image to an image on Dropbox but it crashed - see error message below. I think this is down to image format as I accidentally linked in a. jpeg image. I thought Craft would accept png and jpg/jpeg. Will convert image and try it again with png.

Edit: nope crashed with png as well. The error says that the call for the image requires a string. I put in asset path links, but in both tests I loaded an image into a variable and used the variable name. Is Craft, or just this Craft call, limited to string paths for objects like images?

Edit2: Ignore my ramblings, I think this is a feature of the construction of texture types for Craft, the few lines above the setTexture command sets up the asset path etc so playing with this a bit further unless someone can point out the syntax needed for path definition of textures in Craft Voxel from Dropbox.

Wall of 10,000 digits of Pi.

displayMode(FULLSCREEN)

function setup()
    digits=10000
    calc()
    rectMode(CENTER)
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
    scene = craft.scene()
    v=scene.camera:add(OrbitViewer, vec3(86,83,0), 28, 0, 1000)
    v.rx=25
    v.ry=-60
    tab={}
    for z=0,10 do
        tab[z]=image(16,16)    
        setContext(tab[z])
        background(255)
        noFill()
        stroke(0)
        strokeWidth(1)
        rect(8,8,16,16)
        fill(0)
        if z==10 then
            text(".",8,8)
        else
            text(z,8,8)
        end
        setContext()
    end
    cnt=0
    size=100
    for y=1,size do
        for x=1,size do
            cube(size+1-x,size+1-y,0)  
        end        
    end
    fill(255)    
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw()
    text("Wall of "..digits.." digits of Pi",WIDTH/2,HEIGHT-50)
end

function cube(x,y,z)
    cnt=cnt+1
    c=scene:entity()
    c.position=vec3(x,y-12,z)
    c.model = craft.model.cube(vec3(1,1,1))
    c.material = craft.material(asset.builtin.Materials.Standard)
    if cnt==2 then
        p=10
    else
        p=tonumber(string.sub(str,cnt,cnt))
    end
    c.material.map = tab[p]
end

function calc() 
    local pi,tab={},{}
    local size=math.ceil(digits/7)+1
    local size1=math.ceil(size*7)
    local sum,carry,b,z2
    local d1,d2=10000000,9999999 
    str=""
    local sf=string.format
    local mm=math.modf    
    for z=0,size1 do
        tab[z]=5*z+3
    end       
    for t=1,size do
        carry=0
        for z=size1,0,-1 do
            z2=z*z
            sum=tab[z]*d1+carry
            b=27*(z2+z)+6
            tab[z]=sum%b
            carry=(sum//b)*(2*z2-z)
        end       
        tab[0]=sum%d1
        pi[t]=sum//d1
    end        
    carry=0
    for z=size,1,-1 do
        b=pi[z]+carry
        if b>d2 then
            b=b-d1
            carry=1
        else
            carry=0
        end
        pi[z]=sf("%07d",b)
    end       
    pi[1]="3"
    table.insert(pi,2,".")
    str=table.concat(pi)
end