Delete off-screen objects or not?

I am working on 3d game.
When i play html build on laptop with(built-in graphics card) i spend 20-25 ms to draw frame. I think that because i have a lot of go and sprites in screen. So i think that if i draw only visible sprites it will be good.
I split my map to chunks(5x5), then make ray cast in player fov. If ray intersept chunk, chunk visible. If ray intersept wall stop ray.
Without cunk i draw 950 sprites.
With chunk i draw:
best: 100 sprites
worst:600 sprites
average: about 300 sprites

I hide only walls, so all other objects are drawn

It don’t gave me visible boost in graphics, but also add additional work in script. At all i lost some more frames. :thinking: I can make some optimisation for sctipt, but it is not help globaly.
1)Maybe i am doing something wrong? :thinking:

2)Chunks contains one more problem. Sometimes i hide chunk, when i can see it. I can increase number of rays. But it is not 100% solution.

3)So delete objects or not? :thinking:

1 Like

I think you are on the right track thinking about hiding/showing the walls but I’m not sure how you do it.
I understand that you want to simulate that visual feeling from classic raycasting techniques.
If you answer these questions we might get a clearer view to your problem.
Is your leveldata in some kind of grid (so the “cubes” are not placed directly in a collection)
How is one “cube” structured? Is it a gameobject with 4 sprites (in 4 directions)?
How far is your “drawing distance”?
Do you do any hiding of sprites within the gameobjects? (eg two walls facing each other or all sprites facing away from player view)
How do you disable the chunk? Is it by sending disable to the gameobjects or another way?
Is a chunk just a logic table or do you use collections somehow?

2 Likes

I will describe all.

My level is json file. It contains different objects(player,walls,objects,pickups). The wall array looks like

 "geometry": [ 
              [{"texture":2},{"texture":2},{"texture":5}],
              [{"texture":2},{},{"texture":5}] ...
]

Empty object is empty space in map.

The level is grid based.Wall can’t be rotated. (black:wall, white:empty )

Yes.

Not sure what about you asking. I draw all objects.

I hide sprites if two walls facing each other.

I try delete and send enable/disable. Looks like deleting is faster.Not
sure because i don’t make tests. It is my feling

local function disable_chunk(chunk)
	for i,obj in ipairs(chunk.go) do
		go.delete(obj)	
	end
	chunk.go={}
end

chunk is a table. go it is array with all walls(go) in that chunk

chunk = {x,y,end_x,end_y,go,visibility,new_visibility}

The logic of enable/disable chunk looks like:
1)cast rays in player fov.(DDA algorithm like here Raycasting)

  • every ray, mark all cells that it touch

2)check chunks visibility

local function update_visibility(chunk,cells)
    for y=chunk.y,chunk.end_y do
        for x=chunk.x,chunk.end_x do
            if(cells[y][x])then
                chunk.new_visibility=true
                return	
            end	
        end
    end	
end

3)If chunk visibility changed. Create or destroy it.

1 Like

I push all to github. The logic of enable/disable in chunk_factory.script

1 Like

Maybe i am fight with windmills. I test html build on this laptop.
When i draw all sprites(953) i have 12-16ms frames.(6-7ms graphics). Not sure why yesterday i have problem. Maybe something wrong was with browser.
I make a test with bigger map(2352 sprites):6-7 ms graphics.
I make a test with small map(86 sprites):6-7 ms graphics
I make a test with one wall(4 sprites):6-7 ms graphics. :thinking:
no sprites 1 ms.
I don’t understand wtf is happened.
On pc and other laptop, everything ok (0.2-1 ms graphics)(953 sprites)

What browser, and are you building debug or release?

1 Like

firefox release

I test in yandex browser(same as chrome) In big map i get graphics:0.2. But 20-30 ms to frame(game object 10-15ms). So looks like i have a problem with my firefox, on laptop.

In microsoft edge i have graphics: 1-2 ms in biggest map(2352 sprites):thinking:

I disable hardware acceleration and get 2-3 ms in graphics.

In https://github.com/d954mas/defold-3d-shooter/blob/master/pseudo/go/player/camera.camera your draw distance is this.

near_z: 0.1
far_z: 1000.0

1 Like

ok,looks like i can have smaller distance, about 50 i think.

1 Like

Yes, and it should remove need for you to manually hide objects. Though really a game like this should have very little performance issues with modern hardware.

1 Like