requests

Flover Cow – A.K.A. Cover Flow Knockoff

Saturday, January 31st, 2009 | examples, requests | Comments

I’ll do a tutorial on this later. For know, download the source and play with the example.


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package {
	import de.polygonal.ds.DLinkedList;
	import de.polygonal.ds.DListNode;
 
	import gs.TweenMax;
 
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="900", height="480", backgroundColor="#ffffff", frameRate="60")]
	public class FloverCow extends BasicView
	{
		private static const SPACING:Number = 400;
		private static const NUMBER_OF_PLANES:int = 10;
		private static const TIME:Number = .5;
		private static const Z_FOCUS:Number = -500;
		private static const ROTATION_Y:Number = 45;
 
		private var planeList:DLinkedList = new DLinkedList();
 
		public function FloverCow()
		{
			viewport.interactive = true;
 
			for(var i:int = 0; i < NUMBER_OF_PLANES; i++)
			{
				var randomColor:Number = Math.random() * 0xffffff;
				var colorMaterial:ColorMaterial = new ColorMaterial(randomColor);
				colorMaterial.interactive = true;
				var plane:Plane = new Plane(colorMaterial, 400, 400, 4, 4);
				plane.x = i * SPACING;				
 
				plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, plane_objectClickHandler);
 
				planeList.append(plane);
				scene.addChild(plane);
			}
 
			flow(plane);
 
			startRendering();
		}
 
		private function plane_objectClickHandler(event:InteractiveScene3DEvent):void
		{
			var plane:Plane = Plane(event.target);
			flow(plane);			
		}
 
		private function flow(plane:Plane):void
		{
			var xPosition:Number = 0;
			TweenMax.to(plane, TIME, {x:xPosition, z:Z_FOCUS, rotationY:0});
 
			var current:DListNode = planeList.nodeOf(plane).node;
 
			var walkLeft:DListNode = current.prev;
			while(walkLeft)
			{
				plane = Plane(walkLeft.data);
				xPosition -= SPACING;
				TweenMax.to(plane, TIME, {x:xPosition, z:0, rotationY:-ROTATION_Y});
				walkLeft = walkLeft.prev;
			}
 
			xPosition = 0;
			var walkRight:DListNode = current.next;
			while(walkRight)
			{
				plane = Plane(walkRight.data);
				xPosition += SPACING;
				TweenMax.to(plane, TIME, {x:xPosition, z:0, rotationY:ROTATION_Y});
				walkRight = walkRight.next;
			}
		}
	}
}

Tags: , ,

Papervision3D with Box2DFlash Hello World

Saturday, December 20th, 2008 | examples, requests | Comments

I abhor the syntax for Box2DFlash, but I freely embrace it as an incredible 2d physics engine.

This example is about as basic as I can make it. I’ll follow up soon with more advanced examples with different shapes, mouse interactions, joints, etc.


source

package
{
	import Box2D.Collision.Shapes.b2CircleDef;
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2World;
 
	import flash.events.Event;
 
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class Box2DFlashWithPapervision3D extends BasicView
	{
		private var WORLD_SCALE:Number = 30;
		private var WIDTH:Number = 640;
		private var HEIGHT:Number = 480;
 
		private var world:b2World;
 
		private var iterations:int = 10;
		private var timeStep:Number = 1.0/30.0;
 
		public function Box2DFlashWithPapervision3D()
		{
			setupPapervision3D();
			createWorld();
			createFloor();
			createShapes();
 
			startRendering();
		}
 
		private function setupPapervision3D():void
		{
			camera.focus = 10;
			camera.zoom = 100;
		}
 
		private function createWorld():void {
			var worldBounds:b2AABB = new b2AABB();
			worldBounds.lowerBound = new b2Vec2( 0, 0 );
			worldBounds.upperBound = new b2Vec2(WIDTH/WORLD_SCALE, HEIGHT/WORLD_SCALE);
 
 			var gravity:b2Vec2 = new b2Vec2(0, 10);
			var sleep:Boolean = true;
 
			world = new b2World(worldBounds, gravity, sleep);
		}
 
		private function createFloor():void
		{
			// Create border of boxes
			var floorShapeDef:b2PolygonDef = new b2PolygonDef();
			var floorBodyDef:b2BodyDef = new b2BodyDef();
			var floor:b2Body;
 
			//bottom shape definition
			floorShapeDef.SetAsBox((WIDTH+40)/WORLD_SCALE/2, 100/WORLD_SCALE);
 
			// Bottom
			floorBodyDef.position = new b2Vec2(WIDTH/WORLD_SCALE/2, (HEIGHT+95)/WORLD_SCALE);
			floor = world.CreateBody(floorBodyDef);
			floor.CreateShape(floorShapeDef);
 
			floor.SetMassFromShapes();
		}
 
		private function createShapes():void
		{
			for (var i:Number = 0; i < 20; i++)
			{
				//radius for physics circle and sphere
				var radius:Number = Math.random() * 50 + 10;
 
				var bodyDef:b2BodyDef = new b2BodyDef();
				//random position toward the top of the stage
				bodyDef.position = new b2Vec2(Math.random() * WIDTH / WORLD_SCALE, Math.random() * 50 /WORLD_SCALE);
 
				var body:b2Body = world.CreateBody(bodyDef);
 
				var shapeDef:b2CircleDef = new b2CircleDef();
				shapeDef.radius = radius/WORLD_SCALE;
				shapeDef.density = 1;
				shapeDef.friction = .7;
				shapeDef.restitution = .7;
				body.CreateShape(shapeDef);
				body.SetMassFromShapes();
 
				var sphere:Sphere = new Sphere(null, radius);
 
				scene.addChild(sphere);
				body.m_userData = sphere;
			}
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			world.Step(timeStep, iterations);
 
			//sets the position of any DisplayObject3D to the body position
			for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next)
			{
                if (bb.m_userData is DisplayObject3D)
                {
                        bb.m_userData.x = bb.GetPosition().x * WORLD_SCALE - WIDTH * .5;
                        bb.m_userData.y = -bb.GetPosition().y * WORLD_SCALE + HEIGHT * .5;
                        bb.m_userData.rotationZ = -bb.GetAngle() * (180/Math.PI);
                }
			}
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags: ,

Dragging Planes and lookAt Camera

Friday, December 19th, 2008 | examples, requests | Comments


source

package
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
 
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.core.math.Plane3D;
	import org.papervision3d.core.proto.MaterialObject3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class DraggingPlanes extends BasicView
	{
		private const NUM_PLANES:int = 11;
 
		private var planeToDragOn:Plane3D;
		private var currentPlane:Plane;
 
		private var light:PointLight3D;
 
		public function DraggingPlanes()
		{
			viewport.interactive = true;
 
			camera.y = 100;
 
			for(var i:int = 0; i < NUM_PLANES; i++)
			{
				var material:MaterialObject3D = createMaterial();
				material.interactive = true;
				/*
				Please note that, by default, the material on a plane
				is placed on the "back" of a plane (since the camera defaults
				to -1000 z. So if you tell the plane to "lookAt" the camera,
				you will see the front of the plane with nothing on it. You can
				solve this by making the material doubleSided
				*/
				material.doubleSided = true;
				var plane:Plane = new Plane(material, 100, 100, 4, 4);
				plane.x = (i - NUM_PLANES/2) * 200;
 
				scene.addChild(plane);
				plane.lookAt(camera);
 
				plane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler);
				stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			}
 
			var up:Number3D = new Number3D(0, 1, 0);
			planeToDragOn = new Plane3D(up, new Number3D(0,0,0));
 
			startRendering(); 	
		}
 
		private function createMaterial():MaterialObject3D
		{
			var bitmapData:BitmapData = new BitmapData(300, 200, false, Math.random() * 0xffffff);
			var bitmapBorder:BitmapData = new BitmapData(320, 220, false, 0xffffff);
 
			bitmapBorder.copyPixels(bitmapData, bitmapData.rect, new Point(10, 10));
 
			var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmapBorder, true);
			bitmapMaterial.smooth = true;
 
			return bitmapMaterial;
		}
 
		private function objectPressHandler(event:InteractiveScene3DEvent):void
		{
			currentPlane = event.displayObject3D as Plane;
		}
 
		private function mouseUpHandler(event:MouseEvent):void
		{
			currentPlane = null;
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
			ray = Number3D.add(ray, camera.position);
 
			var cameraVertex3D:Vertex3D = new Vertex3D(camera.x, camera.y, camera.z);
			var rayVertex3D:Vertex3D = new Vertex3D(ray.x, ray.y, ray.z);
 
			var intersectPoint:Vertex3D = planeToDragOn.getIntersectionLine(cameraVertex3D, rayVertex3D);
 
			if(currentPlane)
			{
				currentPlane.x = intersectPoint.x;
				currentPlane.y = intersectPoint.y;
				currentPlane.z = intersectPoint.z;
				currentPlane.lookAt(camera);
			}
 
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags:

Shaders / ShadedMaterial Example

Friday, December 19th, 2008 | examples, requests | Comments


source

package
{
	import flash.display.Bitmap;
	import flash.events.Event;
 
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shaders.PhongShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class ShadersExample extends BasicView
	{
		[Embed(source="assets/santa.jpg")]
		private var santaAsset:Class;
 
		private var plane:Plane;
 
		public function ShadersExample()
		{
			camera.z = -500;
 
			var santaBitmap:Bitmap = new santaAsset() as Bitmap;
 
			var light:PointLight3D = new PointLight3D();
 
			var bitmapMaterial:BitmapMaterial = new BitmapMaterial(santaBitmap.bitmapData, true);
			var shader:PhongShader = new PhongShader(light, 0xffffff, 0x000000, 10);
			var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial, shader);
			shadedMaterial.doubleSided = true;
 
 
			plane = new Plane(shadedMaterial);
 
			scene.addChild(plane);
			startRendering();	
 		}
 
 		override protected function onRenderTick(event:Event=null):void
 		{
 			plane.rotationY = viewport.containerSprite.mouseX / 4;
 			super.onRenderTick(event);
 		}
	}
}

Tags: ,

Search

Recommended Books

Speaking at FITC Toronto

 

May 2012
M T W T F S S
« May    
 123456
78910111213
14151617181920
21222324252627
28293031  

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