unity 2d 點光源/路燈/手電筒效果怎麼做?
示意圖如上。
unity 2d遊戲開發,某點光源發出的扇形光區域,夾角可調整,角平分線斜率可調整。shader小白,應該怎麼實現上述效果呢?
2d的話直接畫三角扇就可以,邊緣alpha衰減,再加個雜訊圖模擬浮沉感覺。
3d的話我這倒是有個函數,模擬聚光燈的。 private Mesh CreateSpotLightMesh() { // copy pasted from other project, the geometry is too complex, should be simplifiedMesh mesh = new Mesh();
const int segmentCount = 16;
Vector3[] vertices = new Vector3[2 + segmentCount * 3]; Color32[] colors = new Color32[2 + segmentCount * 3];vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(0, 0, 1);float angle = 0;
float step = Mathf.PI * 2.0f / segmentCount; float ratio = 0.9f;for (int i = 0; i &< segmentCount; ++i)
{vertices[i + 2] = new Vector3(-Mathf.Cos(angle) * ratio, Mathf.Sin(angle) * ratio, ratio);
colors[i + 2] = new Color32(255, 255, 255, 255); vertices[i + 2 + segmentCount] = new Vector3(-Mathf.Cos(angle), Mathf.Sin(angle), 1); colors[i + 2 + segmentCount] = new Color32(255, 255, 255, 0); vertices[i + 2 + segmentCount * 2] = new Vector3(-Mathf.Cos(angle) * ratio, Mathf.Sin(angle) * ratio, 1); colors[i + 2 + segmentCount * 2] = new Color32(255, 255, 255, 255); angle += step; }mesh.vertices = vertices;
mesh.colors32 = colors;int[] indices = new int[segmentCount * 3 * 2 + segmentCount * 6 * 2];
int index = 0;for (int i = 2; i &< segmentCount + 1; ++i)
{ indices[index++] = 0; indices[index++] = i; indices[index++] = i + 1; }indices[index++] = 0;
indices[index++] = segmentCount + 1;indices[index++] = 2;
for (int i = 2; i &< segmentCount + 1; ++i)
{ indices[index++] = i; indices[index++] = i + segmentCount; indices[index++] = i + 1;indices[index++] = i + 1;
indices[index++] = i + segmentCount; indices[index++] = i + segmentCount + 1; }indices[index++] = 2;
indices[index++] = 1 + segmentCount; indices[index++] = 2 + segmentCount;indices[index++] = 2 + segmentCount;
indices[index++] = 1 + segmentCount; indices[index++] = 1 + segmentCount + segmentCount;//------------
for (int i = 2 + segmentCount; i &< segmentCount + 1 + segmentCount; ++i) { indices[index++] = i;indices[index++] = i + segmentCount;
indices[index++] = i + 1;indices[index++] = i + 1;
indices[index++] = i + segmentCount; indices[index++] = i + segmentCount + 1; }indices[index++] = 2 + segmentCount;
indices[index++] = 1 + segmentCount * 2; indices[index++] = 2 + segmentCount * 2;indices[index++] = 2 + segmentCount * 2;
indices[index++] = 1 + segmentCount * 2;
indices[index++] = 1 + segmentCount * 3;////-------------------------------------
for (int i = 2 + segmentCount * 2; i &< segmentCount * 3 + 1; ++i) { indices[index++] = 1; indices[index++] = i + 1; indices[index++] = i; }indices[index++] = 1;
indices[index++] = 2 + segmentCount * 2;
indices[index++] = segmentCount * 3 + 1;mesh.triangles = indices;
mesh.RecalculateBounds(); return mesh; }直接做一個三角形的面片,然後在上面放一個邊緣發光中間略微透明的貼圖,用particles/additive這個shader可以模擬一個這樣的效果。
最後還是去asset store里找到了一個插件,小貴但基本能滿足問題需求。
2D Dynamic Lights and Shadows - 2DDL PRO
這裡面有很多2d光照的案例,手電筒、蠟燭、雷達什麼的都有,代碼略複雜,看得懂的話可以參考。
濾鏡SHADER實現
推薦閱讀:
※有什麼龍珠相關的遊戲嗎?
※玩遊戲玩到該遊戲停止運營是怎樣一種體驗?
※「圍住神經貓」這樣的網頁小遊戲是怎樣火起來的?
※NGUI製作背包等有大量物品圖片的界面時候如何優化Drawcall?
※遊戲的原版源代碼還在,資料片的丟失,想要復刻出資料片難度和工作量有多大?