Unity Shader Example 8 (光照贴图)
来源:互联网 发布:淘宝评价添加小视频 编辑:程序博客网 时间:2024/06/04 19:22
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);# elsereturn (decodeInstructions.x * data.a) * data.rgb;# endif#elsereturn (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 );#elsereturn 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 0
- Unity Shader Example 8 (光照贴图)
- shader 光照贴图
- Unity Shader Example 2 (流光与模拟光照)
- Unity光照贴图
- Unity Shader 光照模式
- 【Unity Shader】基础光照
- unity shader:初探光照
- unity shader:深入光照
- Unity Shader 光照 小计
- unity shader 贴图流动
- Unity Shader基础的使用 基础、法线贴图及光照模型代码的注释
- Unity 5光照贴图烘焙
- Unity教程之-Unity光照贴图Lightmapping
- 【Unity Shaders】Shader中的光照
- Unity Shader自定义光照模型
- Unity Shader 五 光照模型
- Unity Shader 什么是法线贴图
- OpenGL-- Shader 颜色 光照 3 光照贴图 LightMap
- PlayFair密码的实现
- AfterEffects的模板网站汇总
- MyBatis学习
- iOS开发便捷工具
- 练习四1012
- Unity Shader Example 8 (光照贴图)
- hdu5328(尺取)
- rest_framework中的Serializer
- 110. Balanced Binary Tree
- #pragma once
- LeetCode 279 Perfect Squares
- linux、android下网桥实现
- Linux堆内存管理深入分析
- Spring和Mybatis结合