Skip to content

Commit 42c3fc5

Browse files
committed
Better normal map handling
1 parent 66ee2ac commit 42c3fc5

1 file changed

Lines changed: 211 additions & 5 deletions

File tree

src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

Lines changed: 211 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ var ForwardDiffuseLightPipeline = new Class({
3636
config.fragShader = ShaderSourceFS.replace('%LIGHT_COUNT%', LIGHT_COUNT.toString());
3737

3838
TextureTintPipeline.call(this, config);
39+
40+
/**
41+
* Default normal map texture to use.
42+
*
43+
* @name Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#defaultNormalMap
44+
* @type {Phaser.Texture.Frame}
45+
* @private
46+
* @since 3.11.0
47+
*/
48+
this.defaultNormalMap;
49+
},
50+
51+
/**
52+
* Called when the Game has fully booted and the Renderer has finished setting up.
53+
*
54+
* By this stage all Game level systems are now in place and you can perform any final
55+
* tasks that the pipeline may need that relied on game systems such as the Texture Manager.
56+
*
57+
* @method Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#boot
58+
* @override
59+
* @since 3.11.0
60+
*/
61+
boot: function ()
62+
{
63+
this.defaultNormalMap = this.game.textures.getFrame('__DEFAULT');
3964
},
4065

4166
/**
@@ -134,6 +159,181 @@ var ForwardDiffuseLightPipeline = new Class({
134159
return this;
135160
},
136161

162+
/**
163+
* Generic function for batching a textured quad
164+
*
165+
* @method Phaser.Renderer.WebGL.Pipelines.TextureTintPipeline#batchTexture
166+
* @since 3.0.0
167+
*
168+
* @param {Phaser.GameObjects.GameObject} gameObject - Source GameObject
169+
* @param {WebGLTexture} texture - Raw WebGLTexture associated with the quad
170+
* @param {integer} textureWidth - Real texture width
171+
* @param {integer} textureHeight - Real texture height
172+
* @param {number} srcX - X coordinate of the quad
173+
* @param {number} srcY - Y coordinate of the quad
174+
* @param {number} srcWidth - Width of the quad
175+
* @param {number} srcHeight - Height of the quad
176+
* @param {number} scaleX - X component of scale
177+
* @param {number} scaleY - Y component of scale
178+
* @param {number} rotation - Rotation of the quad
179+
* @param {boolean} flipX - Indicates if the quad is horizontally flipped
180+
* @param {boolean} flipY - Indicates if the quad is vertically flipped
181+
* @param {number} scrollFactorX - By which factor is the quad affected by the camera horizontal scroll
182+
* @param {number} scrollFactorY - By which factor is the quad effected by the camera vertical scroll
183+
* @param {number} displayOriginX - Horizontal origin in pixels
184+
* @param {number} displayOriginY - Vertical origin in pixels
185+
* @param {number} frameX - X coordinate of the texture frame
186+
* @param {number} frameY - Y coordinate of the texture frame
187+
* @param {number} frameWidth - Width of the texture frame
188+
* @param {number} frameHeight - Height of the texture frame
189+
* @param {integer} tintTL - Tint for top left
190+
* @param {integer} tintTR - Tint for top right
191+
* @param {integer} tintBL - Tint for bottom left
192+
* @param {integer} tintBR - Tint for bottom right
193+
* @param {number} tintEffect - The tint effect (0 for additive, 1 for replacement)
194+
* @param {number} uOffset - Horizontal offset on texture coordinate
195+
* @param {number} vOffset - Vertical offset on texture coordinate
196+
* @param {Phaser.Cameras.Scene2D.Camera} camera - Current used camera
197+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentTransformMatrix - Parent container
198+
*/
199+
batchTexture: function (
200+
gameObject,
201+
texture,
202+
textureWidth, textureHeight,
203+
srcX, srcY,
204+
srcWidth, srcHeight,
205+
scaleX, scaleY,
206+
rotation,
207+
flipX, flipY,
208+
scrollFactorX, scrollFactorY,
209+
displayOriginX, displayOriginY,
210+
frameX, frameY, frameWidth, frameHeight,
211+
tintTL, tintTR, tintBL, tintBR, tintEffect,
212+
uOffset, vOffset,
213+
camera,
214+
parentTransformMatrix)
215+
{
216+
if (!this.active)
217+
{
218+
return;
219+
}
220+
221+
this.renderer.setPipeline(this);
222+
223+
var normalTexture;
224+
225+
if (gameObject.texture)
226+
{
227+
normalTexture = gameObject.texture.dataSource[gameObject.frame.sourceIndex];
228+
}
229+
else if (gameObject.tileset)
230+
{
231+
normalTexture = gameObject.tileset.image.dataSource[0];
232+
}
233+
234+
if (!normalTexture)
235+
{
236+
normalTexture = this.defaultNormalMap;
237+
}
238+
239+
this.setTexture2D(normalTexture.glTexture, 1);
240+
241+
var camMatrix = this._tempMatrix1;
242+
var spriteMatrix = this._tempMatrix2;
243+
var calcMatrix = this._tempMatrix3;
244+
245+
var width = srcWidth;
246+
var height = srcHeight;
247+
248+
var x = -displayOriginX;
249+
var y = -displayOriginY;
250+
251+
// Invert the flipY if this is a RenderTexture
252+
flipY = flipY ^ (texture.isRenderTexture ? 1 : 0);
253+
254+
if (flipX)
255+
{
256+
width *= -1;
257+
x += srcWidth;
258+
}
259+
260+
if (flipY)
261+
{
262+
height *= -1;
263+
y += srcHeight;
264+
}
265+
266+
if (camera.roundPixels)
267+
{
268+
x |= 0;
269+
y |= 0;
270+
}
271+
272+
var xw = x + width;
273+
var yh = y + height;
274+
275+
spriteMatrix.applyITRS(srcX, srcY, rotation, scaleX, scaleY);
276+
277+
camMatrix.copyFrom(camera.matrix);
278+
279+
if (parentTransformMatrix)
280+
{
281+
// Multiply the camera by the parent matrix
282+
camMatrix.multiplyWithOffset(parentTransformMatrix, -camera.scrollX * scrollFactorX, -camera.scrollY * scrollFactorY);
283+
284+
// Undo the camera scroll
285+
spriteMatrix.e = srcX;
286+
spriteMatrix.f = srcY;
287+
288+
// Multiply by the Sprite matrix, store result in calcMatrix
289+
camMatrix.multiply(spriteMatrix, calcMatrix);
290+
}
291+
else
292+
{
293+
spriteMatrix.e -= camera.scrollX * scrollFactorX;
294+
spriteMatrix.f -= camera.scrollY * scrollFactorY;
295+
296+
// Multiply by the Sprite matrix, store result in calcMatrix
297+
camMatrix.multiply(spriteMatrix, calcMatrix);
298+
}
299+
300+
var tx0 = x * calcMatrix.a + y * calcMatrix.c + calcMatrix.e;
301+
var ty0 = x * calcMatrix.b + y * calcMatrix.d + calcMatrix.f;
302+
303+
var tx1 = x * calcMatrix.a + yh * calcMatrix.c + calcMatrix.e;
304+
var ty1 = x * calcMatrix.b + yh * calcMatrix.d + calcMatrix.f;
305+
306+
var tx2 = xw * calcMatrix.a + yh * calcMatrix.c + calcMatrix.e;
307+
var ty2 = xw * calcMatrix.b + yh * calcMatrix.d + calcMatrix.f;
308+
309+
var tx3 = xw * calcMatrix.a + y * calcMatrix.c + calcMatrix.e;
310+
var ty3 = xw * calcMatrix.b + y * calcMatrix.d + calcMatrix.f;
311+
312+
if (camera.roundPixels)
313+
{
314+
tx0 |= 0;
315+
ty0 |= 0;
316+
317+
tx1 |= 0;
318+
ty1 |= 0;
319+
320+
tx2 |= 0;
321+
ty2 |= 0;
322+
323+
tx3 |= 0;
324+
ty3 |= 0;
325+
}
326+
327+
var u0 = (frameX / textureWidth) + uOffset;
328+
var v0 = (frameY / textureHeight) + vOffset;
329+
var u1 = (frameX + frameWidth) / textureWidth + uOffset;
330+
var v1 = (frameY + frameHeight) / textureHeight + vOffset;
331+
332+
this.setTexture2D(texture, 0);
333+
334+
this.batchVertices(tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect);
335+
},
336+
137337
/**
138338
* Sets the Game Objects normal map as the active texture.
139339
*
@@ -144,19 +344,25 @@ var ForwardDiffuseLightPipeline = new Class({
144344
*/
145345
setNormalMap: function (gameObject)
146346
{
147-
if (!this.active || !gameObject || !gameObject.texture)
347+
if (!this.active || !gameObject)
148348
{
149349
return;
150350
}
151351

152-
// var normalTexture = tilemapLayer.tileset.image.dataSource[0];
153-
var normalTexture = gameObject.texture.dataSource[gameObject.frame.sourceIndex];
352+
var normalTexture;
154353

155-
if (normalTexture)
354+
if (gameObject.texture)
156355
{
157-
this.setTexture2D(normalTexture.glTexture, 1);
356+
normalTexture = gameObject.texture.dataSource[gameObject.frame.sourceIndex];
158357
}
159358

359+
if (!normalTexture)
360+
{
361+
normalTexture = this.defaultNormalMap;
362+
}
363+
364+
this.setTexture2D(normalTexture.glTexture, 1);
365+
160366
this.renderer.setPipeline(gameObject.defaultPipeline);
161367
},
162368

0 commit comments

Comments
 (0)