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: Layers Demo

Bringing layers to front using BasicView - revision 677 - also uses minimalcomps

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. package com.dehash.pv3d.examples.basic
  7. {
  8.  
  9. import org.papervision3d.objects.DisplayObject3D;
  10. import org.papervision3d.objects.primitives.Plane;
  11. import org.papervision3d.view.BasicView;
  12. import org.papervision3d.materials.ColorMaterial;
  13. import org.papervision3d.view.layer.util.ViewportLayerSortMode;
  14. import flash.events.Event;
  15. // http://code.google.com/p/minimalcomps/
  16. import com.bit101.components.PushButton;
  17.  
  18. [SWF(width="800", height="600", backgroundColor="0x000000")]
  19.  
  20. public class LayersDemo extends BasicView
  21. {
  22.     private var redPlane:Plane;
  23.     private var greenPlane:Plane;
  24.     private var bluePlane:Plane;
  25.     private var quality:uint = 16;
  26.     private var planeWidth:uint = 400;
  27.     private var planeHeight:uint = 400;
  28.     private var topDepth:uint = 100;
  29.     private var world:DisplayObject3D;
  30.  
  31.  
  32. public function LayersDemo(viewportWidth:Number = 800, viewportHeight:Number = 600,
  33. scaleToStage:Boolean=true, interactive:Boolean=false, cameraType:String="CAMERA3D")
  34. {
  35.  
  36. super(viewportWidth, viewportHeight, scaleToStage, interactive, cameraType);
  37.  
  38. viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
  39.  
  40. redPlane = new Plane(new ColorMaterial(0xFF0000, 1), planeWidth, planeHeight, quality);
  41. greenPlane = new Plane(new ColorMaterial(0x00FF00, 1), planeWidth, planeHeight, quality);
  42. bluePlane = new Plane(new ColorMaterial(0x0000FF, 1), planeWidth, planeHeight, quality);
  43.  
  44. redPlane.x = redPlane.y = -105;
  45. greenPlane.x = greenPlane.y = 100;
  46. bluePlane.x = bluePlane.y = 250;
  47.  
  48. world = new DisplayObject3D();
  49.  
  50. world.addChild(redPlane);
  51. world.addChild(greenPlane);
  52. world.addChild(bluePlane);
  53. scene.addChild(world);
  54.  
  55. viewport.getChildLayer(redPlane, true).layerIndex = ++topDepth;
  56. viewport.getChildLayer(greenPlane, true).layerIndex = ++topDepth;
  57. viewport.getChildLayer(bluePlane, true).layerIndex = ++topDepth;
  58.  
  59. camera.target = world;
  60.  
  61. addChild(new PushButton(null, 50, 50, "RED to Front", redToFrontHandler));
  62. addChild(new PushButton(null, 200, 50, "GREEN to Front", greenToFrontHandler));
  63. addChild(new PushButton(null, 350, 50, "BLUE to Front", blueToFrontHandler));
  64.  
  65.  
  66.  
  67.  
  68. renderer.renderScene(scene, camera, viewport);
  69. this.startRendering();
  70. }
  71.  
  72. private function redToFrontHandler(event:Event):void {
  73. viewport.getChildLayer(redPlane, true).layerIndex = ++topDepth;
  74. }
  75. private function greenToFrontHandler(event:Event):void {
  76. viewport.getChildLayer(greenPlane, true).layerIndex = ++topDepth;
  77. }
  78. private function blueToFrontHandler(event:Event):void {
  79. viewport.getChildLayer(bluePlane, true).layerIndex = ++topDepth;
  80. }
  81.  
  82. protected override function onRenderTick(event:Event = null):void {
  83. var rotY: Number = (mouseY-(stage.stageHeight/2))/(stage.height/2)*(1600);
  84. var rotX: Number = (mouseX-(stage.stageWidth/2))/(stage.width/2)*(-1600);
  85. camera.x= camera.x+(rotX-camera.x)/5;
  86. camera.y = camera.y + (rotY - camera.y) / 5;
  87. renderer.renderScene(scene, camera, viewport)
  88. }
  89.  
  90. }
  91. }