How to render sprites when using perspective camera?

Hey everyone. Sorry for posting so many questions but I am working rapidly on trying to create a game and have many questions :stuck_out_tongue: I have figured out how to load multiple tilemaps into my game collection w/ associated collision objects and things are working well. However, I am toying with the idea of using a slight perspective view on the map to give a fake 3D perspective. However, when I set the game to use a perspective camera my sprites become invisible:

Is there a simple way to allow these sprites to render on top of the tilemap surface? I tried experimenting with different Z levels but the sprites never appear. I searched the forum but I donā€™t see anything which quite answers this question.

Thanks for any help if anyone has suggestions :blush:

(Please add questions under the ā€œQuestionsā€ category)

Whoops, sorry about that :stuck_out_tongue:

EDIT: Iā€™m noticing also, if I destroy an enemy it flies back with an animation. If it flies outside of the tilemap itā€™s viewable against the black background. So the sprites are rendering but for some reason regardless of the Z value I give them they donā€™t show up against the tilemap surfaceā€¦

1 Like

From the box all 2d objects as tilemaps, sprites, etc are rendering without the depth test, just ordering by pivotā€™s Z position in view projection coordinare system. In your example a tilemap likely overlaps sprites on this camera angle.
To fix this you can create your own materials (just copy from builtins folder), and set them new tag, see


Then add to renderscript render.draw(my_new_predicate). In this case you can constrol the order of render.

Also check my example with fake 3d view: https://github.com/Dragosha/slasher-prototype

4 Likes

Also, you can enable depth testing in the render script. It is disabled for ā€œtileā€ predicate in the default and rendercamā€™s render scripts.

-- render.disable_state(render.STATE_DEPTH_TEST)
render.enable_state(render.STATE_DEPTH_TEST)
1 Like

Thanks, I have enabled the depth test in the rendercam script. However, I still donā€™t see sprites unless I set a very low view distance and then move the camera (following the player) above them (see screenshot below.) If I set the view distance to a ā€œgoodā€ higher value then the sprites never render.

imstupid

How did you configure the camera near and far planes? Could it be that the sprites are outside?

Here are my camera settings:

And in my game script I have:

function update(self, dt)
if player_ref == nil then
return
end
local camera_pos = go.get(player_ref, ā€œpositionā€)
camera_pos.y = camera_pos.y - 200
camera_pos.z = 400
go.set("/camera", ā€œpositionā€, camera_pos)
end

The camera moves around tracking my invisible player, so I know the update loop is working to set the camera position. I tried Near Z: -1200 as well and if I change Far Z to a lower value then the terrain cuts off with a black bar before reaching the top of the screen.

Also, Iā€™ve noticed at certain locations for the camera, even the tilemap is having depth issues (hiding the wall layer and displaying only the background tiles:)

Also, I tried experimenting with rotating the camera using an animation:

go.animate("/camera", ā€œeuler.zā€, go.PLAYBACK_ONCE_FORWARD, 360, go.EASING_LINEAR, 5)

However this seems to be the limit of my creativity :stuck_out_tongue: :blush:

Assertion failed: size <= Capacity(), file D:\a\defold\defold\tmp\dynamo_home\sdk\include\dmsdk/dlib/array.h, line 456

The camera starts rotating properly but after ~30 degrees of rotation the game crashes with the error.

I now have the proper camera perspective working!

image

I followed the tutorial here ( https://defold.com/manuals/render/#render-predicates ) and
created a custom material with the tag ā€œspriteā€ and attached this to each game sprite. Then, I
modified the renderer to render this predicate after the models. Now the sprites show up as expected :slight_smile:

However, the other two issues still exist (tiles in the top layer flickering while the background tile layer at Z: 0 remains visible, and the euler.z camera rotation crashing the game.) If anyone knows how to fix these Iā€™d be highly grateful :smiley:

4 Likes

can you share your material and code? iā€™m learning how to do 2.5d and 3d too