I’m sorry to bump an old thread but I could use some help with this too. I’m trying to create an effect like Arthur did, where the screen space uv is scaled and translated based on the object’s world position relative to the camera. I have scaling based on the distance between the camera and the object itself, but I can’t figure out how to make the uv translate based on the object position.
Here’s my material graph for the screen space uvs so far.
I’m not used to node-based editors so can’t help you there, but I think what you need is to convert the object position from world space to screen space, and then use the XY values as an offset. I also wonder if the Z value could actually replace your distance-based calculation here.
Hey guys! I was just trying to do something similar in Unity 5.6. Really love that wallpaper-like effect!
I used @Arthur_Gould’s shader as a base but for some reason the texture still scrolls quite a bit when I move the object or the camera. Do you know if something changed in Unity recently? I couldn’t figure out what’s wrong, yet
Does it work if you use the code verbatim? I don’t have 5.6 right now so I can’t test it but I can’t imagine anything that would have changed to cause it to not work.
One thing to be careful with though is that this only works on meshes that are not batched. If you have two batched objects using this shader, then the texture will only follow one of them (because they’ll share their UNITY_MATRIX_MVP)
@Arthur_Gould Just checked again with a clean project and your original code in Unity 5.6 and it looks like there actually is a bit of a scaling issue now. Maybe something changed with the clip space but I couldn’t figure it out, yet
But it looks like it doesn’t scale a lot, only when reaching the far clipping plane. Gonna try to figure out what happens there
Ok so the problem was that when I cleaned up the shader I also forgot that you can’t just use the POSITION semantic from the vertex shader and expect to be able to use the clip position in the fragment shader. It mostly works in the “Scene” tab (which is what I tested before posting, also why it might appear to work sometimes and not others), but not in the “Game”. so I just changed the code to store the clip space in TEXCOORD0 and that appears to work for both (but the texture is inverted along the Y axis in the “Scene” tab, because Unity is silly like that).
Here’s the new code. Sorry about the formatting, still haven’t found the best way to copy code into this forum:
Wow, thanks @Arthur_Gould for the explanation and the update!
Interestingly it didn’t work for me at first but I realized when changing
screenUV *= SSobjectPosition.z;
to
screenUV *= SSobjectPosition.w;
in the GetScreenUV function it works perfectly. No idea though why the .w component works like that now. .z would make much more sense.
Now the only thing left I’d like to figure out is if it’s possible to have the texture scale when the object scales.
I’ll try to fiddle a bit and see how far I can get
I’ll keep you up to date and many thanks for the help!
But I would like to be able to constrain uv position and scale based on object’s position and distance from camera. I tried your examples but I also faced some issues and for the moment I don’t see how to fix them. Here’s the code :