Last active
August 19, 2017 21:40
-
-
Save ronyx69/9c0026648d26b1e1b09cb8ec8a831b17 to your computer and use it in GitHub Desktop.
Script to replace any texture of a prop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Textures must be placed in gamefolder/textures/props | |
// For example if my prop file name is snow1 and asset name is also snow1, | |
// then the textures would be called snow1.snow1_Data_d.png | |
// snow1.snow1_Data_a.png, snow1.snow1_Data_n.png and so on... | |
var assetname = "snow1.snow1_Data"; // CHANGE ONLY THIS | |
var asset = PrefabCollection<PropInfo>.FindLoaded(assetname); | |
var assetMaterial = asset.m_material; var texturePath = " "; | |
Texture2D providedTexture=null; Color px; | |
// Bools | |
bool _d, _a, _c, _i, _n, _s, aci, xys, gamma; | |
_d = _a = _c = _i = _n = _s = aci = xys = gamma = false; | |
// Defaults | |
var defaultA = 1f; var defaultC = 1f; var defaultI = 0f; | |
var defaultX = 0.5f; var defaultY = 0.5f; var defaultS = 0f; | |
// _d (Diffuse) | |
Texture2D textureD; textureD = new Texture2D(1, 1); | |
texturePath = "textures/props/" + assetname + "_d.png"; | |
if (File.Exists(texturePath)) { | |
textureD.LoadImage(File.ReadAllBytes(texturePath)); | |
_d=true; | |
} | |
if(_d) assetMaterial.SetTexture("_MainTex", textureD); | |
// ACI START | |
// _a (Alpha) | |
Texture2D textureA; textureA = new Texture2D(1, 1); | |
texturePath = "textures/props/" + assetname + "_a.png"; | |
if (File.Exists(texturePath)) { | |
textureA.LoadImage(File.ReadAllBytes(texturePath)); | |
providedTexture=textureA; _a=true; | |
} | |
// _c (Color) | |
Texture2D textureC; textureC = new Texture2D(1, 1); | |
texturePath = "textures/props/" + assetname + "_c.png"; | |
if (File.Exists(texturePath)) { | |
textureC.LoadImage(File.ReadAllBytes(texturePath)); | |
providedTexture=textureC; _c=true; | |
} | |
// _i (Illumination) | |
Texture2D textureI; textureI = new Texture2D(1, 1); | |
texturePath = "textures/props/" + assetname + "_i.png"; | |
if (File.Exists(texturePath)) { | |
textureI.LoadImage(File.ReadAllBytes(texturePath)); | |
providedTexture=textureI; _i=true; | |
} | |
// previous ACI | |
if(assetMaterial.GetTexture("_ACIMap")) | |
{ | |
var preACI = assetMaterial.GetTexture("_ACIMap") as Texture2D; | |
if(providedTexture) providedTexture = new Texture2D(providedTexture.width, providedTexture.height); | |
else providedTexture = new Texture2D(preACI.width, preACI.height); | |
if(preACI.name!="replaced") { | |
gamma = true; | |
} | |
providedTexture.SetPixels(preACI.GetPixels()); | |
providedTexture.anisoLevel = preACI.anisoLevel; | |
providedTexture.filterMode = preACI.filterMode; | |
providedTexture.wrapMode = preACI.wrapMode; | |
providedTexture.Apply(); | |
aci=true; GameObject.Destroy(preACI); | |
} | |
// ACI COMBINATION | |
if(providedTexture) | |
{ | |
for (int i = 0; i < textureA.width; i++) | |
{ | |
for (int j = 0; j < textureA.height; j++) | |
{ | |
px=providedTexture.GetPixel(i, j); | |
if(_a) px.r=textureA.GetPixel(i, j).r; else if(!aci) px.r=defaultA; | |
if(_c) px.g=textureC.GetPixel(i, j).g; else if(!aci) px.g=defaultC; | |
if(_i) px.b=textureI.GetPixel(i, j).b; else if(!aci) px.b=defaultI; | |
if(_a) px.r=Mathf.LinearToGammaSpace(1-px.r); | |
if(_c) px.g=Mathf.LinearToGammaSpace(1-px.g); | |
if(_i) px.b=Mathf.LinearToGammaSpace(px.b); | |
providedTexture.SetPixel(i, j, px); | |
} | |
} | |
providedTexture.Apply(); providedTexture.name="replaced"; | |
assetMaterial.SetTexture("_ACIMap", providedTexture); | |
} | |
// ACI END | |
// XYS START | |
providedTexture=null; gamma=false; | |
// _n (Normal) | |
Texture2D textureN; textureN = new Texture2D(1, 1); | |
texturePath = "textures/props/" + assetname + "_n.png"; | |
if (File.Exists(texturePath)) { | |
textureN.LoadImage(File.ReadAllBytes(texturePath)); | |
providedTexture=textureN; _n=true; | |
} | |
// _s (Specular) | |
Texture2D textureS; textureS = new Texture2D(1, 1); | |
texturePath = "textures/props/" + assetname + "_s.png"; | |
if (File.Exists(texturePath)) { | |
textureS.LoadImage(File.ReadAllBytes(texturePath)); | |
providedTexture=textureS; _s=true; | |
} | |
// previous XYS | |
if(assetMaterial.GetTexture("_XYSMap")) | |
{ | |
var preXYS = assetMaterial.GetTexture("_XYSMap") as Texture2D; | |
if(providedTexture) providedTexture = new Texture2D(providedTexture.width, providedTexture.height); | |
else providedTexture = new Texture2D(preXYS.width, preXYS.height); | |
if(preXYS.name!="replaced") { | |
gamma = true; | |
} | |
providedTexture.SetPixels(preXYS.GetPixels()); | |
providedTexture.anisoLevel = preXYS.anisoLevel; | |
providedTexture.filterMode = preXYS.filterMode; | |
providedTexture.wrapMode = preXYS.wrapMode; | |
providedTexture.Apply(); | |
xys=true; GameObject.Destroy(preXYS); | |
} | |
// XYS COMBINATION | |
if(providedTexture) | |
{ | |
for (int i = 0; i < providedTexture.width; i++) | |
{ | |
for (int j = 0; j < providedTexture.height; j++) | |
{ | |
px=providedTexture.GetPixel(i, j); | |
if(_n) { | |
px.r=textureN.GetPixel(i, j).r; | |
px.g=textureN.GetPixel(i, j).g; | |
} | |
else if(!xys) { | |
px.r=defaultX; | |
px.g=defaultY; | |
} | |
if(_s) px.b=textureS.GetPixel(i, j).b; else if (!xys) px.b=defaultS; | |
if(_n||gamma) px.r=Mathf.LinearToGammaSpace(px.r); | |
if(_n||gamma) px.g=Mathf.LinearToGammaSpace(px.g); | |
if(_s||gamma) px.b=Mathf.LinearToGammaSpace(1-px.b); | |
providedTexture.SetPixel(i, j, px); | |
} | |
} | |
providedTexture.Apply(); providedTexture.name="replaced"; | |
assetMaterial.SetTexture("_XYSMap", providedTexture); | |
} | |
// XYS END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment