Example #1
0
    void Update()
    {
        var deltaTime = Time.deltaTime;
        var level     = 0;
        var root      = children[level][0];

        root.spinAngle += root.spinSpeed * deltaTime;
        var rootTransform = transform;

        root.worldRotation =
            math.mul(rootTransform.rotation, math.mul(root.rotation, quaternion.RotateY(root.spinAngle)));
        root.worldPosition = rootTransform.position;
        children[level][0] = root;
        var scale = rootTransform.lossyScale.x;

        matrices[level][0] = UpdateFractalLevelJob.Matrix(root, scale);

        level++;
        JobHandle jobHandle = default;

        for (; level < children.Length; level++)
        {
            scale *= CHILD_SCALE;
            var job = new UpdateFractalLevelJob {
                childCount = CHILD_ROTATION.Length,
                deltaTime  = deltaTime,
                scale      = scale,
                parents    = children[level - 1],
                children   = children[level],
                matrices   = matrices[level]
            };
            jobHandle = job.ScheduleParallel(children[level].Length, 5, jobHandle);
        }
        jobHandle.Complete();

        var bounds    = new Bounds(root.worldPosition, 3 * scale * Vector3.one);
        var leafIndex = matricesBuffers.Length - 1;

        for (var index = 0; index < matricesBuffers.Length; index++)
        {
            var buffer = matricesBuffers[index];
            buffer.SetData(matrices[index]);
            propertyBlock.SetBuffer(MATRICES, buffer);

            Mesh  instanceMesh;
            Color color1, color2;
            if (index == leafIndex)
            {
                color1       = leafColor1;
                color2       = leafColor2;
                instanceMesh = leafMesh;
            }
            else
            {
                var gradientInterpolator = index / (matricesBuffers.Length - 2f);
                color1       = gradient1.Evaluate(gradientInterpolator);
                color2       = gradient2.Evaluate(gradientInterpolator);
                instanceMesh = mesh;
            }

            propertyBlock.SetColor(COLOR_1, color1);
            propertyBlock.SetColor(COLOR_2, color2);
            propertyBlock.SetVector(SEQUENCE_NUMBERS, sequenceNumbers[index]);
            Graphics.DrawMeshInstancedProcedural(instanceMesh, 0, material, bounds, buffer.count, propertyBlock);
        }
    }