Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 3d Quads to Represent 2d Surfaces

Author
Message
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 5th Jul 2010 02:28 Edited at: 5th Jul 2010 02:29
I'm creating a modern style 2d engine, all those sprite effects and everything. Though due to lack of control over sprites I'm going to have to use 3d quads to represent 2d surfaces, even better would be to use 3d triangles or single polys for a surface for better performance, but let's just do quads for now. The advantage of 3d is pixel shaders and subdivided quads (so subdivided plains you could ripple or wave, which you could use vertex shaders). The major disadvantage (in DBP's case) of using 3d and shaders for alpha blending is lack of control over Z-Sorting. No matter what you do it's based off object creation order. A way round this I think:

- Every frame check if a new entity is introduced into the scene.
- Insert it into it's layer, then reset and assign every object after it to correct the draw order( so the entity with the greatest zdepth will be assign to object 1. You'd then run through each layer or depth greatest to lowest and assign an entity to a object starting from 1 and upwards)

The Z-sorting issue when using shaders is a pain. Even if you position the object well behind the other, it's still based off creation order, even the Object Creation command doesn't help. I opted to not change the depth(z position) of the objects because they don't clip each when using a pixel shader for alpha blending, plus there's no point because of the Z-sorting issue.

Anyway, I should actually ask for what I needed: How do you scale and position a quad to fit a 2d size. So if you had a box 128x128 in pixels at position x:256 y:128, how would you represent that using a 3d quad?

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Jul 2010 03:05
For depth sorting, either set object transparency on for the objects that need it (even if you are doing transparency via a shader), or SET GLOBAL OBJECT CREATION 6 to do it for all objects every frame - it's reasonably fast since I revamped the sorting for 7.4.

Only if that is still too slow should you try and handle it all manually.

Cloggys offers the ability to set an orthographic camera view or if you don't mind a little extra work, you can use my plug-ins too:


Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 5th Jul 2010 03:45 Edited at: 5th Jul 2010 03:46
Huh!, I swear set object transparency didn't work with shaders, but it seems it does. Your right, transparency mode 2 (others work, but I think this one doesn't affect the blending result) corrects the z-sorting issue. An your right about SET GLOBAL OBJECT CREATION 6, I wonder which is faster though...

With your orthographic camera code, all the objects disappear, any ideas whats up?

Thanks btw IanM

So now my question needs to be altered:

How do you scale and position a quad to fit a 2d size. So if you had a box 128x128 in pixels at position x:256 y:128, how would you represent that using a 3d quad at any depth? So if I position the object way off into the background, it still amounts to the same size in pixels.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 5th Jul 2010 04:08 Edited at: 5th Jul 2010 04:08
Ahh, forgot about the orthographic size and position of the camera. Think this is right:



That should be 1:1 pixel - 3d size/coord relation if that's correct.

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 5th Jul 2010 17:23
The transparency (or not) in shaders overrides the transparency (or not) in DBPro so from that perspective, setting transparency in DBPro doesn't matter. However DBPro does need to know whether transparency is enabled (or not) to ensure that the objects can be sorted and rendered in the correct sequence.

If you use the SET GLOBAL OBJECT CREATION to 6 or 7, then DBPro won't care about transparency at all and will automatically always sort all objects by depth.

And as you are also using planes that face the camera, you don't have any overlap issues that Fallout had with his 3D scenes.

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 5th Jul 2010 18:10
As a side note, here's a little function that makes it easy to apply sprite sheets to plains - sets the UV coordinates to suit a grid...



Like if you have a sprite sheet with a grid of 8x8 sprites, this would set object 100 to use sprite 3,3:

set_uvs(100,2,2,8,8)

Might help avoid the pain of re-texturing. I use an array to hold each objects current tile, then if the tile is the same I just exitfunction.


Health, Ammo, and bacon and eggs!

Login to post a reply

Server time is: 2026-07-25 08:13:14
Your offset time is: 2026-07-25 08:13:14