Test if a plane is within the view of the camera (aka testing if culled)

Wednesday, July 1st, 2009 | examples

Once a plane leaves the camera’s view, it will turn red. So the next time you see it, you will be able to see that it has been culled before. Hit the space bar to reset all to green.


source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package {
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
 
	import org.papervision3d.cameras.CameraType;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="900", height="480", backgroundColor="#000000", frameRate="31")]
	public class TestIfCulled extends BasicView
	{
		public function TestIfCulled()
		{
			super(900, 480, true, false, CameraType.DEBUG);
 
			//sidenote: if you don't want objects culled, disable culling
			//camera.useCulling = false;
 
			var length:int = 120;
			for(var i:int = 0; i < length; i++)
			{
				var greenMaterial:ColorMaterial = new ColorMaterial(0x00cc00);
				var plane:Plane = new Plane(greenMaterial);
				plane.x = Math.random() * 6000 - 3000;
				plane.y = Math.random() * 6000 - 3000;
				plane.z = Math.random() * 6000;
 
				scene.addChild(plane);
			}
 
			startRendering(); 
 
			trace("hit the space bar to reset visible planes to green");
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
		}
 
		protected function keyDownHandler(event:KeyboardEvent):void
		{
			//reset all the visible to green when you hit space
			if(event.keyCode == Keyboard.SPACE)
			{
				for each(var plane:Plane in scene.children)
				{
					plane.material.fillColor = 0xc00cc00;
				}	
			}
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick(event);
 
			for each(var plane:Plane in scene.children)
			{
				//if it's culled change to red
				if(plane.culled)
				{
					plane.material.fillColor = 0xcc0000;
				}
			}	
		}
	}
}

Tags: ,

  •  Please add more good information that would  help others in such good way.

  • This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! 

  • Wonderful, I tried and works

  • Adam

    Hi,
    I'm stuck with the camera :(
    I create a 'gallery' made of planes, and on click I would like the camera to look at the plane and reach its coordinates...but what I get it's a rotationY-like weird effect (http://www.pixoyo.no/gallery.s...
    What's wrong with my code? Thx for any help:

    package
    {
    import flash.display.MovieClip;
    import flash.display.Stage
    import org.papervision3d.core.proto.DisplayObjectContainer3D;
    import org.papervision3d.materials.ColorMaterial;
    //import flash.events.TimerEvent;
    //import flash.utils.Timer

    //import org.papervision3d.materials.MovieMaterial;
    import org.papervision3d.materials.BitmapFileMaterial
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.view.BasicView;
    import org.papervision3d.events.FileLoadEvent;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.events.InteractiveScene3DEvent;
    import org.papervision3d.cameras.Camera3D;
    //
    import gs.TweenMax
    import gs.easing.Cubic

    //

    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    //
    /**
    * ...
    * @author Giulio Grasso
    */
    public class Gallery extends BasicView
    {


    //
    private var lookAtMe:DisplayObject3D;
    private var lookAtTarget:DisplayObject3D;



    public function Gallery():void
    {
    viewport.interactive = true;
    viewport.buttonMode = true;
    //

    //
    var camera:Camera3D = new Camera3D();

    camera.zoom = 10;
    camera.focus = 100;
    //

    createGallery();

    startRendering();
    }

    private function createGallery() : void {

    var hRow:int = 0;
    var vRow:int = -1;
    for(var i:int = 0; i < 12; i++){



    var pmat:ColorMaterial = new ColorMaterial(0xFF00FF,100,true);
    var p:Plane = new Plane(pmat, 300, 300, 4, 4);
    p.z = -100;
    p.id=i
    scene.addChild(p);
    //action
    p.addEventListener( InteractiveScene3DEvent.OBJECT_PRESS, planeClicked );


    if(i%6 == 0){

    hRow = 0;
    vRow++;

    }

    p.x = hRow * 350;
    p.y = vRow * 350;

    hRow++;
    }
    lookAtMe = new DisplayObject3D();
    lookAtMe.z = 1000;
    lookAtTarget = new DisplayObject3D();

    camera.target = lookAtMe;
    camera.roll(90)
    //TweenMax.to (camera, 7, { x:300 } )

    scene.addChild(lookAtMe);


    }

    private function planeClicked(e:InteractiveScene3DEvent):void
    {
    lookAtTarget.copyTransform(e.displayObject3D);

    var time:Number = 1;
    var tweenObject:Object = {};
    tweenObject.x = lookAtTarget.x;
    tweenObject.y = lookAtTarget.y;
    //tweenObject.z = lookAtTarget.z;
    tweenObject.rotationX = lookAtTarget.rotationX;
    tweenObject.rotationY = lookAtTarget.rotationY;
    tweenObject.rotationZ = lookAtTarget.rotationZ;
    tweenObject.onUpdate = onSeatTween;
    tweenObject.ease = Cubic.easeInOut;
    TweenMax.to(camera, time, tweenObject);

    }

    private function onSeatTween():void
    {
    camera.lookAt(lookAtMe);
    }

    }
    }

  • Adam

    Sorry for the weirdo link :P :
    http://www.pixoyo.no/gallery.s...

  • This is great, I was actually doing this by hand by calculating the distance between objects and the camera then removing the objects out of view. Checking for the culled value is going to be much faster.

    One question though, do you think testing every DisplayObject3D on the render tick is intensive? Wondering how to better optimize something like this. Does PV3D take advantage of stage invalidation? It would be nice to do all of this before the screen is rendered so you make sure you are only rendering exactly what you need. Going to play around with it right now.

    I would love to see more posts about high level optimization for papervision sites.

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

July 2009
M T W T F S S
« Jun   Aug »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...