PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Main.as

http://github.com/timknip/papervision3d
ActionScript | 181 lines | 124 code | 39 blank | 18 comment | 0 complexity | 8e38c9174ba2aef84e63ec0fd26b8fd8 MD5 | raw file
  1. package
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display.Sprite;
  5. import flash.display.StageAlign;
  6. import flash.display.StageQuality;
  7. import flash.display.StageScaleMode;
  8. import flash.events.Event;
  9. import flash.text.TextField;
  10. import flash.text.TextFormat;
  11. import net.hires.debug.Stats;
  12. import org.papervision3d.cameras.Camera3D;
  13. import org.papervision3d.core.geom.provider.VertexGeometry;
  14. import org.papervision3d.core.ns.pv3d;
  15. import org.papervision3d.core.render.clipping.ClipFlags;
  16. import org.papervision3d.core.render.data.RenderData;
  17. import org.papervision3d.core.render.data.RenderStats;
  18. import org.papervision3d.core.render.pipeline.BasicPipeline;
  19. import org.papervision3d.materials.BitmapMaterial;
  20. import org.papervision3d.materials.WireframeMaterial;
  21. import org.papervision3d.objects.DisplayObject3D;
  22. import org.papervision3d.objects.primitives.Cube;
  23. import org.papervision3d.render.BasicRenderEngine;
  24. import org.papervision3d.view.Viewport3D;
  25. [SWF (backgroundColor="#000000")]
  26. public class Main extends Sprite
  27. {
  28. use namespace pv3d;
  29. public var container :Sprite;
  30. public var vertexGeometry :VertexGeometry;
  31. public var cube :Cube;
  32. public var camera :Camera3D;
  33. public var pipeline :BasicPipeline;
  34. public var viewport :Viewport3D;
  35. public var scene :DisplayObject3D;
  36. public var renderData :RenderData;
  37. public var renderer :BasicRenderEngine;
  38. public var tf :TextField;
  39. public var camera2 :Camera3D;
  40. public function Main()
  41. {
  42. init();
  43. }
  44. private function init():void
  45. {
  46. stage.align = StageAlign.TOP_LEFT;
  47. stage.scaleMode = StageScaleMode.NO_SCALE;
  48. stage.frameRate = 60;
  49. stage.quality = StageQuality.LOW;
  50. // Thanks doob!
  51. addChild(new Stats());
  52. tf = new TextField();
  53. addChild(tf);
  54. tf.x = 1;
  55. tf.y = 110;
  56. tf.width = 300;
  57. tf.height = 200;
  58. tf.defaultTextFormat = new TextFormat("Arial", 10, 0xff0000);
  59. tf.selectable = false;
  60. tf.multiline = true;
  61. tf.text = "Papervision3D - version 3.0";
  62. viewport = new Viewport3D(0, 0, true);
  63. addChild(viewport);
  64. scene = new DisplayObject3D("Scene");
  65. camera = new Camera3D(30, 400, 2300, "Camera01");
  66. scene.addChild( camera );
  67. camera.enableCulling = false
  68. camera.showFrustum = false;
  69. camera.z = 800;
  70. renderer = new BasicRenderEngine();
  71. renderer.clipFlags = ClipFlags.ALL;
  72. var bmp:BitmapData = new BitmapData(256, 256);
  73. bmp.perlinNoise(256, 256, 2, 300, true, false);
  74. cube = new Cube(new BitmapMaterial(bmp), 100, "Cube");
  75. var cubeChildx : Cube = new Cube(new BitmapMaterial(new BitmapData(256, 256, true, 0x6600FFFF)), 100);
  76. cubeChildx.x = 100;
  77. cube.addChild(cubeChildx);
  78. //cube = new Cube(new WireframeMaterial(0xFF0000), 100, "Cube");
  79. var cubeChild0 :Cube = new Cube(new WireframeMaterial(0xFF0000), 100, "red");
  80. cube.addChild( cubeChild0 );
  81. cubeChild0.x = 300;
  82. //cubeChild0.z = -500;
  83. var cubeChild1 :Cube = new Cube(new WireframeMaterial(0x00FF00), 100, "blue");
  84. cube.addChild( cubeChild1 );
  85. cubeChild1.z = 100;
  86. var cubeChild2 :Cube = new Cube(new WireframeMaterial(0x0000FF), 100, "green");
  87. cube.addChild( cubeChild2 );
  88. cubeChild2.y = 200;
  89. cubeChild2.z = 10;
  90. cubeChild1.scaleX = 5;
  91. cubeChild1.scaleY = 5;
  92. cubeChild1.scaleZ = 0.1;
  93. scene.addChild( cube );
  94. camera2 = new Camera3D(50, 50, 500);
  95. cube.addChild(camera2);
  96. camera2.showFrustum = true;
  97. //cube.scaleX = 2;
  98. //var plane :Plane = new Plane(new WireframeMaterial(0x0000FF), 400, 400, 1, 1, "Plane0");
  99. //scene.addChild(plane);
  100. addEventListener(Event.ENTER_FRAME, render);
  101. }
  102. private var _r :Number = 0;
  103. private var _s :Number = 0;
  104. private function render(event:Event=null):void
  105. {
  106. camera2.frustumGeometry.update(camera2);
  107. // rotation in global frame of reference : append
  108. // cube.x ++;
  109. cube.rotationY--;
  110. //cube.getChildByName("blue").x += 0.1;
  111. //cube.getChildByName("blue").rotationZ--;
  112. // cube.getChildByName("blue").lookAt( cube.getChildByName("red") );
  113. cube.getChildByName("blue").rotationZ += 0.1;
  114. cube.getChildByName("blue").transform.eulerAngles.y--;
  115. cube.getChildByName("green").lookAt( cube.getChildByName("red") );
  116. //cube.lookAt(cube.getChildByName("blue"));
  117. cube.getChildByName("red").transform.eulerAngles.z--;
  118. cube.getChildByName("red").transform.eulerAngles.y += 4;
  119. cube.getChildByName("red").transform.dirty = true;
  120. // cube.getChildByName("red").rotateAround(_s++, new Vector3D(0, _s, _s));
  121. // cube.getChildByName("red").scaleX = 2;
  122. // cube.getChildByName("red").rotateAround(_s, new Vector3D(0, -_s, 0));
  123. // cube.getChildByName("green").rotateAround(_r++, Vector3D.X_AXIS);
  124. camera.x = Math.sin(_r) * 950;
  125. camera.y = 500;
  126. camera.z = Math.cos(_r) * 950;
  127. _r += Math.PI / 180 * 0.25;
  128. _r = _r > Math.PI * 2 ? 0 : _r;
  129. camera.lookAt(cube);
  130. //camera.lookAt( cube.getChildByName("blue") );
  131. //trace(cube.getChildByName("red").transform.position);
  132. renderer.renderScene(scene, camera, viewport);
  133. var stats :RenderStats = renderer.renderData.stats;
  134. tf.text = "Papervision3D - version 3.0\n" +
  135. "\ntotal objects: " + stats.totalObjects +
  136. "\nculled objects: " + stats.culledObjects +
  137. "\n\ntotal triangles: " + stats.totalTriangles +
  138. "\nculled triangles: " + stats.culledTriangles +
  139. "\nclipped triangles: " + stats.clippedTriangles;
  140. }
  141. }
  142. }