/src/away3d/materials/methods/EnvMapMethod.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 145 lines · 96 code · 19 blank · 30 comment · 10 complexity · d688179283ce69391ed55c5af7dbf780 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.CubeTextureBase;
  8. import away3d.textures.Texture2DBase;
  9. import flash.display3D.Context3D;
  10. use namespace arcane;
  11. /**
  12. * EnvMapMethod provides a material method to perform reflection mapping using cube maps.
  13. */
  14. public class EnvMapMethod extends EffectMethodBase
  15. {
  16. private var _cubeTexture:CubeTextureBase;
  17. private var _alpha:Number;
  18. private var _mask:Texture2DBase;
  19. /**
  20. * Creates an EnvMapMethod object.
  21. * @param envMap The environment map containing the reflected scene.
  22. * @param alpha The reflectivity of the surface.
  23. */
  24. public function EnvMapMethod(envMap:CubeTextureBase, alpha:Number = 1)
  25. {
  26. super();
  27. _cubeTexture = envMap;
  28. _alpha = alpha;
  29. }
  30. /**
  31. * An optional texture to modulate the reflectivity of the surface.
  32. */
  33. public function get mask():Texture2DBase
  34. {
  35. return _mask;
  36. }
  37. public function set mask(value:Texture2DBase):void
  38. {
  39. if (Boolean(value) != Boolean(_mask) ||
  40. (value && _mask && (value.hasMipMaps != _mask.hasMipMaps || value.format != _mask.format))) {
  41. invalidateShaderProgram();
  42. }
  43. _mask = value;
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. override arcane function initVO(vo:MethodVO):void
  49. {
  50. vo.needsNormals = true;
  51. vo.needsView = true;
  52. vo.needsUV = _mask != null;
  53. }
  54. /**
  55. * The cubic environment map containing the reflected scene.
  56. */
  57. public function get envMap():CubeTextureBase
  58. {
  59. return _cubeTexture;
  60. }
  61. public function set envMap(value:CubeTextureBase):void
  62. {
  63. _cubeTexture = value;
  64. }
  65. /**
  66. * @inheritDoc
  67. */
  68. override public function dispose():void
  69. {
  70. }
  71. /**
  72. * The reflectivity of the surface.
  73. */
  74. public function get alpha():Number
  75. {
  76. return _alpha;
  77. }
  78. public function set alpha(value:Number):void
  79. {
  80. _alpha = value;
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. arcane override function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
  86. {
  87. var context:Context3D = stage3DProxy._context3D;
  88. vo.fragmentData[vo.fragmentConstantsIndex] = _alpha;
  89. context.setTextureAt(vo.texturesIndex, _cubeTexture.getTextureForStage3D(stage3DProxy));
  90. if (_mask)
  91. context.setTextureAt(vo.texturesIndex + 1, _mask.getTextureForStage3D(stage3DProxy));
  92. }
  93. /**
  94. * @inheritDoc
  95. */
  96. arcane override function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
  97. {
  98. var dataRegister:ShaderRegisterElement = regCache.getFreeFragmentConstant();
  99. var temp:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
  100. var code:String = "";
  101. var cubeMapReg:ShaderRegisterElement = regCache.getFreeTextureReg();
  102. vo.texturesIndex = cubeMapReg.index;
  103. vo.fragmentConstantsIndex = dataRegister.index*4;
  104. regCache.addFragmentTempUsages(temp, 1);
  105. var temp2:ShaderRegisterElement = regCache.getFreeFragmentVectorTemp();
  106. // r = I - 2(I.N)*N
  107. code += "dp3 " + temp + ".w, " + _sharedRegisters.viewDirFragment + ".xyz, " + _sharedRegisters.normalFragment + ".xyz \n" +
  108. "add " + temp + ".w, " + temp + ".w, " + temp + ".w \n" +
  109. "mul " + temp + ".xyz, " + _sharedRegisters.normalFragment + ".xyz, " + temp + ".w \n" +
  110. "sub " + temp + ".xyz, " + temp + ".xyz, " + _sharedRegisters.viewDirFragment + ".xyz \n" +
  111. getTexCubeSampleCode(vo, temp, cubeMapReg, _cubeTexture, temp) +
  112. "sub " + temp2 + ".w, " + temp + ".w, fc0.x \n" + // -.5
  113. "kil " + temp2 + ".w\n" + // used for real time reflection mapping - if alpha is not 1 (mock texture) kil output
  114. "sub " + temp + ", " + temp + ", " + targetReg + " \n";
  115. if (_mask) {
  116. var maskReg:ShaderRegisterElement = regCache.getFreeTextureReg();
  117. code += getTex2DSampleCode(vo, temp2, maskReg, _mask, _sharedRegisters.uvVarying) +
  118. "mul " + temp + ", " + temp2 + ", " + temp + "\n";
  119. }
  120. code += "mul " + temp + ", " + temp + ", " + dataRegister + ".x \n" +
  121. "add " + targetReg + ", " + targetReg + ", " + temp + " \n";
  122. regCache.removeFragmentTempUsage(temp);
  123. return code;
  124. }
  125. }
  126. }