/src/sl2d/slTexture.as

https://github.com/sleep2death/sl2d-framework
ActionScript | 185 lines | 145 code | 39 blank | 1 comment | 3 complexity | cb68c1fb120248ccb3f0819478f188d5 MD5 | raw file
  1. package sl2d {
  2. import flash.display3D.textures.Texture;
  3. import flash.display3D.Context3DBlendFactor;
  4. import flash.display3D.Context3DCompareMode;
  5. import flash.display.Sprite;
  6. import flash.geom.Rectangle;
  7. public class slTexture {
  8. public static const FAILED : int = -1;
  9. public static const NOT_READY : int = 0;
  10. public static const PREPARING : int = 1;
  11. public static const READY : int = 2;
  12. public static const INITIALIZED : int = 3;
  13. public var status : uint = NOT_READY;
  14. public var root : Sprite;
  15. public var compareMode : String = Context3DCompareMode.GREATER_EQUAL;
  16. public var blendFactor_src : String = Context3DBlendFactor.ONE;
  17. public var blendFactor_dst : String = Context3DBlendFactor.ZERO;
  18. public var texture : Texture;
  19. public var width : uint = 0;
  20. public var height : uint = 0;
  21. public var frameCount : uint = 0;
  22. public function slTexture(texture : Texture = null, w : uint = 0, h : uint = 0, isReady : Boolean = true) : void {
  23. this.texture = texture;
  24. this.width = w;
  25. this.height = h;
  26. }
  27. public var vertexVector : Vector.<Number> = new Vector.<Number>();
  28. public var uvVector : Vector.<Number> = new Vector.<Number>();
  29. public var colorVector : Vector.<Number> = new Vector.<Number>();
  30. protected var recycled : Vector.<slBounds> = new Vector.<slBounds>();
  31. public function createBounds() : slBounds {
  32. if(recycled.length > 0) return recycled.shift();
  33. var len : uint = vertexVector.push(
  34. 0, 0, 0,
  35. 1, 0, 0,
  36. 0, 1, 0,
  37. 1, 1, 0
  38. );
  39. colorVector.push(
  40. 1, 1, 1,
  41. 1, 1, 1,
  42. 1, 1, 1,
  43. 1, 1, 1
  44. );
  45. uvVector.push(
  46. 0, 0, 1,
  47. 1, 0, 1,
  48. 0, 1, 1,
  49. 1, 1, 1
  50. );
  51. bc ++;
  52. vc = bc * 4;
  53. ic = bc * 2;
  54. return new slBounds(this, len - 12);
  55. }
  56. protected var bc : uint = 0;
  57. protected var vc : uint = 0;
  58. protected var ic : uint = 0;
  59. public function get bufferCount() : uint {
  60. return bc;
  61. }
  62. public function get vertexCount() : uint {
  63. return vc;
  64. }
  65. public function get indexCount() : uint {
  66. return ic;
  67. }
  68. public function recycle(bounds : slBounds) : void {
  69. resetVector(bounds.offset);
  70. recycled.push(bounds);
  71. }
  72. public function setV(offset : uint, position : uint, value : Number) : void {
  73. vertexVector[offset + position] = value;
  74. }
  75. public function getV(offset : uint, position : uint) : Number {
  76. return vertexVector[offset + position];
  77. }
  78. public function setU(offset : uint, position : uint, value : Number) : void {
  79. uvVector[offset + position] = value;
  80. }
  81. public function getU(offset : uint, position : uint) : Number {
  82. return vertexVector[offset + position];
  83. }
  84. public function resetVector(offset : uint) : void {
  85. vertexVector[offset + 0] = 0;
  86. vertexVector[offset + 1] = 0;
  87. vertexVector[offset + 2] = 0;
  88. vertexVector[offset + 3] = 1;
  89. vertexVector[offset + 4] = 0;
  90. vertexVector[offset + 5] = 0;
  91. vertexVector[offset + 6] = 0;
  92. vertexVector[offset + 7] = 1;
  93. vertexVector[offset + 8] = 0;
  94. vertexVector[offset + 9] = 1;
  95. vertexVector[offset + 10] = 1;
  96. vertexVector[offset + 11] = 0;
  97. uvVector[offset + 0] = 0;
  98. uvVector[offset + 1] = 0;
  99. uvVector[offset + 2] = 1;
  100. uvVector[offset + 3] = 1;
  101. uvVector[offset + 4] = 0;
  102. uvVector[offset + 5] = 1;
  103. uvVector[offset + 6] = 0;
  104. uvVector[offset + 7] = 1;
  105. uvVector[offset + 8] = 1;
  106. uvVector[offset + 9] = 1;
  107. uvVector[offset + 10] = 1;
  108. uvVector[offset + 11] = 1;
  109. }
  110. //UV Info
  111. public var uvIndex : Vector.<uint> = Vector.<uint>([0]);
  112. public var uvInfo : Vector.<Number> = Vector.<Number>([0, 0, 1, 1, 0, 0, 1, 1]);//left, top, right, bottom, offsetX, offsetY, width, height
  113. public function getTextureCoord(index_id : uint, vector : Vector.<Number>) : void {
  114. var index : int = uvIndex.indexOf(index_id);
  115. if(index >= 0){
  116. index = index * 8;
  117. vector[0] = uvInfo[index];
  118. vector[1] = uvInfo[index + 1];
  119. vector[2] = uvInfo[index + 2];
  120. vector[3] = uvInfo[index + 3];
  121. vector[4] = uvInfo[index + 4];
  122. vector[5] = uvInfo[index + 5];
  123. vector[6] = uvInfo[index + 6];
  124. vector[7] = uvInfo[index + 7];
  125. }else{
  126. throw new Error("Can't find the uv info:" + index_id);
  127. }
  128. }
  129. public function setTextureCoord(index_id : uint, vector : Vector.<Number>) : void {
  130. var index : int = uvIndex.indexOf(index_id);
  131. if(index >= 0){
  132. uvInfo[index] = vector[0];
  133. uvInfo[index + 1] = vector[1];
  134. uvInfo[index + 2] = vector[2];
  135. uvInfo[index + 3] = vector[3];
  136. uvInfo[index + 4] = vector[4];
  137. uvInfo[index + 5] = vector[5];
  138. uvInfo[index + 6] = vector[6];
  139. uvInfo[index + 7] = vector[7];
  140. }else{
  141. uvInfo = uvInfo.concat(vector);
  142. uvIndex.push(index_id);
  143. }
  144. }
  145. }
  146. }