Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

UTMultiTeam.UTMT_TeamMaterialTransformer


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
//-----------------------------------------------------------
// Based upon Lotus's CustomUT skin shaders, code and shaders
// used with permission.
//
// Copyright 2007, Infinity Impossible
//-----------------------------------------------------------
class UTMT_TeamMaterialTransformer extends ReplicationInfo
	abstract;


struct ReplacementMaterial
{
	var string MaterialName;
	var MaterialInterface ReplacementMaterial;
};


struct VectorParameterTransform
{
	var name ParamName;
	var LinearColor ParamValue;
};


/**
 * Describes the transforms performed on a given material to turn it into
 * the new team color using the new materials. Hue is the same as the hue value
 * in the HSV color space (use the interwebs to find info on hue, saturation
 * and value if you want more info ;)). R, G and B express a multiplier on the
 * transformed values: 1 leaves the skin the same after the transform, greater
 * then 1 makes the skins brigher, less than 1 makes them darker.
 *
 * VectorParams can be used to express material-specific parameter changes. For
 * example, on a character skin you might want something along the lines of:
 *
 *		ParamName="Char_TeamColor",ParamValue=(R=2,G=0,B=0,A=2)
 *
 * which would change the "Char_TeamColor" variable, that controls the glowstick
 * distance effect to be red for this transform.
 */
struct TeamColorTransform
{
	var float Hue;
	var float R;
	var float G;
	var float B;
	var array<VectorParameterTransform> VectorParams;
};


/** Named material texture parameters to copy from the old material to the new */
var array<name> TextureParameterNames;
/** Named material scalar parameters to copy from the old material to the new */
var array<name> ScalarParameterNames;
/** Named material vector parameters to copy from the old material to the new */
var array<name> VectorParameterNames;


/**
 * Any base material with a given replacement name is swapped for the
 * corresponding replacement material.
 */
var array<ReplacementMaterial> ReplacementMaterials;


var LinearColor RedMask;
var LinearColor BlueMask;
var LinearColor GreenMask;


simulated function MaterialInterface GetTeamColorShader(string MatName)
{
	local int i;
	local MaterialInterface Mat;

	Mat = none;
	for(i = 0; i < ReplacementMaterials.Length; ++i)
	{
		if( (ReplacementMaterials[i].MaterialName ~= MatName)
			|| (string(ReplacementMaterials[i].ReplacementMaterial) ~= MatName) )
		{
			Mat = ReplacementMaterials[i].ReplacementMaterial;
		}
	}

	return Mat;
}


simulated function TransformMaterial(MaterialInstanceConstant OldMIC, TeamColorTransform TCT)
{
	local MaterialInstanceConstant NewMIC;
	local string NewMatName;
	local MaterialInterface NewMaterial;

	NewMatName = GetRootMaterialName(OldMIC);
	NewMaterial = GetTeamColorShader(NewMatName);

	if(NewMaterial == None)
	{
		LogInternal("No team color material found for Material by the name of:"@NewMatName);
	}
	else
	{
		if( !(GetRootMaterialName(OldMIC) ~= string(NewMaterial)) )
		{
			NewMIC = new class'MaterialInstanceConstant';
			NewMIC.SetParent(NewMaterial);

			CopySkinParameters(NewMIC, OldMIC);
			OldMIC.SetParent(NewMIC);
		}

		SetTeamHue(OldMIC, TCT);
	}
}


simulated function SetTeamHue(MaterialInstanceConstant MIC, TeamColorTransform TCT)
{
	local LinearColor NewRed;
	local LinearColor NewGreen;
	local LinearColor NewBlue;
	local LinearColor LC;
	local VectorParameterTransform Param;
	local float ParamSize;
	local int i;

	TCT.Hue += 120.0;

	NewRed = HueTransform(RedMask, TCT.Hue) * TCT.R;
	NewGreen = HueTransform(GreenMask, TCT.Hue) * TCT.G;
	NewBlue = HueTransform(BlueMask, TCT.Hue) * TCT.B;

	for(i = 0; i < TCT.VectorParams.Length; ++i)
	{
		Param = TCT.VectorParams[i];
		MIC.GetVectorParameterValue(Param.ParamName, LC);
		ParamSize = Sqrt(LC.R*LC.R + LC.B*LC.B + LC.G*LC.G);
		MIC.SetVectorParameterValue(Param.ParamName, Param.ParamValue * ParamSize);
	}

	MIC.SetVectorParameterValue('ColorMult_Red', NewRed);
	MIC.SetVectorParameterValue('ColorMult_Green', NewGreen);
	MIC.SetVectorParameterValue('ColorMult_Blue', NewBlue);
}


/**
 * Return the name of the material at the top of the instancing chain. This
 * assumes that once we reach a MaterialInterface that has itself as its
 * parent, we have reached the top of the chain.
 */
static simulated function string GetRootMaterialName(MaterialInterface Mat)
{
	local MaterialInterface LastMat;

    LastMat = Mat;
   	Mat = Mat.GetMaterial();

   	while(Mat != LastMat)
   	{
   		LastMat = Mat;
	   	Mat = Mat.GetMaterial();
   	}

	return string(Mat);
}


/**
 * Spins the given color from its current hue by the amount given as deltaH
 * and returns the transformed color.
 */
simulated function LinearColor HueTransform(LinearColor L, float deltaH)
{
    local float Max, Min, R, G, B, H, S, V, f, p, q, t;
    local int sector;

	R = L.R/255.0;
	G = L.G/255.0;
	B = L.B/255.0;

	if(R == G && G == B)
		return L;

    Max = FMax(R, FMax(G, B));
    Min = FMin(R, FMin(G, B));

    if(max == R)
    {
        if(G >= B)
            H = 60.0*(G-B)/(Max-Min);
        else
            H = 60.0*(G-B)/(Max-Min)+360.0;
    }
    else if(max == G)
    {
        H = 60.0*(B-R)/(Max-Min)+120.0;
    }
    else
    {
        H = 60.0*(R-G)/(Max-Min)+240.0;
    }

    if(Max == 0.0)
        S = 0.0;
    else
        S = 1.0-Min/Max;

    V = max;


    H = (H + deltaH)%360;
    sector = int(H/60)%6;
    f = H/60.0-sector;
    p = v*(1-s);
    q = v*(1-f*s);
    t = v*(1-(1-f)*s);

	switch (sector)
	{
		case 0:   return MakeLinearColor(v,t,p,1);
		case 1:   return MakeLinearColor(q,v,p,1);
		case 2:   return MakeLinearColor(p,v,t,1);
		case 3:   return MakeLinearColor(p,q,v,1);
		case 4:   return MakeLinearColor(t,p,v,1);
		case 5:   return MakeLinearColor(v,p,q,1);
	}

    return L;
}


/**
 * Copy all skin parameters manually, since, eg, MI.TextureParameterValues.Length
 * is 0, even though it has parameters we can use...
 */
simulated function CopySkinParameters(MaterialInstanceConstant NewMI, MaterialInterface MI)
{
	local name ParamName;
	local LinearColor LC;
	local float F;
	local texture Tex;
	local int i;


	//===============================================
	// Texture Paramaters
	//===============================================
	for(i = 0; i < TextureParameterNames.Length; ++i)
	{
		ParamName = TextureParameterNames[i];
		MI.GetTextureParameterValue(ParamName, Tex);
		NewMI.SetTextureParameterValue(ParamName, Tex);
	}


	//===============================================
	// Scalar Paramaters
	//===============================================
	for(i = 0; i < ScalarParameterNames.Length; ++i)
	{
		ParamName = ScalarParameterNames[i];
		MI.GetScalarParameterValue(ParamName, F);
		NewMI.SetScalarParameterValue(ParamName, F);
	}


	//===============================================
	// Vector Paramaters
	//===============================================
	for(i = 0; i < default.ScalarParameterNames.Length; ++i)
	{
		ParamName = ScalarParameterNames[i];
		MI.GetVectorParameterValue(ParamName, LC);
		NewMI.SetVectorParameterValue(ParamName, LC);
	}
}

defaultproperties
{
   RedMask=(R=255.000000,G=0.000000,B=0.000000,A=1.000000)
   BlueMask=(R=0.000000,G=0.000000,B=255.000000,A=1.000000)
   GreenMask=(R=0.000000,G=255.000000,B=0.000000,A=1.000000)
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: Mon 20/12/2010 19:06:40.000 - Creation time: Fri 23/12/2011 20:31:26.613 - Created with UnCodeX