You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Height Texture用途咨询:Asset Store下载的XX_height.png在片元着色器中的用法

高度纹理(Height Texture)在片元着色器中的常见用法

这类高度纹理无需修改顶点高度,核心是通过采样纹理值来影响片元的光照计算或材质属性,下面是几个实用场景:

1. 视差映射(伪位移效果)

不用动顶点,通过高度值偏移UV,让平面模拟出凹凸深度感,适合墙面、地面这类大面积平面:

// Unity Shader 示例代码
sampler2D _MainTex;
sampler2D _HeightTex;
float _HeightScale; // 控制位移强度,建议取值0~0.1

float2 ParallaxOffset(float height, float3 viewDir, float scale) {
    float3 viewDirTangent = normalize(mul(viewDir, unity_WorldToObject));
    return viewDirTangent.xy * (height * scale);
}

fixed4 frag(v2f i) : SV_Target {
    float height = tex2D(_HeightTex, i.uv).r;
    float2 offset = ParallaxOffset(height, i.viewDir, _HeightScale);
    fixed4 col = tex2D(_MainTex, i.uv + offset);
    return col;
}

2. 调整光影强度,模拟凹凸细节

用高度值控制漫反射、高光的强弱,模拟凸起/凹陷处的光影差异,适合做磨损、刮痕效果:

sampler2D _HeightTex;
float _HeightBias; // 调整高度基准值

fixed4 frag(v2f i) : SV_Target {
    float height = tex2D(_HeightTex, i.uv).r;
    // 凹陷处漫反射更暗
    fixed diff = saturate(dot(i.lightDir, i.normal) * (height + _HeightBias));
    fixed4 col = _MainColor * diff;
    // 凸起处高光更强
    float spec = pow(saturate(dot(i.halfDir, i.normal)), _Gloss) * height;
    col += _SpecColor * spec;
    return col;
}

3. 作为材质混合遮罩

用高度值区分区域,实现不同材质的自然过渡,比如光滑瓷砖和磨损水泥的拼接:

sampler2D _HeightTex;
sampler2D _TileTex;
sampler2D _ConcreteTex;
float _BlendThreshold; // 混合阈值,控制过渡范围

fixed4 frag(v2f i) : SV_Target {
    float height = tex2D(_HeightTex, i.uv).r;
    fixed4 tileCol = tex2D(_TileTex, i.uv);
    fixed4 concreteCol = tex2D(_ConcreteTex, i.uv);
    // 高度高的区域显示瓷砖,低区域显示水泥
    fixed4 col = lerp(concreteCol, tileCol, saturate((height - _BlendThreshold)/0.1));
    return col;
}

4. 半透明材质的次表面散射遮罩

针对皮肤、玉石这类材质,用高度值控制次表面散射强度,凸起处散射弱、凹陷处散射强:

sampler2D _HeightTex;
float _SSSIntensity;

fixed4 frag(v2f i) : SV_Target {
    float height = tex2D(_HeightTex, i.uv).r;
    float sssAmount = (1 - height) * _SSSIntensity;
    fixed4 baseCol = tex2D(_MainTex, i.uv);
    fixed4 sssCol = baseCol * sssAmount * dot(i.lightDir, i.normal);
    return baseCol + sssCol;
}

内容的提问来源于stack exchange,提问作者user754458

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:05:19