scene_blend alpha_blend sorting

What it says on the tin: a place to discuss proposed new features.
Post Reply
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

scene_blend alpha_blend sorting

Post by mkultra333 »

I've got opaque materials ok, and I've got 100% transparent materials ok. But now that I'm trying to alpha blend for partially transparent materials, I'm finding that Ogre doesn't do it properly because it only depth sorts for scene_blend add, not scene_blend alpha_blend.

This doesn't make much sense to me. Perhaps I'm not understanding it correctly, but to my way of thinking you need to sort partially transparent things just as much as you need to sort fully transparent things. I'll show what I mean with the error that I'm getting.

Here is the scene with alpha_blend add. There's a few black blocks of garbage since I've made everything black except the transparent faces, so ignore the black bits. It renders correctly. This image shows the scene from 4 different angles to give a feel for the depth of the objects.

Image

Now I add an alpha channel to the material using the following texture. It makes the top of the texture transparent, the bottom opaque, and smoothly changes from one to the other inbetween.

Image

But now, because scene_blend alpha_blend doesn't draw things from furthest to closest, the scene is messed up.

Image

The only way I can think to make this work is maybe with multiple passes, but that's a frame rate killer and should be unnecessary since we'd get the correct effect for semi-transparent objects if they just depth sorted the same as fully transparent items.

I don't really like the way Ogre decides how to sort items. I'd like more control over it. For a start, I'd like to be able to designate material passes to be sorted as transparent, rather then leaving it up to Ogre to (erroneously) decided when to sort.

Also, I'd like the option to have transparency sorted in the reverse order if I want (not related to the issue above, but something that came up as a useful option in one of the shaders that was recording depth information... I wanted to record the depth of the furthest transparent face rather than the closest.)
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: scene_blend alpha_blend sorting

Post by xavier »

Maybe I am misunderstanding what you have, but it seems you have objects (Entity instances) that have some materials solid, and some transparent. The problem is, these different alpha types share the same origins, and if you are having issues sorting properly within the same Entity instance, there's nothing that really can be done about it, other than you have to change the way your objects are modeled.

Or do I misunderstand your situation?
mkultra333 wrote:I don't really like the way Ogre decides how to sort items. I'd like more control over it. For a start, I'd like to be able to designate material passes to be sorted as transparent, rather then leaving it up to Ogre to (erroneously) decided when to sort.

You have this control -- sorting orders can be overridden in Ogre. IIRC you have to implement RenderQueue callback interface(s), the names of which escape me at the moment, to supply your own sorting algorithm.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: scene_blend alpha_blend sorting

Post by mkultra333 »

xavier wrote:Maybe I am misunderstanding what you have, but it seems you have objects (Entity instances) that have some materials solid, and some transparent. The problem is, these different alpha types share the same origins, and if you are having issues sorting properly within the same Entity instance, there's nothing that really can be done about it, other than you have to change the way your objects are modeled.

Or do I misunderstand your situation?
No, that's not the issue. Ignore the black blocks. All the above have the same material, and it is partially transparent. In this case, the top of the material is totally transparent, the bottom opaque, and in between it goes from one to the other. The problem is that they need to be depth sorted to render correctly, since they are (semi-) transparent. But Ogre only depth sorts for scene_blend add, not scene_blend alpha_blend.

Imagine a scene with lots of glass, but the glass is dirty with dust, so it is not as transparent as clean glass. The alpha channel tells you how dusty the glass is at any particular point. Ogre will render clean glass correctly, but gets messed up trying to render dirty glass.
Last edited by mkultra333 on Sun Aug 30, 2009 3:36 am, edited 2 times in total.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: scene_blend alpha_blend sorting

Post by mkultra333 »

Perhaps the black block in the above is confusing, here's another image with the error circled in red.

Image

Edit:
You have this control -- sorting orders can be overridden in Ogre. IIRC you have to implement RenderQueue callback interface(s), the names of which escape me at the moment, to supply your own sorting algorithm.
That will solve my need for reverse sorting (though just having a material keyword would be much easier.) But can that solve the issue of there being no sorting at all unless you use scene_blend add?
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: scene_blend alpha_blend sorting

Post by xavier »

mkultra333 wrote:All the above have the same material, and it is partially transparent.
This is self-contradictory -- a material ultimately consists of N passes, each of which have their own "transparency" setting. There is no such thing as "partially transparent" -- something either acts as a transparent object (it is blended into the scene based on alpha channel values, traditionally speaking), or it is solid (alpha is disregarded).

Ogre renders passes, not materials, not Entities. So what you have is a series of passes that are separated (in terms of when they are rendered) primarily by transparency -- solids first, then transparents from back to front (because otherwise sorting happens incorrectly). How Ogre sorts those transparents depends on the origin of the submesh being rendered, which comes from the parent mesh. So if you have dirty glass, so to speak, that shares an origin with something else that is also transparent, there can be no guarantee on how it will be sorted (unless you provide that sorting). Ogre itself will only deal with it on a back-to-front basis, and it's entirely possible that objects will share an origin and therefore render in an arbitrary order.

The problem with specifying behavior of core features based on one's immediate needs, is that those needs most often are not applicable in the general sense. What if you add a keyword that acts how you need it to, and someone else needs that keyword to behave differently? This is why Ogre has so many callback interfaces -- you can alter the default behavior to fit your needs without having to change the core to do it.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: scene_blend alpha_blend sorting

Post by mkultra333 »

Actually I think there may be another issue that's my fault. All the above transparent stuff is a single entity. If it were to be rendered correctly it would need to be sorted by faces , but ogre sorts by entities, not faces, so it isn't going to do any sorting on the above at all. That's my problem and I'll need to fix that.
There is no such thing as "partially transparent" -- something either acts as a transparent object (it is blended into the scene based on alpha channel values, traditionally speaking), or it is solid (alpha is disregarded).
Maybe I don't understand, I had read here that you need alpha_blend add or ogre won't sort things.
nullsqured: You need "depth_write off". That plus "scene_blend add" tells Ogre to sort the transparent objects back-to-front, otherwise you'll get incorrect blending as you can currently see
Perhaps blend_mode alpha_blend works too... I haven't checked yet since realizing my above "it's all one entity" error.

However, what I mean by "semi-transparent" is that the object is not entirely transparent, for instance 90% opaque and only 10% transparent, based on the alpha channel and blend_mode alpha_blend. If Ogre actually does render that as a transparency pass, with sorting, then I won't have a problem, and this has been my screwup due to the single-entity thing. :)
The problem with specifying behavior of core features based on one's immediate needs, is that those needs most often are not applicable in the general sense. What if you add a keyword that acts how you need it to, and someone else needs that keyword to behave differently? This is why Ogre has so many callback interfaces -- you can alter the default behavior to fit your needs without having to change the core to do it.
I agree, but on the other hand I think for things like material keywords it doesn't hurt to have a lot, even if they do things that don't seem immediately useful to everyone. This is because I consider the point of a material script is to allow fast, easy experimentation among other things. To make it easy to come up with all different kinds of complex behaviours without having to do a heap of programming. Makes it easier for artists, for one thing.

I think being able to force sorting on or off, and reverse the order, are the kinds of things that have generic usefulness. Turing sorting off was added not that long ago, a complemenatary turn-sorting-on keyword and reverse-sorting keyword would be neat.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: scene_blend alpha_blend sorting

Post by Kojack »

The conditions for depth sorting are as follows:
A renderable is pushed into the transparent render queue (which is sorted) if the renderable's first pass contains any destination blend factor (if dest factor isn't zero or the source factor contains a dest component, both scene_blend add and scene_blend alpha_blend meet that criteria), and at least one of depth write, depth check or colour write is disabled.

So make sure when using scene_blend alpha_blend that you have depth write off (if depth check is off then transparent objects won't be occluded by closer non transparent objects).

Transparent objects are sorted by the distance from the camera to the closest (to the camera) corner of the entity's bounding box. It's possible for an object to have it's geometry behind another object, but it's bounding box has a closer corner so it gets drawn on top. That's really the pitfall of non per-pixel alpha sorting, however you sort whole objects there's going to be a case where it can come out wrong (especially for intersecting transparents).

Edit: ah, yes, a single entity will be problematic for transparency.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 114

Re: scene_blend alpha_blend sorting

Post by mkultra333 »

Thanks for the extra info, Kojack.

I found something odd in the script parsing, don't know if it's a bug. For an hour or so I'd been confounded by weird shader behaviour, blend modes were not behaving as expected. Finally I found the following.

Code: Select all

		pass
		{
				scene_blend add //modulate //alpha_blend
				depth_write off
I'd commented out the other modes as I tried to get it working. But it was just overdrawing, no add or, when I used it, modulate. Finally I read the log and noticed:
Compiler error: fewer parameters expected in 0_GEL_Generic.material(70): scene_blend supports at most 2 arguments
At most two arguments? I thought I was only putting in one. So I tried

Code: Select all

		pass
		{
				scene_blend add
				depth_write off
and it worked. All I did was get rid of the comments. Seems the material parser isn't picking up comments properly.

Anyhow, I eliminated the "single entity" issue temporarily by making every transparent triangle an entity in itself. No, not a good solution, but just a stop gap while I worked out the alpha_blend stuff. Unfortunately it led to really weird behaviour... triangles half-overdrawing each other and no apparent sorting, even though every check I ran for bounding boxes, entities, etc seemed fine. I've left the issue for the moment because I don't want to get stuck for a week with no progress, I've moved onto getting simple alpha reject working, and that's going ok.

I may even try to avoid alpha_blend completely, because I now realize if I use it I'll have to break those alpha blended parts of the map up into lots of different entities, which means lots of different meshes and bad performance. It seems using mixtures of modulate and add are ok, they come out looking right enough even without sorting, which is why I never noticed the problem until now. So I'll see if add and modulate can be used instead.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
Kenshido
Gremlin
Posts: 153
Joined: Tue Jun 09, 2009 9:31 am
Location: Russia
x 12
Contact:

Re: scene_blend alpha_blend sorting

Post by Kenshido »

Hello.

I looked over Lineage2's tree, and it's made via alpha blending that works correctly. Only characters don't take a part in this blending, and it's pretty old engine! So, there's no solution for alpha_blend in ogre? Some people recomend to sort 3d model of a tree on a stage of modeling, does anybody know how to do this? And is it an ordinary solution for ogre?
Post Reply