light
Dragging Spheres
package { import flash.events.Event; import flash.events.MouseEvent; import org.papervision3d.core.geom.renderables.Vertex3D; import org.papervision3d.core.math.Number3D; import org.papervision3d.core.math.Plane3D; import org.papervision3d.events.InteractiveScene3DEvent; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.shadematerials.FlatShadeMaterial; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.view.BasicView; [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")] public class DraggingASphere extends BasicView { private const NUM_SPHERES:int = 11; private var planeToDragOn:Plane3D; private var currentSphere:Sphere; private var light:PointLight3D; public function DraggingASphere() { viewport.interactive = true; camera.y = 400; light = new PointLight3D(true); scene.addChild(light); for(var i:int = 0; i < NUM_SPHERES; i++) { var material:FlatShadeMaterial = new FlatShadeMaterial(light, 0xeeeeee, 0x2d2d2d, 10); material.interactive = true; var sphere:Sphere = new Sphere(material); sphere.x = (i - NUM_SPHERES/2) * 200; scene.addChild(sphere); sphere.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 objectPressHandler(event:InteractiveScene3DEvent):void { currentSphere = event.displayObject3D as Sphere; } private function mouseUpHandler(event:MouseEvent):void { currentSphere = 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(currentSphere) { currentSphere.x = intersectPoint.x; currentSphere.y = intersectPoint.y; currentSphere.z = intersectPoint.z; } light.x = intersectPoint.x; light.y = intersectPoint.y + 100; light.z = intersectPoint.z; renderer.renderScene(scene, camera, viewport); } } }
Collada with CellShader and PointLight3D
Pretty much the same as the last example, but this one has a Cell Shader overlaying the BitmapMaterial. Also, the light follows the camera as the camera orbits the collada model.
Drag mouse to orbit. Scroll wheel to zoom.

source
package { import flash.display.Bitmap; import flash.events.MouseEvent; import flash.utils.ByteArray; import org.papervision3d.lights.PointLight3D; import org.papervision3d.materials.BitmapMaterial; import org.papervision3d.materials.shaders.CellShader; import org.papervision3d.materials.shaders.ShadedMaterial; import org.papervision3d.objects.parsers.DAE; import org.papervision3d.view.BasicView; [SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")] public class CellShadedCollada extends BasicView { [Embed(source="assets/c.DAE", mimeType = "application/octet-stream")] private var daeAsset:Class; [Embed(source="assets/materials/c.png")] private var materialAsset:Class; private var cameraPitch:Number = 90; private var cameraYaw:Number = 270; private var isOrbiting:Boolean = false; private var previousMouseX:Number; private var previousMouseY:Number; private var light:PointLight3D; public function CellShadedCollada() { var byteArray:ByteArray = new daeAsset() as ByteArray; var dae:DAE = new DAE(); dae.load(byteArray); var bitmap:Bitmap = new materialAsset() as Bitmap; var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData, true); light = new PointLight3D(); var cellShader:CellShader = new CellShader(light, 0xffffff, 0x000000, 10); var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial, cellShader); dae.materials.addMaterial(shadedMaterial, "cMaterial"); scene.addChild(dae); startRendering(); stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler); } private function mouseWheelHandler(event:MouseEvent):void { camera.moveForward(10 * event.delta); } private function onMouseDown(event:MouseEvent):void { isOrbiting = true; previousMouseX = event.stageX; previousMouseY = event.stageY; } private function onMouseUp(event:MouseEvent):void { isOrbiting = false; } private function onMouseMove(event:MouseEvent):void { var differenceX:Number = event.stageX - previousMouseX; var differenceY:Number = event.stageY - previousMouseY; if(isOrbiting) { 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); light.position = camera.position; } } } }
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(); } } }
Search
Recommended Books
Speaking at FITC Toronto
Recent Posts
- 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
- MorphController – Mighty Morphing Papervision3D
- End dump
- Test if a plane is within the view of the camera (aka testing if culled)
- Materials Reference
Recent Comments
- BAM5 on haXe Tutorial
- AlexG on Finding 2D Coordinates of a DisplayObject3D
- Josh on ActionScript 3 – Model View Controller (MVC)
- martin everett on requests
- martin everett on requests
- lillacska on Dragging Spheres
- Guy Ritchie on MXML without the Flex framework
- Pedro on ActionScript 3 – Namespaces
- daveevolve on AS3DMod Perlin Noise
- sebomoto on haXe Tutorial
Categories
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Jan | ||||||
| 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 | ||||
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



