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...
-
So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...
-
Often, when building a game, you need to test if objects are colliding. The objects could be spaceships, rocks, mouse pointers, laser beams....
-
Summary: NodeJS wins. Test Program ab -n 10000 -c 5 http://localhost/ Gevent Code from gevent import wsgi class WebServer(object): def a...
-
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...
-
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...
-
I've just finished refactoring an awful C# class. I had been delaying the job for a while because I didn't want to do it. Then, whil...
-
Dear Lazyweb. Imagine a nice RESTful interface for working with Tags. The URL: /tags/ will return a list of all the tags. The URL: /tags/fo...
-
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...
-
We're almost done with our Giant Robot Project. Realising we had only a few hours before the robots would be deconstructed and sent awa...
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