dehash »  AS3 Papervision3D GreatWhite example code «

Some AS3 code examples that use Papervision3D GreatWhite from SVN. Made with FlashDevelop and Flex3 SDK but as they are pure AS3 examples they should work mostly the same in Flash 9 too. Some examples are clean and basic but others are less so and some are just ideas for things to explore later when more examples are added. Will try to put build/revision number in each post but note when using SVN it is inevitable that if some time has passed parts of these they may not work without tweaking in later builds. The source code text is released as open source under the BSD License which is a very open license with few restrictions. Any external media used however (bitmaps, flv, sound etc) is not covered by this license. If you notice mistakes please email dehash_at_gmail_dot_com.

 

Title: SwapCubeMaterial

Swapping a cube material at runtime. Mouse click on stage changes cube material. Textures created using GouraudMaterial with DropShadowFilter. Uses BasicView - revision 702

Actionscript:
  1. /**
  2. * v0.1 code by dehash.com 2008
  3. * Released as open source under the BSD License
  4. * http://www.opensource.org/licenses/bsd-license.php
  5. */
  6.  
  7. package com.dehash.pv3d.examples.materials {
  8. import org.papervision3d.materials.shadematerials.GouraudMaterial;
  9. import org.papervision3d.objects.primitives.Cube;
  10. import org.papervision3d.view.BasicView;
  11. import org.papervision3d.lights.PointLight3D;
  12. import org.papervision3d.materials.utils.MaterialsList;
  13. import flash.filters.DropShadowFilter;
  14. import flash.events.MouseEvent;
  15. import flash.events.Event;
  16.  
  17. [SWF(width="800", height="600", backgroundColor="0xFFFFFF")]
  18.  
  19. public class SwappingCubeMaterial extends BasicView {
  20.  
  21. private var cube:Cube;
  22. private var light:PointLight3D;
  23.  
  24. public function SwappingCubeMaterial(viewportWidth:Number = 800,
  25. viewportHeight:Number = 600, scaleToStage:Boolean = true,
  26. interactive:Boolean=false, cameraType:String="CAMERA3D") {
  27. super(viewportWidth, viewportHeight, scaleToStage, interactive, cameraType);
  28.  
  29. stage.addEventListener(MouseEvent.MOUSE_DOWN, changeMaterial);
  30.  
  31. light = new PointLight3D();
  32.  
  33. var materialsList:MaterialsList = new MaterialsList();
  34. materialsList.addMaterial(new GouraudMaterial(light, 0x491ca5, 0x270e59), "left");
  35. materialsList.addMaterial(new GouraudMaterial(light, 0x491ca5, 0x270e59), "back");
  36. materialsList.addMaterial(new GouraudMaterial(light, 0x491ca5, 0x270e59), "top");
  37. materialsList.addMaterial(new GouraudMaterial(light, 0x491ca5, 0x270e59), "right");
  38. materialsList.addMaterial(new GouraudMaterial(light, 0x491ca5, 0x270e59), "bottom");
  39. materialsList.addMaterial(new GouraudMaterial(light, 0x491ca5, 0x270e59), "front");
  40.  
  41. cube = new Cube(materialsList, 400, 400, 400, 8, 8, 8);
  42. cube.useOwnContainer = true;
  43. cube.filters = [new DropShadowFilter(12, 45, 0, 0.75, 12, 12)];
  44. scene.addChild(cube);
  45. camera.target = cube;
  46.  
  47. renderer.renderScene(scene, camera, viewport);
  48. this.startRendering();
  49. }
  50.  
  51. protected override function onRenderTick(event:Event = null):void {
  52. cube.yaw((mouseY - (stage.stageHeight/2))/(stage.height / 2) * 5);
  53. cube.roll((mouseX - (stage.stageWidth/2))/(stage.width / 2) * -5);
  54. camera.x -= (camera.x - Math.sin(mouseX * 0.01) * 1000) / 8;
  55. camera.z -= (camera.z - Math.cos(mouseX * 0.01) * 1000) / 8;
  56. camera.y -= (camera.y - viewport.containerSprite.mouseY * 5) / 8;
  57. light.copyTransform( camera );
  58. renderer.renderScene(scene, camera, viewport);
  59. }
  60.  
  61.  
  62.  
  63. private function changeMaterial(event:MouseEvent):void {
  64. cube.replaceMaterialByName(new GouraudMaterial(light, 0xa51c49, 0x590e27), 'left');
  65. cube.replaceMaterialByName(new GouraudMaterial(light, 0xa51c49, 0x590e27), 'back');
  66. cube.replaceMaterialByName(new GouraudMaterial(light, 0xa51c49, 0x590e27), 'top');
  67. cube.replaceMaterialByName(new GouraudMaterial(light, 0xa51c49, 0x590e27), 'right');
  68. cube.replaceMaterialByName(new GouraudMaterial(light, 0xa51c49, 0x590e27), 'bottom');
  69. cube.replaceMaterialByName(new GouraudMaterial(light, 0xa51c49, 0x590e27), 'front');
  70. }
  71.  
  72. }
  73. }