PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/broomstick/Examples/Away3D/as/src/SimpleTerrainTest.as

http://away3d.googlecode.com/
ActionScript | 188 lines | 142 code | 36 blank | 10 comment | 5 complexity | f39b316076285214dc4ee0946795a730 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-3.0, LGPL-3.0
  1. package
  2. {
  3. import away3d.containers.View3D;
  4. import away3d.debug.AwayStats;
  5. import away3d.filters.BloomFilter3D;
  6. import away3d.filters.DepthOfFieldFilter3D;
  7. import away3d.filters.MotionBlurFilter3D;
  8. import away3d.lights.DirectionalLight;
  9. import away3d.lights.LightBase;
  10. import away3d.lights.PointLight;
  11. import away3d.materials.BitmapMaterial;
  12. import away3d.materials.methods.FilteredShadowMapMethod;
  13. import away3d.materials.methods.FogMethod;
  14. import away3d.materials.methods.SoftShadowMapMethod;
  15. import away3d.materials.methods.TerrainDiffuseMethod;
  16. import away3d.materials.utils.CubeMap;
  17. import away3d.primitives.Cube;
  18. import away3d.extrusions.Elevation;
  19. import away3d.primitives.Plane;
  20. import away3d.primitives.SkyBox;
  21. import away3d.primitives.Sphere;
  22. import away3d.containers.ObjectContainer3D;
  23. import flash.display.BitmapData;
  24. import flash.display.Sprite;
  25. import flash.display.StageAlign;
  26. import flash.display.StageScaleMode;
  27. import flash.events.Event;
  28. import flash.events.KeyboardEvent;
  29. import flash.ui.Keyboard;
  30. import flash.utils.getTimer;
  31. [SWF(width="1024", height="576", frameRate="60")]
  32. public class SimpleTerrainTest extends Sprite
  33. {
  34. private var _view : View3D;
  35. private var _light : DirectionalLight;
  36. // Environment map.
  37. [Embed(source="../embeds/envMap/snow_positive_x.jpg")]
  38. private var EnvPosX:Class;
  39. [Embed(source="../embeds/envMap/snow_positive_y.jpg")]
  40. private var EnvPosY:Class;
  41. [Embed(source="../embeds/envMap/snow_positive_z.jpg")]
  42. private var EnvPosZ:Class;
  43. [Embed(source="../embeds/envMap/snow_negative_x.jpg")]
  44. private var EnvNegX:Class;
  45. [Embed(source="../embeds/envMap/snow_negative_y.jpg")]
  46. private var EnvNegY:Class;
  47. [Embed(source="../embeds/envMap/snow_negative_z.jpg")]
  48. private var EnvNegZ:Class;
  49. [Embed(source="../embeds/Heightmap.jpg")]
  50. private var HeightMap : Class;
  51. [Embed(source="../embeds/terrain_tex.jpg")]
  52. private var Albedo : Class;
  53. [Embed(source="../embeds/terrain_norms.jpg")]
  54. private var Normals : Class;
  55. [Embed(source="../embeds/grass.jpg")]
  56. private var Grass : Class;
  57. [Embed(source="../embeds/terrainGrassBlend.jpg")]
  58. private var GrassBlend : Class;
  59. [Embed(source="../embeds/rock.jpg")]
  60. private var Rock : Class;
  61. [Embed(source="../embeds/terrainRockBlend.jpg")]
  62. private var RockBlend : Class;
  63. [Embed(source="../embeds/beach.jpg")]
  64. private var Beach : Class;
  65. [Embed(source="../embeds/terrainBeachBlend.jpg")]
  66. private var BeachBlend : Class;
  67. private var _camController : FlightController;
  68. private var _terrain : Elevation;
  69. private var _cubeMap : CubeMap;
  70. private var _stickToFloor : Boolean = true;
  71. private var _motionBlur : MotionBlurFilter3D;
  72. private var _prevX : Number = 0;
  73. private var _prevY : Number = 0;
  74. private var _strength : Number = 0;
  75. public function SimpleTerrainTest()
  76. {
  77. var material : BitmapMaterial;
  78. var terrainMethod : TerrainDiffuseMethod;
  79. var fog : FogMethod;
  80. super();
  81. _view = new View3D();
  82. _motionBlur = new MotionBlurFilter3D();
  83. _view.filters3d = [ new BloomFilter3D(20, 20, .75, 1, 3) ];
  84. // _view.filters3d = [ new DepthOfFieldFilter3D(10, 10) ];
  85. _view.antiAlias = 4;
  86. _view.camera.lens.far = 14000;
  87. _view.camera.lens.near = .1;
  88. // _view.camera.y = 500;
  89. _camController = new FlightController(_view.camera, stage);
  90. _camController.moveSpeed = 1;
  91. _cubeMap = new CubeMap( new EnvPosX().bitmapData, new EnvNegX().bitmapData,
  92. new EnvPosY().bitmapData, new EnvNegY().bitmapData,
  93. new EnvPosZ().bitmapData, new EnvNegZ().bitmapData);
  94. _view.scene.addChild(new SkyBox(_cubeMap));
  95. addChild(_view);
  96. addEventListener(Event.ENTER_FRAME, _handleEnterFrame);
  97. terrainMethod = new TerrainDiffuseMethod();
  98. terrainMethod.setSplattingLayer(0, new Grass().bitmapData, new GrassBlend().bitmapData, 150);
  99. terrainMethod.setSplattingLayer(1, new Rock().bitmapData, new RockBlend().bitmapData, 100);
  100. terrainMethod.setSplattingLayer(2, new Beach().bitmapData, new BeachBlend().bitmapData, 50);
  101. terrainMethod.normalizeSplats();
  102. material = new BitmapMaterial(new Albedo().bitmapData);
  103. material.diffuseMethod = terrainMethod;
  104. material.normalMap = new Normals().bitmapData;
  105. material.ambientColor = 0x202030;
  106. material.specular = .2;
  107. fog = new FogMethod(4000, 0xcfd9de);
  108. material.addMethod(fog);
  109. // mountain like
  110. _terrain = new Elevation(material, new HeightMap().bitmapData, 5000, 1300, 5000, 175, 175);
  111. // canyon like
  112. //_terrain = new Elevation(material, new HeightMap().bitmapData, 5000, 1300, 5000, 175, 175, 100);
  113. // if your map has noise, the class generates a smoother map to prevent choppy camera movements when using _terrain.getHeightAt
  114. //_terrain = new Elevation(material, new HeightMap().bitmapData, 5000, 1300, 5000, 175, 175, 255, 0, true);
  115. //you can access the map after generation via _terrain.smoothedHeightMap;
  116. _view.scene.addChild(_terrain);
  117. _light = new DirectionalLight(-300, -300, -5000);
  118. _light.color = 0xfffdc5;
  119. material.lights = [_light];
  120. _view.scene.addChild(_light);
  121. stage.scaleMode = StageScaleMode.NO_SCALE;
  122. stage.align = StageAlign.TOP_LEFT;
  123. stage.addEventListener(Event.RESIZE, onStageResize);
  124. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  125. addChild(new AwayStats(_view));
  126. }
  127. private function onKeyUp(event : KeyboardEvent) : void
  128. {
  129. if (event.keyCode == Keyboard.SPACE) _stickToFloor = !_stickToFloor;
  130. _camController.moveSpeed = _stickToFloor? 1 : 5;
  131. }
  132. private function onStageResize(event : Event) : void
  133. {
  134. _view.width = stage.stageWidth;
  135. _view.height = stage.stageHeight;
  136. }
  137. private function _handleEnterFrame(ev : Event) : void
  138. {
  139. var mx : Number = mouseX, my : Number = mouseY;
  140. var dx : Number = mx - _prevX, dy : Number = my - _prevY;
  141. var dist : Number = .4 + (dx*dx + dy*dy) / 300;
  142. if (dist > .9) dist = .9;
  143. _strength += (dist-_strength)*.05;
  144. // _motionBlur.strength = _strength;
  145. _prevX = mx;
  146. _prevY = my;
  147. var h : Number = _terrain.getHeightAt(_view.camera.x, _view.camera.z) + 10;
  148. if (_stickToFloor || h > _view.camera.y) _view.camera.y += (h - _view.camera.y)*.05;
  149. _view.render();
  150. }
  151. }
  152. }