Skip to content

Commit f84f4e6

Browse files
committed
Graphics canvas texture generation
1 parent 8be3ee4 commit f84f4e6

4 files changed

Lines changed: 35 additions & 30 deletions

File tree

v3/src/gameobjects/graphics/Graphics.js

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Commands = require('./Commands');
66
var MATH_CONST = require('../../math/const');
77
var GetValue = require('../../utils/object/GetValue');
88
var CanvasPool = require('../../dom/CanvasPool');
9+
var Camera = require('../../camera/Camera.js');
910

1011
var Graphics = new Class({
1112

@@ -42,9 +43,6 @@ var Graphics = new Class({
4243

4344
this.setDefaultStyles(options);
4445

45-
this.dstRenderTexture = null;
46-
this.dstRenderTarget = null;
47-
4846
var resourceManager = state.game.renderer.resourceManager;
4947

5048
if (resourceManager !== undefined)
@@ -382,41 +380,32 @@ var Graphics = new Class({
382380
return this;
383381
},
384382

385-
createRenderTarget: function (width, height)
386-
{
387-
var resourceManager = this.resourceManager;
388-
if (this.dstRenderTarget === null)
389-
{
390-
if (resourceManager !== undefined)
391-
{
392-
var gl = this.gl;
393-
this.dstRenderTexture = resourceManager.createTexture(0, gl.LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.RGBA, null, width, height);
394-
this.dstRenderTarget = resourceManager.createRenderTarget(width, height, this.dstRenderTexture, null);
395-
state.game.renderer.currentTexture = null; // force rebinding of prev texture
396-
}
397-
else
398-
{
399-
this.dstRenderTexture = CanvasPool.create2D(null, width, height);
400-
this.dstRenderTarget = this.dstRenderTexture.getContext('2d');
401-
}
402-
}
403-
},
404-
405383
generateTexture: function (key, width, height)
406384
{
407385
width = (typeof width === 'number') ? width : this.state.game.config.width;
408386
height = (typeof height === 'number') ? height : this.state.game.config.height;
409387

410388
if (this.gl)
411389
{
412-
this.state.game.textures.create(key, null, width, height);
390+
var texture = this.state.game.textures.create(key, null, width, height);
391+
var glTexture = texture.source[0].glTexture;
392+
var renderTarget = this.resourceManager.createRenderTarget(width, height, glTexture, null);
393+
glTexture.width = width;
394+
glTexture.height = height;
413395
}
414396
else
415397
{
416-
this.state.game.textures.createCanvas(key, width, height);
398+
var texture = this.state.game.textures.createCanvas(key, width, height);
399+
var ctx = texture.source[0].image.getContext('2d');
400+
Graphics.TargetCamera.setViewport(0, 0, width, height);
401+
Graphics.TargetCamera.scrollX = this.x;
402+
Graphics.TargetCamera.scrollY = this.y;
403+
this.renderCanvas(this.state.game.renderer, this, 0, Graphics.TargetCamera, ctx);
417404
}
418405
}
419406

420407
});
421408

409+
Graphics.TargetCamera = new Camera(0, 0, 0, 0);
410+
422411
module.exports = Graphics;

v3/src/gameobjects/graphics/GraphicsCanvasRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var Commands = require('./Commands');
22
var MATH_CONST = require('../../math/const');
33

4-
var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
4+
var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, camera, renderTargetCtx)
55
{
66
if (this.renderMask !== this.renderFlags)
77
{
@@ -16,7 +16,7 @@ var GraphicsCanvasRenderer = function (renderer, src, interpolationPercentage, c
1616
var srcScaleY = src.scaleY;
1717
var srcRotation = src.rotation;
1818
var commandBuffer = src.commandBuffer;
19-
var ctx = renderer.currentContext;
19+
var ctx = renderTargetCtx || renderer.currentContext;
2020
var value;
2121
var lineAlpha = 1.0;
2222
var fillAlpha = 1.0;

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ WebGLRenderer.prototype = {
117117
this.setBlendMode(0);
118118
},
119119

120-
createTexture: function (source)
120+
createTexture: function (source, width, height)
121121
{
122122
var gl = this.gl;
123123
var filter = gl.NEAREST;
@@ -134,7 +134,22 @@ WebGLRenderer.prototype = {
134134
filter = gl.NEAREST;
135135
}
136136

137-
source.glTexture = this.resourceManager.createTexture(
137+
if (typeof width === 'number' && typeof height === 'number')
138+
{
139+
source.glTexture = this.resourceManager.createTexture(
140+
0,
141+
filter,
142+
filter,
143+
gl.CLAMP_TO_EDGE,
144+
gl.CLAMP_TO_EDGE,
145+
gl.RGBA,
146+
null,
147+
width, height
148+
);
149+
}
150+
else
151+
{
152+
source.glTexture = this.resourceManager.createTexture(
138153
0,
139154
filter,
140155
filter,
@@ -143,6 +158,7 @@ WebGLRenderer.prototype = {
143158
gl.RGBA,
144159
source.image
145160
);
161+
}
146162
}
147163

148164
this.currentTexture = null;

v3/src/textures/TextureSource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var TextureSource = function (texture, source, width, height)
123123

124124
if (game.config.renderType === CONST.WEBGL)
125125
{
126-
game.renderer.createTexture(this);
126+
game.renderer.createTexture(this, width, height);
127127
}
128128
};
129129

0 commit comments

Comments
 (0)