What Material you choose will impact your GeometryModel3D in three ways:
- Lighting model
- Depth write
- Blend with the back buffer
The first one is straightforward since it’s right in the name of the Material plus it’s documented. The last two aren’t obvious at all and this is probably the first you’ve heard of them.
Depth write and the depth buffer
If you haven’t heard of the depth buffer (a.k.a. the z buffer) before, it’s essentially how the card keeps track of the distance to an object from the camera per pixel. When a polygon is being rasterized at a pixel that has already been written to, do you keep the old value or take the new value? For opaque objects, you typically want the one closest to the camera to win because you won’t be able to see the occluded one. This is what we tell the card to do (for you D3D programmers out there, we use D3DCMP_LESSEQUAL).
For example, say you draw a triangle with a DiffuseMaterial. If you then draw another triangle directly behind, it will be ignored in the area of overlap because the first triangle is in front of it. But if a polygon doesn’t write to the depth buffer, then it’s like the card doesn’t know it’s there. Say you draw a triangle with an EmissiveMaterial. If you then draw an overlapping triangle it doesn’t matter if it’s behind or in front — the new triangle overwrites the old one because the card has no “record” of the old one.
Can you think of why the depth buffer might cause problems with transparent objects in a dynamic scene? That’s a topic for another post 🙂
In the equation above, S means source (the GeometryModel3D you’re drawing) and D means destination (what you’ve drawn to the back buffer already). From the equations you can make a few observations like a fully opaque DiffuseMaterial will never let you see what was previously written or even opaque additive Materials will appear transparent because they don’t block out what is already there.
Why are there different behaviors?
The behaviors came out of how we think most people use Materials. Just about every Model3D will contain a DiffuseMaterial and will want to write to the depth buffer. If you then want your Model3D to glow or shine, you throw in an EmissiveMaterial or SpecularMaterial. Since you already wrote the depth, you don’t need to do it again in the emissive and specular passes. Furthermore, you don’t want emissive and specular to subtract away from the diffuse layer you just put down.
“AmbientMaterial” is in quotes because it doesn’t exist but you can make a Material that only responds to ambient light. See my earlier color knobs post.
If you’d like one of the additive Materials to write depth, you can do so by putting them in a MaterialGroup along with a transparent black DiffuseMaterial. That way the DiffuseMaterial doesn’t affect the color but does affect the depth buffer. You can put this “depth pass” DiffuseMaterial before or after the other Materials but you will get different results between the two if you have any overlapping front faces (or back faces if we’re talking about BackMaterial).