PageRenderTime 410ms CodeModel.GetById 211ms app.highlight 16ms RepoModel.GetById 180ms app.codeStats 0ms

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