- Home /
The question is answered, right answer was accepted
Is it possible to texture objects like this in unity? (Video inside) [Solved. Wanted screen space texture]
Right, so I have a lot of differently shaped and sized objects with the same texture, and I wan't them all to match up when they're next to each other. Since they scale in all 3 dimensions during runtime, I can't(or at least don't know how to) scale the texture in different values on different sides of the object.
But then I saw my kid watching this cartoon. http://www.youtube.com/watch?v=Uoowh63Mckg Notice how the purple (and later the blue) clothes are "textured". The clothes move, but the texture always stays in place. Like it has a global position independent of the textured object. If my texture was set up like this, I could place objects anywhere and whatever shape I wanted, they would always match up. So is this possible?
Answer by Philipp · Jul 03, 2013 at 10:37 AM
I believe what you want is a shader that does texturing in screen space. There is one that you might be able to modify, in Unity's surface shader examples here: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html - search that page for "Detail Texture in Screen Space". You'll want to rewrite this part:
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
screenUV *= float2(8,6);
o.Albedo *= tex2D (_Detail, screenUV).rgb * 2;
to say
float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
o.Albedo = tex2D (_Detail, screenUV).rgb;
Which makes it use only the Detail texture and ignore the Main texture. If you want to be thorough, remove the lines "_MainTex ("Texture", 2D) = "white" {}" and "float2 uv_MainTex;" and "sampler2D _MainTex;" as well.
Ok, this is embarrassing. This is exactly what I asked for. Problem is this is not what I wanted. All the textures do line up perfectly now, regardless of what shape the object is and where I place it, HOWEVER, when the camera moves, so do the textures.
I did a bit more digging, and I found this http://wiki.unity3d.com/index.php?title=WorldUVs
I think this is what I'm looking for, but I can't get it to work... For one, its says to select "my parent world object in the inspector". Uhh.. what? $$anonymous$$y scene is empty but for the camera and plane where I build everything on. The objects I want textured properly are prefabs and only get scaled properly and instantiated during runtime. Help?
EDIT: Found something that works and does EXACTLY what I was looking for all along. http://www.blog.radiator.debacle.us/2012/01/joys-of-using-world-space-procedural.html
@Philipp I'm tagging your answer as correct, as well... it is. Its my fault I didn't know what to ask for. And in the end, its your answer that got me on the right track, so thank you very much!