Smooth Gradient for Terrax Lighting

nelsonwilliam

Warper
Member
Joined
May 20, 2014
Messages
1
Reaction score
1
First Language
PT-BR
Primarily Uses

Smooth Gradient for Terrax Lighting


by Nelson William




IntroductionThis is a tiny plugin that smooths the lights in Terrax Lighting by slightly changing the way that gradients are drawn.


Screenshots





As you can see, all lights are a bit smaller so you'll have to tweak yours a little bit.


Also, the difference from default brightness and highest brightness (the "B99" notetag) becomes smaller, but I don't know how to improve it yet.


How to Use


Plug-and-play, just make sure to place it below Terrax Lighting in the Plugin Manager.


Requirements


As expected, it requires the Terrax Lighting...


It was made for the version 1.4.2 of Terrax Lighting (for RPG Maker MV 1.3 and above). I don't know if it works with the Pixi 3 version.

Script

//=============================================================================
// Nelson William's Smooth Gradient for Terrax Lighting
// TerraxLighting_SmoothGradient.js
// Version: 1.0.1
//=============================================================================

//=============================================================================
/*:
* @plugindesc v1.0.0 Smoothes Terrax Lighting's gradients used in lights.
* @author Nelson William
* @help
* TerraxLighting is required, of course.
* Please, place this script below TerraxLighting.
*
* Free to use in commercial and non-commercial games.
* Credit is optional.
*/
//=============================================================================

/* CHANGELOG
v 1.0.0 - Release
v 1.0.1 - Fixed brightness being always changed to 100 (for all colors)
*/

var Imported = Imported || {};
Imported.TerraxLighting_SmoothGradient = true;

function ColorUtil_RgbToHsv () {
var rr, gg, bb,
r = arguments[0] / 255,
g = arguments[1] / 255,
b = arguments[2] / 255,
h, s,
v = Math.max(r, g, b),
diff = v - Math.min(r, g, b),
diffc = function(c){
return (v - c) / 6 / diff + 1 / 2;
};

if (diff == 0) {
h = s = 0;
} else {
s = diff / v;
rr = diffc(r);
gg = diffc(g);
bb = diffc(b);

if (r === v) {
h = bb - gg;
}else if (g === v) {
h = (1 / 3) + rr - bb;
}else if (b === v) {
h = (2 / 3) + gg - rr;
}
if (h < 0) {
h += 1;
}else if (h > 1) {
h -= 1;
}
}
return {
h: Math.round(h * 360),
s: Math.round(s * 100),
v: Math.round(v * 100)
};
}

function ColorUtil_HsvToRgb(h, s, v) {
h /= 360;
s /= 100;
v /= 100;

var r, g, b, i, f, p, q, t;
if (arguments.length === 1) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}

function ColorUtil_RgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}

(function() {

//Overwrites TerraxLighting radial gradient fill rect with a smoothed one
//Code is not the best but works... as far as I know
Bitmap.prototype.radialgradientFillRect = function(x1, y1, r1, r2, color1, color2, flicker, brightness, direction) {
x1=x1+20;

if (!brightness) { brightness = 0.0; }
if (!direction) {direction = 0; }
var context = this._context;
var grad;
var wait = Math.floor((Math.random()*8)+1);
if (flicker == true && wait == 1) {
var flickerradiusshift = $gameVariables.GetFireRadius();
var flickercolorshift = $gameVariables.GetFireColorshift();
var gradrnd = Math.floor((Math.random()*flickerradiusshift)+1);
var colorrnd = Math.floor((Math.random()*flickercolorshift)-(flickercolorshift/2));

var r = hexToRgb(color1).r;
var g = hexToRgb(color1).g;
var b = hexToRgb(color1).b;
g = g + colorrnd;
if (g<0) { g = 0; }
if (g>255) { g = 255; }
color1 = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
r2 = r2 - gradrnd;
if (r2 < 0) r2=0;
}

grad = context.createRadialGradient(x1, y1, r1, x1, y1, r2);

var r1 = hexToRgb(color1).r;
var g1 = hexToRgb(color1).g;
var b1 = hexToRgb(color1).b;
var h1 = ColorUtil_RgbToHsv(r1, g1, b1).h;
var s1 = ColorUtil_RgbToHsv(r1, g1, b1).s;
var v1 = ColorUtil_RgbToHsv(r1, g1, b1).v;
var brightnessFactor = (v1/100) * (1 + brightness);

v1 = Math.min(100 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0, color1Changed);

v1 = Math.min(50 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.33, color1Changed);

v1 = Math.min(33 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.5, color1Changed);

v1 = Math.min(16 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.7, color1Changed);

v1 = Math.min(11 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.75, color1Changed);

v1 = Math.min(7 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.8, color1Changed);

v1 = Math.min(4 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.85, color1Changed);

v1 = Math.min(2 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.9, color1Changed);

v1 = Math.min(1 * brightnessFactor, 100);
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(0.95, color1Changed);

v1 = 0;
r1 = ColorUtil_HsvToRgb(h1, s1, v1).r;
g1 = ColorUtil_HsvToRgb(h1, s1, v1).g;
b1 = ColorUtil_HsvToRgb(h1, s1, v1).b;
var color1Changed = ColorUtil_RgbToHex(r1, g1, b1);
grad.addColorStop(1, color1Changed);

grad.addColorStop(1, color2);

context.save();
context.fillStyle = grad;
direction = Number(direction);
var pw = $gameMap.tileWidth()/2;
var ph = $gameMap.tileHeight()/2;
switch(direction) {
case 0:
context.fillRect(x1-r2, y1-r2, r2*2, r2*2);
break;
case 1:
context.fillRect(x1-r2, y1-ph, r2*2, r2*2);
break;
case 2:
context.fillRect(x1-r2, y1-r2, r2*1+pw, r2*2);
break;
case 3:
context.fillRect(x1-r2, y1-r2, r2*2, r2*1+ph);
break;
case 4:
context.fillRect(x1-pw, y1-r2, r2*2, r2*2);
break;
}

// context.fillRect(x1-r2, y1-r2, r2*2, r2*2);
context.restore();
this._setDirty();
};

})();

Credit


Free for commercial and non-commercial games.


Crediting me is optional.


Author's Note


I am not good at programming so the code may be wrong, use at your own risk. Again, I ain't no programmer, so the code is a bit weird and probably not well optimized. But it works for me project so I thought it would be nice to share it to you guys. If there is already something that does that, please tell me.
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts


finally, the B-tile is added to the forum and steam!
I hope my annual #DoItForHargon contest gets lots of entries this year (since a Playstation 5 is a pretty big prize lol) XD On further news - I'm back to game devving next week after hiatus cause of that tragedy of losing everything I own.
Rome Trastevere deserve a tileset. Mumble mumble...
Before the full time job, I can make 1 MZ plugin per day, even though it's still hard; With the full time job, making 1 such plugin per week becomes just as hard lol

Forum statistics

Threads
102,989
Messages
996,505
Members
134,463
Latest member
adafon
Top