码迷,mamicode.com
首页 > 其他好文 > 详细

Skyshop.Detail Maps

时间:2019-11-25 00:21:23      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:surface   utf-8   version   map   yuv   htm   添加   ack   stat   

Secondary Maps(Detail Maps) & Detail Mask
  增加模型细节,而不需要使用单张的超大贴图。
  应用:增加皮肤细节,比如毛孔;砖墙添加细小的裂缝和青苔;大型金属容器上增加一些小的划痕和擦痕。
 
toolbag.shader: 
//inherits "normalMap.frag"
USE_TEXTURE2D(tDetailNormalMap);
USE_TEXTURE2D(tDetailWeightMap);
uniform float    uDetailWeight;
uniform vec4    uDetailTiling;    //xy-scale, zw-offset
uniform vec4    uDetailWeightSwizzle;
uniform float    uDetailUseSecondaryUV;    //0.0 - use primary uv, 1.0 - use secondary
uniform vec3    uDetailNormalMapScale;    //typically 2,2,2
uniform    vec3    uDetailNormalMapBias;    //typically -1,-1,-1
void    SurfaceDetailNormalMap( inout FragmentState s )
{
    SurfaceNormalMap(s);
    
    //look up detail normal
    vec2 uv = lerp( s.vertexTexCoord.xy, s.vertexTexCoordSecondary.xy, uDetailUseSecondaryUV );
    uv = uv*uDetailTiling.xy + uDetailTiling.zw;
    vec3 dn = texture2D( tDetailNormalMap, uv ).xyz;
    dn = uDetailNormalMapScale*dn + uDetailNormalMapBias;
    //ortho-normalization of new tangent basis
    vec3 T = s.vertexTangent;
    vec3 B = s.vertexBitangent;
    vec3 N = s.normal;
    T -= dot(T,N)*N;
    T = normalize(T);
    B -= dot(B,N)*N + dot(B,T)*T;
    B = normalize(B);
    
    //blend in the detail normal
    dn =    dn.x * T +
            dn.y * B +
            dn.z * N;
    float detailWeight = dot( texture2D( tDetailWeightMap, s.vertexTexCoord ), uDetailWeightSwizzle );
    detailWeight *= uDetailWeight;
    s.normal = normalize( s.normal + dn * detailWeight );
}
#undef Surface
#define    Surface    SurfaceDetailNormalMap

 

unity.shader:

#ifdef _NORMALMAP
half3 NormalInTangentSpace(float4 texcoords)
{
    half3 normalTangent = UnpackScaleNormal(tex2D (_BumpMap, texcoords.xy), _BumpScale);
#if _DETAIL && defined(UNITY_ENABLE_DETAIL_NORMALMAP)
    half mask = DetailMask(texcoords.xy);
    half3 detailNormalTangent = UnpackScaleNormal(tex2D (_DetailNormalMap, texcoords.zw), _DetailNormalMapScale);
    #if _DETAIL_LERP
        normalTangent = lerp(
            normalTangent,
            detailNormalTangent,
            mask);
    #else
        normalTangent = lerp(
            normalTangent,
            BlendNormals(normalTangent, detailNormalTangent),
            mask);
    #endif
#endif
    return normalTangent;
}
#endif

// 法线在切空间叠加
half3 BlendNormals(half3 n1, half3 n2)
{
    return normalize(half3(n1.xy + n2.xy, n1.z*n2.z));
}

 

Skyshop.Detail Maps

标签:surface   utf-8   version   map   yuv   htm   添加   ack   stat   

原文地址:https://www.cnblogs.com/sifenkesi/p/11925177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!