- Home /
Simple Pixel Shader
Hello,
I wanted to know if Unity has a simple way, via script, to change the shaders color of a texture.
e.g. While the script runs, a 3D sphere starts pulsing Red
Answer by hvilela · Oct 12, 2012 at 12:43 AM
gameObject.renderer.material.color = yourDesiredColor;
Answer by sparkzbarca · Oct 12, 2012 at 12:52 AM
make a new script add this to the start()
this.renderer.material.color = new Color(1f,0f,0f,.5f);
this will turn an object red when attached to it.
renderer.material.color is what your looking for.
material.color takes a color object
color(red,green,blue,alpha)
red,green,blue is a value between 0 and 255
http://www.colorspire.com/rgb-color-wheel/
that will allow you to pick a color and tell you the RGB value of it.
Alpha is the transparency
0 = invisible
.5 = opaque
1 = full visible
color also has basic color shorthand such as
color.red color.green
Answer by aldonaletto · Oct 12, 2012 at 12:51 AM
You can modify renderer.material.color with some periodic function:
var speed = 1.0; // cycle takes about 6.28/speed seconds
var color1 = Color(0.2, 0, 0, 1);
var color2 = Color(0.9, 0, 0, 1);
function Update(){
// create sinusoidal value between 0 and 1:
var sine = (Mathf.Sin(Time.deltaTime * speed)+1)/2;
// swing between the two colors according to sine:
renderer.material.color = Color.Lerp(color1, color2, sine);
}