Skip to content

Commit a3763d1

Browse files
committed
Added setSampler2DBuffer method and willRender override.
1 parent 0d42bb2 commit a3763d1

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

src/gameobjects/shader/Shader.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,22 @@ var Shader = new Class({
361361
this.setShader(key, textures);
362362
},
363363

364+
/**
365+
* Compares the renderMask with the renderFlags to see if this Game Object will render or not.
366+
* Also checks the Game Object against the given Cameras exclusion list.
367+
*
368+
* @method Phaser.GameObjects.Shader#willRender
369+
* @since 3.0.0
370+
*
371+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to check against this Game Object.
372+
*
373+
* @return {boolean} True if the Game Object should be rendered, otherwise false.
374+
*/
375+
willRender: function (camera)
376+
{
377+
return !(!this.renderToTexture || GameObject.RENDER_MASK !== this.renderFlags || (this.cameraFilter !== 0 && (this.cameraFilter & camera.id)));
378+
},
379+
364380
/**
365381
* Changes this Shader so instead of rendering to the display list it renders to a
366382
* WebGL Framebuffer and WebGL Texture instead. This allows you to use the output
@@ -617,12 +633,66 @@ var Shader = new Class({
617633
}
618634
},
619635

636+
/**
637+
* Sets a sampler2D uniform on this shader where the source texture is a WebGLTexture.
638+
*
639+
* This allows you to feed the output from one Shader into another:
640+
*
641+
* ```javascript
642+
* let shader1 = this.add.shader(baseShader1, 0, 0, 512, 512).setRenderToTexture();
643+
* let shader2 = this.add.shader(baseShader2, 0, 0, 512, 512).setRenderToTexture('output');
644+
*
645+
* shader1.setSampler2DBuffer('iChannel0', shader2.glTexture, 512, 512);
646+
* shader2.setSampler2DBuffer('iChannel0', shader1.glTexture, 512, 512);
647+
* ```
648+
*
649+
* In the above code, the result of baseShader1 is fed into Shader2 as the `iChannel0` sampler2D uniform.
650+
* The result of baseShader2 is then fed back into shader1 again, creating a feedback loop.
651+
*
652+
* If you wish to use an image from the Texture Manager as a sampler2D input for this shader,
653+
* see the `Shader.setSampler2D` method.
654+
*
655+
* @method Phaser.GameObjects.Shader#setSampler2DBuffer
656+
* @since 3.19.0
657+
*
658+
* @param {string} uniformKey - The key of the sampler2D uniform to be updated, i.e. `iChannel0`.
659+
* @param {WebGLTexture} texture - A WebGLTexture reference.
660+
* @param {integer} width - The width of the texture.
661+
* @param {integer} height - The height of the texture.
662+
* @param {integer} [textureIndex=0] - The texture index.
663+
* @param {any} [textureData] - Additional texture data.
664+
*
665+
* @return {this} This Shader instance.
666+
*/
667+
setSampler2DBuffer: function (uniformKey, texture, width, height, textureIndex, textureData)
668+
{
669+
if (textureIndex === undefined) { textureIndex = 0; }
670+
if (textureData === undefined) { textureData = {}; }
671+
672+
var uniform = this.uniforms[uniformKey];
673+
674+
uniform.value = texture;
675+
676+
textureData.width = width;
677+
textureData.height = height;
678+
679+
uniform.textureData = textureData;
680+
681+
this._textureCount = textureIndex;
682+
683+
this.initSampler2D(uniform);
684+
685+
return this;
686+
},
687+
620688
/**
621689
* Sets a sampler2D uniform on this shader.
622690
*
623691
* The textureKey given is the key from the Texture Manager cache. You cannot use a single frame
624692
* from a texture, only the full image. Also, lots of shaders expect textures to be power-of-two sized.
625693
*
694+
* If you wish to use another Shader as a sampler2D input for this shader, see the `Shader.setSampler2DBuffer` method.
695+
*
626696
* @method Phaser.GameObjects.Shader#setSampler2D
627697
* @since 3.17.0
628698
*

0 commit comments

Comments
 (0)