/src/away3d/materials/utils/WireframeMapGenerator.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 98 lines · 59 code · 14 blank · 25 comment · 8 complexity · f53ff0a407270a1dccda86a3ba8b44fe MD5 · raw file

  1. package away3d.materials.utils
  2. {
  3. import away3d.arcane;
  4. import away3d.core.base.ISubGeometry;
  5. import away3d.entities.Mesh;
  6. import flash.display.BitmapData;
  7. import flash.display.Graphics;
  8. import flash.display.Sprite;
  9. import flash.display.TriangleCulling;
  10. use namespace arcane;
  11. /**
  12. * WireframeMapGenerator is a utility class to generate a wireframe texture for uniquely mapped meshes.
  13. */
  14. public class WireframeMapGenerator
  15. {
  16. /**
  17. * Create a wireframe map with a texture fill.
  18. * @param mesh The Mesh object for which to create the wireframe texture.
  19. * @param bitmapData The BitmapData to use as the fill texture.
  20. * @param lineColor The wireframe's line colour.
  21. * @param lineThickness The wireframe's line thickness.
  22. */
  23. public static function generateTexturedMap(mesh:Mesh, bitmapData:BitmapData, lineColor:uint = 0xffffff, lineThickness:Number = 2):BitmapData
  24. {
  25. bitmapData = bitmapData.clone();
  26. for (var i:uint = 0; i < mesh.subMeshes.length; ++i)
  27. drawLines(lineColor, lineThickness, bitmapData, mesh.subMeshes[i].subGeometry);
  28. return bitmapData;
  29. }
  30. /**
  31. * Create a wireframe map with a solid colour fill.
  32. * @param mesh The Mesh object for which to create the wireframe texture.
  33. * @param lineColor The wireframe's line colour.
  34. * @param lineThickness The wireframe's line thickness.
  35. * @param fillColor The colour of the wireframe fill.
  36. * @param fillAlpha The alpha of the wireframe fill.
  37. * @param width The texture's width.
  38. * @param height The texture's height.
  39. * @return A BitmapData containing the texture underneath the wireframe.
  40. */
  41. public static function generateSolidMap(mesh:Mesh, lineColor:uint = 0xffffff, lineThickness:Number = 2, fillColor:uint = 0, fillAlpha:Number = 0, width:uint = 512, height:uint = 512):BitmapData
  42. {
  43. var bitmapData:BitmapData;
  44. if (fillAlpha > 1)
  45. fillAlpha = 1;
  46. else if (fillAlpha < 0)
  47. fillAlpha = 0;
  48. bitmapData = new BitmapData(width, height, fillAlpha == 1? false : true, (fillAlpha << 24) | (fillColor & 0xffffff));
  49. for (var i:uint = 0; i < mesh.subMeshes.length; ++i)
  50. drawLines(lineColor, lineThickness, bitmapData, mesh.subMeshes[i].subGeometry);
  51. return bitmapData;
  52. }
  53. /**
  54. * Draws the actual lines.
  55. */
  56. private static function drawLines(lineColor:uint, lineThickness:Number, bitmapData:BitmapData, subGeom:ISubGeometry):void
  57. {
  58. var sprite:Sprite = new Sprite();
  59. var g:Graphics = sprite.graphics;
  60. var uvs:Vector.<Number> = subGeom.UVData;
  61. var i:uint, len:uint = uvs.length;
  62. var w:Number = bitmapData.width, h:Number = bitmapData.height;
  63. var texSpaceUV:Vector.<Number> = new Vector.<Number>(len, true);
  64. var indices:Vector.<uint> = subGeom.indexData;
  65. var indexClone:Vector.<int>;
  66. do {
  67. texSpaceUV[i] = uvs[i]*w;
  68. ++i;
  69. texSpaceUV[i] = uvs[i]*h;
  70. } while (++i < len);
  71. len = indices.length;
  72. indexClone = new Vector.<int>(len, true);
  73. i = 0;
  74. // awesome, just to convert from uint to int vector -_-
  75. do
  76. indexClone[i] = indices[i];
  77. while (++i < len);
  78. g.lineStyle(lineThickness, lineColor);
  79. g.drawTriangles(texSpaceUV, indexClone, null, TriangleCulling.NONE);
  80. bitmapData.draw(sprite);
  81. g.clear();
  82. }
  83. }
  84. }