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
1
Question by robert_d · Mar 04, 2012 at 02:03 AM · shadertexturelerp

Mixing two textures using lerp function doesn't work as it should

I want to get smooth transitions between texture layers, e.g. sand texture smoothly passes into grass texture. I tried to use lerp function for this, but it doesn't work as it should. Here is simple shader that mixes 2 textures

 Shader "Custom/LerpDemo"
 {
     Properties
     {
         _Sand("_Sand", 2D) = "black" {}
         _TerrainFloor("_TerrainFloor", 2D) = "black" {}
         _Blend("_Blend", Range(0,1) ) = 0.5
     }
    
     SubShader
     {
         Tags
         {
             "Queue"="Geometry"
             "IgnoreProjector"="False"
             "RenderType"="Opaque"
         }
  
         Cull Back
         ZWrite On
         ZTest LEqual
         ColorMask RGBA
        
         CGPROGRAM
         #pragma surface surf BlinnPhongEditor  
         #pragma target 2.0
        
         sampler2D _TerrainFloor;
         sampler2D _Sand;
         float _Blend;
  
         struct EditorSurfaceOutput {
             half3 Albedo;
             half3 Normal;
             half3 Emission;
             half3 Gloss;
             half Specular;
             half Alpha;
             half4 Custom;
         };
        
         inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
         {
             half3 spec = light.a * s.Gloss;
             half4 c;
             c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
             c.a = s.Alpha;
             return c;
         }
  
             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
             {
                 half3 h = normalize (lightDir + viewDir);
                
                 half diff = max (0, dot ( lightDir, s.Normal ));
                
                 float nh = max (0, dot (s.Normal, h));
                 float spec = pow (nh, s.Specular*128.0);
                
                 half4 res;
                 res.rgb = _LightColor0.rgb * diff;
                 res.w = spec * Luminance (_LightColor0.rgb);
                 res *= atten * 2.0;
  
                 return LightingBlinnPhongEditor_PrePass( s, res );
             }
            
             struct Input {
                 float2 uv_Sand;
                 float2 uv_TerrainFloor;
             };
  
             void surf (Input IN, inout EditorSurfaceOutput o)
             {
                 o.Normal = float3(0.0,0.0,1.0);
                 o.Alpha = 1.0;
                 o.Albedo = 0.0;
                 o.Emission = 0.0;
                 o.Gloss = 0.0;
                 o.Specular = 0.0;
                 o.Custom = 0.0;
                
                 float4 Sampled2D1=tex2D(_Sand,IN.uv_Sand.xy);
                 float4 Sampled2D0=tex2D(_TerrainFloor,IN.uv_TerrainFloor.xy);
                 float4 Lerp0=lerp(Sampled2D1,_Blend.xxxx,Sampled2D0);
                
                 o.Albedo = Lerp0;
  
                 o.Normal = normalize(o.Normal);
             }
         ENDCG
     }
     Fallback "Diffuse"
 }

So in theory if _Blend equals 0 I should get the first texture and if it is 1 then I should get the second one. But it doesn't work that way - I am getting much darker texture than original and in case if one texture is lighter then one can see the darker texture underneath. 1. Why it doesn't work?

2.If someone knows how to get smooth transitions between texture layers, e.g. sand texture is used on terrain up to 1 meter height , grass texture is used on terrain from 1 to 10 meters height etc., please let me know. I can't use Unity Terrain Toolkit because I generate terrain on the fly, I don't use Unity Terrain objects.

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

2 Replies

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

Answer by aldonaletto · Mar 04, 2012 at 04:22 AM

I suspect that the lerp parameters are in the wrong order - at least according to the CG NVidia tutorial, the order should be:

   float4 Lerp0=lerp(Sampled2D1,Sampled2D0,_Blend.xxxx);

Another thing: maybe you should use _Blend.x instead of _Blend.xxxx.

About the terrain layers, the Unity Terrain Toolkit allows you to texture in function of the height, like you want.

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 robert_d · Mar 04, 2012 at 01:00 PM 0
Share

I can't use Unity Terrain Toolkit because I generate terrain on the fly, I don't use Unity Terrain objects.

avatar image
0

Answer by Owen-Reynolds · Mar 04, 2012 at 04:24 AM

Lerp is (color1, color2, 0to1 blend value). You've got the order flipped. Use: o.Albedo=lerp(Sampled2D0, Sampled2D1, _Blend);

To blend over a range, you just need to scale it into a 0-1. Suppose you have height in H and you want it to blend as it goes from 1-10. Subtract the start and divide by the range: lerpVal = (H-1)/9; (check: (10-1)/9 is 1.) Then you have to stop if from going out of 0-1, so use: lerpVal=clamp( (H-1)/9, 0, 1);

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Two Textures Dissolve Shader (clip & lerp) 1 Answer

Material trouble 0 Answers

How can i get my quad to only render my texture without stretching it? 1 Answer

Help Shader: Texture mask using sliced mask and tiled texture. 1 Answer

How to get precise pixel values form a Texture2D using uv coordinates. 1 Answer


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