Saturday, March 1, 2014

Unity Difficulties: Blurring an individual game object

Ok, so this won't work on literally everything. It can more than likely be converted to work with anything, but I've only got a solution immediately for a flat object.

Let's start with the object. In my case, I wanted a blurred, glowing, flickering, basically plasma object holding up a shelf. Weird, I know, but for my game it worked well. So, Unity3D doesn't have a single plugin that allows for blurring a single game object. Below is my final product, so you can see what it looks like.


Does it need some work? Yea, sure, but it's certainly looking blurry.

How did I do it? Let's start right in with the 3D.

Here's the object as it was originally designed.


Thin, dependable, beautiful. Looks great from the side, too. But, not blurry, so...Nope. So, I made thin versions of the same object, (this made sure the size and shape were the same). Like below.


And then came the script. At the end, I'll copy and paste it. Feel free to modify it and use it in your own projects. Just let me know so I can take a look!

I applied the shelf supports onto a separate layer, calling it "No Shadows". I put in a light, and lined it up to the middle of the support and went to the culling mask. I removed the layer "No Shadows" from the culling mask, which allowed it to not show shadows, (and I wanted shadows, for the effect). Have fun adjusting the color and light effects to create a nice atmosphere!

From there, it was just deciding the flicker, light intensity, and alpha. The script I wrote took care of 90% of that, once it was given the initial values. 

It looks wonderful, blurry, flickers, and creates a nice eerie effect. I'll let you know when it's finalized, but for now, here's the script, and I hope you guys enjoy!



var speed : float;
//Set the minValue and maxValue to whatever you want, but everything will be
//connected to these numbers in the long run.
var minValue : float;
var maxValue : float;
var mat : Material;
private var timer : float;
var attachedLight : Light;

//The left side of the gameobject, which creates the blurring effect.
var leftOne : GameObject;
var leftTwo : GameObject;
var leftThree : GameObject;
var leftFour : GameObject;

//The right side of the gameobject.
var rightOne : GameObject;
var rightTwo : GameObject;
var rightThree : GameObject;
var rightFour : GameObject;

function Start () {
mat = gameObject.renderer.material;
}

function Update () {
timer += Time.deltaTime;

if (timer >= speed)
{
mat.color.a = Random.Range(minValue, maxValue);
leftOne.renderer.material.color.a = mat.color.a *.5;
leftTwo.renderer.material.color.a = mat.color.a * .3;
leftThree.renderer.material.color.a = mat.color.a * .2;
leftFour.renderer.material.color.a = mat.color.a * .05;
rightOne.renderer.material.color.a = mat.color.a * .5;
rightTwo.renderer.material.color.a = mat.color.a * .3;
rightThree.renderer.material.color.a = mat.color.a * .2;
rightFour.renderer.material.color.a = mat.color.a * .05;
timer = 0;
speed = Random.Range(0.00, .2);
attachedLight.intensity = Mathf.Lerp(0, .33, mat.color.a / maxValue);
}
}