-
Notifications
You must be signed in to change notification settings - Fork 751
/
HueRotateAttribute.js
142 lines (110 loc) · 3.41 KB
/
HueRotateAttribute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const vertShader = `
#define SHADER_NAME PHASER_MULTI_HUE_VS
precision mediump float;
uniform mat4 uProjectionMatrix;
attribute vec2 inPosition;
attribute vec2 inTexCoord;
attribute float inTexId;
attribute float inSpeed;
varying vec2 outTexCoord;
varying float outTexId;
varying float outSpeed;
void main ()
{
gl_Position = uProjectionMatrix * vec4(inPosition, 1.0, 1.0);
outTexCoord = inTexCoord;
outTexId = inTexId;
outSpeed = inSpeed;
}
`;
const fragShader = `
#define SHADER_NAME PHASER_MULTI_HUE_FS
precision mediump float;
uniform sampler2D uMainSampler[%count%];
uniform float uTime;
varying vec2 outTexCoord;
varying float outTexId;
varying float outSpeed;
void main()
{
vec4 texture;
%forloop%
float c = cos(uTime * outSpeed);
float s = sin(uTime * outSpeed);
mat4 r = mat4(0.299, 0.587, 0.114, 0.0, 0.299, 0.587, 0.114, 0.0, 0.299, 0.587, 0.114, 0.0, 0.0, 0.0, 0.0, 1.0);
mat4 g = mat4(0.701, -0.587, -0.114, 0.0, -0.299, 0.413, -0.114, 0.0, -0.300, -0.588, 0.886, 0.0, 0.0, 0.0, 0.0, 0.0);
mat4 b = mat4(0.168, 0.330, -0.497, 0.0, -0.328, 0.035, 0.292, 0.0, 1.250, -1.050, -0.203, 0.0, 0.0, 0.0, 0.0, 0.0);
mat4 hueRotation = r + g * c + b * s;
gl_FragColor = texture * hueRotation;
}
`;
export default class HueRotateAttributePipeline extends Phaser.Renderer.WebGL.Pipelines.MultiPipeline
{
constructor (game)
{
super({
game,
vertShader,
fragShader,
uniforms: [
'uProjectionMatrix',
'uMainSampler',
'uTime'
],
attributes: [
{
name: 'inPosition',
size: 2
},
{
name: 'inTexCoord',
size: 2
},
{
name: 'inTexId',
size: 1
},
{
name: 'inSpeed',
size: 1
}
]
});
this._time = 0;
}
onPreRender ()
{
this.set1f('uTime', this.game.loop.time);
}
batchQuad (gameObject, x0, y0, x1, y1, x2, y2, x3, y3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect, texture, unit)
{
if (unit === undefined) { unit = this.currentUnit; }
var hasFlushed = false;
if (this.shouldFlush(6))
{
this.flush();
hasFlushed = true;
unit = this.setTexture2D(texture);
}
var speed = gameObject.pipelineData.speed;
this.batchVert(x0, y0, u0, v0, unit, speed);
this.batchVert(x1, y1, u0, v1, unit, speed);
this.batchVert(x2, y2, u1, v1, unit, speed);
this.batchVert(x0, y0, u0, v0, unit, speed);
this.batchVert(x2, y2, u1, v1, unit, speed);
this.batchVert(x3, y3, u1, v0, unit, speed);
return hasFlushed;
}
batchVert (x, y, u, v, unit, speed)
{
var vertexViewF32 = this.vertexViewF32;
var vertexOffset = (this.vertexCount * this.currentShader.vertexComponentCount) - 1;
vertexViewF32[++vertexOffset] = x;
vertexViewF32[++vertexOffset] = y;
vertexViewF32[++vertexOffset] = u;
vertexViewF32[++vertexOffset] = v;
vertexViewF32[++vertexOffset] = unit;
vertexViewF32[++vertexOffset] = speed;
this.vertexCount++;
}
}