Categories
General

Papervision interactive Line3D

Just a very quick post to show you a little demo I’ve knocked up – Andy Zupko asked me if I could implement interactivity in the Papervision Line3D object, and it looks like it’s working… I’ll commit the changes to the code library tomorrow after I’ve had some sleep!

But in the meantime, here’s the demo…

[kml_flashembed movie=”/wp-content/uploads/manual/2008/line3dhittest.swf” width=”480″ height=”360″ FVERSION=”9″ QUALITY=”high” /]

[UPDATE] these changes have now been committed to both the Effects and the Great White branches and here’s the source for the example :

package
{

	import org.papervision3d.view.BasicView;
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.core.geom.renderables.Vertex3D;

	import flash.events.Event;

	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.objects.DisplayObject3D;

	/**
	 * @author Seb Lee-Delisle
	 */
	public class Line3DHitTest extends BasicView
	{
		public var lineContainer:DisplayObject3D;
		private var lines : Array;

		public function Line3DHitTest()
		{
			super(stage.stageWidth, stage.stageHeight, false, true);
			init();


		}

		private function init():void
		{

			lineContainer = new DisplayObject3D("Line Container");
			lines = new Array();

			for(var i:int = 0; i<100; i++)
			{
				var l:Number = (Math.random()*100)+600;
				var v0:Vertex3D = new Vertex3D(-l, 0,0);
				var v1:Vertex3D = new Vertex3D(l,0,0);

				var lm:LineMaterial = new LineMaterial(0x8000ff,0.2);
				lm.interactive = true;

				var lines3D:Lines3D = new Lines3D(lm,"Lines3d#"+i);
				lines3D.addNewLine(4,v0.x,v0.y,v0.z,v1.x,v1.y,v1.z);

				lines3D.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, lineOver);

				lines3D.rotationY = Math.random()*360;
				lines3D.rotationZ = Math.random()*180;


				lines3D.extra = {spin: 0};

				lineContainer.addChild(lines3D);
				lines.push(lines3D);

			}

			scene.addChild(lineContainer);

			singleRender();

			addEventListener(Event.ENTER_FRAME, frameLoop);
		}




		private function lineOver(e : InteractiveScene3DEvent) : void
		{
			e.displayObject3D.extra["spin"] = 5;

		}

		function frameLoop(e:Event): void
		{

			lineContainer.rotationY+=((stage.stageWidth/2)-mouseX)/300;
			lineContainer.rotationX+=((stage.stageHeight/2)-mouseY)/300;
			var drag:Number = 0.96;

			for(var i:int=0; i0.1)
				{
					line.extra["spin"]*=drag;
					line.rotationY+=spin;
					var newcol:Number = (spin/5*0xff);
					line.material.lineColor = ((0x80+(newcol>>1))<<16) | (0xff);
					line.material.lineAlpha = (spin/5*0.2)+0.2;


				}
				else
				{
					line.extra["spin"] = 0;
				}


			}


			singleRender();


		}
	}
}

16 replies on “Papervision interactive Line3D”

very nice!

Could it be your code got a little messed up because of your blog editor? Maybe because the ‘lesser than’ symbol in the frameLoop function?

for(var i:int=0; i
0.1)

Awesome. Thanks, this works really well for what I’m currently working on! I did run into one issue though (feel free to yell at me if I am just being a papervision newb).

When I have an interactive line, and then move the control vertex, the line bends, but the interactive surface seems to stay in the same place. This means if I have a line and click and drag on it to move the control vertex somewhere I cannot interactive with this line again unless I click on the area where the line used to be.

Hey Steven!

Thanks for the nice comments.

I’m afraid to say that the line hit test code was built around the original Line3D object (ie the one without the control point). The maths is a lot more complex to find a line/point hit with a curved line. It’s on my list but it’s not that high a priority right now. Sorry! In the meantime I’ll add a warning so that people aren’t confused in the future.

cheers!

Seb

FYI, I was having some memory leaks using the LineMaterial, and realized part of this was because all my line materials were getting registered with MaterialManager and LineMaterial had no way of removing these because destroy is a protected function. Had to add this chunk of code:

/**
* clean
* wraps the destroy method to remove references to this material
* Steven J Baughman
*/
public function clean(): void {
super.destroy();
}

hey Steven,

thanks for that, I’ve been looking into this quite heavily the last couple of days and there are definitely some issues with Materials not being cleared properly in the Great White and Effects branches.

Material destruction should occur automatically, but in the meantime, I’d reuse your line materials rather than re-make them.

But watch out for some fixes coming soon!

hope this helps!

Seb

var l:Number = (Math.random()*100)+600;
var v0:Vertex3D = new Vertex3D(-l, 0,0);
var v1:Vertex3D = new Vertex3D(l,0,0);

this is why the letter L should not be used as a variable name!

Awesome tut tho, thanks!

😀

Comments are closed.