- Home /
How to map texture across multiple objects?
Hi, I'm in the middle of creating a procedural dungeon that is built out of cubic template models. How do I apply a giant texture that spreads across multiple meshes (multiple models) and that tiles seamlessly (assuming the texture is tiled)? I'm trying to achieve a realistic look and this isn't possible if each tile has the same, small texture.
Would I have to write my own shader using CG or is this possible without resorting to that?
Thanks, Lucas.
I've been working on this for the last few hours without much luck. I don't think it's possible to import multiple objects into one uv-map in Blender so I've been trying to write a custom cg script to map the world position on the object ins$$anonymous$$d. So far I've been partly successful where I've been able to map one side of the dungeon, but the rest aren't working correctly. It looks like this: http://i.imgur.com/DjzLL.jpg
It seems that the shader is taking one side and just stretching it on the z-axis across the world ins$$anonymous$$d of mapping each side in 3d. Here's the shader: http://pastebin.com/70vNCEQk
What can I change to fix the problem?
Thanks for the help.
Answer by raminsh · Jun 27, 2011 at 07:40 AM
Hello actually i have done exact same thing for a big block of city, a jungle, a cave... However i didn't make a shader. What i did was simply inside belender or maya, and put all the uv set in one layout and then applied one single material that had that texture and ofcourse they were all multiple objects. The cave or the road was used in a procedural case. You just have to setup the uvs properly. I hope that helps.
I'm sorry, I'm a bit confused. Would you please elaborate? The objects I want to map are all different shapes that connect together, so their uv layout will look different because they are different shapes. What do you mean by putting them in one layout then?
Also, doing this, how does Unity know to map only a small part of the texture to each object and how does it know how to apply the offset of the texture correctly?
Answer by Borgo · Jun 27, 2011 at 03:16 PM
No need to write your own shader, just make your meshes to share the same UV name in your 3D model application. e.g.: We have a room with 3 meshes: floor, walls and ceiling. We want to bake a Lightmap in the 3D software and use in these 3 meshes. You can make 3 UVs with same name like "lm_uv". This UV will be shared with the meshes.
I'm using Blender. Do all the objects have to be in the same .blend file or does it not matter? And the 5 objects I'm using all have the same uv map name of "UVTex" but when I import the models into Unity, they are renamed to "(nameofobject)-$$anonymous$$". What am I doing wrong? Thanks.
borgo in right. if you are using blender, I recommend have all the objects in one scene and make them share the same material with that specific texture you have in $$anonymous$$d. however when you export them, all you need to do is export them seperately. I usually make sure that there are only one uvset for the objects because sometimes in gets messed up when imported to the engine.
I apologize, I can't seem to figure this out. All my objects are in one .blend file which are assigned to the same material. How do I then set up the UV maps correctly? (They just have to be the same name?) Would somebody be so kind as to give me a step-by-step tutorial in Blender?
okay I certainly do not $$anonymous$$d but the problem is I don't know how to layout the uv maps in blender, I use max and maya. but this probably would help you : http://wiki.blender.org/index.php/Doc:$$anonymous$$anual/Textures/UV/Editing_the_Layout have in $$anonymous$$d that you need to select all the objects before entering the uveditor so you can see all the related uvs and lay them out together.
Answer by Owen-Reynolds · Jun 29, 2011 at 04:40 AM
Your shader is doing a front projection. Switch to xz for a top projection. Your shader:
void surf (Input IN, inout SurfaceOutput o) {
float3 worlduv = IN.worldPos;
o.Albedo = tex2D (_MainTex, worlduv);
}
The texture lookup (last line) uses a float2. Since your worlduv is a float3, it just slices off the last part and uses worlduv.xy
for the lookup, which is a front projection.
For a top down, change the last line to:
tex2D (_MainTex, worlduv.xz); // FYI, yz is a side projection
Of course, that will map the floor just fine, but smear over the sides of walls. [EDIT] If your objects will always have flat sides facing at 0,90,180,270 degrees, you could guess a front/side/top projection using the normal (code has been tested):
struct Input {
float2 uv_MainTex; // unused
float3 worldNormal;
float3 worldPos;
};
void surf (Input IN, inout SurfaceOutput o) {
// Guess correct planar map from normal. 0.5 is an arbitrary cutoff
float2 UV;
// NOTE: assuming no bottom-facing, otherwise use abs()
if(IN.worldNormal.y>0.5) UV = IN.worldPos.xz; // top
else if(abs(IN.worldNormal.x)>0.5) UV = IN.worldPos.yz; // side
else UV = IN.worldPos.xy; // front
// 0.1 is an arbitrary x10 texture size scale
half4 c = tex2D (_MainTex, UV*0.1);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
The edges all have bad seems, but there's no way around that.
Ah I see. Is there anyway to texture using a front projection without touching the top projection at all, so that I can use o.Albedo += tex2D (_$$anonymous$$ainTex, worlduv.xy) to add them both together? If not, could I use another method within the same shader? It will be hard to use separate shaders because that requires me to use separate models for each plane which would greatly complicate things.
You could make several prefabs with the same plane, rotated however, and different shaders. Otherwise, I added to my post how to do a "guess correct projection" shader.
Answer by Wesww · Jul 27, 2011 at 04:15 AM
Hey cabot, what did you end up using in the end?
Also, did you ever get Owen Reynolds world based uv shader to work?
Your answer
![](https://koobas.hobune.stream/wayback/20220612123038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Procedural Texturing on Multiple Terrain Mesh 1 Answer
Seamless 3D tiles 1 Answer
Do I need to split my mesh up to properly UV texture it? 1 Answer
Assigning UV Map to model at runtime 0 Answers