'Applying bump map in HLSL to diffuse texture
I have multiple layers on this shader which I am using to blend between to define map regions onto a terrain. I am looking to apply height map textures and normal textures to the diffuse textures being used but I am not sure how I should go about doing this. I have added the option to select a bump map for layer 2 but I am not sure how to apply this. I found the Unity documentation quite confusing regarding this since the set up was very different. Very new to HLSL and shaders, would appreciate any help regarding this.
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.1
_Smoothness("Smoothness", Range(0, 1)) = 0.1
_BlendSharpness ("Blend Sharpness", Range(0.06, 4)) = 0.75
[Space(30)]
_Layer1("Layer 1", 2D) = "white" {}
_Layer1Height("Layer 1 Height", Range(0, 20)) = 1
[Space(20)]
_Layer2("Layer 2", 2D) = "white" {}
_BumpMap("Bump map", 2D) = "bump" {}
_Layer2Height("Layer 2 Height", Range(0, 20)) = 1
[Space(20)]
_Layer3("Layer 3", 2D) = "white" {}
_Layer3Height("Layer 3 Height", Range(0, 20)) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
float _Metalness;
float _Smoothness;
UNITY_DECLARE_TEX2D(_MainTex);
float _BaseHeight;
float _BlendSharpness;
UNITY_DECLARE_TEX2D(_Layer1);
float _Layer1Height;
UNITY_DECLARE_TEX2D(_Layer2);
UNITY_DECLARE_TEX2D(_BumpMap);
float _Layer2Height;
UNITY_DECLARE_TEX2D(_Layer3);
float _Layer3Height;
struct Input
{
float2 uv_MainTex;
float2 uv_Layer1;
float2 uv_Layer2;
float2 uv_BumpMap;
float2 uv_Layer3;
float3 worldPos;
};
struct blendingData {
float height;
float4 result;
};
blendingData BlendLayer(float4 layer, float layerHeight, blendingData bd)
{
bd.height = max(0, bd.height - layerHeight);
float t = min(1, bd.height * _BlendSharpness);
bd.result = lerp(bd.result, layer, t);
return bd;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
blendingData bdata;
bdata.height = IN.worldPos.y - _BaseHeight;
bdata.result = UNITY_SAMPLE_TEX2D(_MainTex, IN.uv_MainTex);
float4 layer1 = UNITY_SAMPLE_TEX2D(_Layer1, IN.uv_Layer1);
float4 layer2 = UNITY_SAMPLE_TEX2D(_Layer2, IN.uv_Layer2);
float4 layer3 = UNITY_SAMPLE_TEX2D(_Layer3, IN.uv_Layer3);
bdata = BlendLayer(layer1, _Layer1Height, bdata);
bdata = BlendLayer(layer2, _Layer2Height, bdata);
bdata = BlendLayer(layer3, _Layer3Height, bdata);
o.Albedo = bdata.result;
o.Metallic = _Metallic;
o.Smoothness = _Smoothness;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
