- Home /
Offset texture on an object
Hi! On https://constrvct.com/designs/new has an interesting feature: when you move the mouse texture in the central large square is shifted textures on 3D objects. How is it possible to implement in Unity? Does anyone have a ready script?
Answer by robertbu · Feb 11, 2014 at 04:50 PM
This can be done using Material.SetTextureOffset(). Below is a example script. Create and object and a Quad. Put the same material on both. Attach this script to the Quad. Drag and drop the other object to the 'object' variable in the script attached to the quad.
#pragma strict
var object : GameObject;
var factor = 0.01;
private var startPos : Vector2;
private var startOffset : Vector2;
function Start() {
// Could be done in the editor
renderer.material.SetTextureScale("_MainTex", Vector2(0.2, 0.2));
}
function OnMouseDown() {
startPos = Input.mousePosition;
startOffset = renderer.material.GetTextureOffset("_MainTex");
}
function OnMouseDrag() {
var v = (Input.mousePosition - startPos) * factor;
renderer.material.SetTextureOffset("_MainTex", startOffset - v);
object.renderer.material.SetTextureOffset("_MainTex", startOffset - v);
}
There is one problem with this code I did add the code to solve. This code uses mouse coordinates to calculate the amount of movement of the texture offset, so it is not platform independent. You'll need additional work if you are targeting multiple platforms.
In the editor do:
Game Object > Create Other > Quad
A Quad is a vertical plane composed of only two triangles, has a world size of 1 x 1, and the backside of the plane is the surface. So in a new scene, if you create a Quad at the origin, the material placed on the Quad will be visible to the camera. Quads were introduced around Unity 4.1. If you are using an earlier version of Unity, then you can use the CreatePlane editor script from the Wiki to produce the same kind of object.
Your answer
Follow this Question
Related Questions
Interactive Textures? 1 Answer
How to add render able Texture in Unity 3d for iOS 0 Answers
Infintely scrolling texture ends?(only scrolls through once) 1 Answer
How can I create a UV move tool? 0 Answers
Animation Tiling/Offset 0 Answers