Advertisement
Guest User

Pixelate Post Effect Playcanvas

a guest
Jul 18th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------------- POST EFFECT DEFINITION------------------------//
  2. pc.extend(pc, function () {
  3.     // Constructor - Creates an instance of our post effect
  4.     var PixelatePostEffect = function (graphicsDevice, vs, fs) {
  5.         // this is the shader definition for our effect
  6.         this.shader = new pc.Shader(graphicsDevice, {
  7.             attributes: {
  8.                 aPosition: pc.SEMANTIC_POSITION
  9.             },
  10.             vshader: [
  11.                 "attribute vec2 aPosition;",
  12.                 "",
  13.                 "varying vec2 vUv0;",
  14.                 "",
  15.                 "void main(void)",
  16.                 "{",
  17.                 "    gl_Position = vec4(aPosition, 0.0, 1.0);",
  18.                 "    vUv0 = (aPosition.xy + 1.0) * 0.5;",
  19.                 "}"
  20.             ].join("\n"),
  21.             fshader: [
  22.                 "precision " + graphicsDevice.precision + " float;",
  23.                 "",
  24.                 "uniform vec2 uResolution;",
  25.                 "uniform float uPixelize;",
  26.                 "uniform sampler2D uColorBuffer;",
  27.                 "",
  28.                 "varying vec2 vUv0;",
  29.                 "",
  30.                 "void main() {",
  31.                 "    vec2 uv = gl_FragCoord.xy / uResolution.xy;",
  32.                 "    vec2 div = vec2(uResolution.x * uPixelize / uResolution.y, uPixelize);",
  33.                 "    uv = floor(uv * div)/div;",
  34.                 "    vec4 color = texture2D(uColorBuffer, uv);",
  35.                 "    gl_FragColor = color;",
  36.                 "}"
  37.             ].join("\n")
  38.         });
  39.  
  40.         this.resolution = new Float32Array(2);
  41.         this.pixelize = 100;
  42.     };
  43.  
  44.     // Our effect must derive from pc.PostEffect
  45.     PixelatePostEffect = pc.inherits(PixelatePostEffect, pc.PostEffect);
  46.  
  47.     PixelatePostEffect.prototype = pc.extend(PixelatePostEffect.prototype, {
  48.         // Every post effect must implement the render method which
  49.         // sets any parameters that the shader might require and
  50.         // also renders the effect on the screen
  51.         render: function (inputTarget, outputTarget, rect) {
  52.             var device = this.device;
  53.             var scope = device.scope;
  54.  
  55.             // Set the input render target to the shader. This is the image rendered from our camera
  56.             scope.resolve("uColorBuffer").setValue(inputTarget.colorBuffer);
  57.            
  58.             this.resolution[0] = inputTarget.width;
  59.             this.resolution[1] = inputTarget.height;
  60.             scope.resolve("uResolution").setValue(this.resolution);      
  61.            
  62.             scope.resolve("uPixelize").setValue(this.pixelize);
  63.            
  64.             // Draw a full screen quad on the output target. In this case the output target is the screen.
  65.             // Drawing a full screen quad will run the shader that we defined above
  66.             pc.drawFullscreenQuad(device, outputTarget, this.vertexBuffer, this.shader, rect);
  67.         }
  68.     });
  69.  
  70.     return {
  71.         PixelatePostEffect: PixelatePostEffect
  72.     };
  73. }());
  74.  
  75.  
  76. //--------------- SCRIPT DEFINITION------------------------//
  77. var PostEffectPixelate = pc.createScript('PostEffectPixelate');
  78.  
  79. PostEffectPixelate.attributes.add('pixelize', {
  80.     type: 'number',
  81.     default: 100
  82. });
  83.  
  84. // initialize code called once per entity
  85. PostEffectPixelate.prototype.initialize = function() {
  86.     var effect = new pc.PixelatePostEffect(this.app.graphicsDevice);
  87.    
  88.     // add the effect to the camera's postEffects queue
  89.     var queue = this.entity.camera.postEffects;
  90.     queue.addEffect(effect);
  91.    
  92.     // when the script is enabled add our effect to the camera's postEffects queue
  93.     this.on('enable', function () {
  94.         queue.addEffect(effect, false);
  95.     });
  96.    
  97.     // when the script is disabled remove our effect from the camera's postEffects queue
  98.     this.on('disable', function () {
  99.         queue.removeEffect(effect);
  100.     });
  101.    
  102.     this.on('attr:pixelize', function (value, prev) {
  103.         this.pixelize = value;
  104.     });
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement