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 chilunliu · Mar 01, 2014 at 01:26 PM · shadertexturecgfragment

Blend textures in shader

Hi all;

I'm trying to blend three textures without much success. This is what I have at the moment:

alt text

 Shader "Custom/somethingElseElse"
 {
     Properties
     { // two textures
     _tex0 ("Texture1", 2D) = "white" {} // snow
     _tex1 ("Texture2", 2D) = "white" {} // rock
     _tex2 ("Texture3", 2D) = "white" {} // grass
     
     }
  
     SubShader
     {
        Tags {Queue = Geometry} // default for unity, opaque
        ZWrite Off // turn off as we are drawing a semi-transparent object
        Lighting Off // turn off surface lighting
        Pass // do this in one draw call
        {
        CGPROGRAM
             // default for unity, basic shader model 2.0
          #pragma target 2.0 
          // vertex program vert provided for use
          #pragma vertex vert
          // fragment program frag provided for use
          #pragma fragment frag
          // enable use of many cg helper functions
          #include "UnityCG.cginc"
  
          sampler2D _tex0; // enable properties for a 2d texture
          sampler2D _tex1; // enable properties for a 2d texture
           sampler2D _tex2;
  
           // call the struct that takes data from vertex to fragment - v2f
          struct v2f 
          {
           float4 pos : POSITION;
           float4 color : COLOR0; // first colour 
           float4 fragPos : COLOR1; // 2nd colour
           float2  uv : TEXCOORD0; // first UV coordinate
          };
  
          float4 _tex0_ST; // get tiling/offset info of tex0
  
           // takes a appdata_base called v and returns a v2f struct called vert
          v2f vert (appdata_base v) // appdata_base is a vertex holding pos, normal and one texture coordinate
          {
           v2f o;
           o.pos = mul (UNITY_MATRIX_MVP, v.vertex); // UNITY_MATRIX_MVP - current model*view*projection matrix
           o.fragPos = o.pos;
           o.uv = TRANSFORM_TEX (v.texcoord, _tex0); // TRANSFORM_TEX - part of UnityCG.cginc,  make sure texture scale&offset is applied ok
           o.color = float4 (1.0, 1.0, 1.0, 1);
           return o;
          }
  
          half4 frag (v2f i) : COLOR
          {
           float2 q = i.uv.xy / float2(1,1);
           float3 oricol = tex2D (_tex0,float2(q.x,q.y)).xyz;
           float3 col = tex2D (_tex1,float2(i.uv.x,i.uv.y)).xyz;
           float3 othercol = tex2D (_tex2,float2(q.x*0.5,q.y*0.5)).xyz; // third texture
           float comp = smoothstep( 0.1, 0.9, sin(0.5) ); // Interpolate between 0.2 and 0.7 with smoothing at intervals sin(0.5) - default 0.2, 0.7
           col = lerp(col,oricol, clamp(-2.0+2.0*q.y+3.0*comp,0.0,1.0)); // start mixing between min and max - lerp and clamp
           return float4(col,1);
          }
        ENDCG
        }
     }
     FallBack "VertexLit" // default failsafe shader to use
 }

I've managed to blend two textures so far, however whilst trying to add another texture - grass in my case, I don't know what to do as the lerp function i'm using is unable to accept another float parameter (othercol in my case). I have a plane that currently has a material with 3 textures assigned - snow, rock and grass, in which I'm hoping to blend between them smoothly (snow > rock > grass)

I would appreciate any advise on how to tackle this problem. Thank you

capture.jpg (10.7 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

0 Replies

· Add your reply
  • Sort: 

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

Projected Texture Shader Issues 1 Answer

Shader that renders fragment behind 0 Answers

Need help with this Shader; camera Solid color is being rendered on skybox. 0 Answers

How to draw a tiled texture on a post process shader 0 Answers

Find out if an object is in front of a target in fragment shader? 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