
[英]How to create a Tilemap light in Unity's new 2D light system?
[英]2D light rendering for a tilemap with blockers
光照贴图由绘制为三角形拓扑的网格组成:
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 });
}
}
Colors 然后在渲染时根据 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
}
}
}
确保正确生成 colors; 我截取了光照贴图线框的另一个屏幕截图,查看突出显示的区域并将其与其他 3 张图像进行比较:
如何正确实现灯光? 或者更好地说,我如何正确绘制顶点以获得所需的最终效果?
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.