- Home /
Translate or Rotate a texture of a cube
Hello, I've seen an example and tried to apply it to a cube where I selected a texture/shader.
This doesn't seem to do anything to my cube, and I would like to see the texture translate or rotate on my cube.
var rotateSpeed = 30;
var texture : Texture;
function Start () {
var m : Material = new Material (
"Shader \"Rotating Texture\" {" +
"Properties { _MainTex (\"Base\", 2D) = \"white\" {} }" +
"SubShader {" +
" Pass {" +
" Material { Diffuse (1,1,1,0) Ambient (1,1,1,0) }" +
" Lighting On" +
" SetTexture [_MainTex] {" +
" matrix [_Rotation]" +
" combine texture * primary double, texture" +
" }" +
" }" +
"}" +
"}"
);
m.mainTexture = texture;
this.renderer.material = m;
}
function Update () {
// Construct a rotation matrix and set it for the shader
var rot = Quaternion.Euler (0, 0, Time.time * rotateSpeed);
var m = Matrix4x4.TRS (Vector3.zero, rot, Vector3(1,1,1) );
this.renderer.material.SetMatrix ("_Rotation", m);
}
Thank you
Answer by clunk47 · May 25, 2013 at 02:17 AM
Well I've done a simple script to make a texture move, the settings are public in the inspector. This is in C#, but should be easy for you to figure out how to put in JS. If you can't figure out how just let me know and I'll help. I use this for things like water, lava, whatever.. This allows movement of texture and normal map, just use the checkboxes in the inspector.
public class TexOffset : MonoBehaviour
{
public float scrollSpeedX = 0.1f;
public float scrollSpeedY = 0.1f;
public bool main = true;
public bool bump = true;
void Update ()
{
float offsetX = scrollSpeedX * Time.time;
float offsetY = scrollSpeedY * Time.time;
if(main)
{
renderer.material.SetTextureOffset("_MainTex", new Vector2(offsetX, offsetY));
}
if(bump)
{
renderer.material.SetTextureOffset("_BumpMap", new Vector2(offsetX, offsetY));
}
}
}
Answer by Gremory · May 25, 2013 at 02:43 PM
Thankyou very much, here is the code I used.
public var scrollSpeedX:float = 0.0;
public var scrollSpeedY:float = 0.1;
public var main = true;
public var bump = true;
function Update() {
var offsetX:float = scrollSpeedX * Time.time;
var offsetY:float = scrollSpeedY * Time.time;
if(main) {
this.renderer.material.SetTextureOffset("_MainTex", new Vector2(offsetX,offsetY));
}
if(bump) {
this.renderer.material.SetTextureOffset("_BumpMap", new Vector2(offsetX,offsetY));
}
}
Unfortunatly the material doesnt seem to support BumpMap, so it gives an error on that property. But beside that, it works like a charm.
The only problem now, is that while the "skybox" scrolls, I can see the divisions between them, is there a way to solve that?
Just uncheck the "Bump" box in the inspector to resolve that first issue. If(bump) is checking if that statement is true or not. If you uncheck that box, it won't try to move a bumpmap that doesn't exist. In order to fix the second issues, you need a seamless texture. This probably isn't appropriate for a skybox, I use it for something with a seamless texture where you can't tell it's a texture repeating. I'd use a sky DO$$anonymous$$$$anonymous$$ Simply model a sphere in Blender or something, flip the normals where they are facing in, unwrap the sphere and apply a uv map to it. Then you wouldn't even need this script. You would just rotate the sphere itself.
Your answer
Follow this Question
Related Questions
Hatching shader (Two o.Albedo ?) 1 Answer
Question about a good textures / terrain shader 1 Answer
A node in a childnode? 1 Answer
Dynamic Shader to Outline Adjacent Textures? 0 Answers
Creating a shader that shows a texture through a decal 0 Answers