Added: Unity assets for Game of Life simulation, including materials, prefabs, and a scene for Bezier curves

This commit is contained in:
2026-10-01 18:48:07 +02:00
parent c10578da47
commit 67777b6444
69 changed files with 7946 additions and 0 deletions
@@ -0,0 +1,111 @@
using System;
using UnityEngine;
namespace DefaultNamespace
{
public enum BezierCurveType
{
Linear,
Quadratic,
Cubic,
}
public class BezierCurves : MonoBehaviour
{
[SerializeField] private BezierCurveType _bezierCurveType;
[SerializeField] private Color _color = Color.white;
[SerializeField] private Transform[] _positions = Array.Empty<Transform>();
[SerializeField, Min(1)] private int _separations = 1;
private void OnDrawGizmos()
{
Gizmos.color = _color;
switch (_bezierCurveType)
{
case BezierCurveType.Linear:
DrawLinearBezierCurve();
break;
case BezierCurveType.Quadratic:
DrawQuadraticBezierCurve();
break;
case BezierCurveType.Cubic:
DrawCubicBezierCurve();
break;
default:
throw new ArgumentOutOfRangeException();
}
for (int i = 0; i < _positions.Length; i++)
{
Gizmos.DrawSphere(_positions[i].position, 0.25f);
}
}
private void DrawLinearBezierCurve()
{
if (_positions.Length < 2)
return;
// Gizmos.DrawLine(_positions[0].position, _positions[1].position);
Vector3 p0 = _positions[0].position;
Vector3 p1 = _positions[1].position;
for (int i = 0; i < _separations; i++)
{
float t0 = (float)i / _separations;
float t1 = (float)(i + 1) / _separations;
Gizmos.DrawLine(LinearCurvePoint(p0, p1, t0), LinearCurvePoint(p0, p1, t1));
}
}
private void DrawQuadraticBezierCurve()
{
if (_positions.Length < 3)
return;
Vector3 p0 = _positions[0].position;
Vector3 p1 = _positions[1].position;
Vector3 p2 = _positions[2].position;
for (int i = 0; i < _separations; i++)
{
float t0 = (float)i / _separations;
float t1 = (float)(i + 1) / _separations;
Gizmos.DrawLine(QuadraticCurvePoint(p0, p1, p2, t0), QuadraticCurvePoint(p0, p1, p2, t1));
}
}
private void DrawCubicBezierCurve()
{
if (_positions.Length < 4)
return;
Vector3 p0 = _positions[0].position;
Vector3 p1 = _positions[1].position;
Vector3 p2 = _positions[2].position;
Vector3 p3 = _positions[3].position;
for (int i = 0; i < _separations; i++)
{
float t0 = (float)i / _separations;
float t1 = (float)(i + 1) / _separations;
Gizmos.DrawLine(CubicCurvePoint(p0, p1, p2, p3, t0), CubicCurvePoint(p0, p1, p2, p3, t1));
}
}
private Vector3 LinearCurvePoint(Vector3 p0, Vector3 p1, float t) => (1 - t) * p0 + t * p1;
private Vector3 QuadraticCurvePoint(Vector3 p0, Vector3 p1, Vector3 p2, float t) =>
// LinearCurvePoint(LinearCurvePoint(p0, p1, t), LinearCurvePoint(p1, p2, t), t);
(1 - t) * (1 - t) * p0 + 2 * (1 - t) * t * p1 + t * t * p2;
private Vector3 CubicCurvePoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) =>
// LinearCurvePoint(QuadraticCurvePoint(p0, p1, p2, t), QuadraticCurvePoint(p1, p2, p3, t), t);
(1 - t) * (1 - t) * (1 - t) * p0 + 3 * (1 - t) * (1 - t) * t * p1 + 3 * (1 - t) * t * t * p2 + t * t * t * p3;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9875609e230b4b9eb06a8ed0d9ca1e98
timeCreated: 1790866393
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b9d4b5ac0b4240539f2b60d92cd477cb
timeCreated: 1790870400
@@ -0,0 +1,36 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace DefaultNamespace.GameOfLife
{
[RequireComponent(typeof(MeshRenderer))]
public class Cell : MonoBehaviour, IPointerClickHandler
{
[SerializeField] private Material _aliveMaterial;
[SerializeField] private Material _deadMaterial;
private bool _isAlive;
public bool IsAlive
{
get { return _isAlive; }
set
{
_isAlive = value;
_meshRenderer.material = _isAlive ? _aliveMaterial : _deadMaterial;
}
}
private MeshRenderer _meshRenderer;
private void Awake()
{
_meshRenderer = GetComponent<MeshRenderer>();
IsAlive = false;
}
public void OnPointerClick(PointerEventData eventData)
{
IsAlive = !IsAlive;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 508a3eb183534be4b7ddbe222d9904f9
timeCreated: 1790870407
@@ -0,0 +1,131 @@
using UnityEngine;
namespace DefaultNamespace.GameOfLife
{
public class GameOfLife : MonoBehaviour
{
[SerializeField] private Cell _cellPrefab;
[SerializeField, Range(1, 100)] private int _rows = 50;
[SerializeField, Range(1, 100)] private int _columns = 50;
[SerializeField] private float _isAliveRandomThreshold = 0.5f;
private Cell[] _cells;
private int CalculateIndex(int row, int column) => row * _columns + column;
private void Start()
{
_cells = new Cell[_rows * _columns];
for (int row = 0; row < _rows; row++)
{
for (int column = 0; column < _columns; column++)
{
var cell = Instantiate(_cellPrefab, transform);
cell.name = $"Cell {row:000} / {column:000}";
cell.transform.localPosition = new Vector3(row, column, 0);
_cells[CalculateIndex(row, column)] = cell;
}
}
}
private void SimulateStep()
{
// 1. collect new states
bool[] newStates = new bool[_cells.Length];
// 2. change states - apply rules
for (int x = 0; x < _columns; x++)
{
for (int y = 0; y < _rows; y++)
{
int index = CalculateIndex(x, y);
bool cellState = _cells[index].IsAlive;
int livingNeighbors = CountLivingNeighbors(x, y);
if (cellState)
{
// cell is alive
// rule 1: less than 2 living neighbors -> die
if (livingNeighbors < 2)
{
newStates[index] = false;
}
// rule 2: 2 or 3 living neighbors -> stay alive
if (livingNeighbors is 2 or 3)
{
newStates[index] = true;
}
// rule 3: more than 3 living neighbors -> die
if (livingNeighbors > 3)
{
newStates[index] = false;
}
}
else
{
// cell is dead
// rule 4: exactly 3 living neighbors -> come alive
if (livingNeighbors == 3)
{
newStates[index] = true;
}
}
}
}
// 3. apply new states
for (int i = 0; i < _cells.Length; i++)
{
_cells[i].IsAlive = newStates[i];
}
}
private int CountLivingNeighbors(int row, int column)
{
int count = 0;
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
// exception: own cell
if (dx == 0 && dy == 0) continue;
int neighborRow = row + dx;
int neighborColumn = column + dy;
// exception: out of bounds
if (neighborRow < 0 || neighborRow >= _rows) continue;
if (neighborColumn < 0 || neighborColumn >= _columns) continue;
if (_cells[CalculateIndex(neighborRow, neighborColumn)].IsAlive)
{
count++;
}
}
}
return count;
}
private void ResetGrid()
{
for (int i = 0; i < _cells.Length; i++)
{
// _cells[i].IsAlive = false;
_cells[i].IsAlive = Random.value > _isAliveRandomThreshold;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.S)) SimulateStep();
if (Input.GetKeyDown(KeyCode.R)) ResetGrid();
if (Input.GetKeyDown(KeyCode.Space)) InvokeRepeating(nameof(SimulateStep), 0.0f, 0.5f);
if (Input.GetKeyDown(KeyCode.Escape)) CancelInvoke(nameof(SimulateStep));
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 63d8f99d328a48299e1d7058ea31b035
timeCreated: 1790870425