Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //////////////////// DEFINITION ////////////////////
  3. pc.extend(pc, function () {
  4.  
  5.     /**
  6.      * @name pc.MyEffect
  7.      * @class Implements the MyEffect post processing effect
  8.      * @constructor Creates new instance of MyEffect
  9.      * @extends pc.PostEffect
  10.      * @param {app.graphicsDevice} graphicsDevice The graphics device of the application
  11.      * @param {width} width
  12.      * @param {height} height
  13.      */
  14.     var MyEffect = function (graphicsDevice, width, height) {
  15. /*
  16.          var width = graphicsDevice.width;
  17.          var height = graphicsDevice.height;
  18. */
  19.         // Shaders
  20.         var attributes = {
  21.             aPosition: pc.SEMANTIC_POSITION
  22.         };
  23.  
  24.         var passThroughVert = [
  25.             "attribute vec2 aPosition;",
  26.             "",
  27.             "varying vec2 vUv0;",
  28.             "",
  29.             "void main(void)",
  30.             "{",
  31.             "    gl_Position = vec4(aPosition, 0.0, 1.0);",
  32.             "    vUv0 = (aPosition + 1.0) * 0.5;",
  33.             "}"
  34.         ].join("\n");
  35.  
  36.         var fragment1 = [
  37.             "varying vec2 vUv0;",
  38.             "",
  39.             "uniform sampler2D inputTexture;",
  40.             "",
  41.             "void main(void)",
  42.             "{",
  43.             // Look up the original image color.
  44.             "    vec4 color = texture2D(inputTexture, vUv0);",
  45.             "    gl_FragColor = color;",
  46.             "}"
  47.         ].join("\n");
  48.  
  49.         var fragment2 = [
  50.             "varying vec2 vUv0;",
  51.             "",
  52.             "uniform sampler2D inputTexture;",
  53.             "",
  54.             "void main(void)",
  55.             "{",
  56.             // Look up the original image color.
  57.             "    vec4 color = texture2D(inputTexture, vUv0);",
  58.             "    gl_FragColor = color;",
  59.             "}"
  60.         ].join("\n");
  61.  
  62.         this.shader1 = new pc.Shader(graphicsDevice, {
  63.             attributes: attributes,
  64.             vshader: passThroughVert,
  65.             fshader: fragment1
  66.         });
  67.  
  68.         this.shader2 = new pc.Shader(graphicsDevice, {
  69.             attributes: attributes,
  70.             vshader: passThroughVert,
  71.             fshader: fragment2
  72.         });
  73.  
  74.         // Render targets
  75.         this.targets = [];
  76.         for (var i = 0; i < 1; i++) {
  77.             var colorBuffer = new pc.Texture(graphicsDevice, {
  78.                 format: pc.PIXELFORMAT_R8_G8_B8,
  79.                 width: width >> 1,
  80.                 height: height >> 1
  81.             });
  82.             colorBuffer.minFilter = pc.FILTER_LINEAR;
  83.             colorBuffer.magFilter = pc.FILTER_LINEAR;
  84.             colorBuffer.addressU = pc.ADDRESS_CLAMP_TO_EDGE;
  85.             colorBuffer.addressV = pc.ADDRESS_CLAMP_TO_EDGE;
  86.             var target = new pc.RenderTarget(graphicsDevice, colorBuffer, { depth: false });
  87.  
  88.             this.targets.push(target);
  89.         }
  90.  
  91.     };
  92.  
  93.     MyEffect.prototype = pc.extend(MyEffect.prototype, {
  94.         // inputTarget is a RenderTarget
  95.         // outputTarget is a RenderTarget
  96.         render: function (inputTarget, outputTarget, rect) {
  97.             var device = this.device;
  98.             var scope = device.scope;
  99.  
  100.             // Pass 1: draw the scene into rendertarget 1
  101.             scope.resolve("inputTexture").setValue(inputTarget.colorBuffer);
  102.             pc.drawFullscreenQuad(device, this.targets[0], this.vertexBuffer, this.shader1);
  103.  
  104.             // Pass 2: draw the scene into rendertarget 2
  105.             scope.resolve("inputTexture").setValue(this.targets[0].colorBuffer);
  106.             pc.drawFullscreenQuad(device, outputTarget, this.vertexBuffer, this.shader2, rect);
  107.         }
  108.     });
  109.  
  110.     return {
  111.         MyEffect: MyEffect
  112.     };
  113. }());
  114.  
  115. //////////////////// BLOOM SCRIPT DEFINITION ////////////////////
  116. var BloomScript = pc.createScript('BloomScript');
  117.  
  118. // Initialize BloomScript, not to be confused with MyEffect
  119. BloomScript.prototype.initialize = function() {
  120.     this.effect = new pc.MyEffect(this.app.graphicsDevice, 1000, 1000);
  121.  
  122.     // When attribute is changed in editor
  123.     this.on('attr', function(name, value, prev) {
  124.         this.effect[name] = value;
  125.     });
  126. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement