I really wanted a quick and dirty Decals solution, something akin to the Source engine, where I could just specify a position and it would produce a decal at that point.
Unfortunately, the two options with Unity are either the “Decal” shader, which requires you to use the “Decal” shader (instead of something nicer like a Vertex shader or a transparent shader - boo), and the “Projector” system, which requires you to make a few different art assets besides your regular “Decal” texture, like cookies and alpha masks and whatnot (which is a pain).
So I coded up a quick little script to take in a Ray, and a material or texture, and then generate a “decal”, which is basically just a plane that’s very very close to the point that you clicked.
“But, SpikeX, why do you need a Ray? That doesn’t make sense!”
I needed to use rays because I needed not only the position of the decal, but I also needed the normal vector of the point where the decal was going, so that it could be rotated properly (and moved away from the surface slightly so it could be visible). I expected this script to be used in conjunction with raycasts anyway (bullet holes, anyone? :)), so providing the script with a Ray shouldn’t be a problem.
So, what I’d like for you guys, is to give this a quick test, and let me know how it works, and if you have any suggestions on improving it or not.
http://unity.pastebin.com/kpTfTMXV
How to use this script:
Create the file “Decals.cs” anywhere in your assets, and paste the code inside it.
Then, create an empty game object with a script, and put something like this inside of it:
// JS
var myMaterial : Material;
function Update()
{
if (Input.GetMouseButtonDown(0))
{
Decals.CreateDecal(
Camera.main.ScreenPointToRay(Input.mousePosition), myMaterial
);
}
}
Of course, you’ll need to fill in the “myMaterial” property via the inspector.
Then, start your game and click on anything that causes a “hit” via a raycast. You should see a decal appear there.
Again, I’m just looking for some casual feedback on if I could add anything, or if there’s a major flaw here, or anything like that. Bonus points if you post a screenshot of your game with some decals from this script. ![]()
Thanks guys! ![]()
