PageRenderTime 667ms CodeModel.GetById 539ms app.highlight 9ms RepoModel.GetById 116ms app.codeStats 1ms

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