- Home /
Animated material
I want a billboard-lights material that has animating lights. So I made a scrolling-texture animation by animating the main texture offset.
Now the thing is, if I want something to have this material, I have to give that object an animation component and add that animation to it. And this also creates a new material for each object.
But I don't want it to make copys of the material each time it's used, I just want all these objects to use the same animated material without having to give it an animation component. (So what I really want is to store that animation in the material somehow)
Does anyone know how I can do something like this ?
Edit: I made a script to this instead:
var mat:Material;
function Update ()
{
var offset:float = Mathf.Repeat (Time.time*2,1);
offset = Mathf.FloorToInt(offset*4)/4.0f; //clampen op 1/4
Debug.Log("offset: "+offset);
mat.mainTextureOffset.x = offset;
mat.SetTextureOffset("_IlluminTex", new Vector2(offset,0));
}
only problem is that I don't know what the Self-Illumination Texture is called. Anyone knows were to find it ?
EDIT2:
found it, it's "_Illum" If anyone wants to findout how something else is called in the build-in shaders, you can find the shaders here: http://unity3d.com/download_unity/builtin_shaders.zip
Answer by pyro · May 25, 2011 at 01:12 PM
If you are getting the material reference like this:
Material mat;
mat = renderer.material;
change it to this:
Material mat:
mat = renderer.sharedMaterial;
Using the first way will create a separate instance of the material. If you animate the sharedMaterial mainTextureOffset it will animate it for all objects with that material.
edit: since you edited your question with a script, in your script it would be:
var gameObjectsMaterialToAnimate:GameObject;
private var mat:Material;
function Start()
{
mat = gameObjectsMaterialToAnimate.renderer.sharedMaterial;
}
function Update ()
{
var offset:float = Mathf.Repeat (Time.time*2,1);
offset = Mathf.FloorToInt(offset*4)/4.0f; //clampen op 1/4
Debug.Log("offset: "+offset);
mat.mainTextureOffset.x = offset;
mat.SetTextureOffset("_IlluminTex", new Vector2(offset,0));
}
I'm not getting the material refernce at all, there was no scripting involved. I animated the material by creating an animation and changing the the main texture offset in it.
I guess making a script that animates the material is a good enough way (as long as I only need 1 instance of the script).
ok, this works:
var mat:$$anonymous$$aterial;
function Update () { var offset:float = $$anonymous$$athf.Repeat (Time.time*2,1); offset = $$anonymous$$athf.FloorToInt(offset*4)/4.0f; //clampen op 1/4
Debug.Log("offset: "+offset);
mat.mainTextureOffset.x = offset;
mat.SetTextureOffset("_Illu$$anonymous$$Tex", new Vector2(offset,0));
}
only problem is that I don't know what the Self-Illu$$anonymous$$ation Texture is called. Anyone knows were to find it ?
that's close but you're still using the material ins$$anonymous$$d of the shared$$anonymous$$aterial, so it will instance
uhm, no, I'm certain it uses the original material.
I'm not getting the material by renderer.shared$$anonymous$$aterial or renderer.material, I assign it in the scene (well, in a prefab with the script attached to be exact). So what you're saying is not possible.
Your answer
Follow this Question
Related Questions
can i animate image sequence for material animation in unity ? 1 Answer
Changing Material in Animation 2 Answers
Bringing visibilty and material animation from Max to Unity 3 Answers
Can the animation editor create local rotational data? 3 Answers
Spin a texture in just any material -is it possible? 1 Answer