DrawableSpans, Vertex Colors, and Shading

Announcements and discussion regarding any projects related to Cyan Worlds' Plasma Engine including (but not limited to) CyanWorlds.com Engine, Drizzle, OfflineKI, PyPRP, and libHSPlasma.

DrawableSpans, Vertex Colors, and Shading

Postby Christian Walther » Sat Jan 17, 2009 2:43 pm

I've made some discoveries by trial and error, and I'm wondering if anyone of those familiar with Plasma internals could explain them to me (relative newbie to Plasma but with some background in computer graphics).

It took me a fair amount of head-banging until I figured out why my vertex colors weren't working the way I was used to (and wanted). What I found is that there's a difference between having only a vertex color layer (named "Col") in Blender and having two of them, the second named "Alpha" and fully white (i.e. opaque).

With an Alpha layer, the vertex colors work as expected - they are multiplied to the color after ambient and diffuse lighting (and before texturing), i.e. areas with vertex color black always appear black (in absence of specular highlights).

Without the Alpha layer, the vertex colors are visible in a certain way, but I don't understand exactly how. Areas with vertex color black appear black when facing away from light sources (ambient only), but only partially darkened when facing towards light sources. It seems like some diffuse lighting is added after multiplication by the vertex color. What is the exact formula for the shading in this case?

By prcdcing and diffing the two PRP files, I found that the difference is that with the Alpha layer, the objects are put into the plDrawableSpans named "…20000000_0BlendSpans", while without it, they are put into the one named "…00000000_0Spans". What exactly are plDrawableSpans (and BlendSpans and Icicles and whatever other critters involved in how Plasma stores and renders geometry) and why and how do they affect shading?
Christian Walther
 
Posts: 443
Joined: Sun Jun 08, 2008 3:10 am
Location: Switzerland

Re: DrawableSpans, Vertex Colors, and Shading

Postby Nadnerb » Sat Jan 17, 2009 3:30 pm

All plasma vertex colors are stored as RGBA. The Col and Alpha layers in blender are just a hack to allow the use of the Alpha channel, which blender doesn't support editing in vertex paint. Vertex colors are treated as ambient light, so the shading formula is (simplified): ((vertCols + realTimeDiffuse) * textureColor) + realTimeSpecular

DrawableSpans are objects that store large hunks of the drawable geometry. They are broken up into "Spans" of which there is generally one per object, referenced by the SceneObject's DrawInterface. Anyway, there are a number of shading flags that are set on a per-DrawableSpans basis, such as Opaque, Blend, Late, Sort Faces. So what you're seeing happen is when you apply a non-1 alpha to your vertex colors, the plugin is putting their geometry into a separate DrawableSpans with the blend flags set appropriately for transparent objects.
Image
Live KI: 34914 MOULa KI: 23247 Gehn KI: 11588 Available Ages: TunnelDemo3, BoxAge, Odema
Nadnerb
 
Posts: 1057
Joined: Fri Sep 28, 2007 8:01 pm
Location: US (Eastern Time)

Re: DrawableSpans, Vertex Colors, and Shading

Postby Christian Walther » Sun Jan 18, 2009 4:06 am

Hey, why didn't I get a notification of your reply even though I'm subscribed to the topic?

Thanks for the explanation, Nadnerb. The problem is that I'm not setting a non-1 alpha, my alpha layer is all 1, so I wouldn't have expected its presence to make any difference. Plus your formula is proven wrong by my experience in the case of presence of an alpha layer, where the diffuse component is multiplied by the vertex color as well (which is actually what I want, since I'm using the vertex color to simulate a shadow). For what it's worth, here's what my notes from previous research said, although that's proven wrong (or incomplete) now:
Not Shadeless: result = clamp((ambient * material color + diffuse) * vertex color) * texture color + specular
before clamp is per vertex, then clamp, then interpolation per pixel

(the clamp step is important too, that caused me some head-scratching back when I did that research on why texture colors and vertex colors were treated differently).

Here's my follow-up that I had prepared before reading your reply, anyway:

OK, I've made some progress. It all comes down to the kLiteVtxNonPreshaded flag that is set on the icicle properties when the object has an alpha vertex color layer (in addition to moving the object to the blend spans). Why this difference? (prp_DrawClasses.py:1361, maybe a comment would be in order on that line?)

When this flag is not set, the vertex color only seems to be applied to the ambient component, but not to the diffuse components. When the flag is set, it is applied to both. (What I call "ambient" and "diffuse" seems to be called "preshade" and "runtime" by Plasma. Plasma also has something called "ambient" but I haven't researched what it does since PyPRP always sets it to 0.)

I can set the flag manually (without adding an alpha layer) by specifying visual.icicle = 0x480 in AlcScript, but that seems a bit fragile since it might nuke other flags that PyPRP may see fit to set or clear in the future. Is there any disadvantage to setting the flag by adding an alpha layer, i.e. having the object in the blend spans instead of the normal spans, even though it is opaque? (e.g. does it unnecessarily enable depth sorting, or something?)

If anyone still feels like explaining plDrawableSpans etc., I'm still interested. What I've gathered from conversation around here is that their purpose is to specify drawing order, to make sure that transparent objects are drawn after opaque ones, but I don't know if that's an accurate or complete description. If I'm using the proper terms in my ramblings above, that doesn't mean that I understand them. I'm just feeling my way around in the dark. I hope we'll get some documentation on this stuff soon (from Cyan or the reverse-engineers in the community), figuring it all out on your own can be fun, but also frustrating, and it takes so much time.
Christian Walther
 
Posts: 443
Joined: Sun Jun 08, 2008 3:10 am
Location: Switzerland

Re: DrawableSpans, Vertex Colors, and Shading

Postby Lontahv » Sun Jan 18, 2009 5:48 am

plDrawableSpans has plDrawbleSpans-wide sorting and render-level flags. Depending on how they're set the engine renders them in a different order. I don't know if the funky names of the plDrawableSpans have anything to do with how they're ordered... I'm guessing not, since better info can be gotten from the vars inside. :P

Here's an example of how PageEditor displays this data: "<Properties Flags="0x0000000C" Criteria="0x0000000A" RenderLevel="0x40000000" />"

Apart from the sorting stuff there's lots of fun stuff in plDrawableSpans. But there's _lots_ of it and it could take me a whole day to write this post if I had to explain it all. The most important thing to know is that plDrawableSpans is a class whose main goal is speed in the plPipeline. All of the stuff sprouting off of plSceneNode are all modifiers of the plDrawableSpans. As far as I can see the plPipeline only cares plDrawableSpans in an age (who provide plKeys to the lamps, and the materials... perfect for renderin').
Currently getting some ink on my hands over at the Guild Of Ink-Makers (PyPRP2).
User avatar
Lontahv
Councilor of Artistic Direction
 
Posts: 1331
Joined: Wed Oct 03, 2007 2:09 pm

Re: DrawableSpans, Vertex Colors, and Shading

Postby Christian Walther » Sat Jan 24, 2009 3:41 am

Thanks Lontahv. I hope you will find that day in your free time someday, because it would take me even longer to reverse-engineer your code (and Cyan's and the other fan developers') to figure out these things on my own.

Concerning my two concrete questions, were they too well hidden in my last post, or does nobody know the answers?
Christian Walther wrote:Why this difference?
Is there any disadvantage to […] having the object in the blend spans instead of the normal spans, even though it is opaque?


One more thing that just occurred to me: is it possible to modify the colors of a plLayer or the flags of a plIcicle or other PRP object properties at run time using the Plasma Python API? That would make my research a great deal easier.
Christian Walther
 
Posts: 443
Joined: Sun Jun 08, 2008 3:10 am
Location: Switzerland

Re: DrawableSpans, Vertex Colors, and Shading

Postby Lontahv » Sat Jan 24, 2009 4:42 am

You can learn a lot from just looking at the libPlasma source. It's very clean and nice to read thanks to Zrax. :D This is how I started learning stuff about a year back.

The only disadvantage I see to putting a solid object in a blendspans is that the engine would have to use alpha sorting flags on opaque objects (because ordering non-transparent meshes is much easier ;) ). Cyan's exporter breaks the spans by opacity because alpha stuff needs much more care and solid does not--or at least the reason I'm guessing. PyPRP follows the example of Cyan's exported stuff for the most part.

I'm not sure about changing stuff at run-time. Of course if you're running an engine written using libPlasma and have a live console into the classes this would be very possible. ;)
Currently getting some ink on my hands over at the Guild Of Ink-Makers (PyPRP2).
User avatar
Lontahv
Councilor of Artistic Direction
 
Posts: 1331
Joined: Wed Oct 03, 2007 2:09 pm


Return to Plasma Development

Who is online

Users browsing this forum: No registered users and 0 guests

cron