[SOLVED] Scrolling the projectors cookie?

Hey folks!
I’m trying to scroll the texture of a projector, but I’m not very successfull.

On a mesh (as long as there is a mesh renderer) one can scroll for example a water texture, using:

// Scroll main texture based on time

var scrollSpeed = 0.1;
function Update () 
{
    var offset = Time.time * scrollSpeed;
    renderer.material.SetTextureOffset ("_BumpMap", Vector2(offset/-7.0, offset));
    
    renderer.material.SetTextureOffset ("_MainTex", Vector2(offset/10.0, offset));
}

However, a projector doesn’t have a mesh renderer.
Is there any way to do this?

I’m trying to project an image and would like the image to scroll (it’s a tiling texture).
What would be your approach to accomplishing this?

-TT

http://forum.unity3d.com/viewtopic.php?p=205335

Seek and Ye shall find :stuck_out_tongue:

Thanks Amazingint…

…is there any way to do this in Unity Indie?
This post describes a way to do it while custom coding the shader, which of course isn’t possible in the Indie version.

-TT

I don’t know what you mean…of course that’s possible in Indie. There’s nothing Pro-only about shaders at all.

–Eric

Errr… I must have been misstaken.
Whenever I click ‘Edit shader’ though, nothing at all happens… So I’ve always thought that this is because of a pro-only feature…
It isn’t?

-TT

Nope…all of the Pro-only features are shown here. You can’t edit the built-in shaders, Pro or otherwise.

–Eric

Thanks Eric.
Now, if this post wasn’t embarassing enough: how does one create a shader?
Simply by typing the ShaderLab code into a .shader file and dragging it into the project panel??

-TT

Yep.

–Eric

Ok… that’s easy… thanks!

So I checked the linked forum post up top, and I get how to make a shader.

What I don’t get is the part where they animate it:

void Start() 
{ 
   // Get the projector matrix... 
   _Projector = p.material.GetMatrix("_Projector"); 
} 

void Update() 
{ 
   // Scroll the texture: 
   _Projector.m13 += scrollSpeed * Time.deltaTime; 

   if(_Projector.m13 > 1f) 
      _Projector.m13 = 0; 

   Shader.SetGlobalMatrix("_AnimatedProjector", _Projector); 
}

Of course, ‘p’ is not a defined variable, and I don’t get where the “_AnimatedProjector” comes from - is that the name of a custom written shader?
Or is the name “_Projector”?

I don’t have a clue what’s going on in this script… or what shader it’s trying to access…

Could you help??

-TT

I’m seriously lost here and yet so close.
In the other forum post someone else already solved the problem, I just lack the understanding…
Can anyone please explain??

-TT

Here’s what I got so far:

var proj : Projector;
var mat;
var scrollSpeed = 0.5;

function Start() 
{ 
   // Get the projector matrix... 
   proj  = GetComponent (Projector);
   
   mat = proj.material.GetMatrix("_Projector"); 
   
} 

function Update()
{	
	mat.m13 += scrollSpeed * Time.deltaTime;
	
	if(mat.m13 > 1.0) { mat.m13 = 0; }
	
	print(mat.m13);
	
	//proj.material.SetGlobalMatrix(mat);
	//Shader.SetGlobalMatrix("_AnimatedProjector", mat);
}

I understand that ‘m13’ is the 13th item in the 4x4Matrix.
What it does I have no clue about, but I guess it’s the scrolling part.
It doesn’t say anything in the documentation about accessing certain parts of a 4x4 matrix, but I guess that works.

All I need to do now is set the m13 back into the projector’s shader.

That’s where the last two lines come in (the commented).
They both return errors though…

Is there any way to set the m13 back onto the matrix of the projector??

Thanks!

-TT

Sweet, the original author of the script came out and solved it.
If you’re looking for the result, check the last few posts here:
http://forum.unity3d.com/viewtopic.php?p=265851#265851

-TT

The link to the post is broken is there any info on the solution?

It’s been about as long since I’ve used projectors as it’s been since this post was made, but this ought to work based on the documentation:

Projector someProjector;
someProjector.material.mainTextureOffset = new Vector2(offset, 0f);

(Sidenote: This may be the longest-dead necropost I’ve ever seen that’s actually appropriate to necro! Given that the solution shown before is a dead link but this can still come up on google results.)

2 Likes