/src/away3d/entities/TextureProjector.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 122 lines · 74 code · 14 blank · 34 comment · 3 complexity · 0c0fd0427a067c62eb7973681ebeaacf MD5 · raw file

  1. package away3d.entities
  2. {
  3. import away3d.arcane;
  4. import away3d.cameras.lenses.PerspectiveLens;
  5. import away3d.containers.ObjectContainer3D;
  6. import away3d.events.LensEvent;
  7. import away3d.library.assets.AssetType;
  8. import away3d.textures.Texture2DBase;
  9. import flash.geom.Matrix3D;
  10. use namespace arcane;
  11. /**
  12. * TextureProjector is an object in the scene that can be used to project textures onto geometry. To do so,
  13. * the object's material must have a ProjectiveTextureMethod method added to it with a TextureProjector object
  14. * passed in the constructor.
  15. * This can be used for various effects apart from acting like a normal projector, such as projecting fake shadows
  16. * unto a surface, the impact of light coming through a stained glass window, ...
  17. *
  18. * @see away3d.materials.methods.ProjectiveTextureMethod
  19. */
  20. public class TextureProjector extends ObjectContainer3D
  21. {
  22. private var _lens:PerspectiveLens;
  23. private var _viewProjectionInvalid:Boolean = true;
  24. private var _viewProjection:Matrix3D = new Matrix3D();
  25. private var _texture:Texture2DBase;
  26. /**
  27. * Creates a new TextureProjector object.
  28. * @param texture The texture to be projected on the geometry. Since any point that is projected out of the range
  29. * of the projector's cone is clamped to the texture's edges, the edges should be entirely neutral.
  30. */
  31. public function TextureProjector(texture:Texture2DBase)
  32. {
  33. _lens = new PerspectiveLens();
  34. _lens.addEventListener(LensEvent.MATRIX_CHANGED, onInvalidateLensMatrix, false, 0, true);
  35. _texture = texture;
  36. _lens.aspectRatio = texture.width/texture.height;
  37. rotationX = -90;
  38. }
  39. /**
  40. * The aspect ratio of the texture or projection. By default this is the same aspect ratio of the texture (width/height)
  41. */
  42. public function get aspectRatio():Number
  43. {
  44. return _lens.aspectRatio;
  45. }
  46. public function set aspectRatio(value:Number):void
  47. {
  48. _lens.aspectRatio = value;
  49. }
  50. /**
  51. * The vertical field of view of the projection, or the angle of the cone.
  52. */
  53. public function get fieldOfView():Number
  54. {
  55. return _lens.fieldOfView;
  56. }
  57. public function set fieldOfView(value:Number):void
  58. {
  59. _lens.fieldOfView = value;
  60. }
  61. public override function get assetType():String
  62. {
  63. return AssetType.TEXTURE_PROJECTOR;
  64. }
  65. /**
  66. * The texture to be projected on the geometry.
  67. * IMPORTANT: Since any point that is projected out of the range of the projector's cone is clamped to the texture's edges,
  68. * the edges should be entirely neutral. Depending on the blend mode, the neutral color is:
  69. * White for MULTIPLY,
  70. * Black for ADD,
  71. * Transparent for MIX
  72. */
  73. public function get texture():Texture2DBase
  74. {
  75. return _texture;
  76. }
  77. public function set texture(value:Texture2DBase):void
  78. {
  79. if (value == _texture)
  80. return;
  81. _texture = value;
  82. }
  83. /**
  84. * The matrix that projects a point in scene space into the texture coordinates.
  85. */
  86. public function get viewProjection():Matrix3D
  87. {
  88. if (_viewProjectionInvalid) {
  89. _viewProjection.copyFrom(inverseSceneTransform);
  90. _viewProjection.append(_lens.matrix);
  91. _viewProjectionInvalid = false;
  92. }
  93. return _viewProjection;
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. override protected function invalidateSceneTransform():void
  99. {
  100. super.invalidateSceneTransform();
  101. _viewProjectionInvalid = true;
  102. }
  103. private function onInvalidateLensMatrix(event:LensEvent):void
  104. {
  105. _viewProjectionInvalid = true;
  106. }
  107. }
  108. }