PageRenderTime 797ms CodeModel.GetById 515ms app.highlight 6ms RepoModel.GetById 150ms app.codeStats 0ms

/src/away3d/materials/methods/BasicNormalMethod.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 119 lines | 71 code | 14 blank | 34 comment | 9 complexity | ef309f27fd78042176dcc7547ebb803f MD5 | raw file
  1package away3d.materials.methods
  2{
  3	import away3d.arcane;
  4	import away3d.core.managers.Stage3DProxy;
  5	import away3d.materials.compilation.ShaderRegisterCache;
  6	import away3d.materials.compilation.ShaderRegisterElement;
  7	import away3d.textures.Texture2DBase;
  8	
  9	use namespace arcane;
 10
 11	/**
 12	 * BasicNormalMethod is the default method for standard tangent-space normal mapping.
 13	 */
 14	public class BasicNormalMethod extends ShadingMethodBase
 15	{
 16		private var _texture:Texture2DBase;
 17		private var _useTexture:Boolean;
 18		protected var _normalTextureRegister:ShaderRegisterElement;
 19
 20		/**
 21		 * Creates a new BasicNormalMethod object.
 22		 */
 23		public function BasicNormalMethod()
 24		{
 25			super();
 26		}
 27
 28		/**
 29		 * @inheritDoc
 30		 */
 31		override arcane function initVO(vo:MethodVO):void
 32		{
 33			vo.needsUV = Boolean(_texture);
 34		}
 35
 36		/**
 37		 * Indicates whether or not this method outputs normals in tangent space. Override for object-space normals.
 38		 */
 39		arcane function get tangentSpace():Boolean
 40		{
 41			return true;
 42		}
 43		
 44		/**
 45		 * Indicates if the normal method output is not based on a texture (if not, it will usually always return true)
 46		 * Override if subclasses are different.
 47		 */
 48		arcane function get hasOutput():Boolean
 49		{
 50			return _useTexture;
 51		}
 52
 53		/**
 54		 * @inheritDoc
 55		 */
 56		override public function copyFrom(method:ShadingMethodBase):void
 57		{
 58			normalMap = BasicNormalMethod(method).normalMap;
 59		}
 60
 61		/**
 62		 * The texture containing the normals per pixel.
 63		 */
 64		public function get normalMap():Texture2DBase
 65		{
 66			return _texture;
 67		}
 68		
 69		public function set normalMap(value:Texture2DBase):void
 70		{
 71			if (Boolean(value) != _useTexture ||
 72				(value && _texture && (value.hasMipMaps != _texture.hasMipMaps || value.format != _texture.format))) {
 73				invalidateShaderProgram();
 74			}
 75			_useTexture = Boolean(value);
 76			_texture = value;
 77		}
 78
 79		/**
 80		 * @inheritDoc
 81		 */
 82		arcane override function cleanCompilationData():void
 83		{
 84			super.cleanCompilationData();
 85			_normalTextureRegister = null;
 86		}
 87
 88		/**
 89		 * @inheritDoc
 90		 */
 91		override public function dispose():void
 92		{
 93			if (_texture)
 94				_texture = null;
 95		}
 96
 97
 98		/**
 99		 * @inheritDoc
100		 */
101		arcane override function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
102		{
103			if (vo.texturesIndex >= 0)
104				stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
105		}
106
107		/**
108		 * @inheritDoc
109		 */
110		arcane function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
111		{
112			_normalTextureRegister = regCache.getFreeTextureReg();
113			vo.texturesIndex = _normalTextureRegister.index;
114			return getTex2DSampleCode(vo, targetReg, _normalTextureRegister, _texture) +
115				"sub " + targetReg + ".xyz, " + targetReg + ".xyz, " + _sharedRegisters.commons + ".xxx	\n" +
116				"nrm " + targetReg + ".xyz, " + targetReg + ".xyz							\n";
117		}
118	}
119}