Skip to content

Commit 1cb6d52

Browse files
committed
New blitFrame working. Now to adjust when src > target height.
1 parent daadb7d commit 1cb6d52

2 files changed

Lines changed: 268 additions & 7 deletions

File tree

src/gameobjects/rendertexture/RenderTexture.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,7 @@ var RenderTexture = new Class({
10711071

10721072
var util = renderer.pipelines.setUtility();
10731073

1074-
var y = canvasTarget.height - renderTarget.height;
1075-
1076-
util.copyFrameRect(canvasTarget, renderTarget, 0, y, renderTarget.width, renderTarget.height, false);
1074+
util.blitFrame(canvasTarget, renderTarget, 1, false);
10771075
}
10781076
else
10791077
{

src/renderer/webgl/pipelines/UtilityPipeline.js

Lines changed: 267 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,65 @@ var UtilityPipeline = new Class({
104104
}
105105
]);
106106

107-
// x, y, u, v (x/y in NDC)
108-
config.vertices = new Float32Array([
107+
/**
108+
* Vertices array layout:
109+
*
110+
* -1, 1 B----C 1, 1
111+
* 0, 1 | /| 1, 1
112+
* | / |
113+
* | / |
114+
* |/ |
115+
* -1, -1 A----D 1, -1
116+
* 0, 0 1, 0
117+
*
118+
* A = -1, -1 (pos) and 0, 0 (uv)
119+
* B = -1, 1 (pos) and 0, 1 (uv)
120+
* C = 1, 1 (pos) and 1, 1 (uv)
121+
* D = 1, -1 (pos) and 1, 0 (uv)
122+
*
123+
* First tri: A, B, C
124+
* Second tri: A, C, D
125+
*
126+
* Array index:
127+
*
128+
* 0 = Tri 1 - Vert A - x pos
129+
* 1 = Tri 1 - Vert A - y pos
130+
* 2 = Tri 1 - Vert A - uv u
131+
* 3 = Tri 1 - Vert A - uv v
132+
*
133+
* 4 = Tri 1 - Vert B - x pos
134+
* 5 = Tri 1 - Vert B - y pos
135+
* 6 = Tri 1 - Vert B - uv u
136+
* 7 = Tri 1 - Vert B - uv v
137+
*
138+
* 8 = Tri 1 - Vert C - x pos
139+
* 9 = Tri 1 - Vert C - y pos
140+
* 10 = Tri 1 - Vert C - uv u
141+
* 11 = Tri 1 - Vert C - uv v
142+
*
143+
* 12 = Tri 2 - Vert A - x pos
144+
* 13 = Tri 2 - Vert A - y pos
145+
* 14 = Tri 2 - Vert A - uv u
146+
* 15 = Tri 2 - Vert A - uv v
147+
*
148+
* 16 = Tri 2 - Vert C - x pos
149+
* 17 = Tri 2 - Vert C - y pos
150+
* 18 = Tri 2 - Vert C - uv u
151+
* 19 = Tri 2 - Vert C - uv v
152+
*
153+
* 20 = Tri 2 - Vert D - x pos
154+
* 21 = Tri 2 - Vert D - y pos
155+
* 22 = Tri 2 - Vert D - uv u
156+
* 23 = Tri 2 - Vert D - uv v
157+
*/
158+
config.vertices = [
109159
-1, -1, 0, 0,
110160
-1, 1, 0, 1,
111161
1, 1, 1, 1,
112162
-1, -1, 0, 0,
113163
1, 1, 1, 1,
114164
1, -1, 1, 0
115-
]);
165+
];
116166

117167
config.batchSize = 1;
118168

@@ -320,12 +370,75 @@ var UtilityPipeline = new Class({
320370
gl.bindTexture(gl.TEXTURE_2D, null);
321371
},
322372

373+
/**
374+
* Copy the `source` Render Target to the `target` Render Target.
375+
*
376+
* The difference with this copy is that no resizing takes place. If the `source`
377+
* Render Target is larger than the `target` then only a portion the same size as
378+
* the `target` dimensions is copied across.
379+
*
380+
* You can optionally set the brightness factor of the copy.
381+
*
382+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#blitFrame
383+
* @since 3.50.0
384+
*
385+
* @param {Phaser.Renderer.WebGL.RenderTarget} source - The source Render Target.
386+
* @param {Phaser.Renderer.WebGL.RenderTarget} target - The target Render Target.
387+
* @param {number} [brightness=1] - The brightness value applied to the frame copy.
388+
* @param {boolean} [clear=true] - Clear the target before copying?
389+
* @param {boolean} [clearAlpha=true] - Clear the alpha channel when running `gl.clear` on the target?
390+
*/
391+
blitFrame: function (source, target, brightness, clear, clearAlpha)
392+
{
393+
if (brightness === undefined) { brightness = 1; }
394+
if (clear === undefined) { clear = true; }
395+
if (clearAlpha === undefined) { clearAlpha = true; }
396+
397+
var gl = this.gl;
398+
399+
this.set1i('uMainSampler', 0, this.copyShader);
400+
this.set1f('uBrightness', brightness, this.copyShader);
401+
402+
gl.activeTexture(gl.TEXTURE0);
403+
gl.bindTexture(gl.TEXTURE_2D, source.texture);
404+
405+
gl.viewport(0, 0, source.width, source.height);
406+
gl.bindFramebuffer(gl.FRAMEBUFFER, target.framebuffer);
407+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target.texture, 0);
408+
409+
if (clear)
410+
{
411+
if (clearAlpha)
412+
{
413+
gl.clearColor(0, 0, 0, 0);
414+
}
415+
else
416+
{
417+
gl.clearColor(0, 0, 0, 1);
418+
}
419+
420+
gl.clear(gl.COLOR_BUFFER_BIT);
421+
}
422+
423+
this.setVerticesFromTarget(source, target);
424+
425+
gl.bufferData(gl.ARRAY_BUFFER, this.vertexData, gl.STATIC_DRAW);
426+
gl.drawArrays(gl.TRIANGLES, 0, 6);
427+
428+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
429+
gl.bindTexture(gl.TEXTURE_2D, null);
430+
431+
this.resetVertices();
432+
},
433+
323434
/**
324435
* Binds the `source` Render Target and then copies a section of it to the `target` Render Target.
325436
*
326437
* This method is extremely fast because it uses `gl.copyTexSubImage2D` and doesn't
327438
* require the use of any shaders. Remember the coordinates are given in standard WebGL format,
328-
* where x and y specify the lower-left corner of the section, not the top-left.
439+
* where x and y specify the lower-left corner of the section, not the top-left. Also, the
440+
* copy entirely replaces the contents of the target texture, no 'merging' or 'blending' takes
441+
* place.
329442
*
330443
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#copyFrameRect
331444
* @since 3.50.0
@@ -577,6 +690,156 @@ var UtilityPipeline = new Class({
577690
var fbo = this.renderer.currentFramebuffer;
578691

579692
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
693+
},
694+
695+
/**
696+
* Resets the quad vertices to their default values.
697+
*
698+
* The quad is used by all shaders of the Utility Pipeline.
699+
*
700+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#resetVertices
701+
* @since 3.50.0
702+
*/
703+
resetVertices: function ()
704+
{
705+
this.vertexViewF32.set([ -1, -1, 0, 0, -1, 1, 0, 1, 1, 1, 1, 1, -1, -1, 0, 0, 1, 1, 1, 1, 1, -1, 1, 0 ]);
706+
},
707+
708+
/**
709+
* Set the UV values for the 6 vertices that make up the quad used by the shaders
710+
* in the Utility Pipeline.
711+
*
712+
* Be sure to call `resetVertices` once you have finished manipulating the UV coordinates.
713+
*
714+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#resetVertices
715+
* @since 3.50.0
716+
*
717+
* @param {number} uA - The u value of vertex A.
718+
* @param {number} vA - The v value of vertex A.
719+
* @param {number} uB - The u value of vertex B.
720+
* @param {number} vB - The v value of vertex B.
721+
* @param {number} uC - The u value of vertex C.
722+
* @param {number} vC - The v value of vertex C.
723+
* @param {number} uD - The u value of vertex D.
724+
* @param {number} vD - The v value of vertex D.
725+
*/
726+
setUVs: function (uA, vA, uB, vB, uC, vC, uD, vD)
727+
{
728+
var vertexViewF32 = this.vertexViewF32;
729+
730+
vertexViewF32[2] = uA;
731+
vertexViewF32[3] = vA;
732+
vertexViewF32[6] = uB;
733+
vertexViewF32[7] = vB;
734+
vertexViewF32[10] = uC;
735+
vertexViewF32[11] = vC;
736+
vertexViewF32[14] = uA;
737+
vertexViewF32[15] = vA;
738+
vertexViewF32[18] = uC;
739+
vertexViewF32[19] = vC;
740+
vertexViewF32[22] = uD;
741+
vertexViewF32[23] = vD;
742+
},
743+
744+
/**
745+
* Sets the vertices of the quad used by the shaders in the Utility Pipeline
746+
* so that they correctly adjust the texture coordinates for a blit frame effect.
747+
*
748+
* Be sure to call `resetVertices` once you have finished manipulating the UV coordinates.
749+
*
750+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#setVerticesFromTarget
751+
* @since 3.50.0
752+
*
753+
* @param {Phaser.Renderer.WebGL.RenderTarget} source - The source Render Target.
754+
* @param {Phaser.Renderer.WebGL.RenderTarget} target - The target Render Target.
755+
*/
756+
setVerticesFromTarget: function (source, target)
757+
{
758+
var diff = (target.height / source.height);
759+
760+
if (diff > 0.5)
761+
{
762+
diff = 0.5 - (diff - 0.5);
763+
}
764+
else
765+
{
766+
diff = 0.5 + (0.5 - diff);
767+
}
768+
769+
console.log('setVerticesFromTarget UVs', 'game', source.height, 'rt', target.height, diff);
770+
771+
this.setUVs(0, diff, 0, 1 + diff, 1, 1 + diff, 1, diff);
772+
773+
if (target.height >= source.height)
774+
{
775+
console.log('Adjusting Quad Y position');
776+
777+
778+
}
779+
},
780+
781+
/**
782+
* Horizontally flips the UV coordinates of the quad used by the shaders in this
783+
* Utility Pipeline.
784+
*
785+
* Be sure to call `resetVertices` once you have finished manipulating the UV coordinates.
786+
*
787+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#flipX
788+
* @since 3.50.0
789+
*/
790+
flipX: function ()
791+
{
792+
this.setUVs(1, 0, 1, 1, 0, 1, 0, 0);
793+
},
794+
795+
/**
796+
* Vertically flips the UV coordinates of the quad used by the shaders in this
797+
* Utility Pipeline.
798+
*
799+
* Be sure to call `resetVertices` once you have finished manipulating the UV coordinates.
800+
*
801+
* @method Phaser.Renderer.WebGL.Pipelines.UtilityPipeline#flipY
802+
* @since 3.50.0
803+
*/
804+
flipY: function ()
805+
{
806+
this.setUVs(0, 1, 0, 0, 1, 0, 1, 1);
807+
},
808+
809+
setAPos: function (x, y)
810+
{
811+
var vertexViewF32 = this.vertexViewF32;
812+
813+
vertexViewF32[0] = x;
814+
vertexViewF32[1] = y;
815+
vertexViewF32[12] = x;
816+
vertexViewF32[13] = y;
817+
},
818+
819+
setBPos: function (x, y)
820+
{
821+
var vertexViewF32 = this.vertexViewF32;
822+
823+
vertexViewF32[4] = x;
824+
vertexViewF32[5] = y;
825+
},
826+
827+
setCPos: function (x, y)
828+
{
829+
var vertexViewF32 = this.vertexViewF32;
830+
831+
vertexViewF32[8] = x;
832+
vertexViewF32[9] = y;
833+
vertexViewF32[16] = x;
834+
vertexViewF32[17] = y;
835+
},
836+
837+
setDPos: function (x, y)
838+
{
839+
var vertexViewF32 = this.vertexViewF32;
840+
841+
vertexViewF32[20] = x;
842+
vertexViewF32[21] = y;
580843
}
581844

582845
});

0 commit comments

Comments
 (0)