Skip to content

Commit

Permalink
Implement shape transform
Browse files Browse the repository at this point in the history
  • Loading branch information
apua committed Jan 3, 2024
1 parent eec4921 commit e461020
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
25 changes: 14 additions & 11 deletions Assets/Control.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class Control : MonoBehaviour {

Button _sphere, _ring, _2ring, _cage, _2cube, _2sphere, _tetrahedron, _escherianKnot;
Label _amount;

void OnEnable() {
var root = GetComponent<UIDocument>().rootVisualElement;

_sphere = root.Q("sphere") as Button;
_ring = root.Q("ring") as Button;
_2ring = root.Q("2ring") as Button;
_cage = root.Q("cage") as Button;
_2cube = root.Q("2cube") as Button;
_2sphere = root.Q("2sphere") as Button;
_tetrahedron = root.Q("tetrahedron") as Button;
_escherianKnot = root.Q("escherian-knot") as Button;
var universe = GetComponentInParent<Universe>();

_amount = root.Q("Amount") as Label;

var universe = GetComponentInParent<Universe>();
root.Q("add").RegisterCallback<ClickEvent>(evt => universe.AddStar(action: SetAmount));
root.Q("del").RegisterCallback<ClickEvent>(evt => universe.DelStar(action: SetAmount));

string[] shapeNames = {
"sphere",
"ring",
"2ring",
"cage",
"2cube",
"2sphere",
"tetrahedron",
"escherian-knot",
};
foreach (var name in shapeNames)
root.Q(name).RegisterCallback<ClickEvent>(evt => universe.SetShape(name));
}

public void SetAmount(int amount) {
Expand Down
38 changes: 35 additions & 3 deletions Assets/Universe.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;

public class Universe : MonoBehaviour {

GameObject _prototype, _stars;
Color _color = new(0, 0, 1);
Vector3[] _finalPositions;
Vector3[] _velocities;
float _transformShapeDelay = 0f;

const ushort InitialAmount = 42;
const float TotalTransformShapeDelay = 1.25f;

public int DegreePerSecond = 36;
public Vector3Int RotationAxis = new(60, 80, 0);
Expand All @@ -29,6 +32,19 @@ void Update() {
foreach (Transform starTransform in _stars.transform) {
starTransform.gameObject.GetComponent<Renderer>().material.color = _color;
}
// Transform.
if (_transformShapeDelay > 0) {
if (_transformShapeDelay > Time.deltaTime) {
for (int i = 0; i < _stars.transform.childCount; i++)
_stars.transform.GetChild(i).localPosition += _velocities[i] * Time.deltaTime;
_transformShapeDelay -= Time.deltaTime;
} else {
for (int i = 0; i < _stars.transform.childCount; i++)
//_stars.transform.GetChild(i).localPosition += _velocities[i] * _transformShapeDelay;
_stars.transform.GetChild(i).localPosition = _finalPositions[i];
_transformShapeDelay = 0f;
}
}
}

/* ******************** */
Expand All @@ -43,8 +59,8 @@ public void AddStar(Action<int> action = null) {
// Color.
obj.GetComponent<Renderer>().material.color = _color;
// Set random position.
obj.transform.localPosition = PointGenerators.Ring();
//obj.transform.localPosition = PointGenerators.Sphere();
//obj.transform.localPosition = PointGenerators.Ring();
obj.transform.localPosition = PointGenerators.Sphere();
// Enable.
obj.SetActive(true);

Expand Down Expand Up @@ -82,6 +98,22 @@ void UpdateCurrentColor() {
_color.g = 0; _color.b = x; _color.r = 1 - x;
}
}

public void SetShape(string name) {
Func<Vector3> generator = name switch {
"sphere" => PointGenerators.Sphere,
_ => PointGenerators.Ring,
};

var N = _stars.transform.childCount;
_finalPositions = new Vector3[N];
_velocities = new Vector3[N];
_transformShapeDelay = TotalTransformShapeDelay;
for (int i = 0; i < N; i++) {
_finalPositions[i] = generator();
_velocities[i] = (_finalPositions[i] - _stars.transform.GetChild(i).localPosition) / TotalTransformShapeDelay;
}
}
}

class PointGenerators {
Expand Down

0 comments on commit e461020

Please sign in to comment.