Skip to content

Instantly share code, notes, and snippets.

@baobao
Created December 19, 2018 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/c1a5c39a859047557c939ec43ed70810 to your computer and use it in GitHub Desktop.
Save baobao/c1a5c39a859047557c939ec43ed70810 to your computer and use it in GitHub Desktop.
using UnityEngine;
/// <summary>
/// [WIP] 行き当たりばったりでクリスマスっぽい何かを作っている
/// </summary>
[RequireComponent(typeof(MeshRenderer))]
public class ChristmasSomething : MonoBehaviour
{
[SerializeField]
private Vector2Int _texSize = new Vector2Int(300, 300);
private Renderer _renderer;
[SerializeField]
private float _radius = 40f;
private Texture2D _canvas;
[SerializeField]
private Color32 _baseColor = new Color32(0, 0, 0, 255);
[SerializeField]
private Color32 _addColor = new Color32(255, 255, 255, 255);
[SerializeField]
private int _interval = 10;
[SerializeField]
private int _maxParallelDrawCount = 10;
[SerializeField]
private Transform _camTransform;
void Start()
{
_renderer = GetComponent<MeshRenderer>();
_canvas = new Texture2D(_texSize.x, _texSize.y);
_renderer.material.mainTexture = _canvas;
for (int x = 0; x < _texSize.x; x++)
{
for (int y = 0; y < _texSize.y; y++)
{
_canvas.SetPixel(x, y, _baseColor);
}
}
_canvas.Apply();
}
void Update()
{
transform.Rotate(new Vector3(0.3f, 1f, 0), Time.deltaTime * 20f);
bool isApplyed = false;
FadeOut();
if (_interval < 1)
{
_interval = 1;
}
if (Time.frameCount % _interval == 0)
{
int count = Random.Range(1, _maxParallelDrawCount);
for (int i = 0; i < count; i++)
Draw(Random.Range(0, _texSize.x), Random.Range(0, _texSize.y), _radius * Random.Range(0.6f, 1f));
_canvas.Apply();
isApplyed = true;
}
if (isApplyed == false)
_canvas.Apply();
}
void LateUpdate()
{
_camTransform.LookAt(Vector3.zero);
_camTransform.RotateAround(Vector3.zero, Vector3.up, -Time.deltaTime * 20f);
}
void FadeOut()
{
for (int x = 0; x < _texSize.x; x++)
{
for (int y = 0; y < _texSize.y; y++)
{
var color = _canvas.GetPixel(x, y);
var result = Color.Lerp(color, _baseColor, color.b * 0.1f);
_canvas.SetPixel(x, y, result);
}
}
}
void Draw(int cx, int cy, float radius)
{
for (int x = 0; x < _texSize.x; x++)
{
for (int y = 0; y < _texSize.y; y++)
{
var xx = x - radius;
var yy = y - radius;
float distance = Mathf.Sqrt(xx * xx + yy * yy);
if (distance >= radius)
continue;
_canvas.SetPixel(x + cx, y + cy, _addColor);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment