Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Lag On Many Objects

I created a grid with each tile being an entity. It causes slowdown if the grid is too big, which boils down into “I have too many objects, I need to get rid of some”. But, is there anything else in general that causes slowdown?

Tagged:

Comments

  • Posts: 815

    @MalikWilburn - how big is your grid and could you let us know what your system is (OS,iPad gen). A small example code would help as it may not just be generation of your grid.

  • The grid is 23x7 and each individual tile is about 100px x 100px. I’m running this in iOS 11.2.1 on an iPad mini.

  • edited March 17 Posts: 815

    @MalikWilburn - OK, are they connected or part of a 3D structure, you can’t display all those at once. Also is each a flattened cube?

  • edited March 17 Posts: 815

    @MalikWilburn - just borrowed some code off of @dave1707 and changed from spheres to a series of 150 tiles. Displays them all and rotation is smooth. I am using an iPad Pro withiOS 11. Code below:


    -- CraftCubes -- Use this function to perform your initial setup displayMode(FULLSCREEN) function setup() assert(craft, "Please include Craft as a dependency") assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency") scene = craft.scene() skyMaterial=scene.sky.material skyMaterial.sky=color(158, 202, 223, 255) skyMaterial.horizon=color(98, 166, 114, 255) scene.sun.rotation=quat.eulerAngles(20,45,-30) v=scene.camera:add(OrbitViewer, vec3(0,0,0), 200, 0, 1000) -- create a single cube createCube(vec3(8,8,1),vec3(0,0,0)) -- create multiple cubes without a table for z=1,150 do createCube(vec3(10,10,1),vec3(math.random(-50,50), math.random(-50,50),math.random(-50,50))) end -- create multiple cubes using a table tab={} for z=1,150 do table.insert(tab,createCube(vec3(4,4,1),vec3(math.random(-50,50), math.random(-50,50),math.random(-50,50)))) end end function draw() update(DeltaTime) scene:draw() end function update(dt) scene:update(dt) end function createCube(s,p) local pt=scene:entity() pt.position=vec3(p.x,p.y,p.z) pt.model = craft.model.cube(s,p) pt.material = craft.material("Materials:Specular") pt.material.diffuse=color(255,0,0) return(pt) end

    If this doesn’t give you any ideas you need to post some code for us to understand what you need.

    If you need to address individual tiles you need to hold them all in a table.

  • dave1707dave1707 Mod
    edited March 17 Posts: 6,828

    Here’s some code that creates a 23 x 7 100x100 pixel tiles. I set each tile to a random color to see them easier. This run at 59 to 60 fps on my iPad Air.

    displayMode(FULLSCREEN)
    
    function setup()
        assert(craft, "Please include Craft as a dependency")
        assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")        
        scene = craft.scene()
        skyMaterial=scene.sky.material
        skyMaterial.sky=color(158, 202, 223, 255)
        skyMaterial.horizon=color(98, 166, 114, 255)
        scene.sun.rotation=quat.eulerAngles(20,45,-30)
        v=scene.camera:add(OrbitViewer, vec3(0,0,0), 2500, 0, 10000)
        v.camera.farPlane=10000
    
        -- create 23 x 7 100 x 100 tiles of random colors
        tab={}
        for x=-11,11 do
            for y=-3,3 do
                table.insert(tab,createCube(vec3(100,100,1),vec3(x*50,y*50,0))) 
            end     
        end
        fill(255)
    end
    
    function draw()
        update(DeltaTime)
        scene:draw()
        text(1//DeltaTime,WIDTH/2,HEIGHT-50)
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    function createCube(s,p)
        r,g,b=math.random(255),math.random(255),math.random(255)
        local pt=scene:entity()
        pt.position=vec3(p.x,p.y,p.z)
        pt.model = craft.model.cube(s,p)
        pt.material = craft.material("Materials:Specular")
        pt.material.diffuse=color(r,g,b)
        return(pt)
    end
    
  • Posts: 815

    @dave1707 - neat, I was still thinking in 2D, in 3D the grid will be fully visible if your viewer is set to enable it.

Sign In or Register to comment.