-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRS_SimpleLight.js
404 lines (349 loc) · 12.1 KB
/
RS_SimpleLight.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//================================================================
// RS_SimpleLight.js
// ---------------------------------------------------------------
// The MIT License
// Copyright (c) 2022 biud436
// ---------------------------------------------------------------
// Free for commercial and non commercial use.
//================================================================
/*:
* @target MZ
* @plugindesc This plugin allows you to create a light effect. <RS_SimpleLight>
* @author biud436
*
* @help
* ===============================================================
* Detailed Description
* ===============================================================
* This plugin implements a dynamic lighting system that follows the player
* character, creating an immersive lighting effect in your RPG Maker MZ games.
* The light source automatically follows the player and adjusts its direction
* based on the player's facing direction.
*
* ===============================================================
* Features
* ===============================================================
* - Dynamic light that follows the player character
* - Light automatically adjusts based on player's direction
* - Customizable light size/intensity
* - Customizable light color (through script calls)
* - Simple enable/disable commands
*
* ===============================================================
* Plugin Commands (MZ)
* ===============================================================
* This plugin provides two simple commands:
*
* 1. Enable - Turns on the lighting effect
* 2. Disable - Turns off the lighting effect
*
* ===============================================================
* Script Calls
* ===============================================================
* You can use the following script calls to control the light effect:
*
* // Enable/Disable Light
* $gameSystem.setLightProperty("light", true); // Enable
* $gameSystem.setLightProperty("light", false); // Disable
*
* // Adjust Light Size (values between 0.001 and 1.0)
* // Smaller values create larger light areas
* $gameSystem.setLightProperty("size", 0.5);
*
* // Change Light Color (using script calls)
* // Get the filter first
* const filter = SceneManager._scene._spriteset._simpleLightFilter;
*
* // Set to default white light
* filter.setDefaultColorTone();
*
* // Set to red light
* filter.setRedColorTone();
*
* // Set to custom color (RGB values, 0-255)
* filter.setColorTone(255, 180, 120); // Warm orange light
*
* ===============================================================
* Technical Notes
* ===============================================================
* - The plugin uses PIXI.js filters and WebGL shaders to create the lighting effect
* - Light automatically adjusts to screen resolution
* - Performance impact is minimal on most systems
* - Compatible with most other plugins
*
* ===============================================================
* Compatibility
* ===============================================================
* This plugin is compatible with RPG Maker MZ v1.5.0 and higher.
* For RPG Maker MV, use versions prior to v2.0.0.
* ===============================================================
* Version Log
* ===============================================================
* 2016.02.13 (v1.0.0) - First Release
* 2018.12.30 (v1.1.0) :
* - Now it is supported in RPG Maker MV v1.6.1.
* (I've been rewritten shader for RPG Maker MV v1.6.1)
* 2019.02.24 (v1.1.01) :
* - Fixed an issue that is not loaded a save file that you saved before using this plugin.
* 2022.06.06 (v2.0.0-rc1) : RPG Maker MZ v1.5.0 is now supported.
*
* @command enable
* @text Enable
* @desc Enable the lighting effect.
*
* @command disable
* @text Disable
* @desc Disable the lighting effect.
*/
(() => {
"use strict";
let parameters = $plugins.filter(function (i) {
return i.description.contains("<RS_SimpleLight>");
});
// eslint-disable-next-line no-unused-vars
parameters = parameters.length > 0 && parameters[0].parameters;
PIXI.SimpleLightFilter = 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;
const float PI = 3.14159265359;
const float TWO_PI = 6.28318530718;
uniform vec2 u_resolution;
uniform int u_dir;
uniform float u_size;
uniform vec2 u_position;
uniform mat4 u_color;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform vec4 filterArea;
uniform vec4 filterClamp;
vec2 get_player_offset(int d) {
vec2 offset = vec2(0.0);
if(d == 2) {
offset = vec2(0.0, 1.0) * 0.688;
} else if(d == 4) {
offset = vec2(-1.0, 0.0) * 0.792;
} else if(d == 6) {
offset = vec2(1.0, 0.0) * 0.656;
} else if(d == 8) {
offset = vec2(0.0, -1.0) * 0.760;
}
return offset;
}
float get_player_direction(int d) {
float angle = PI;
if(d == 8) {
angle = PI;
} else if(d == 6) {
angle = 0.5235987755982988;
} else if(d == 4) {
angle = 1.5707963267948966;
} else if(d == 2) {
angle = TWO_PI;
}
return angle;
}
// Reference to
// https://thebookofshaders.com/07/
void main(void){
vec2 st = (gl_FragCoord.xy - u_position) / u_resolution;
// st.x *= u_resolution.x / u_resolution.y;
vec4 baseColor = texture2D(uSampler, vTextureCoord);
vec4 color = vec4(1.0);
float d = 0.0;
int dir = u_dir;
vec2 offset = get_player_offset(dir);
vec2 back_offset = u_position * vec2(1.0, 1.0);
st *= mat2(2.0, 0.0, 0.0, 2.0);
st -= back_offset * 2.0;
st -= offset;
int N = 3;
float a = atan(st.x * 1.0, st.y * 1.0) + get_player_direction(dir);
float r = TWO_PI / float(N);
d = cos(floor(0.500 + a / r) * r - a ) * length(st * u_size);
color = vec4(0.1) + vec4( 1.0 - smoothstep(0.128 , 0.378 , d));
color *= u_color;
gl_FragColor = baseColor * vec4(color.rgb, 1.0);
}
`;
PIXI.Filter.call(this, vertexSrc, fragmentSrc, {
u_resolution: [Graphics.width, Graphics.height],
u_dir: 2,
u_size: 1.628,
u_position: [0.5, 0.5],
u_color: [
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0,
],
});
this.enabled = true;
this.resolution = 1;
this.legacy = true;
};
PIXI.SimpleLightFilter.prototype = Object.create(PIXI.Filter.prototype);
PIXI.SimpleLightFilter.prototype.constructor = PIXI.SimpleLightFilter;
PIXI.SimpleLightFilter.prototype.updatePositionX = function () {
const value = $gamePlayer.screenX() / this.uniforms.u_resolution[0];
this.uniforms.u_position[0] = value || 0.0;
};
PIXI.SimpleLightFilter.prototype.updatePositionY = function () {
const value = $gamePlayer.screenY() / this.uniforms.u_resolution[1];
this.uniforms.u_position[1] = value || 0.0;
};
PIXI.SimpleLightFilter.prototype.updatePosition = function () {
this.updatePositionX();
this.updatePositionY();
};
PIXI.SimpleLightFilter.prototype.setResolution = function (options) {
this.uniforms.u_resolution = options;
};
PIXI.SimpleLightFilter.prototype.setDirection = function (dir) {
this.uniforms.u_dir = dir;
};
PIXI.SimpleLightFilter.prototype.setSize = function (value) {
if (value <= 1e-3) value = 1e-3;
if (value >= 1.0) value = 1.0;
this.uniforms.u_size = value;
};
PIXI.SimpleLightFilter.prototype.setDefaultColorTone = function () {
this.uniforms.u_color = [
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
1.0,
];
};
PIXI.SimpleLightFilter.prototype.setRedColorTone = function () {
this.uniforms.u_color = [
0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
1.0,
];
};
/**
* @param {Array} color
*/
PIXI.SimpleLightFilter.prototype.setColorTone = function (red, green, blue) {
const r = parseFloat(red / 255.0);
const g = parseFloat(green / 255.0);
const b = parseFloat(blue / 255.0);
this.uniforms.u_color = [
r,
0.0,
0.0,
0.0,
0.0,
g,
0.0,
0.0,
0.0,
0.0,
b,
0.0,
0.0,
0.0,
0.0,
1.0,
];
};
//=================================================================
// Spriteset_Map
//=================================================================
const alias_Spriteset_Map_createUpperLayer =
Spriteset_Map.prototype.createUpperLayer;
Spriteset_Map.prototype.createUpperLayer = function () {
alias_Spriteset_Map_createUpperLayer.call(this);
this.createSimpleLightFilter();
};
Spriteset_Map.prototype.createSimpleLightFilter = function () {
const isValid = this._simpleLightFilter;
const target = this._baseSprite;
if (!isValid) {
this._simpleLightFilter = new PIXI.SimpleLightFilter();
if (!target.filters) {
target.filters = [];
}
target.filters = [this._simpleLightFilter].concat(target.filters);
if (this._simpleLightFilter) {
this._simpleLightFilter.updatePosition();
}
} else {
if (!target.filters) {
target.filters = [];
}
target.filters = target.filters.filter(function (filter) {
return filter !== isValid;
}, this);
}
};
const alias_Spriteset_Map_update = Spriteset_Map.prototype.update;
Spriteset_Map.prototype.update = function () {
alias_Spriteset_Map_update.call(this);
this.updateSimpleLightFilter();
};
Spriteset_Map.prototype.updateSimpleLightFilter = function () {
if (!$gameSystem) return;
if (!this._simpleLightFilter) return;
const config = $gameSystem._lightProp;
const isValid = $gameSystem.enabledLight();
this._simpleLightFilter.enabled = isValid;
if (isValid) {
this._simpleLightFilter.updatePosition();
this._simpleLightFilter.setSize(config["size"]);
this._simpleLightFilter.setDirection(config["direction"]);
this._simpleLightFilter.setResolution(config["resolution"]);
}
};
// Game_System
const alias_Game_System_initialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () {
alias_Game_System_initialize.call(this);
this.initLightProperty();
};
Game_System.prototype.initLightProperty = function () {
this._lightProp = {
light: false,
size: 1.618,
direction: 2,
resolution: [816, 624],
};
};
Game_System.prototype.updateLightProperty = function () {
if (!this._lightProp) this.initLightProperty();
this.setLightProperty("direction", $gamePlayer.direction());
this.setLightProperty("resolution", [Graphics.width, Graphics.height]);
};
Game_System.prototype.setLightProperty = function (name, value) {
if (!this._lightProp) this.initLightProperty();
this._lightProp[name] = value;
return this._lightProp[name];
};
Game_System.prototype.enabledLight = function () {
if (!this._lightProp) this.initLightProperty();
return this._lightProp["light"] === true;
};
const alias_Game_Map_update = Game_Map.prototype.update;
Game_Map.prototype.update = function (sceneActive) {
alias_Game_Map_update.call(this, sceneActive);
$gameSystem.updateLightProperty();
};
// pluginCommands
const pluginCommands = {
enable() {
$gameSystem.setLightProperty("light", true);
},
disable() {
$gameSystem.setLightProperty("light", false);
},
};
const pluginName = "RS_SimpleLight";
for (const name in pluginCommands) {
PluginManager.registerCommand(pluginName, name, pluginCommands[name]);
}
})();