API Docs for:
Show:

File: ../src/components/frameFX.js

  1. ///@INFO: UNCOMMON
  2. /**
  3. * This component allow to create basic FX applied to the whole scene
  4. * @class FrameFX
  5. * @param {Object} o object with the serialized info
  6. */
  7. function FrameFX(o)
  8. {
  9. this.enabled = true;
  10.  
  11. this.fx = new LS.FXStack( o ? o.fx : null );
  12. this.frame = new LS.RenderFrameContext();
  13. this.frame.use_depth_texture = true;
  14. this.use_antialiasing = false;
  15. this.shader_material = null;
  16.  
  17. if(o)
  18. this.configure(o);
  19. }
  20.  
  21. FrameFX.icon = "mini-icon-fx.png";
  22.  
  23. FrameFX.prototype.configure = function(o)
  24. {
  25. this.enabled = !!o.enabled;
  26. this.use_viewport_size = !!o.use_viewport_size;
  27. this.use_antialiasing = !!o.use_antialiasing;
  28. this.shader_material = o.shader_material;
  29. if(o.fx)
  30. this.fx.configure( o.fx );
  31. if(o.frame)
  32. this.frame.configure( o.frame );
  33. }
  34.  
  35. FrameFX.prototype.serialize = function()
  36. {
  37. return {
  38. object_class: "FrameFX",
  39. enabled: this.enabled,
  40. uid: this.uid,
  41. frame: this.frame.serialize(),
  42. shader_material: this.shader_material,
  43. use_antialiasing: this.use_antialiasing,
  44. use_viewport_size: this.use_viewport_size,
  45. fx: this.fx.serialize()
  46. };
  47. }
  48.  
  49. FrameFX.prototype.getResources = function( res )
  50. {
  51. this.fx.getResources(res);
  52. if(this.shader_material)
  53. res[ this.shader_material ] = true;
  54. return res;
  55. }
  56.  
  57. FrameFX.prototype.onResourceRenamed = function( old_name, new_name, resource )
  58. {
  59. if( this.shader_material == old_name )
  60. this.shader_material = new_name;
  61. else
  62. this.fx.onResourceRenamed( old_name, new_name, resource );
  63. }
  64.  
  65. FrameFX.prototype.addFX = function( name )
  66. {
  67. this.fx.addFX(name);
  68. }
  69.  
  70. FrameFX.prototype.getFX = function(index)
  71. {
  72. return this.fx.getFX( index );
  73. }
  74.  
  75. FrameFX.prototype.moveFX = function( fx, offset )
  76. {
  77. return this.fx.moveFX(fx,offset);
  78. }
  79.  
  80. FrameFX.prototype.removeFX = function( fx )
  81. {
  82. return this.fx.removeFX( fx );
  83. }
  84.  
  85. FrameFX.prototype.onAddedToScene = function( scene )
  86. {
  87. LEvent.bind( scene, "enableFrameContext", this.onBeforeRender, this );
  88. LEvent.bind( scene, "showFrameContext", this.onAfterRender, this );
  89. }
  90.  
  91. FrameFX.prototype.onRemovedFromScene = function( scene )
  92. {
  93. LEvent.unbind( scene, "enableFrameContext", this.onBeforeRender, this );
  94. LEvent.unbind( scene, "showFrameContext", this.onAfterRender, this );
  95. }
  96.  
  97. //hook the RFC
  98. FrameFX.prototype.onBeforeRender = function(e, render_settings)
  99. {
  100. if(!this.enabled)
  101. return;
  102.  
  103. this.enableFrameFBO( render_settings );
  104. }
  105.  
  106. FrameFX.prototype.onAfterRender = function( e, render_settings )
  107. {
  108. if(!this.enabled)
  109. return;
  110. this.showFBO();
  111. }
  112.  
  113. FrameFX.prototype.enableFrameFBO = function( render_settings )
  114. {
  115. if(!this.enabled)
  116. return;
  117.  
  118. this.frame.enable( render_settings );
  119. }
  120.  
  121. FrameFX.prototype.showFBO = function()
  122. {
  123. if(!this.enabled)
  124. return;
  125.  
  126. this.frame.disable();
  127.  
  128. LEvent.trigger( LS.Renderer, "beforeShowFrameContext", this.frame );
  129.  
  130. if(this.shader_material)
  131. {
  132. var material = LS.ResourcesManager.getResource( this.shader_material );
  133. var rendered = false;
  134. if(material && material.constructor === LS.ShaderMaterial )
  135. rendered = material.applyToTexture( this.frame._color_texture );
  136. if(!rendered)
  137. this.frame._color_texture.toViewport(); //fallback in case the shader is missing
  138. return;
  139. }
  140.  
  141. if( this._viewport )
  142. {
  143. gl.setViewport( this._viewport );
  144. this.applyFX();
  145. gl.setViewport( this.frame._fbo._old_viewport );
  146. }
  147. else
  148. this.applyFX();
  149. }
  150.  
  151. FrameFX.prototype.applyFX = function()
  152. {
  153. var color_texture = this.frame._color_texture;
  154. var depth_texture = this.frame._depth_texture;
  155.  
  156. this.fx.apply_fxaa = this.use_antialiasing;
  157. this.fx.filter = this.frame.filter_texture;
  158. this.fx.applyFX( color_texture, null, { depth_texture: depth_texture } );
  159. }
  160.  
  161. LS.registerComponent( FrameFX );