Feedback for potential Wiki Script: Decals!

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. :smile:

Thanks guys! :slight_smile:

Noone has any feedback? … Really? :frowning:

i got this error :frowning:

Assets/Decals.cs(19,53): error CS0117: UnityEngine.Vector2' does not contain a definition for one’

Well that’s stupid… if there’s a Vector3.one, there should be a Vector2.one… nevertheless, it’s been fixed, try it now. Thanks.

http://unity.pastebin.com/kpTfTMXV

I’ll be trying out your script in the morning. Thanks in advance though. I’ve been looking for a solution to bullet holes and blood stains or chalk outlines on the pavement. And I’ll give good constructive feedback.

…fix this too :slight_smile: In JS!
(7,9): BCE0005: Unknown identifier: ‘Decals’.

i second that :wink:

can you make simple project (where standart CharacterController can spray image on the wall and without any errors in scripts)?

Thanks SpikeX for the script!
Yesterday i was thinking about creating a decal in order to make dirty walls when shooting.
I will test it :smile:

i third that :confused:

EDIT: Or use a c# script, like this:

using UnityEngine;
using System.Collections;

public class DecalHit : MonoBehaviour {
	
	public Material myMaterial; 
	
	// Update is called once per frame
	void Update () {

    if (Input.GetMouseButtonDown(0)) 
    { 
        Decals.CreateDecal( 
          Camera.main.ScreenPointToRay(Input.mousePosition), myMaterial); 
    } 
}
}

But this only works where the mouse position is, anyone know how to change it to make it a raycast? so where the crosshair is?

The scripts nice, but the decal isnt able to get into corners when you hit something small for example.

I’ve tried ur script and it looks nice… The only problem is flickering between different mesh as you just create a new plane mesh when u hit something and there has an overlapping on them. Can you fix this by creating a small gap between them…

anyway… ur script is really nice…

Hey, old thread but it just helped me, so I think it’s worth adding one more thing.

From what I can see, the current version of the decal script moves the decal away from the surface (by 0.0001). In addition to that I found that this shader on the wiki works very nicely as described on that page.
Just assign it to the material used by your decals.

Here’s an image of my very quick test. Instead of using the mouse position script, the little yellow ball in the middle fires out Physics.Raycast in random directions and creates a decal (with a randomized texture) wherever it hits the wall. I’m working on a thing to randomly arrange movie posters on the walls of a room.