Z-Sorted Sprites in 3d


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/cleptor/growup.onecm/wp-content/plugins/code-highlighter/geshi.php on line 2148

Sprite3D Z-Sorting

On a recent job, I had a need for a 3d scene with lots of sprites. I decided to use Papervision3d but couldn't find an easy way of having an interactive sprite in the scene. There are classes for billboard planes, but that seemed like too much calculation. This means you can do anything you would normally do with a sprite, but in 2.5d.

The solution turned out to be pretty simple. Just use the screen.x, y and z variables in the DisplayObject3D class to align some sprites to their projected 3d locations, and then z-sort the whole lot of them.

  • Works with Papervision3D 2.0 (GreatWhite)
  • The z-sorting for each SpriteGroup3D completely circumvents papervision's internal sorting so this won't work in conjunction with other types of scene objects (like polygons)

Hopefully someone will find them useful =) View the rest of this post for the code.

Sprite3D.as

package
{
	import flash.display.DisplayObjectContainer;
 
	import org.papervision3d.core.proto.CameraObject3D;
	import org.papervision3d.core.render.data.RenderSessionData;
	import org.papervision3d.objects.DisplayObject3D;
 
	public class Sprite3D extends DisplayObject3D
	{
		public var sprite :DisplayObjectContainer;
		public var camera :CameraObject3D;
 
		public var group  :SpriteGroup3D;
 
		public function Sprite3D(_sprite:DisplayObjectContainer)
		{
			sprite = _sprite;
 
			autoCalcScreenCoords = true;
		}
 
		public function addedToGroup():void
		{
			// Called when the sprite is added to a SpriteGroup3D
		}
 
		public function removedFromGroup():void
		{
			// Called when the sprite is removed from a SpriteGroup3D
		}
 
		public function preProject():void
		{
			// Do things before projection
		}
 
		public function postProject():void
		{
			// Do things after projection
		}
 
		override public function project(parent:DisplayObject3D, renderSessionData:RenderSessionData):Number
		{
			camera = renderSessionData.camera;
 
			preProject();
 
			super.project(parent, renderSessionData);
 
			// Set the sprite position and scale
 
			sprite.x = this.screen.x;
			sprite.y = this.screen.y;
 
			var scale:Number = camera.focus * camera.zoom / (camera.zoom + screen.z);
			sprite.scaleX = sprite.scaleY = scale;
 
			postProject();
 
			return this.screenZ;
		}
 
		public function suicide():void
		{
			if(group)
				group.removeChild(this);
		}
	}
}
 

SpriteGroup3D.as

package
{
	import flash.display.Sprite;
 
	import org.papervision3d.core.render.data.RenderSessionData;
	import org.papervision3d.objects.DisplayObject3D;
 
	public class SpriteGroup3D extends DisplayObject3D
	{
		public var sprites         :Array;
		public var spriteContainer :Sprite;
 
		public function SpriteGroup3D(_spriteContainer:Sprite)
		{
			spriteContainer = _spriteContainer;
			sprites = [];
		}
 
		override public function addChild(child:DisplayObject3D, name:String=null):DisplayObject3D
		{
			if(child is Sprite3D) {
				var c3d:Sprite3D = child as Sprite3D;
				c3d.group = this;
				c3d.addedToGroup();
			}
 
			return super.addChild(child, name);
		}
 
		override public function removeChild(child:DisplayObject3D):DisplayObject3D
		{
			if(child is Sprite3D) {
				var c3d:Sprite3D = child as Sprite3D;
				if(spriteContainer.contains(c3d.sprite))
					spriteContainer.removeChild(c3d.sprite);
				c3d.group = null;
				c3d.removedFromGroup();
			}
 
			return super.removeChild(child);
		}
 
		override public function project(parent:DisplayObject3D, renderSessionData:RenderSessionData):Number
		{
			super.project(parent, renderSessionData);
 
			// Z-Sort the sprites
 
			sprites = [];
			for each(var obj:DisplayObject3D in children)
				if(obj is Sprite3D)
					sprites.push(obj);
			sprites.sort(zSort);
 
			for each(var s3d:Sprite3D in sprites) {
				if(spriteContainer.contains(s3d.sprite))
					spriteContainer.setChildIndex(s3d.sprite, 0);
				else
					spriteContainer.addChildAt(s3d.sprite, 0);
			}
 
			return screenZ;
		}
 
		private function zSort(a:Sprite3D, b:Sprite3D):int
		{
			if(a.screen.z > b.screen.z) return 1;
			if(a.screen.z < b.screen.z) return -1;
			return 0;
		}
	}
}
 

Tags: , , , , ,

2 Responses to “Z-Sorted Sprites in 3d”

  1. Morna Says:

    Great work.

  2. Chrome-fusion Says:

    I’m so lazy, do you have an FLA? This is great code.