- Home /
Blend textures in shader
Hi all;
I'm trying to blend three textures without much success. This is what I have at the moment:
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
Your answer
Follow this Question
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