How to make a Diffuse Bump Blending (440706)

Hello every one.

I’m trying to realize a diffuse bump blending (whith shader).

For the moment I wrote this :

    Shader "Diffuse Bump Blending" {
    Properties {
        _Blend ("Blend", Range(0, 1)) = 0.5 
        _MainTex ("Texture", 2D) = ""
        _Texture2 ("Texture 2", 2D) = ""
        _BumpMap ("Bumpmap", 2D) = "bump" {}
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert
      struct Input {
        float2 uv_MainTex;
        float2 uv_BumpMap;
      };

      sampler2D _MainTex;
      sampler2D _BumpMap;
      void surf (Input IN, inout SurfaceOutput o) {
        o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
        o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

I don’t know how to add the blending effect to this shader. I have this code for the blending :

Shader "Blend 2 Textures" { 

Properties {
    _Blend ("Blend", Range(0, 1)) = 0.5 
    _MainTex ("Texture 1", 2D) = "" 
    _Texture2 ("Texture 2", 2D) = ""
}

SubShader { 
    Pass {
        SetTexture[_MainTex]
        SetTexture[_Texture2] { 
            ConstantColor (0,0,0, [_Blend]) 
            Combine texture Lerp(constant) previous
        }      
    }
} 

}

Any idea ?

Thanks !

Well, use lerp.

I think the syntax is lerp(texture1.rgb, texture2.rgb, blendmap.a)

EDIT:

Also, I saw that in your shader you don’t have a map specifically to drive the blending. What are you planning to use? The blend wants 1 channel, so it should preferably the alpha channel in one of the maps you are using.

I’m a bit lost. I just want to import the second shader in the first one. Mix blending of 2 texture with a Diffuse bump effect. Is it possible?

Presumably Ceetix wants the blending done uniformly with the _Blend property, as it is done in the fixed function shader.

Indeed this is what I would like.

Well, he could still use lerp and use a value instead of the blend map (that works, I think, right?), or just have a gray alpha map.

Sample both albedo textures (you’re only sampling _MainTex right now), and then use lerp() to blend between the samples.

Well, here you go :

    Shader "Diffuse Bump Blending" {
    Properties {
        _Blend ("Blend", Range(0, 1)) = 0.5 
        _MainTex ("Texture", 2D) = ""
        _Texture2 ("Texture 2", 2D) = ""
        _BumpMap ("Bumpmap", 2D) = "bump" {}
    }
    SubShader {
      Tags { "RenderType" = "Opaque" }
      CGPROGRAM
      #pragma surface surf Lambert
      struct Input {
        float2 uv_MainTex;
        float2 uv_BumpMap;
      };
	  float _Blend;
      sampler2D _MainTex;
	  sampler2D _Texture2;
      sampler2D _BumpMap;
      void surf (Input IN, inout SurfaceOutput o) {
		o.Albedo = lerp( tex2D (_MainTex, IN.uv_MainTex).rgb, tex2D (_Texture2, IN.uv_MainTex).rgb, _Blend);
        o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

…or just wait until AcidArrow does all the work for you!

Yeah, I was going to avoid doing that so he could understand a couple of things, but in the end I didn’t :stuck_out_tongue:

So since I went ahead and did that, I might as well go ahead and explain what I did.

You had the _Blend and _Texture2 properties in the shader, but you weren’t doing anything with them. Actually, you weren’t even sampling them as Daniel said. So I added the float _Blend; and sampler2D _Texture2; lines, so that now you can actually use those properties in the void surf (which actually does all the work).

So you had o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; which took the _MainTex texture and applied it on the model using the uv_MainTex UVs. But that’s not what you wanted. You wanted to mix two textures together based on a value and the lerp (short for linear interpolation), does exactly that (well, not specifically textures, it blends floating point values, see here for example). So for the first parameter of lerp, we have tex2D (_MainTex, IN.uv_MainTex).rgb which is exactly what you had in the albedo before, which is the first texture. For the second parameter, I replaced _MainTex with _Texture2, so that the second texture is used. Finally, for the blend amount we used the _Blend property.

…And typing this took me way more time than what it took to fix the shader, so I feel kind of stupid explaining something so simple… :stuck_out_tongue:

Thanks for all your explanation AcidArrow.
I understand a bit more but I think I still have work to do to understand everything all.

Thanks again.