
No License
C#
2022年04月21日
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class SetTileShadow : MonoBehaviour
{
[SerializeField] Tilemap tile;
[SerializeField] int length = 8;
private void OnValidate()
{
if (!tile) tile = GetComponent<Tilemap>();
}
private void Start()
{
Set();
}
public void Set()
{
tile.CompressBounds();
var bound = tile.cellBounds;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
//最初に全タイルを黒くする
for (int x = bound.min.x; x < bound.max.x; x++)
{
for (int y = bound.min.y; y < bound.max.y; y++)
{
Color b = Color.black;
if (tile.HasTile(new Vector3Int(x, y, 0))) tile.SetColor(new Vector3Int(x, y, 0), b);
}
}
//上から下に白を広げる
for (int x = bound.min.x; x < bound.max.x; x++)
{
for (int y = bound.min.y + 5; y < bound.max.y; y++)
{
if (!tile.HasTile(new Vector3Int(x, y + 1, 0)) && tile.HasTile(new Vector3Int(x, y, 0)))
{
for (int f = 0; f <= length; f++)
{
float tc = tile.GetColor(new Vector3Int(x, y - f, 0)).r;
float c = ((float)length - f) / (float)length;
float b = Mathf.Max(tc, c);
tile.SetColor(new Vector3Int(x, y - f), new Color(b, b, b, 1));
//更に左に影響を与える
for (int fx = 0; fx < length - f; fx++)
{
float ftc = tile.GetColor(new Vector3Int(x - fx, y - f, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fx) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x - fx, y - f), new Color(fb, fb, fb, 1));
}
//更に右に影響を与える
for (int fx = 0; fx < length - f; fx++)
{
float ftc = tile.GetColor(new Vector3Int(x + fx, y - f, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fx) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x + fx, y - f), new Color(fb, fb, fb, 1));
}
}
}
}
}
//左から右にに白を広げる
for (int x = bound.max.x - 5; x > bound.min.x; x--)
{
for (int y = bound.min.y; y < bound.max.y; y++)
{
if (!tile.HasTile(new Vector3Int(x - 1, y, 0)) && tile.HasTile(new Vector3Int(x, y, 0)))
{
for (int f = 0; f <= length; f++)
{
float tc = tile.GetColor(new Vector3Int(x + f, y, 0)).r;
float c = ((float)length - f) / (float)length;
float b = Mathf.Max(tc, c);
tile.SetColor(new Vector3Int(x + f, y), new Color(b, b, b, 1));
//更に下に影響を与える
for (int fy = 0; fy < length - f; fy++)
{
float ftc = tile.GetColor(new Vector3Int(x + f, y - fy, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fy) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x + f, y - fy), new Color(fb, fb, fb, 1));
}
//更に上に影響を与える
for (int fy = 0; fy < length - f; fy++)
{
float ftc = tile.GetColor(new Vector3Int(x + f, y + fy, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fy) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x + f, y + fy), new Color(fb, fb, fb, 1));
}
}
}
}
}
//下から上に白を広げる
for (int x = bound.min.x; x < bound.max.x; x++)
{
for (int y = bound.max.y - 5; y > bound.min.y; y--)
{
if (!tile.HasTile(new Vector3Int(x, y - 1, 0)) && tile.HasTile(new Vector3Int(x, y, 0)))
{
for (int f = 0; f <= length; f++)
{
float tc = tile.GetColor(new Vector3Int(x, y + f, 0)).r;
float c = ((float)length - f) / (float)length;
float b = Mathf.Max(tc, c);
tile.SetColor(new Vector3Int(x, y + f), new Color(b, b, b, 1));
//更に左に影響を与える
for (int fx = 0; fx < length - f; fx++)
{
float ftc = tile.GetColor(new Vector3Int(x - fx, y + f, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fx) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x - fx, y + f), new Color(fb, fb, fb, 1));
}
//更に右に影響を与える
for (int fx = 0; fx < length - f; fx++)
{
float ftc = tile.GetColor(new Vector3Int(x + fx, y + f, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fx) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x + fx, y + f), new Color(fb, fb, fb, 1));
}
}
}
}
}
//右から左にに白を広げる
for (int x = bound.min.x + 5; x < bound.max.x; x++)
{
for (int y = bound.min.y; y < bound.max.y; y++)
{
if (!tile.HasTile(new Vector3Int(x + 1, y, 0)) && tile.HasTile(new Vector3Int(x, y, 0)))
{
for (int f = 0; f <= length; f++)
{
float tc = tile.GetColor(new Vector3Int(x - f, y, 0)).r;
float c = ((float)length - f) / (float)length;
float b = Mathf.Max(tc, c);
tile.SetColor(new Vector3Int(x - f, y), new Color(b, b, b, 1));
//更に下に影響を与える
for (int fy = 0; fy < length - f; fy++)
{
float ftc = tile.GetColor(new Vector3Int(x - f, y - fy, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fy) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x - f, y - fy), new Color(fb, fb, fb, 1));
}
//更に上に影響を与える
for (int fy = 0; fy < length - f; fy++)
{
float ftc = tile.GetColor(new Vector3Int(x - f, y + fy, 0)).r;
float fc = Mathf.Lerp(0, b, (((float)length - f) - fy) / (float)length);
float fb = Mathf.Max(ftc, fc);
tile.SetColor(new Vector3Int(x - f, y + fy), new Color(fb, fb, fb, 1));
}
}
}
}
}
sw.Stop();
Debug.Log($"陰をつける処理にかかった時間 : {sw.ElapsedMilliseconds} ms");
}
}
No one still commented. Please first comment.