/src/away3d/materials/methods/ShadingMethodBase.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 234 lines · 111 code · 32 blank · 91 comment · 6 complexity · a4ac99e98a0a9bf06998cfc832ec6f59 MD5 · raw file

  1. package away3d.materials.methods
  2. {
  3. import away3d.*;
  4. import away3d.cameras.*;
  5. import away3d.core.base.*;
  6. import away3d.core.managers.*;
  7. import away3d.events.*;
  8. import away3d.library.assets.*;
  9. import away3d.materials.compilation.*;
  10. import away3d.materials.passes.*;
  11. import away3d.textures.*;
  12. import flash.display3D.*;
  13. use namespace arcane;
  14. /**
  15. * ShadingMethodBase provides an abstract base method for shading methods, used by compiled passes to compile
  16. * the final shading program.
  17. */
  18. public class ShadingMethodBase extends NamedAssetBase
  19. {
  20. protected var _sharedRegisters:ShaderRegisterData;
  21. protected var _passes:Vector.<MaterialPassBase>;
  22. /**
  23. * Create a new ShadingMethodBase object.
  24. * @param needsNormals Defines whether or not the method requires normals.
  25. * @param needsView Defines whether or not the method requires the view direction.
  26. */
  27. public function ShadingMethodBase() // needsNormals : Boolean, needsView : Boolean, needsGlobalPos : Boolean
  28. {
  29. }
  30. /**
  31. * Initializes the properties for a MethodVO, including register and texture indices.
  32. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  33. */
  34. arcane function initVO(vo:MethodVO):void
  35. {
  36. }
  37. /**
  38. * Initializes unchanging shader constants using the data from a MethodVO.
  39. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  40. */
  41. arcane function initConstants(vo:MethodVO):void
  42. {
  43. }
  44. /**
  45. * The shared registers created by the compiler and possibly used by methods.
  46. */
  47. arcane function get sharedRegisters():ShaderRegisterData
  48. {
  49. return _sharedRegisters;
  50. }
  51. arcane function set sharedRegisters(value:ShaderRegisterData):void
  52. {
  53. _sharedRegisters = value;
  54. }
  55. /**
  56. * Any passes required that render to a texture used by this method.
  57. */
  58. public function get passes():Vector.<MaterialPassBase>
  59. {
  60. return _passes;
  61. }
  62. /**
  63. * Cleans up any resources used by the current object.
  64. */
  65. public function dispose():void
  66. {
  67. }
  68. /**
  69. * Creates a data container that contains material-dependent data. Provided as a factory method so a custom subtype can be overridden when needed.
  70. */
  71. arcane function createMethodVO():MethodVO
  72. {
  73. return new MethodVO();
  74. }
  75. /**
  76. * Resets the compilation state of the method.
  77. */
  78. arcane function reset():void
  79. {
  80. cleanCompilationData();
  81. }
  82. /**
  83. * Resets the method's state for compilation.
  84. * @private
  85. */
  86. arcane function cleanCompilationData():void
  87. {
  88. }
  89. /**
  90. * Get the vertex shader code for this method.
  91. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  92. * @param regCache The register cache used during the compilation.
  93. * @private
  94. */
  95. arcane function getVertexCode(vo:MethodVO, regCache:ShaderRegisterCache):String
  96. {
  97. return "";
  98. }
  99. /**
  100. * Sets the render state for this method.
  101. *
  102. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  103. * @param stage3DProxy The Stage3DProxy object currently used for rendering.
  104. * @private
  105. */
  106. arcane function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
  107. {
  108. }
  109. /**
  110. * Sets the render state for a single renderable.
  111. *
  112. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  113. * @param renderable The renderable currently being rendered.
  114. * @param stage3DProxy The Stage3DProxy object currently used for rendering.
  115. * @param camera The camera from which the scene is currently rendered.
  116. */
  117. arcane function setRenderState(vo:MethodVO, renderable:IRenderable, stage3DProxy:Stage3DProxy, camera:Camera3D):void
  118. {
  119. }
  120. /**
  121. * Clears the render state for this method.
  122. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  123. * @param stage3DProxy The Stage3DProxy object currently used for rendering.
  124. */
  125. arcane function deactivate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
  126. {
  127. }
  128. /**
  129. * A helper method that generates standard code for sampling from a texture using the normal uv coordinates.
  130. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  131. * @param targetReg The register in which to store the sampled colour.
  132. * @param inputReg The texture stream register.
  133. * @param texture The texture which will be assigned to the given slot.
  134. * @param uvReg An optional uv register if coordinates different from the primary uv coordinates are to be used.
  135. * @param forceWrap If true, texture wrapping is enabled regardless of the material setting.
  136. * @return The fragment code that performs the sampling.
  137. */
  138. protected function getTex2DSampleCode(vo:MethodVO, targetReg:ShaderRegisterElement, inputReg:ShaderRegisterElement, texture:TextureProxyBase, uvReg:ShaderRegisterElement = null, forceWrap:String = null):String
  139. {
  140. var wrap:String = forceWrap || (vo.repeatTextures? "wrap" : "clamp");
  141. var filter:String;
  142. var format:String = getFormatStringForTexture(texture);
  143. var enableMipMaps:Boolean = vo.useMipmapping && texture.hasMipMaps;
  144. if (vo.useSmoothTextures)
  145. filter = enableMipMaps? "linear,miplinear" : "linear";
  146. else
  147. filter = enableMipMaps? "nearest,mipnearest" : "nearest";
  148. uvReg ||= _sharedRegisters.uvVarying;
  149. return "tex " + targetReg + ", " + uvReg + ", " + inputReg + " <2d," + filter + "," + format + wrap + ">\n";
  150. }
  151. /**
  152. * A helper method that generates standard code for sampling from a cube texture.
  153. * @param vo The MethodVO object linking this method with the pass currently being compiled.
  154. * @param targetReg The register in which to store the sampled colour.
  155. * @param inputReg The texture stream register.
  156. * @param texture The cube map which will be assigned to the given slot.
  157. * @param uvReg The direction vector with which to sample the cube map.
  158. */
  159. protected function getTexCubeSampleCode(vo:MethodVO, targetReg:ShaderRegisterElement, inputReg:ShaderRegisterElement, texture:TextureProxyBase, uvReg:ShaderRegisterElement):String
  160. {
  161. var filter:String;
  162. var format:String = getFormatStringForTexture(texture);
  163. var enableMipMaps:Boolean = vo.useMipmapping && texture.hasMipMaps;
  164. if (vo.useSmoothTextures)
  165. filter = enableMipMaps? "linear,miplinear" : "linear";
  166. else
  167. filter = enableMipMaps? "nearest,mipnearest" : "nearest";
  168. return "tex " + targetReg + ", " + uvReg + ", " + inputReg + " <cube," + format + filter + ">\n";
  169. }
  170. /**
  171. * Generates a texture format string for the sample instruction.
  172. * @param texture The texture for which to get the format string.
  173. * @return
  174. */
  175. private function getFormatStringForTexture(texture:TextureProxyBase):String
  176. {
  177. switch (texture.format) {
  178. case Context3DTextureFormat.COMPRESSED:
  179. return "dxt1,";
  180. break;
  181. case "compressedAlpha":
  182. return "dxt5,";
  183. break;
  184. default:
  185. return "";
  186. }
  187. }
  188. /**
  189. * Marks the shader program as invalid, so it will be recompiled before the next render.
  190. */
  191. protected function invalidateShaderProgram():void
  192. {
  193. dispatchEvent(new ShadingMethodEvent(ShadingMethodEvent.SHADER_INVALIDATED));
  194. }
  195. /**
  196. * Copies the state from a ShadingMethodBase object into the current object.
  197. */
  198. public function copyFrom(method:ShadingMethodBase):void
  199. {
  200. }
  201. }
  202. }