- Home /
Road Texture Is Stretching
I'm building a city building game like Sim City or Cities XL. I'm working on creating a tool that will let players build roads. Although, im having an issue when the road stretches to reach the mouse cursor. The texture stretches as well. How can I make the texture tile instead of stretch?
Here is an example of what I have. http://www.youtube.com/watch?v=9_BhisKH0WU
heres an example of what i'd like to acheive http://www.youtube.com/watch?v=qdOHN9bVIz0
Answer by Statement · Mar 29, 2012 at 01:36 PM
You'd have to scale the texture coordinates as well.
how would I do that? I dont want to simply make the texture larger, I want to tile it over the road.
Answer by daivuk · Mar 29, 2012 at 09:19 PM
The way I am seeing is to play with the UVs in the shader directly. if your texture is good for 4 meters, you can do something like: uv = roadLength / 4 And pass that road length as a shader property.
i've never modified textures before in code. Could you provide an example of how I would do this?
Answer by brycedaawg · Aug 28, 2012 at 03:41 AM
Hey man, I don't know if this is of any use to you (or if you're still interested), but you can achieve a tiling effect like the roads in Cities XL by using an ObjectLinear shader. This basically projects your texture straight down onto your plane, the result of which won't stretch like a standard UV mapped texture, but instead will tile quite nicely. The advantage here is that you don't have to stretch each plane's UVs individually, in fact you don't even need to touch them. here's a shader I made that I'm using in a tile based side scroller I'm working on:
Shader "Custom/ObjectLinear"
{
Properties
{
_MainTex("Texture", 2D) = "" {TexGen ObjectLinear}
}
SubShader
{
Blend SRCAlpha OneMinusSRCAlpha
Tags {queue = transparent}
cull back
BindChannels
{
Bind "vertex", vertex
Bind "color", color
Bind "texcoord1", texcoord
}
Pass
{
SetTexture[_MainTex]
}
}
}
Remember, this shader will project down the z-axis, so for it to work on a plane, it has to be facing the z-axis. If you don't, you'll just get a line of pixels stretched across the plane. The standard Unity plane faces the y-axis, so it won't work for that, but you probably won't want to use it anyway since it has a lot of unnecessary geometry. So yeah, just create a plane in 3ds Max or Maya or something and rotate it's pivot so the plane faces the z-axis. Import it into Unity (and check that it's still facing the right direction since I'm pretty sure Maya flips the Z and Y axis'), create a new shader, put that code in I posted and, finally, chuck that shader onto your plane (as a GameObject). If you try stretching it, you'll see that the texture will tile, not stretch. 'Hope this helps.
thanks for replying, I will try out the script when I can. I have not been working on this particular project for awhile. But I never got a solution to the problem. Thanks again!