/src/away3d/materials/methods/EnvMapAmbientMethod.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 88 lines · 51 code · 14 blank · 23 comment · 0 complexity · 253201f57d0c91a4a49b9330e6b2c683 MD5 · raw file

  1. package away3d.materials.methods
  2. {
  3. import away3d.arcane;
  4. import away3d.core.managers.Stage3DProxy;
  5. import away3d.materials.methods.MethodVO;
  6. import away3d.materials.compilation.ShaderRegisterCache;
  7. import away3d.materials.compilation.ShaderRegisterElement;
  8. import away3d.textures.CubeTextureBase;
  9. use namespace arcane;
  10. /**
  11. * EnvMapDiffuseMethod provides a diffuse shading method that uses a diffuse irradiance environment map to
  12. * approximate global lighting rather than lights.
  13. */
  14. public class EnvMapAmbientMethod extends BasicAmbientMethod
  15. {
  16. private var _cubeTexture:CubeTextureBase;
  17. /**
  18. * Creates a new EnvMapDiffuseMethod object.
  19. * @param envMap The cube environment map to use for the diffuse lighting.
  20. */
  21. public function EnvMapAmbientMethod(envMap:CubeTextureBase)
  22. {
  23. super();
  24. _cubeTexture = envMap;
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. override arcane function initVO(vo:MethodVO):void
  30. {
  31. super.initVO(vo);
  32. vo.needsNormals = true;
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. override public function dispose():void
  38. {
  39. }
  40. /**
  41. * The cube environment map to use for the diffuse lighting.
  42. */
  43. public function get envMap():CubeTextureBase
  44. {
  45. return _cubeTexture;
  46. }
  47. public function set envMap(value:CubeTextureBase):void
  48. {
  49. _cubeTexture = value;
  50. }
  51. /**
  52. * @inheritDoc
  53. */
  54. arcane override function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):void
  55. {
  56. super.activate(vo, stage3DProxy);
  57. stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _cubeTexture.getTextureForStage3D(stage3DProxy));
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. arcane override function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg:ShaderRegisterElement):String
  63. {
  64. var code:String = "";
  65. var cubeMapReg:ShaderRegisterElement = regCache.getFreeTextureReg();
  66. vo.texturesIndex = cubeMapReg.index;
  67. code += getTexCubeSampleCode(vo, targetReg, cubeMapReg, _cubeTexture, _sharedRegisters.normalFragment);
  68. _ambientInputRegister = regCache.getFreeFragmentConstant();
  69. vo.fragmentConstantsIndex = _ambientInputRegister.index;
  70. code += "add " + targetReg + ".xyz, " + targetReg + ".xyz, " + _ambientInputRegister + ".xyz\n";
  71. return code;
  72. }
  73. }
  74. }