Thanks for that, @Camurai. Several posts have alluded to using applyTransformation(), but yours is the first I’ve seen with a concrete code sample.
Offsetting geometry y-depths from the mesh’s y-depth is a usable work-around. I’m using it for PlaneGeometries that were clipping other geometries at certain view angles. This work-around forces the correct z-ordering and doesn’t restrict you to using certain alphaThreshold values, (or giving up on using alpha blending).
In case anyone else needs it, here’s a couple of generic functions that you can paste in and use.
/**
* Adjust Mesh Y-depth to work around z-index alpha blending problems.
* (Don't use with meshes that share geometries.)
*/
private function adjustMeshYDepth(mesh:Mesh, offset:int):void {
adjustGeometryYDepth(mesh.geometry, offset);
mesh.y -= offset;
}
/**
* Adjust Geometry Y-depth to work around z-index alpha blending
* problems. (If called directly, owning mesh.y must have offset manually
* subtracted.)
*/
private function adjustGeometryYDepth(geometry:Geometry, offset:int):void {
var matrix:Matrix3D = new Matrix3D();
matrix.appendTranslation(0, offset, 0);
geometry.subGeometries.forEach(function(item:SubGeometry, index:int, vector:Vector.<SubGeometry>):void {
item.applyTransformation(matrix);
});
}
If your mesh’s geometry isn’t shared by other meshes, then you can just call adjustMeshYDepth() like so:
var geometry:Geometry = ...
var mesh:Mesh = new Mesh(geometry, ...
var offset:int = 1000;
adjustMeshYDepth(mesh, offset);
However, if a geometry is used in more than one mesh, you’ll need to call adjustGeometryYDepth() just once for the geometry, then subtract the offset value from each mesh.y value in your own code. Like so:
var sharedGeometry:Geometry = ...
var meshFoo:Mesh = new Mesh(sharedGeometry, ...
var meshBar:Mesh = new Mesh(sharedGeometry, ...
var meshBaz:Mesh = new Mesh(sharedGeometry, ...
var offset:int = 1000;
// Only adjust shared geometry once
adjustGeometryYDepth(sharedGeometry, offset);
// Manually adjust each mesh that uses the shared geometry
meshFoo.y -= offset;
meshBar.y -= offset;
meshBaz.y -= offset;