3 Doors Down MP3 songs Music download Edge Of Sanity Gipsy Kings Coil Download MP3 Verve She and Him Billy Talent Thraw Elton John MP3 site

Fix Z-Sorting Issues Using Viewport Layers

viewport-layer

download-source

Take a look at the sphere on the left. Ever had this happen to you and wondered if there was a good way to get rid of it? One of the ways we use is by adding a viewport layer. This works much the same as adding a new layer in flash or adding movieclip over another in AS3.

The best thing about this approach is that it's very simple and quick to implement:

Actionscript:
  1. viewportLayer = viewport.getChildLayer(sphere, true);
  2. viewportLayer.addDisplayObject3D(sphere);

For even more in depth discussion on viewport layers take a look at:
http://blog.zupko.info/?p=129

Here is the full class:

Actionscript:
  1. package 
  2. {
  3.     import flash.events.Event;
  4.     import org.papervision3d.materials.ColorMaterial;
  5.     import org.papervision3d.objects.primitives.Plane;
  6.     import org.papervision3d.objects.primitives.Sphere;
  7.     import org.papervision3d.view.BasicView;
  8.     import org.papervision3d.objects.primitives.Cone;
  9.     import org.papervision3d.materials.BitmapFileMaterial;
  10.     import org.papervision3d.view.layer.ViewportLayer;
  11.    
  12.     public class Main extends BasicView
  13.     {
  14.         protected var sphere:Sphere
  15.         protected var plane:Plane;
  16.         protected var bitmapMaterial:BitmapFileMaterial;
  17.         protected var colorMaterial:ColorMaterial;
  18.         protected var viewportLayer:ViewportLayer;
  19.         public function Main()
  20.         {
  21.             super();
  22.             createChildren();
  23.             commitProperties();
  24.             startRendering();
  25.         }
  26.         protected function createChildren():void
  27.         {
  28.            
  29.             //Create a new 3D object
  30.             colorMaterial = new ColorMaterial(0x000000);
  31.             bitmapMaterial = new BitmapFileMaterial("images/ourtex.jpg")
  32.  
  33.             //Create 3D Objects
  34.             plane = new Plane(colorMaterial,1000,1000,5,5)
  35.             sphere = new Sphere(bitmapMaterial, 50, 10,10);
  36.  
  37.            
  38.             //Add to scene
  39.             scene.addChild(plane);
  40.             scene.addChild(sphere);
  41.            
  42.             //To see the z-sorting issue comment out these two lines
  43.             viewportLayer = viewport.getChildLayer(sphere, true);
  44.             viewportLayer.addDisplayObject3D(sphere);
  45.         }
  46.        
  47.         protected function commitProperties():void
  48.         {
  49.             //Set some properties
  50.             sphere.scale = 4;
  51.             sphere.pitch( -10);
  52.             plane.rotationX = 60;
  53.         }
  54.         override protected function onRenderTick(event:Event = null):void
  55.         {
  56.             super.onRenderTick(event);
  57.  
  58.             //Rotate
  59.             sphere.yaw(1);
  60.         }
  61.     }
  62. }

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

7 Responses to “Fix Z-Sorting Issues Using Viewport Layers”


Leave a Reply


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed