/src/away3d/materials/DefaultMaterialBase.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 350 lines · 222 code · 49 blank · 79 comment · 14 complexity · 6a5babbee75599b784f0b66f20ed5d59 MD5 · raw file

  1. package away3d.materials
  2. {
  3. import away3d.arcane;
  4. import away3d.cameras.Camera3D;
  5. import away3d.core.managers.Stage3DProxy;
  6. import away3d.materials.methods.BasicAmbientMethod;
  7. import away3d.materials.methods.BasicDiffuseMethod;
  8. import away3d.materials.methods.BasicNormalMethod;
  9. import away3d.materials.methods.BasicSpecularMethod;
  10. import away3d.materials.methods.EffectMethodBase;
  11. import away3d.materials.methods.ShadowMapMethodBase;
  12. import away3d.materials.passes.DefaultScreenPass;
  13. import away3d.textures.Texture2DBase;
  14. import flash.display3D.Context3D;
  15. import flash.geom.ColorTransform;
  16. use namespace arcane;
  17. /**
  18. * DefaultMaterialBase forms an abstract base class for the default materials provided by Away3D and use methods
  19. * to define their appearance.
  20. */
  21. public class DefaultMaterialBase extends MaterialBase
  22. {
  23. protected var _screenPass : DefaultScreenPass;
  24. private var _alphaBlending : Boolean;
  25. /**
  26. * Creates a new DefaultMaterialBase object.
  27. */
  28. public function DefaultMaterialBase()
  29. {
  30. super();
  31. addPass(_screenPass = new DefaultScreenPass(this));
  32. }
  33. /**
  34. * The minimum alpha value for which pixels should be drawn. This is used for transparency that is either
  35. * invisible or entirely opaque, often used with textures for foliage, etc.
  36. * Recommended values are 0 to disable alpha, or 0.5 to create smooth edges. Default value is 0 (disabled).
  37. */
  38. public function get alphaThreshold() : Number
  39. {
  40. return _screenPass.diffuseMethod.alphaThreshold;
  41. }
  42. public function set alphaThreshold(value : Number) : void
  43. {
  44. _screenPass.diffuseMethod.alphaThreshold = value;
  45. _depthPass.alphaThreshold = value;
  46. _distancePass.alphaThreshold = value;
  47. }
  48. arcane override function activateForDepth(stage3DProxy : Stage3DProxy, camera : Camera3D, distanceBased : Boolean = false, textureRatioX : Number = 1, textureRatioY : Number = 1) : void
  49. {
  50. if (distanceBased) {
  51. _distancePass.alphaMask = _screenPass.diffuseMethod.texture;
  52. }
  53. else {
  54. _depthPass.alphaMask = _screenPass.diffuseMethod.texture;
  55. }
  56. super.activateForDepth(stage3DProxy, camera, distanceBased, textureRatioX, textureRatioY);
  57. }
  58. public function get specularLightSources() : uint
  59. {
  60. return _screenPass.specularLightSources;
  61. }
  62. public function set specularLightSources(value : uint) : void
  63. {
  64. _screenPass.specularLightSources = value;
  65. }
  66. public function get diffuseLightSources() : uint
  67. {
  68. return _screenPass.diffuseLightSources;
  69. }
  70. public function set diffuseLightSources(value : uint) : void
  71. {
  72. _screenPass.diffuseLightSources = value;
  73. }
  74. /**
  75. * The ColorTransform object to transform the colour of the material with.
  76. */
  77. public function get colorTransform() : ColorTransform
  78. {
  79. return _screenPass.colorTransform;
  80. }
  81. public function set colorTransform(value : ColorTransform) : void
  82. {
  83. _screenPass.colorTransform = value;
  84. }
  85. /**
  86. * @inheritDoc
  87. */
  88. override public function get requiresBlending() : Boolean
  89. {
  90. return super.requiresBlending || _alphaBlending || (_screenPass.colorTransform && _screenPass.colorTransform.alphaMultiplier < 1);
  91. }
  92. /**
  93. * The method to perform ambient shading. Note that shading methods cannot
  94. * be reused across materials.
  95. */
  96. public function get ambientMethod() : BasicAmbientMethod
  97. {
  98. return _screenPass.ambientMethod;
  99. }
  100. public function set ambientMethod(value : BasicAmbientMethod) : void
  101. {
  102. _screenPass.ambientMethod = value;
  103. }
  104. /**
  105. * The method to render shadows cast on this surface. Note that shading methods can not
  106. * be reused across materials.
  107. */
  108. public function get shadowMethod() : ShadowMapMethodBase
  109. {
  110. return _screenPass.shadowMethod;
  111. }
  112. public function set shadowMethod(value : ShadowMapMethodBase) : void
  113. {
  114. _screenPass.shadowMethod = value;
  115. }
  116. /**
  117. * The method to perform diffuse shading. Note that shading methods can not
  118. * be reused across materials.
  119. */
  120. public function get diffuseMethod() : BasicDiffuseMethod
  121. {
  122. return _screenPass.diffuseMethod;
  123. }
  124. public function set diffuseMethod(value : BasicDiffuseMethod) : void
  125. {
  126. _screenPass.diffuseMethod = value;
  127. }
  128. /**
  129. * The method to generate the (tangent-space) normal. Note that shading methods can not
  130. * be reused across materials.
  131. */
  132. public function get normalMethod() : BasicNormalMethod
  133. {
  134. return _screenPass.normalMethod;
  135. }
  136. public function set normalMethod(value : BasicNormalMethod) : void
  137. {
  138. _screenPass.normalMethod = value;
  139. }
  140. /**
  141. * The method to perform specular shading. Note that shading methods can not
  142. * be reused across materials.
  143. */
  144. public function get specularMethod() : BasicSpecularMethod
  145. {
  146. return _screenPass.specularMethod;
  147. }
  148. public function set specularMethod(value : BasicSpecularMethod) : void
  149. {
  150. _screenPass.specularMethod = value;
  151. }
  152. /**
  153. * Adds a shading method to the end of the shader. Note that shading methods can
  154. * not be reused across materials.
  155. */
  156. public function addMethod(method : EffectMethodBase) : void
  157. {
  158. _screenPass.addMethod(method);
  159. }
  160. public function get numMethods() : int
  161. {
  162. return _screenPass.numMethods;
  163. }
  164. public function hasMethod(method : EffectMethodBase) : Boolean
  165. {
  166. return _screenPass.hasMethod(method);
  167. }
  168. public function getMethodAt(index : int) : EffectMethodBase
  169. {
  170. return _screenPass.getMethodAt(index);
  171. }
  172. /**
  173. * Adds a shading method to the end of a shader, at the specified index amongst
  174. * the methods in that section of the shader. Note that shading methods can not
  175. * be reused across materials.
  176. */
  177. public function addMethodAt(method : EffectMethodBase, index : int) : void
  178. {
  179. _screenPass.addMethodAt(method, index);
  180. }
  181. public function removeMethod(method : EffectMethodBase) : void
  182. {
  183. _screenPass.removeMethod(method);
  184. }
  185. /**
  186. * @inheritDoc
  187. */
  188. override public function set mipmap(value : Boolean) : void
  189. {
  190. if (_mipmap == value) return;
  191. super.mipmap = value;
  192. }
  193. /**
  194. * The tangent space normal map to influence the direction of the surface for each texel.
  195. */
  196. public function get normalMap() : Texture2DBase
  197. {
  198. return _screenPass.normalMap;
  199. }
  200. public function set normalMap(value : Texture2DBase) : void
  201. {
  202. _screenPass.normalMap = value;
  203. }
  204. /**
  205. * A specular map that defines the strength of specular reflections for each texel in the red channel, and the gloss factor in the green channel.
  206. * You can use SpecularBitmapTexture if you want to easily set specular and gloss maps from greyscale images, but prepared images are preffered.
  207. */
  208. public function get specularMap() : Texture2DBase
  209. {
  210. return _screenPass.specularMethod.texture;
  211. }
  212. public function set specularMap(value : Texture2DBase) : void
  213. {
  214. if (_screenPass.specularMethod) _screenPass.specularMethod.texture = value;
  215. else throw new Error("No specular method was set to assign the specularGlossMap to");
  216. }
  217. /**
  218. * The sharpness of the specular highlight.
  219. */
  220. public function get gloss() : Number
  221. {
  222. return _screenPass.specularMethod? _screenPass.specularMethod.gloss : 0;
  223. }
  224. public function set gloss(value : Number) : void
  225. {
  226. if (_screenPass.specularMethod) _screenPass.specularMethod.gloss = value;
  227. }
  228. /**
  229. * The strength of the ambient reflection.
  230. */
  231. public function get ambient() : Number
  232. {
  233. return _screenPass.ambientMethod.ambient;
  234. }
  235. public function set ambient(value : Number) : void
  236. {
  237. _screenPass.ambientMethod.ambient = value;
  238. }
  239. /**
  240. * The overall strength of the specular reflection.
  241. */
  242. public function get specular() : Number
  243. {
  244. return _screenPass.specularMethod? _screenPass.specularMethod.specular : 0;
  245. }
  246. public function set specular(value : Number) : void
  247. {
  248. if (_screenPass.specularMethod) _screenPass.specularMethod.specular = value;
  249. }
  250. /**
  251. * The colour of the ambient reflection.
  252. */
  253. public function get ambientColor() : uint
  254. {
  255. return _screenPass.ambientMethod.ambientColor;
  256. }
  257. public function set ambientColor(value : uint) : void
  258. {
  259. _screenPass.ambientMethod.ambientColor = value;
  260. }
  261. /**
  262. * The colour of the specular reflection.
  263. */
  264. public function get specularColor() : uint
  265. {
  266. return _screenPass.specularMethod.specularColor;
  267. }
  268. public function set specularColor(value : uint) : void
  269. {
  270. _screenPass.specularMethod.specularColor = value;
  271. }
  272. /**
  273. * Indicate whether or not the material has transparency. If binary transparency is sufficient, for
  274. * example when using textures of foliage, consider using alphaThreshold instead.
  275. */
  276. public function get alphaBlending() : Boolean
  277. {
  278. return _alphaBlending;
  279. }
  280. public function set alphaBlending(value : Boolean) : void
  281. {
  282. _alphaBlending = value;
  283. }
  284. /**
  285. * @inheritDoc
  286. */
  287. arcane override function updateMaterial(context : Context3D) : void
  288. {
  289. if (_screenPass._passesDirty) {
  290. clearPasses();
  291. if (_screenPass._passes) {
  292. var len : uint = _screenPass._passes.length;
  293. for (var i : uint = 0; i < len; ++i)
  294. addPass(_screenPass._passes[i]);
  295. }
  296. addPass(_screenPass);
  297. _screenPass._passesDirty = false;
  298. }
  299. }
  300. }
  301. }