Advertisement
Guest User

Untitled

a guest
Aug 30th, 2012
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // =================================================================================================
  2. //
  3. //  Starling Framework
  4. //  Copyright 2012 Gamua OG. All Rights Reserved.
  5. //
  6. //  This program is free software. You can redistribute and/or modify it
  7. //  in accordance with the terms of the accompanying license agreement.
  8. //
  9. // =================================================================================================
  10.  
  11. package starling.filters
  12. {
  13.     import flash.display3D.Context3D;
  14.     import flash.display3D.Context3DProgramType;
  15.     import flash.display3D.Program3D;
  16.    
  17.     import starling.textures.Texture;
  18.  
  19.     public class PixelateFilter extends FragmentFilter
  20.     {
  21.         private var mQuantifiers:Vector.<Number>;
  22.         private var mShaderProgram:Program3D;
  23.        
  24.         /**
  25.          *
  26.          * @param   pixelSize       size of pixel effect
  27.          * @param   stagewidth      width of stage
  28.          * @param   stageheight     height of stage
  29.          */
  30.         public function PixelateFilter(pixelSize:int, stagewidth:int, stageheight:int)
  31.         {
  32.             var xr:Number = 1 / stagewidth;
  33.             var yr:Number = 1 / stageheight;
  34.             mQuantifiers = new <Number>[pixelSize * xr, pixelSize * yr, 1.0,1.0];
  35.         }
  36.        
  37.         public override function dispose():void
  38.         {
  39.             if (mShaderProgram) mShaderProgram.dispose();
  40.             super.dispose();
  41.         }
  42.        
  43.         protected override function createPrograms():void
  44.         {
  45.             var fragmentProgramCode:String =
  46.                 "div ft0, v0, fc0                       \n" +
  47.                 "frc ft1, ft0                           \n" +
  48.                 "sub ft0, ft0, ft1                      \n" +
  49.                 "mul ft0, ft0, fc0                      \n" +
  50.                 "tex oc, ft0, fs0<2d, clamp, nearest>"
  51.            
  52.             mShaderProgram = assembleAgal(fragmentProgramCode);
  53.         }
  54.        
  55.         protected override function activate(pass:int, context:Context3D, texture:Texture):void
  56.         {
  57.             // already set by super class:
  58.             //
  59.             // vertex constants 0-3: mvpMatrix (3D)
  60.             // vertex attribute 0:   vertex position (FLOAT_2)
  61.             // vertex attribute 1:   texture coordinates (FLOAT_2)
  62.             // texture 0:            input texture
  63.            
  64.             context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, mQuantifiers, 1);
  65.             context.setProgram(mShaderProgram);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement