光照贴图由绘制为三角形拓扑的网格组成:
const int map_width = 18;
const int map_height = 14;
const int vertex_count = (map_width + 1) * (map_height + 1); // 285
Mesh lightMesh = new Mesh(); // vertices; indices; colors;
// implementing vertices
for (int y = 0; y < map_height + 1; ++y)
for (int x = 0; x < map_width + 1; ++x)
lightMesh.vertices.Add({x, y});
// and so you can expect how indices are generated
int[] triangles = new int[vertex_count * 6];
for (int y = 0; y < Constants.MapSizeY; y++) {
int rowsize = Constants.MapSizeX + 1;
int row = y * rowsize;
for (int x = 0; x < Constants.MapSizeX; x++) {
int v0 = x + row;
int v1 = v0 + 1;
int v2 = v0 + rowsize;
int v3 = v2 + 1;
triangles.AddRange(new int[] { v0, v1, v3, v0, v2, v3 });
}
}
然后在渲染时根据tilemap计算颜色。此屏幕快照显示了此实现的行为方式。 但是您注意到这不是很准确;角落里有三角形,与光线不一致。
用于绘制此内容的着色器(在着色器实验室中-一种具有乘法混合功能的非常基本的颜色)
Shader "OpenTibiaUnity/Lightmap"
{
Properties
{
}
SubShader
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
Blend DstColor Zero
BlendOp Add
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
};
struct v2f {
float4 vertex : SV_POSITION;
float4 color : COLOR;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
return o;
}
fixed4 frag(v2f i) : SV_Target {
return i.color;
}
ENDCG
}
}
}
确保正确生成颜色;我拍摄了光照贴图的线框的另一个屏幕截图,查看突出显示的区域并将其与其他3个图像进行比较:
如何正确安装照明灯?或者更好地说,如何正确绘制顶点以获得所需的最终效果?
致谢