Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rageingnonsense · May 29, 2015 at 05:39 PM · shadersplatmap

UV offsets and splat maps issue

I am trying to add road placement functionality to my game. I've decided to go with a splatmap approach, where the splat map defines the road locations, and the road types. I use the following shader to accomplish this:

 Shader "Custom/Terrain" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
 
         // For roads
         _RoadMap ("Road Splat Map", 2D) = "white" 
         _RoadMapIDDirt ("Dirt Road Value", Float) = 128
         _RoadDirt ("Road Dirt Texture", 2D) = "white" {}
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         // for debugging
         #pragma enable_d3d11_debug_symbols
 
         sampler2D _MainTex;
 
         // road stuff
         sampler2D _RoadMap;
         float _RoadMapIDDirt;
         sampler2D _RoadDirt;
 
         struct Input {
             float2 uv_MainTex;
             float2 uv_RoadMap;
         };
 
         half _Glossiness;
         half _Metallic;
         fixed4 _Color;
 
         void surf (Input IN, inout SurfaceOutputStandard o) {        
             // Albedo comes from a texture tinted by color
             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
             o.Albedo = c.rgb;
             // Metallic and smoothness come from slider variables
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = c.a;
             
             // Handle roads
             // Roads are drawn using a splat map where one pixel is equivalent
             // to one tile in the terrain grid.
             // Color defines the road type
             fixed4 road = tex2D (_RoadMap, IN.uv_RoadMap - (0.5 / 128));
             if(road.a == 1) {
                 if(round(road.r * 255) == _RoadMapIDDirt) {
                     o.Albedo = tex2D (_RoadDirt, IN.uv_MainTex);                    
                 }
             }
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }

The game is grid based, and currently is 128x128 tiles, defined by a 129x129 vertex plane. _RoadMap is a 128x128 pixel texture, with filter mode point.

The problem I am having is that all of the roads are offset by 0.5 grid units, and as such they show up between tiles, not on tiles. I understand this has to do with the way UVs are sampled, and I tried to fix this (as demonstrated in the above shader). While this fixes it in the dead center of the terrain, there is still a slight offset that increases the further away from center I get:

alt text

I use the following splat map:

alt text

How could I go about getting the road to line up with the grid properly?

uvoffset.jpg (88.2 kB)
roadmap.png (3.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rageingnonsense · May 29, 2015 at 06:38 PM

I think I figured this out on my own. The trick was to sample the _RoadMap texture like so:

 fixed4 road = tex2D (_RoadMap, IN.uv_RoadMap / (_RoadMap_TexelSize.xy * (_RoadMap_TexelSize.zw - 1)));

xy is the texel size, and zw is the size of the texture in pixels. Not sure why I had to subtract 1 from zw; but this works.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fourthings · Jun 21, 2019 at 03:24 PM 0
Share

What type is Roadmap_TexelSize? Could you post the whole shader?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader - Texture Array with Blendmap Neighbour textures bleeding 1 Answer

How to modify output color of built-in Terrain Shader with more than 4 Splat Textures? 2 Answers

How to force the compilation of a shader in Unity? 5 Answers

Splat Map shader no longer working in Unity 5 1 Answer

Splat Map with transition texture? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges