- Home /
Script vs Shader for animated texture
Quick question...I need to make an animated texture, and I was thinking of using a sprite-sheet and moving the UVs with code, with something like Animating Tiled Texture UVs.
But I was wondering what would be the best way to achieve this, by script like the above, or creating a special shader that basically does the same thing? And what about performance between both methods?
Thanks for any help, and for your time! Stephane
Currently working on an animation shader, I was looking for a free way to animate a texture myself. I will share my shader in the answers once I have a working solution.
According to my experiences, I like doing things using shaders rather than scripts. If you plan to have a lot of of animated textures, I would definitely go with shaders.
I haven't started experimenting with the shader quite yet, but I think you should start looking at http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.GetTexture.html and http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.SetTexture.html. Depending on how you want to do it, you can simply assign the shader a texture every frame, or maybe you could encode all the needed textures into the shader so that it can run it in sequence, which may or may not be faster. (but it sounds pretty fast)
To set a trigger to start and stop animation, or choose what kind of animation, you could probably assign the shader a small texture that acts as data storage, and you could use GetTexture and SetTexture to manipulate that data.
I'm sorry if I'm being very vague with my suggestions, I haven't really touched it quite yet, and I can't really tell you what is and isn't faster without knowing it myself, and only tell you places to start. I'll be back with you when I have more answers.
Also the link to "Animating Tiled Texture UVs" doesn't work at the moment so I can only make out the title of what you have shown me, so sorry if I miss anything obvious.
Thanks for the suggestions Ribbon! I have been using the "Advanced Animated Tiled UVs" script from the wiki (which is down right now so I can't get the link), and it's been working really good for me. I guess it really depends on each specific scenario...I'll definitively write a shader which animates textures because I know I'll need it at one point or another :)
Answer by FWCorey · Sep 25, 2012 at 07:37 PM
Wouldn't using a script on multiple instances of the same material create an extra draw call for each instance?
That's a good question, I would think that if using shared$$anonymous$$aterial, then no, otherwise, maybe...if anyone can give a final answer on this, it would be very helpful!
Answer by ShenYuan · Sep 29, 2012 at 07:08 AM
In my point, put uv calculation into the GPU(shader) side may not be a good idea especially when your target platform is mobile or some devices with poor graphic processor.
What's more, calculate uv offset by using "modf(GameTime / Cycle)" can easily cause precision problem when the game need to be played for a long time or "Cycle" is too small. I myself have encountered this problem many many times. But if you made your calculation on the C# side, this gonna be a very very easy problem(just make a private sum time value instead of a global one, and reset it after every "Cycle").
I used to only put animate logic in shader when it happends on ParticleSystem. Because the particle system of my previous graphics engine only provide normalized particle age for each particle vertex. The only and efficient way for me is calculate them in shader.
Keep in mind "switch texture" will cost extra performance. If you have a lot of instanced materials using different texture in one frame, then DO NOT USE Material.SetTexture().
But sprite-sheet have a big disadvantage, all frames of the animation must load at the same time and you can not unload those "Out of sight frame" (because they are stored in one texture). This means a big waste of memory some time.
If you are trying to display a non-loop, long and high resolution animated frames, go for the Material.SetTexture(). Also don't forget use Resources.Unload() to handle the memory issue. Or just simply use VideoTexture(not available on iOS, but u can buy one plugin developed by PRIME31).
Answer by YoungXi · Jan 02, 2014 at 04:06 AM
Hi, Stephane: I know it has been a long time, but I still want know which one did you use to make the animated and tiled texture, Shader or Script? I already know how to do that with scripts, but I'm dying to know how to make it work also in a Shader. I tried, but it didn't work out well. So I wonder if you know anything, plz let me know :D. Happy new year! Young.
Answer by YoungXi · Jan 02, 2014 at 04:06 AM
Hi, Stephane: I know it has been a long time since the last reply, but I still want know which one did you use to make the animated and tiled texture, Shader or Script? I already know how to do that with scripts, but I'm dying to know how to make it work also in a Shader. I tried, but it didn't work out well. So I wonder if you know anything, plz let me know :D. Happy new year! Young.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
How to automatically apply different textures on terrain based on height? 5 Answers
Difference between TRANSFORM_TEX and MultiplyUV 0 Answers
Double Layer Material 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers