PageRenderTime 337ms CodeModel.GetById 40ms RepoModel.GetById 21ms app.codeStats 0ms

/src/aerys/minko/render/resource/Context3DResource.as

https://bitbucket.org/HopeSky/mars_nd2d
ActionScript | 329 lines | 275 code | 54 blank | 0 comment | 42 complexity | 099e8c9e6d1587dd65a6ae50f2632ee5 MD5 | raw file
  1. package aerys.minko.render.resource
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display3D.Context3D;
  5. import flash.display3D.IndexBuffer3D;
  6. import flash.display3D.Program3D;
  7. import flash.display3D.VertexBuffer3D;
  8. import flash.display3D.textures.CubeTexture;
  9. import flash.display3D.textures.Texture;
  10. import flash.display3D.textures.TextureBase;
  11. import flash.geom.Rectangle;
  12. public final class Context3DResource
  13. {
  14. private var _context : Context3D = null;
  15. private var _enableErrorChecking : Boolean = false;
  16. private var _rttTarget : TextureBase = null;
  17. private var _rttDepthAndStencil : Boolean = false;
  18. private var _rttAntiAliasing : int = 0;
  19. private var _rttSurfaceSelector : int = 0;
  20. private var _rectangle : Rectangle = null;
  21. private var _depthMask : Boolean = false;
  22. private var _passCompareMode : String = null;
  23. private var _program : Program3D = null;
  24. private var _blendingSource : String = null;
  25. private var _blendingDestination : String = null;
  26. private var _triangleCulling : String = null;
  27. private var _vertexBuffers : Vector.<VertexBuffer3D> = new Vector.<VertexBuffer3D>(8, true);
  28. private var _vertexBuffersOffsets : Vector.<int> = new Vector.<int>(8, true);
  29. private var _vertexBuffersFormats : Vector.<String> = new Vector.<String>(8, true);
  30. private var _textures : Vector.<TextureBase> = new Vector.<TextureBase>(8, true);
  31. private var _colorMaskRed : Boolean = false;
  32. private var _colorMaskGreen : Boolean = false;
  33. private var _colorMaskBlue : Boolean = false;
  34. private var _colorMaskAlpha : Boolean = false;
  35. public function get enabledErrorChecking() : Boolean
  36. {
  37. return _enableErrorChecking;
  38. }
  39. public function set enableErrorChecking(value : Boolean) : void
  40. {
  41. if (value != _enableErrorChecking)
  42. {
  43. _enableErrorChecking = value;
  44. _context.enableErrorChecking = value;
  45. }
  46. }
  47. public function Context3DResource(context : Context3D)
  48. {
  49. _context = context;
  50. }
  51. public function clear(red : Number = 0.0,
  52. green : Number = 0.0,
  53. blue : Number = 0.0,
  54. alpha : Number = 1.0,
  55. depth : Number = 1.0,
  56. stencil : uint = 0,
  57. mask : uint = 0xffffffff) : void
  58. {
  59. _context.clear(red, green, blue, alpha, depth, stencil);
  60. }
  61. public function configureBackBuffer(width : int,
  62. height : int,
  63. antiAlias : int,
  64. enabledDepthAndStencil : Boolean) : Context3DResource
  65. {
  66. _context.configureBackBuffer(width, height, antiAlias, enabledDepthAndStencil);
  67. return this;
  68. }
  69. public function createCubeTexture(size : int,
  70. format : String,
  71. optimizeForRenderToTexture : Boolean) : CubeTexture
  72. {
  73. return _context.createCubeTexture(size, format, optimizeForRenderToTexture);
  74. }
  75. public function createIndexBuffer(numIndices : int) : IndexBuffer3D
  76. {
  77. return _context.createIndexBuffer(numIndices);
  78. }
  79. public function createProgram() : Program3D
  80. {
  81. return _context.createProgram();
  82. }
  83. public function createTexture(width : int,
  84. height : int,
  85. format : String,
  86. optimizeForRenderToTexture : Boolean) : Texture
  87. {
  88. return _context.createTexture(width, height, format, optimizeForRenderToTexture);
  89. }
  90. public function createVertexBuffer(numVertices : int, data32PerVertex : int) : VertexBuffer3D
  91. {
  92. return _context.createVertexBuffer(numVertices, data32PerVertex);
  93. }
  94. public function dispose() : void
  95. {
  96. _context.dispose();
  97. }
  98. public function drawToBitmapData(destination : BitmapData) : Context3DResource
  99. {
  100. _context.drawToBitmapData(destination);
  101. return this;
  102. }
  103. public function drawTriangles(indexBuffer : IndexBuffer3D,
  104. firstIndex : int = 0,
  105. numTriangles : int = -1) : Context3DResource
  106. {
  107. _context.drawTriangles(indexBuffer, firstIndex, numTriangles);
  108. return this;
  109. }
  110. public function present() : Context3DResource
  111. {
  112. _context.present();
  113. return this;
  114. }
  115. public function setBlendFactors(source : String, destination : String) : Context3DResource
  116. {
  117. if (source != _blendingSource || destination != _blendingDestination)
  118. {
  119. _blendingSource = source;
  120. _blendingDestination = destination;
  121. _context.setBlendFactors(_blendingSource, _blendingDestination);
  122. }
  123. return this;
  124. }
  125. public function setColorMask(red : Boolean,
  126. green : Boolean,
  127. blue : Boolean,
  128. alpha : Boolean) : Context3DResource
  129. {
  130. if (red != _colorMaskRed || green != _colorMaskGreen
  131. || blue != _colorMaskBlue || alpha != _colorMaskAlpha)
  132. {
  133. _colorMaskRed = red;
  134. _colorMaskGreen = green;
  135. _colorMaskBlue = blue;
  136. _colorMaskAlpha = alpha;
  137. _context.setColorMask(
  138. _colorMaskRed,
  139. _colorMaskGreen,
  140. _colorMaskBlue,
  141. _colorMaskAlpha
  142. );
  143. }
  144. return this;
  145. }
  146. public function setCulling(triangleCulling : String) : Context3DResource
  147. {
  148. if (triangleCulling != _triangleCulling)
  149. {
  150. _triangleCulling = triangleCulling;
  151. _context.setCulling(triangleCulling);
  152. }
  153. return this;
  154. }
  155. public function setDepthTest(depthMask : Boolean, passCompareMode : String) : Context3DResource
  156. {
  157. if (depthMask != _depthMask || passCompareMode != _passCompareMode)
  158. {
  159. _depthMask = depthMask;
  160. _passCompareMode = passCompareMode;
  161. _context.setDepthTest(_depthMask, _passCompareMode);
  162. }
  163. return this;
  164. }
  165. public function setProgram(program : Program3D) : Context3DResource
  166. {
  167. if (_program != program)
  168. {
  169. _program = program;
  170. _context.setProgram(program);
  171. }
  172. return this;
  173. }
  174. public function setProgramConstantsFromVector(programeType : String,
  175. offset : uint,
  176. values : Vector.<Number>,
  177. numRegisters : int = -1) : Context3DResource
  178. {
  179. _context.setProgramConstantsFromVector(programeType, offset, values, numRegisters);
  180. return this;
  181. }
  182. public function setRenderToBackBuffer() : Context3DResource
  183. {
  184. if (_rttTarget != null)
  185. {
  186. _rttTarget = null;
  187. _context.setRenderToBackBuffer();
  188. }
  189. return this;
  190. }
  191. public function setRenderToTexture(texture : TextureBase,
  192. enableDepthAndStencil : Boolean = false,
  193. antiAlias : int = 0,
  194. surfaceSelector : int = 0) : Context3DResource
  195. {
  196. if (texture != _rttTarget || enableDepthAndStencil != _rttDepthAndStencil
  197. || antiAlias != _rttAntiAliasing || surfaceSelector != _rttSurfaceSelector)
  198. {
  199. _rttTarget = texture;
  200. _rttDepthAndStencil = enableDepthAndStencil;
  201. _rttAntiAliasing = antiAlias;
  202. _rttSurfaceSelector = surfaceSelector;
  203. _context.setRenderToTexture(
  204. texture,
  205. enableDepthAndStencil,
  206. antiAlias,
  207. surfaceSelector
  208. );
  209. }
  210. return this;
  211. }
  212. public function setScissorRectangle(rectangle : Rectangle) : Context3DResource
  213. {
  214. if (_rectangle != rectangle)
  215. {
  216. _rectangle = rectangle;
  217. _context.setScissorRectangle(_rectangle);
  218. }
  219. return this;
  220. }
  221. public function setTextureAt(sampler : uint,
  222. texture : TextureBase) : Context3DResource
  223. {
  224. if (_textures[sampler] != texture)
  225. {
  226. _textures[sampler] = texture;
  227. _context.setTextureAt(sampler, texture);
  228. }
  229. return this;
  230. }
  231. public function setVertexBufferAt(index : int,
  232. vertexBuffer : VertexBuffer3D,
  233. bufferOffset : int = 0,
  234. format : String = 'float4') : Context3DResource
  235. {
  236. if (_vertexBuffers[index] != vertexBuffer
  237. || _vertexBuffersOffsets[index] != bufferOffset
  238. || _vertexBuffersFormats[index] != format)
  239. {
  240. _vertexBuffers[index] = vertexBuffer;
  241. _vertexBuffersOffsets[index] = bufferOffset;
  242. _vertexBuffersFormats[index] = format;
  243. _context.setVertexBufferAt(
  244. index,
  245. vertexBuffer,
  246. bufferOffset,
  247. format
  248. );
  249. }
  250. return this;
  251. }
  252. public function setStencilReferenceValue(refNum : uint = 0,
  253. readMask : uint = 255,
  254. writeMask : uint = 255) : Context3DResource
  255. {
  256. _context.setStencilReferenceValue(refNum, readMask, writeMask);
  257. return this;
  258. }
  259. public function setStencilActions(triangleFace : String = 'frontAndBack',
  260. compareMode : String = 'always',
  261. actionOnBothPass : String = 'keep',
  262. actionOnDepthFail : String = 'keep',
  263. actionOnDepthPassStencilFail : String = 'keep') : Context3DResource
  264. {
  265. _context.setStencilActions(
  266. triangleFace,
  267. compareMode,
  268. actionOnBothPass,
  269. actionOnDepthFail,
  270. actionOnDepthPassStencilFail
  271. );
  272. return this;
  273. }
  274. }
  275. }