Unity Shader Example 8 (光照贴图)

Shader "LightMap"
{
    Properties {
        _Color ("Main Color", Color) = (1, 1, 1, 1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _LightMap ("Lightmap (RGB)", 2D) = "white" {}
    }

    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            fixed4 _Color;
            float4 _MainTex_ST;
            float4 _LightMap_ST;
            sampler2D _MainTex;
            sampler2D _LightMap;

            struct appdata_t {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                float2 texcoord1 : TEXCOORD1;
            };

            struct v2f {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                float2 texcoord1 : TEXCOORD1;
            };

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.texcoord1 = TRANSFORM_TEX(v.texcoord1, _LightMap);
                return o;
            }

            fixed4 frag (v2f i) : COLOR
            {
                fixed4 c = _Color * tex2D(_MainTex, i.texcoord);
                c.rgb *= DecodeLightmap(tex2D(_LightMap, i.texcoord1));
                return c;
            }
            ENDCG
        }
    }
}


参考:


#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

// Decodes HDR textures
// handles dLDR, RGBM formats
// Called by DecodeLightmap when UNITY_NO_RGBM is not defined.
inline half3 DecodeLightmapRGBM (half4 data, half4 decodeInstructions)
{
	// If Linear mode is not supported we can skip exponent part
	#if defined(UNITY_NO_LINEAR_COLORSPACE)
	# if defined(UNITY_FORCE_LINEAR_READ_FOR_RGBM)
		return (decodeInstructions.x * data.a) * sqrt(data.rgb);
	# else
		return (decodeInstructions.x * data.a) * data.rgb;
	# endif
	#else
		return (decodeInstructions.x * pow(data.a, decodeInstructions.y)) * data.rgb;
	#endif
}

// Decodes doubleLDR encoded lightmaps.
inline half3 DecodeLightmapDoubleLDR( fixed4 color )
{
	return 2.0 * color.rgb;
}

half4 unity_Lightmap_HDR;

inline half3 DecodeLightmap( fixed4 color )
{
#if defined(UNITY_NO_RGBM)
	return DecodeLightmapDoubleLDR( color );
#else
	return DecodeLightmapRGBM( color, unity_Lightmap_HDR );
#endif
}

解析:

o.texcoord1 = TRANSFORM_TEX(v.texcoord1, _LightMap); == o.texcoord1  = v.texcoord1.xy * _LightMap_ST.xy + _LightMap_ST.zw



对于GamePlay:


在.vert文件中

#if defined(LIGHTMAP)
v_texCoord1 = a_texCoord1 * u_lightmapTextureST.xy + u_lightmapTextureST.zw;
#endif

在.frag文件中

<pre class="plain" name="code">#if defined(LIGHTMAP)
vec4 lightColor = texture2D(u_lightmapTexture, v_texCoord1);
gl_FragColor.rgb *= lightColor.rgb;
#endif

 

理解uv2:

uv2就类似uv,uv2是在建模的时候确定的(unity里面可以自动生成,相当于在模型中建立uv2)。

在生成光照贴图的时候,会根据顶点的uv2来生成。

理解Unity3D的Lightmap的Scale和Offset:

The UV scale & offset used for a lightmap.

A lightmap is a texture atlas and multiple Renderers can use different portions of the same lightmap.The vector's x and y refer to UV scale, while z and w refer to UV offset.

补充:

更换光照贴图

 mat.SetTexture("_LightMap", lightMap);
 mat.SetTextureScale("_LightMap", new Vector2(scaleOffset.x, scaleOffset.y));
 mat.SetTextureOffset("_LightMap", new Vector2(scaleOffset.z, scaleOffset.w));





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值