/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

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