/src/LightingTest.as

https://github.com/osoker/away3d-examples-broomstick · ActionScript · 153 lines · 117 code · 28 blank · 8 comment · 1 complexity · 9437ca77bcfa482bc007864b48ff02e6 MD5 · raw file

  1. package
  2. {
  3. import away3d.containers.View3D;
  4. import away3d.entities.Mesh;
  5. import away3d.events.LoaderEvent;
  6. import away3d.events.MouseEvent3D;
  7. import away3d.lights.DirectionalLight;
  8. import away3d.lights.LightBase;
  9. import away3d.lights.PointLight;
  10. import away3d.loaders.Loader3D;
  11. import away3d.loaders.misc.AssetLoaderContext;
  12. import away3d.loaders.parsers.OBJParser;
  13. import away3d.materials.BitmapMaterial;
  14. import flash.display.BitmapData;
  15. import flash.display.Sprite;
  16. import flash.display.StageAlign;
  17. import flash.display.StageScaleMode;
  18. import flash.events.Event;
  19. import flash.events.MouseEvent;
  20. [SWF(width="1024", height="576", frameRate="60", backgroundColor="0xffffff")]
  21. public class LightingTest extends Sprite
  22. {
  23. [Embed(source="/../embeds/head/head.obj", mimeType="application/octet-stream")]
  24. private var OBJData : Class;
  25. [Embed(source="/../embeds/head/Images/Map-COL.jpg")]
  26. private var Albedo : Class;
  27. [Embed(source="/../embeds/head/Images/Map-spec.jpg")]
  28. private var Specular : Class;
  29. [Embed(source="/../embeds/head/Images/Infinite-Level_02_Tangent_NoSmoothUV.jpg")]
  30. private var Normal : Class;
  31. private var _view : View3D;
  32. private var _loader : Loader3D;
  33. private var _light : PointLight;
  34. private var _light2 : PointLight;
  35. private var _light3 : LightBase;
  36. private var _mesh : Mesh;
  37. private var _camController : HoverDragController;
  38. private var _bg : BitmapData;
  39. public function LightingTest()
  40. {
  41. _view = new View3D();
  42. _view.antiAlias = 4;
  43. _view.backgroundColor = 0x0010000;
  44. _view.backgroundAlpha = 0;
  45. _bg = new BitmapData(500, 300, false, 0);
  46. _bg.perlinNoise(50, 50, 8, 5, true, true, 7, true);
  47. _view.backgroundImage = _bg;
  48. _light = new PointLight(); // DirectionalLight();
  49. _light.x = -5000;
  50. _light.y = 5000;
  51. _light.z = 10000;
  52. // _light.radius = 2000;
  53. // _light.fallOff = 3000;
  54. _light.color = 0xff1111;
  55. _light2 = new PointLight(); // DirectionalLight();
  56. _light2.x = 5000;
  57. _light2.y = 5000;
  58. _light2.z = -10000;
  59. // _light2.radius = 2000;
  60. // _light2.fallOff = 3000;
  61. _light2.color = 0x1111ff;
  62. _light3 = new DirectionalLight(0, -1, 1);
  63. _light3.specular = 1;
  64. _light3.diffuse = 1;
  65. _light3.color = 0xffffff;
  66. _view.scene.addChild(_light);
  67. _view.scene.addChild(_light2);
  68. _view.scene.addChild(_light3);
  69. this.addChild(_view);
  70. Loader3D.enableParser(OBJParser);
  71. _loader = new Loader3D();
  72. _loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onResourceComplete);
  73. _loader.parseData(OBJData, null, new AssetLoaderContext(false));
  74. _loader.scale(50);
  75. _view.scene.addChild(_loader);
  76. _view.forceMouseMove = true;
  77. this.addEventListener(Event.ENTER_FRAME, _handleEnterFrame);
  78. // stage.addEventListener(MouseEvent.CLICK, onClick);
  79. stage.scaleMode = StageScaleMode.NO_SCALE;
  80. stage.align = StageAlign.TOP_LEFT;
  81. stage.addEventListener(Event.RESIZE, onStageResize);
  82. _camController = new HoverDragController(_view.camera, stage);
  83. }
  84. private function onStageResize(event : Event) : void
  85. {
  86. _view.width = stage.stageWidth;
  87. _view.height = stage.stageHeight;
  88. }
  89. private function onClick(event : MouseEvent) : void
  90. {
  91. _mesh.geometry.subGeometries[0].useFaceWeights = !_mesh.geometry.subGeometries[0].useFaceWeights;
  92. }
  93. private function onResourceComplete(ev : LoaderEvent) : void
  94. {
  95. var mesh : Mesh;
  96. var len : uint = _loader.numChildren;
  97. var material : BitmapMaterial = new BitmapMaterial(new Albedo().bitmapData);
  98. material.normalMap = new Normal().bitmapData;
  99. material.specularMap = new Specular().bitmapData;
  100. material.specular = .5;
  101. material.gloss = 50;
  102. material.lights = [_light, _light2, _light3 ];
  103. // material.specularMethod = null;
  104. for (var i : uint = 0; i < len; ++i) {
  105. mesh = Mesh(_loader.getChildAt(i));
  106. mesh.material = material;
  107. // mesh.geometry.subGeometries[0].autoDeriveVertexNormals = true;
  108. // mesh.geometry.subGeometries[0].autoDeriveVertexTangents = true;
  109. }
  110. _mesh = mesh;
  111. _mesh.mouseEnabled = true;
  112. _mesh.mouseDetails = true;
  113. _mesh.addEventListener(MouseEvent3D.CLICK, onClick3D)
  114. }
  115. private function onClick3D(event : MouseEvent3D) : void
  116. {
  117. _mesh.scale(Math.random()+.5);
  118. }
  119. private function _handleEnterFrame(ev : Event) : void
  120. {
  121. _loader.rotationY += 1;
  122. _view.render();
  123. }
  124. }
  125. }