-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
RS_GhostEffect.js
298 lines (250 loc) · 9.17 KB
/
RS_GhostEffect.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//================================================================
// RS_GhostEffect.js
// ---------------------------------------------------------------
// The MIT License
// Copyright (c) 2019 biud436
// ---------------------------------------------------------------
// Free for commercial and non commercial use.
//================================================================
/*:
* @plugindesc This plugin allows you to implement the character effect like as ghost. <RS_GhostEffect>
* @author biud436
*
* @param Uniform
*
* @param lifeTime
* @parent Uniform
* @type number
* @desc All of the effects has a lifeTime, After that time, the effect pattern will be changed.
* @default 100
* @min 1
*
* @param threshold
* @parent Uniform
* @type number
* @desc if threshold is higher, the effect can decrease.
* @default 0.7
* @decimals 2
* @min 0.10
* @max 1.00
*
* @param xoffset
* @parent Uniform
* @type number
* @desc When the value approaches 0.0, it would look like the paper burning.
* @default 0.07
* @decimals 2
*
* @help
*
* Note that this plugin only works in WebGL mode.
*
* =======================================================
* Scripts Call
* =======================================================
* In the event command called 'Set Move Route',
*
* To activate ghost mode, you call the following script code:
* this.ghostModeOn();
*
* To disable ghost mode, you call the following script code:
* this.ghostModeOff();
*
* =======================================================
* Plugin Commands
* =======================================================
* All of the effects has a lifeTime.
* After that time, the effect pattern will be changed.
* The time unit is milliseconds,
* so setting it to 100 will result in 0.1 second.
*
* GhostEffect lifetime 100
*
* If the threshold is close to 1, the effect may be reduced.
*
* GhostEffect threshold 0.7
*
* When the value approaches 0.0, it would look like the paper burning.
*
* GhostEffect xoffset 0.07
*
* =======================================================
* Version Log
* =======================================================
* 2019.01.19 (v1.0.0) - First Release.
* 2023.08.12 (v1.0.1) - ES6 Refactoring and tested in RMMV 1.6.2 (nw v0.55.0)
*/
(() => {
'use strict';
const RS = window.RS || {};
RS.GhostEffect = RS.GhostEffect || {};
let parameters = $plugins.filter(i => {
return i.description.contains('<RS_GhostEffect>');
});
parameters = parameters.length > 0 && parameters[0].parameters;
RS.GhostEffect.Params = RS.GhostEffect.Params || {};
RS.GhostEffect.Params.lifeTime = parseInt(parameters.lifeTime || 100, 10);
RS.GhostEffect.Params.threshold = parseFloat(parameters.threshold || 0.7);
RS.GhostEffect.Params.xoffset = parseFloat(parameters.xoffset || 0.07);
//============================================================================
// PIXI.GhostEffect
//============================================================================
PIXI.GhostEffect = function () {
const vertexSrc = [
'attribute vec2 aVertexPosition;',
'attribute vec2 aTextureCoord;',
'uniform mat3 projectionMatrix;',
'varying vec2 vTextureCoord;',
'void main(void){',
' gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);',
' vTextureCoord = aTextureCoord;',
'}',
].join('\n');
const fragmentSrc = `
precision mediump float;
uniform vec2 u_scale;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform vec2 dimensions;
uniform float u_xoffset;
uniform vec4 filterArea;
uniform vec4 filterClamp;
void main(void){
vec4 baseColor = texture2D(uSampler, vTextureCoord);
vec2 vRec = (vTextureCoord * filterArea.xy) / dimensions;
vRec -= 0.5;
vRec *= u_scale;
vRec += 0.5;
vRec.x += u_xoffset;
vec4 recColor = texture2D(uSampler, vRec);
gl_FragColor = baseColor * recColor;
}
`;
PIXI.Filter.call(this, vertexSrc, fragmentSrc);
this.uniforms.dimensions = new Float32Array(2);
this.uniforms.u_scale = [0.5, 0.5];
this.uniforms.u_xoffset = 0.07;
this._effectVal = 0;
this._time = performance.now();
this.enabled = true;
this.resolution = 1;
};
PIXI.GhostEffect.prototype = Object.create(PIXI.Filter.prototype);
PIXI.GhostEffect.prototype.constructor = PIXI.GhostEffect;
PIXI.GhostEffect.prototype.apply = function (
filterManager,
input,
output,
clear
) {
this.uniforms.dimensions[0] = input.sourceFrame.width;
this.uniforms.dimensions[1] = input.sourceFrame.height;
filterManager.applyFilter(this, input, output, clear);
};
PIXI.GhostEffect.prototype.updateEffect = function () {
const isInvalidUpdateEffect =
performance.now() - this._time < RS.GhostEffect.Params.lifeTime;
if (isInvalidUpdateEffect) return;
this._effectVal = Math.random();
if (this._effectVal > RS.GhostEffect.Params.threshold) {
this._effectVal = RS.GhostEffect.Params.threshold;
}
this.uniforms.u_scale[0] = this._effectVal;
this.uniforms.u_scale[1] = this._effectVal;
this.uniforms.u_xoffset = RS.GhostEffect.Params.xoffset;
this._time = performance.now();
};
//============================================================================
// Game_CharacterBase
//============================================================================
const alias_Game_CharacterBase_initMembers =
Game_CharacterBase.prototype.initMembers;
Game_CharacterBase.prototype.initMembers = function () {
alias_Game_CharacterBase_initMembers.call(this);
this._isGhost = false;
};
Game_CharacterBase.prototype.isGhost = function () {
return this._isGhost;
};
Game_CharacterBase.prototype.ghostModeOn = function () {
this._isGhost = true;
};
Game_CharacterBase.prototype.ghostModeOff = function () {
this._isGhost = false;
};
Game_Player.prototype.ghostModeOn = function () {
this._isGhost = true;
this._followers.forEach(follower => {
follower.ghostModeOn();
});
};
Game_Player.prototype.ghostModeOff = function () {
this._isGhost = false;
this._followers.forEach(follower => {
follower.ghostModeOff();
});
};
//============================================================================
// Sprite_Character
//============================================================================
const alias_Sprite_Character_initialize =
Sprite_Character.prototype.initialize;
Sprite_Character.prototype.initialize = function (character) {
alias_Sprite_Character_initialize.call(this, character);
this.createGhostEffect();
};
const alias_Sprite_Character_update = Sprite_Character.prototype.update;
Sprite_Character.prototype.update = function () {
alias_Sprite_Character_update.call(this);
this.updateGhostEffect();
};
Sprite_Character.prototype.createGhostEffect = function () {
const isValid = this._GhostEffect;
if (!isValid) {
this._GhostEffect = new PIXI.GhostEffect();
if (!this.filters) {
this.filters = [];
}
this.filters = [this._GhostEffect].concat(this.filters);
} else {
if (!this.filters) {
this.filters = [];
}
this.filters = this.filters.filter(function (filter) {
return filter !== isValid;
}, this);
}
};
Sprite_Character.prototype.updateGhostEffect = function () {
if (!$gameSystem) return;
if (!this._GhostEffect) return;
if (!this._character) return;
const isValid = this._character.isGhost();
this._GhostEffect.enabled = isValid;
if (isValid) {
this._GhostEffect.updateEffect();
}
};
const alias_Game_Interpreter_pluginCommand =
Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
alias_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === 'GhostEffect') {
switch (args[0]) {
case 'lifetime':
RS.GhostEffect.Params.lifeTime = Number(args[1] || 100);
break;
case 'threshold':
RS.GhostEffect.Params.threshold = parseFloat(
args[1] || 0.7
);
break;
case 'xoffset':
RS.GhostEffect.Params.xoffset = parseFloat(args[1] || 0.07);
break;
default:
break;
}
}
};
})();