Finding 2d Vertices in a DisplayObject3D

Monday, November 17th, 2008 | examples

This is kind of a ridiculous example, but it shows how you can loop through all of the vertices of a DisplayObject3D and pull out the 2d x and y coordinates.

The stars start out in random locations then tween to the vertices of the sphere.

View example
source

package {
	import gs.TweenMax;
	import gs.easing.*;
 
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class Finding2DVertexPoints extends BasicView
	{
		private var sphere:Sphere;
		private var stars:Array = [];
		private var vertices:Array = [];
 
		public function Finding2DVertexPoints()
		{
			sphere = new Sphere(new ColorMaterial(), 300, 15, 15);
			sphere.material.fillAlpha = 0;
			scene.addChild(sphere);
 
			//sphere.geometry.vertices is an array of vertex3D's
			//each vertex3D contains a vertex3DInstance which
			//has the 2d x and y property
			//long-hand sytax: sphere.geometry.vertices[i].vertex3DInstance.x	
			for each(var vertex3D:Vertex3D in sphere.geometry.vertices)
			{
				var dummyStar:star = new star();
				dummyStar.x = Math.random() * this.width;
				dummyStar.y = Math.random() * this.height;
				stars.push(dummyStar);
				this.addChild(dummyStar);
 
				//render once to calculate vertex data
				singleRender(); 
				//push all the sphere's vertices into an array
				vertices.push(vertex3D.vertex3DInstance); 
			}
 
			startTween();
		}
 
		private function startTween():void
		{
			for(var i:int = 0; i<vertices.length; i++)
			{
				var time:Number = 4;
 
				var tweenObject:Object = {};
				tweenObject.yoyo = true;
				tweenObject.ease = Quad.easeInOut;
				//assign the endpoint of the tween to vertices gathered above
				tweenObject.x = vertices[i].x + width/2; 
				tweenObject.y = vertices[i].y + height/2;
				tweenObject.scaleX = tweenObject.scaleY = .2;
				tweenObject.bezierThrough = [{x:Math.random() * width, y:Math.random() * height}];
				tweenObject.orientToBezier = true;
 
				TweenMax.to(stars[i], time, tweenObject);
			}
		}
	}
}

Tags: , ,

  • Right, i know this is old and probably no one follows it, but i was wondering if there's a way to do the opposite?

    How can you set a vertex's position, from the mouse coordinates?

    I'm close, but my system is quite buggy:

    var tX:int = int(_activeVertex.vertex3DInstance.x);
    var tY:int = int(_activeVertex.vertex3DInstance.y);

    var mX:int = int(_scope.viewport.mouseX - _scope.viewport.width/2);
    var mY:int = int(_scope.viewport.mouseY - _scope.viewport.height/2);

    var vX:int = int(_activeVertex.x);
    var vY:int = int(_activeVertex.y);


    _activeVertex.x = int(mX * vX / tX);
    _activeVertex.y = int(mY * vY / tY);

  • Ian

    What does the "vertex3DInstance" property of a Vertex3D really refer to, and how is it any different from the Vertex3D object itself?

  • bojan

    @Gabriel: If you are using Flash then you have to make a symbol called star in the library and export it for actionscript and draw something in that symbol... a star for example. :)

  • I have a problem in this code:

    var dummyStar:star = new star();

    what is the star? where can I get it?

  • bojan

    Sorry for beginner question, but how do you make it loop? I used your code and got same result exept for looping.

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

November 2008
M T W T F S S
    Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

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 ...