using UnityEngine;
using UnityEditor;
using System.Collections;
class ModifyTextures : AssetPostprocessor
{
void OnPostprocessTexture (Texture2D texture)
{
if (assetPath.Contains ("_SPECULAR")) {
var diffusePath = assetPath.Replace ("_SPECULAR", "_DIFFUSE");
var bytes = System.IO.File.ReadAllBytes (diffusePath);
var diffuse = new Texture2D (texture.width, texture.height);
diffuse.LoadImage (bytes);
var colors = diffuse.GetPixels ();
var specular = texture.GetPixels ();
if (colors.Length == specular.Length) {
Debug.LogWarning (assetPath + ": Adding specular values to diffuse map alpha.");
for (var i = 0; i < colors.Length; i++) {
colors[i].a = (specular[i].r + specular[i].g + specular[i].b) / 3;
}
diffuse.SetPixels (colors);
diffuse.Apply ();
System.IO.File.WriteAllBytes (diffusePath, diffuse.EncodeToPNG ());
AssetDatabase.ImportAsset (diffusePath);
AssetDatabase.DeleteAsset(assetPath);
} else {
Debug.LogWarning (assetPath + ": specular size must match diffuse size.");
}
return;
}
}
}
Friday, January 21, 2011
The Unity3D Asset Pipeline.
One of the great things about Unity3D, is the programmable content pipeline. I wrote this script so that when I add a texture named "x_SPECULAR.png" to my project, it looks for a texture named "x_DIFFUSE.png", and then copies the specular texture into the alpha channel of the diffuse texture (so that it can be used in a Unity3D specular shader). This automates what would usually be a tedious Photoshop task.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
These are the robots I've been working on for the last 12 months. They each weigh about 11 tonnes and have a 17 meter reach. The control...
-
This hard-to-see screenshot is a Generic Node Graph Editing framework I'm building. I'm hoping it can be used for any kind of node...
-
So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...
-
After my last post, I decided to benchmark the scaling properties of Stackless, Kamaelia, Fibra using the same hackysack algorithm. Left axi...
-
It is about 8 degrees C this morning. So cold, especially when last week we had high twenties. To help solve the problem, a friend suggeste...
-
MiddleMan: A Pub/Sub and Request/Response server in Go. This is my first Go project. It is a rewrite of an existing Python server, based o...
-
Why would I ask that question? Python 3 has been available for some time now, yet uptake is slow. There aren't a whole lot of packages i...
-
I'm now using bzr instead of svn. I'm pushing my repositories to: http://exactlysimilar.org/bzr/ I'm also auto publishing docume...
-
I've just read a newspaper article (courtesy of Kranzky ) from WA Business News documenting the malfeasance, gross negligence and misc...
-
In my spare time this week, instead of building spaceships, playing games or even drinking beer... I will make a game for the Apple App Stor...
9 comments:
Thank you very much for sharing this!
I've tried converting it to C#, and I get an error CS0117 : 'System.IO.File' does not contain a definition for 'ReadAllBytes' and 'WriteAllBytes'
I've searched the forums and so forth, but can't seem to find a solution. Any insight?
This code is already C#, no need to convert it. UnityScript and C# can look quite similar at times!
Is your Unity project using .NET 2 or greater?
Oh, isn't the use of var a .js thing? Confusion...
Yes, I'm on pro with 2.0 in the settings. I'll try a straight copy and paste then...
I get the same CS0117 as before, and now a 1502 and 1053 due to a conversion error. Hm.
The var keyword is something we can now use, as Unity now has an up to date .NET implementation. It is used with local variable to implicitly type them, avoiding the need to declare their types.
These are strange errors. I'll see if I can replicate them...
I believe Unity supports .NET 3.5. Not entirely up to date, but enough for using the keyword var, yes.
Is there a particular reason you chose to use the var feature so many times in this script?
Less typing.
the Jack: Are you using Unity 3.X? I suspect this code will not compile under 2.6 or earlier.
Yes, the very latest update, Unity Pro, saved off in Monodevelop.
If it's working fine for you, as I suspect it is, it could be a wider-ranging issue with my dev environment.
Thanks for looking into it though!
Post a Comment