Simple animated UV's?

I’ve been looking for a simple animated uv shader
similar to the water but without all of the fancy stuff. Just need to have animated uv coordinates.
Does anyone have one?
Thanks
Josh

there are some videos about shaders from unite08 done by amir. later part of the video shows how to do this.

going through that video now… also found another one that was this, but I couldn’t get it to work.
var scrollSpeed = 0.5;
function Update() {
var offset = Time.time * scrollSpeed;
renderer.material.mainTextureOffset = Vector2 (offset%1,0)
}

http://unity3d.com/support/resources/example-projects/animationapi

there’s an example of just that in that example project. I got it to work like i expected.

That’s not a shader, that’s a script (also, it works fine). You don’t need a shader to do UV animation.

–Eric

thanks!
I got the rotation one to work, but I’m getting an error on the movement script.

works now. Here it is for future posterity:


var scrollSpeed :float = 0.5;
var u : boolean = false;
var v : boolean = false;

function Update () {
var offset : float = Time.time * scrollSpeed % 1;
if (u == true v == true)
{
renderer.material.SetTextureOffset (“_MainTex”, Vector2(offset,offset));
}
else if (u == true)
{
renderer.material.SetTextureOffset (“_MainTex”, Vector2(offset,0));
}
else if (v == true)
{
renderer.material.SetTextureOffset (“_MainTex”, Vector2(0,offset));
}

}

2 Likes

Is it possible to do something similar directly in the shader ?

For exemple, I’m willing to animate only the uv offset for one of the textures on my object (a mask), and I would like to do that automatically for every models that has this material.

It could be really more clean to do it in the shader (if it is possible), than associating a script to every models that has this material, or to create a loop which will check every models to find the ones that has this material, or anything else.

Just animate renderer.sharedMaterial in one script, and it will animate everything that’s using the same material. Animating renderer.material makes a duplicate scene material and animates that. So if you have 10 scripts animating renderer.material, you actually end up with 10 materials, not 1. With sharedMaterial, you keep it down to just 1 only.

–Eric

Oh, thank you for the tip ! Could be really useful !

How can this script be edited to scroll a bump map?

Replace “_MainTex” with the name of the bumpmap property. This is “_BumpMap” for all built-in shaders.

Ok, I really DO need to do this in the shader, anyone actually know how to?

What I have:

Shader "UV Scroll" {
	Properties {
		_offset ("Scroll Speed", Range (-0.99, 0.99)) = 0.99
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_BumpMap ("Bump (RGB) Illumin (A)", 2D) = "bump" {}
	}
	SubShader {
		Pass{
CGPROGRAM
#pragma vertex vert
#include "UnityCG.cginc"

struct v2f{
	float4 uv : TEXCOORD0;
	float3 color : COLOR;
};
		
v2f vert (appdata_base v) {
	v2f o;
	o.uv = float4(v.texcoord.x + _offset,v.texcoord.y +  _offset, 0, 1);
	return o;
}
ENDCG
		}
	} 
	FallBack "Diffuse"
}

Errors I get:

Cg in program ‘vert’: error C0000: syntax error, unexpected identifier, expecting ‘;’ or ‘,’ at token “vert” at line 20

Cg in program ‘vert’: error C0501: type name expected at token “vert” at line 20

Cg in program ‘vert’: error C1002: the name “v2f” is already defined at (15) at line 20

Cg in program ‘vert’: error C1016: expression type incompatible with function return type at line 23

Cg in program ‘vert’: warning C7011: implicit cast from “float4” to “float3” at line 79

My CG is a little rusty, and I don’t know where it’s getting line 79 from when the shader only has 28 lines in it. Any help would be greatly appreciated.

(doh) I should have known that. Well I had had one there at some point, don’t know where it went.

Now I can’t pick up my offset variable, have edited the above code with the last fix.

Edit: worked that one out, soldiering on now. Thanks for the help

All the built-in shaders have scaling and tiling properties for their textures. You don’t need to write a shader to add an offset: it’s already there.

1 Like

I have been searching for ages to learn how to animate uv based on system time rather than frames per second.

I have built a clock www.unobtainium.eu I would like to get it running in unity as a working clock, I did this quite easily using guitext but i then realised guitext passes through all objects, so I now want to animate the clock getting information from the system time and animating the digits with animated uv’s

I found the 2 scripts below one for the the clock with guitext, and one for animating the uv’s I am sure the answer is in there somewhere but I am a modeller not a coder.

If you like my clock and would like to help me make it into an app, I would be forever indebted.

oops! I forgot to put the code in.

import System;

var date = DateTime.Now;

InvokeRepeating(“Start”, 2, 0.10);

function Start () {

var TheHour = System.DateTime.Now.ToString(“HH”);
var TheMinute = System.DateTime.Now.ToString(“MM”);
var TheSecond = System.DateTime.Now.ToString(“ss”);
var TheMonth = System.DateTime.Now.get_Month();
var TheDay = System.DateTime.Now.get_Day();
var TheYear = System.DateTime.Now.get_Year();
var tm : TextMesh = gameObject.GetComponent(TextMesh);
tm.text = TheSecond;
//renderer.material.color = Color(1, 0.7, 0.0, 1.2);
//var color : Color = Color(0.2, 0.3, 0.4, 0.5);
}

var uvAnimationTileX = 10; //number of columns per sheet.

var uvAnimationTileY = 1; //number of rows per sheet.

var framesPerSecond = 1; //replace this with get time ??

function Update () {

// Calculate index
var index : int = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);

// constructs the vector
var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);

// split into horizontal and vertical index
var uIndex = index % uvAnimationTileX;
var vIndex = index / uvAnimationTileX;

// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);

renderer.material.SetTextureOffset (“_MainTex”, offset);
renderer.material.SetTextureScale (“_MainTex”, size);
}

if (u == true v == true) requires a logical AND. two ampersands. &&.
For posterity.

Can anyone comment on the efficiency of running SetTextureOffset(“_MainTex”, Vector2(0,offset)); every frame? I’ve been curious about shaders and uv animation for a while but is this the best way to do it?