mouse
Mouse follower
Saw this question come up on the mailing list. I thought it would make a nice demo:
package { import flash.events.Event; import org.papervision3d.core.utils.Mouse3D; import org.papervision3d.materials.ColorMaterial; import org.papervision3d.objects.primitives.Plane; import org.papervision3d.view.BasicView; import org.papervision3d.view.layer.ViewportLayer; import org.papervision3d.view.layer.util.ViewportLayerSortMode; [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")] public class Mouse3DFloor extends BasicView { private var mouse3D:Mouse3D; private var follower:Plane; public function Mouse3DFloor() { viewport.interactive = true; Mouse3D.enabled = true; mouse3D = viewport.interactiveSceneManager.mouse3D; var redColorMaterial:ColorMaterial = new ColorMaterial(0xcc0000); redColorMaterial.interactive = true; //10, 10 represent the number of segments in the plane var floor:Plane = new Plane(redColorMaterial, 2000, 2000, 10, 10); floor.rotationX = 90; var greenColorMaterial:ColorMaterial = new ColorMaterial(0x00cc00); follower = new Plane(greenColorMaterial, 300, 300); follower.rotationX = 90; scene.addChild(floor); scene.addChild(follower); camera.y = 1000; camera.lookAt(floor); var floorViewportLayer:ViewportLayer = new ViewportLayer(viewport, floor); var followerViewportLayer:ViewportLayer = new ViewportLayer(viewport, follower); viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT; floorViewportLayer.layerIndex = 0; followerViewportLayer.layerIndex = 1; viewport.containerSprite.addLayer(floorViewportLayer); viewport.containerSprite.addLayer(followerViewportLayer); startRendering(); } override protected function onRenderTick(event:Event=null):void { follower.x += (mouse3D.x - follower.x) * .1; follower.y += (mouse3D.y - follower.y) * .1; follower.z += (mouse3D.z - follower.z) * .1; renderer.renderScene(scene, camera, viewport); } } }
Drag and Release
Sproing! I’ll go into “camera.unproject” and “rays” in more detail in the future.
package { import flash.events.Event; import flash.events.MouseEvent; import gs.TweenMax; import gs.easing.Elastic; import org.papervision3d.core.geom.Lines3D; import org.papervision3d.core.geom.renderables.Line3D; import org.papervision3d.core.geom.renderables.Vertex3D; import org.papervision3d.core.math.Number3D; import org.papervision3d.events.InteractiveScene3DEvent; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.shadematerials.FlatShadeMaterial; import org.papervision3d.materials.special.LineMaterial; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.view.BasicView; [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")] public class DragAndRelease extends BasicView { private var sphere:Sphere; private var line:Line3D; private const LENGTH:Number = 2000; private var isSpherePressed:Boolean = false; public function DragAndRelease() { viewport.interactive = true; camera.fov = 110; var light:PointLight3D = new PointLight3D(); light.x = -1000, light.y = 1000; var material:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000); material.interactive = true; sphere = new Sphere(material, 200, 12, 12); sphere.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler); sphere.z = LENGTH + camera.z; stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); var lineMaterial:LineMaterial = new LineMaterial(0x00cc00); var lines:Lines3D = new Lines3D(); line = new Line3D(lines, lineMaterial, 1, new Vertex3D(1000,1000,1000), new Vertex3D(-1000,0,0)); lines.addLine(line); scene.addChild(lines); scene.addChild(sphere); startRendering(); } override protected function onRenderTick(event:Event=null):void { var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY); line.v0.x = ray.x, line.v0.y = ray.y, line.v0.z = ray.z; line.v1.x = sphere.x, line.v1.y = sphere.y, line.v1.z = sphere.z; ray.normalize(); ray.multiplyEq(LENGTH); ray = Number3D.add(ray, new Number3D(camera.x, camera.y, camera.z)); if(isSpherePressed) { sphere.position = ray; } renderer.renderScene(scene, camera, viewport); } private function objectPressHandler(event:InteractiveScene3DEvent):void { TweenMax.killTweensOf(sphere); isSpherePressed = true; } private function mouseUpHandler(event:MouseEvent):void { isSpherePressed = false; TweenMax.to(sphere, 1, {x:0, y:0, z:LENGTH + camera.z, ease:Elastic.easeOut}); } } }
Mouse3D moving light
Mouse3D is bound to the interactiveSceneManager of your viewport. So to get the 3d “renderHitData” of where your mouse is, you need to:
Mouse3D.enabled = true; viewport.interactive = true; mouse3D = viewport.interactiveSceneManager.mouse3D;
Then you can get 3d data from your mouse3D object like you would any other DisplayObject3D. Just remember the mouse will need to be over an object with a “material.interactive = true” to get any data back.
package { import flash.events.Event; import flash.ui.Mouse; import org.papervision3d.core.utils.Mouse3D; import org.papervision3d.events.InteractiveScene3DEvent; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.shadematerials.PhongMaterial; 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 Mouse3DExample extends BasicView { private var mouse3D:Mouse3D; private var pivotPoint:DisplayObject3D; private var light:PointLight3D; public function Mouse3DExample() { var headerText:headerContainer = new headerContainer(); headerText.header.text = "Mouse over the sphere to move the light"; addChild(headerText); viewport.interactive = true; Mouse3D.enabled = true; mouse3D = viewport.interactiveSceneManager.mouse3D; light = new PointLight3D(true); pivotPoint = new DisplayObject3D(); var material:PhongMaterial = new PhongMaterial(light, 0xcc0000, 0x000000, 10); material.interactive = true; var sphere:Sphere = new Sphere(material, 500, 30, 30); sphere.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, objectOverHandler); sphere.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, objectOutHandler); var smallSphere1:Sphere = new Sphere(new PhongMaterial(light, 0x00cc00, 0x000000, 10), 100, 10, 10); var smallSphere2:Sphere = new Sphere(new PhongMaterial(light, 0x0000cc, 0x000000, 10), 100, 10, 10); smallSphere1.x = -500; smallSphere1.y = 500; smallSphere2.x = 400; smallSphere2.y = -600; pivotPoint.addChild(smallSphere1); pivotPoint.addChild(smallSphere2); scene.addChild(sphere); scene.addChild(pivotPoint); scene.addChild(light); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function objectOverHandler(event:InteractiveScene3DEvent):void { Mouse.hide(); } private function objectOutHandler(event:InteractiveScene3DEvent):void { Mouse.show(); } private function enterFrameHandler(event:Event):void { pivotPoint.yaw(1); pivotPoint.pitch(.5); light.copyTransform(mouse3D); light.moveBackward(100); singleRender(); } } }
Dragging mouse for camera orbit
This little gem comes from a chat I had a while back with Tim Knip:
package { import flash.events.MouseEvent; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.shadematerials.FlatShadeMaterial; 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 OrbitingCameraExample extends BasicView { private var isOribiting:Boolean; private var cameraPitch:Number = 90; private var cameraYaw:Number = 270; private var cameraTarget:DisplayObject3D = DisplayObject3D.ZERO; private var previousMouseX:Number; private var previousMouseY:Number; private var light:PointLight3D; public function OrbitingCameraExample() { light = new PointLight3D(); var material:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000); var sphere1:Sphere = new Sphere(material, 300, 10, 10); var sphere2:Sphere = new Sphere(material, 100, 10, 10); sphere2.x = 300; sphere2.y = 300; sphere2.z = 700 var sphere3:Sphere = new Sphere(material, 100, 10, 10); sphere3.x = 600; sphere3.y = -400; sphere3.z = -200; var sphere4:Sphere = new Sphere(material, 100, 10, 10); sphere4.x = -700; sphere3.z = -100; scene.addChild(sphere1); scene.addChild(sphere2); scene.addChild(sphere3); scene.addChild(sphere4); stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); startRendering(); } private function onMouseDown(event:MouseEvent):void { isOribiting = true; previousMouseX = event.stageX; previousMouseY = event.stageY; } private function onMouseUp(event:MouseEvent):void { isOribiting = false; } private function onMouseMove(event:MouseEvent):void { var differenceX:Number = event.stageX - previousMouseX; var differenceY:Number = event.stageY - previousMouseY; if(isOribiting) { cameraPitch += differenceY; cameraYaw += differenceX; cameraPitch %= 360; cameraYaw %= 360; cameraPitch = cameraPitch > 0 ? cameraPitch : 0.0001; cameraPitch = cameraPitch < 90 ? cameraPitch : 89.9999; previousMouseX = event.stageX; previousMouseY = event.stageY; camera.orbit(cameraPitch, cameraYaw, true, cameraTarget); } } } }
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- Moving to johnlindquist.com
- AsyncCommand with Robotlegs, Signals, Flight, MinimalComps
- Search Widget – Robotlegs, Signals, Flight, Minimal Comps, Yahoo Astra
- FDT Super Awesome March Deal
- FDT Theme Designer
- haXe Tutorial
- AS3 Signals Tutorial
- Preferred Video Tutorial Resolution?
- TweenMax – Tweening a timeline (Advanced Tweening)
- Robotlegs + Flight + Union Platform
- Back in the saddle
- Eclipse Theme Designer Preview
- RobotLegs Hello World Video Tutorial
- 10 Things Every Senior Flash Developer Should Know
- Efflex – 3D Effects for Flex
Recent Comments
- Katie on Augmented Reality – Recursive Webcam
- software akuntansi terbaik on Back in the saddle
- loan rates on Augmented Reality – Recursive Webcam
- loan rates on Looking around the inside of a Sphere
- reverse phone lookup on Looking around the inside of a Sphere
- Oidhreachta on The Spotlight Effect (Dimming the unselected)
- Hosting company on archive
- aanbae on Back in the saddle
- Domain registration on Looking around the inside of a Sphere
- website designing company on Looking around the inside of a Sphere
Categories
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « May | ||||||
| 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 | ||||
Archives
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





