Procedural Drawing
This commit is contained in:
@@ -4,13 +4,15 @@ public class Fractal : MonoBehaviour
|
||||
{
|
||||
private struct FractalPart
|
||||
{
|
||||
public Vector3 direction;
|
||||
public Quaternion rotation;
|
||||
public Transform transform;
|
||||
public Vector3 direction, worldPosition;
|
||||
public Quaternion rotation, worldRotation;
|
||||
public float spinAngle;
|
||||
}
|
||||
|
||||
FractalPart[][] parts;
|
||||
|
||||
Matrix4x4[][] matrices;
|
||||
|
||||
[SerializeField, Range(1, 8)]
|
||||
int depth = 4;
|
||||
|
||||
@@ -38,72 +40,120 @@ public class Fractal : MonoBehaviour
|
||||
Quaternion.Euler(-90f, 0f, 0f)
|
||||
};
|
||||
|
||||
private FractalPart CreatePart(int levelIndex, int childIndex, float scale)
|
||||
private FractalPart CreatePart(int childIndex)
|
||||
{
|
||||
var go = new GameObject("Fractal Part L" + levelIndex + " C" + childIndex);
|
||||
go.transform.localScale = scale * Vector3.one;
|
||||
go.transform.SetParent(transform, false);
|
||||
go.AddComponent<MeshFilter>().mesh = mesh;
|
||||
go.AddComponent<MeshRenderer>().material = material;
|
||||
|
||||
return new FractalPart()
|
||||
{
|
||||
direction = directions[childIndex],
|
||||
rotation = rotations[childIndex],
|
||||
transform = go.transform
|
||||
rotation = rotations[childIndex]
|
||||
};
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
ComputeBuffer[] matricesBuffers;
|
||||
|
||||
static readonly int matricesId = Shader.PropertyToID("_Matrices");
|
||||
|
||||
static MaterialPropertyBlock propertyBlock;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
parts = new FractalPart[depth][];
|
||||
matrices = new Matrix4x4[depth][];
|
||||
matricesBuffers = new ComputeBuffer[depth];
|
||||
int stride = 16 * 4;
|
||||
for (int i = 0, length = 1; i < parts.Length; i++, length *= 5)
|
||||
{
|
||||
parts[i] = new FractalPart[length];
|
||||
matrices[i] = new Matrix4x4[length];
|
||||
matricesBuffers[i] = new ComputeBuffer(length, stride);
|
||||
}
|
||||
|
||||
var scale = 1f;
|
||||
parts[0][0] = CreatePart(0, 0, scale);
|
||||
|
||||
parts[0][0] = CreatePart(0);
|
||||
for (int li = 1; li < parts.Length; li++)
|
||||
{
|
||||
scale *= 0.5f;
|
||||
FractalPart[] levelParts = parts[li];
|
||||
for (int fpi = 0; fpi < levelParts.Length; fpi += 5)
|
||||
{
|
||||
for (int ci = 0; ci < 5; ci++)
|
||||
{
|
||||
levelParts[fpi + ci] = CreatePart(li, ci, scale);
|
||||
levelParts[fpi + ci] = CreatePart(ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (propertyBlock == null)
|
||||
{
|
||||
propertyBlock = new MaterialPropertyBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
for (int i = 0; i < matricesBuffers.Length; i++)
|
||||
{
|
||||
matricesBuffers[i].Release();
|
||||
}
|
||||
parts = null;
|
||||
matrices = null;
|
||||
matricesBuffers = null;
|
||||
}
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
if (parts != null && enabled)
|
||||
{
|
||||
OnDisable();
|
||||
OnEnable();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Quaternion deltaRotation = Quaternion.Euler(0f, 22.5f * Time.deltaTime, 0f);
|
||||
|
||||
float spinAngleDelta = 22.5f * Time.deltaTime;
|
||||
FractalPart rootPart = parts[0][0];
|
||||
rootPart.rotation *= deltaRotation;
|
||||
rootPart.transform.localRotation = rootPart.rotation;
|
||||
rootPart.spinAngle += spinAngleDelta;
|
||||
rootPart.worldRotation =
|
||||
transform.rotation *
|
||||
(rootPart.rotation * Quaternion.Euler(0f, rootPart.spinAngle, 0f));
|
||||
rootPart.worldPosition = transform.position;
|
||||
parts[0][0] = rootPart;
|
||||
float objectScale = transform.lossyScale.x;
|
||||
matrices[0][0] = Matrix4x4.TRS(
|
||||
rootPart.worldPosition, rootPart.worldRotation, objectScale * Vector3.one
|
||||
);
|
||||
|
||||
float scale = objectScale;
|
||||
for (int li = 1; li < parts.Length; li++)
|
||||
{
|
||||
scale *= 0.5f;
|
||||
FractalPart[] parentParts = parts[li - 1];
|
||||
FractalPart[] levelParts = parts[li];
|
||||
Matrix4x4[] levelMatrices = matrices[li];
|
||||
for (int fpi = 0; fpi < levelParts.Length; fpi++)
|
||||
{
|
||||
var parentTransform = parentParts[fpi / 5].transform;
|
||||
var parent = parentParts[fpi / 5];
|
||||
var part = levelParts[fpi];
|
||||
part.rotation *= deltaRotation;
|
||||
part.transform.rotation =
|
||||
parentTransform.localRotation * part.rotation;
|
||||
part.transform.localPosition =
|
||||
parentTransform.localPosition +
|
||||
parentTransform.localRotation *
|
||||
(1.5f * part.transform.localScale.x * part.direction);
|
||||
part.spinAngle += spinAngleDelta;
|
||||
part.worldRotation =
|
||||
parent.worldRotation *
|
||||
(part.rotation * Quaternion.Euler(0f, part.spinAngle, 0f));
|
||||
part.worldPosition =
|
||||
parent.worldPosition +
|
||||
parent.worldRotation * (1.5f * scale * part.direction);
|
||||
levelParts[fpi] = part;
|
||||
levelMatrices[fpi] = Matrix4x4.TRS(
|
||||
part.worldPosition, part.worldRotation, scale * Vector3.one
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
var bounds = new Bounds(rootPart.worldPosition, 3f * objectScale * Vector3.one);
|
||||
for (int i = 0; i < matricesBuffers.Length; i++)
|
||||
{
|
||||
ComputeBuffer buffer = matricesBuffers[i];
|
||||
buffer.SetData(matrices[i]);
|
||||
propertyBlock.SetBuffer(matricesId, buffer);
|
||||
Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user