/// <summary>
        /// Optionally Set to a different L-system, and reset the state and the current runtime parameters
        /// </summary>
        private void ResetState(
            LSystemState <float> newState,
            LSystemStepper newSystem)
        {
            if (lSystemPendingCompletable != null)
            {
                lSystemPendingCompletable.Cancel();
            }
            lastState?.currentSymbols.Dispose();
            lastState = null;
            currentState?.currentSymbols.Dispose();
            currentState = null;

            totalSteps        = 0;
            lastUpdateChanged = true;
            currentState      = newState;

            runtimeParameters = systemObject.GetRuntimeParameters();

            if (newSystem != null)
            {
                SetNewCompiledSystem(newSystem);
            }

            // clear out the next state handle. if an update is pending, just abort it.

            OnSystemStateUpdated?.Invoke();
        }
    //Creates the L-System states using the list of known rules by expanding each character with the use of StringBuilder
    public void IterateState()
    {
        LSystemState            currentState;
        LSystemRule             currentRule;
        Predicate <LSystemRule> ruleFinder;
        StringBuilder           sb = new StringBuilder();

        for (int i = 1; i <= iterations; i++)
        {
            currentState = stateList.Last.Value;
            sb.Length    = 0;
            foreach (char character in currentState.State)
            {
                ruleFinder  = delegate(LSystemRule x){ return(x.Index == character); };
                currentRule = ruleList.Find(ruleFinder);
                if (currentRule != null)
                {
                    sb.Append(currentRule.Rule);
                }
                else
                {
                    sb.Append(character);
                }
            }
            currentState = new LSystemState(angle, i, sb.ToString());
            stateList.AddLast(currentState);
        }
    }
示例#3
0
    public void Generate(bool clean = false)
    {
        // save original sentence
        lSystem.SaveOriginalSentence();

        if (clean)
        {
            CleanExistingLSystem();
        }

        if (lSystem == null)
        {
            Debug.LogError("You must have an lSystem defined");
            enabled = false;
        }
        if (lSystem.RuleCount == 0)
        {
            Debug.LogError("You must have at least one rule defined");
            enabled = false;
        }

        lineRenderer = GetComponent <LineRenderer>();
        lineRenderer.positionCount = 2;

        for (int i = 0; i < numberOfGenerations; i++)
        {
            savedState.Push(state.Clone());

            lSystem.Generate();

            state = savedState.Pop();
        }

        DrawLines();
    }
        public JobHandle ApplySunlightToSymbols(
            LSystemState <float> systemState,
            CustomRuleSymbols customSymbols)
        {
            var idsNativeArray = uniqueSunlightAssignments.ActiveData;

            if (idsNativeArray == null)
            {
                Debug.LogError("no sunlight data available");
                return(default);
            public SavedData(LSystemSteppingHandle source)
            {
                this.totalSteps        = source.totalSteps;
                this.lastUpdateChanged = source.lastUpdateChanged;
                this.useSharedSystem   = source.useSharedSystem;

                this.currentState = source.currentState;
                this.lastState    = source.lastState;
                this.oldHandle    = source.globalResourceHandle;

                this.systemObjectId = source.systemObject.myId;

                this.runtimeParameters = source.runtimeParameters;
                this.compiledGlobalCompiletimeReplacements = source.compiledGlobalCompiletimeReplacements;
            }
    public void reset()
    {
        ruleList  = new List <LSystemRule>();
        stateList = new LinkedList <LSystemState>();

        /*Set the initial state of the rule system*/
        state = new LSystemState(angle, 0, start);
        stateList.AddFirst(state);

        this.RuleAggregation();
        this.IterateState();


        LSystemPainter painter = (LSystemPainter)this.GetComponent("LSystemPainter");

        painter.create(stateList);
    }
示例#7
0
    public void LSystemReproducesStochasticResultFromReplicatedState()
    {
        var globalParameters = new string[] { "global" };

        LSystemState <float> state = new DefaultLSystemState("A(0)");

        using var basicLSystem = LSystemBuilder.FloatSystem(new string[] {
            "P(0.5) | A(x) : x < global -> A(x + 1)",
            "P(0.5) | A(x) : x < global -> A(x + 0.5)",
            "P(0.5) | A(x) : x >= global -> A(x - 1)",
            "P(0.5) | A(x) : x >= global -> A(x - 0.5)",
        }, globalParameters);

        var defaultGlobalParams = new float[] { 3 };

        var resultSequence   = new List <string>();
        var resultSampleSize = 12;

        resultSequence.Add(state.currentSymbols.Data.ToString());

        LSystemState <float> systemCopyAt5 = null;

        for (int i = 1; i < resultSampleSize; i++)
        {
            if (i == 5)
            {
                systemCopyAt5 = state;
                state         = basicLSystem.StepSystem(state, defaultGlobalParams, false);
            }
            else
            {
                state = basicLSystem.StepSystem(state, defaultGlobalParams);
            }
            resultSequence.Add(state.currentSymbols.Data.ToString());
        }
        state.currentSymbols.Dispose();

        for (int i = 5; i < resultSampleSize; i++)
        {
            systemCopyAt5 = basicLSystem.StepSystem(systemCopyAt5, defaultGlobalParams);
            Assert.AreEqual(resultSequence[i], systemCopyAt5.currentSymbols.Data.ToString(), $"Index {i}");
        }
        systemCopyAt5.currentSymbols.Dispose();
    }
示例#8
0
    void DrawLines()
    {
        state = new LSystemState()
        {
            x     = 0,
            y     = 0,
            size  = lineLength,
            angle = 0
        };

        string sentence = lSystem.GeneratedSentence;

        for (int i = 0; i < sentence.Length; i++)
        {
            char c = sentence[i];
            switch (c)
            {
            case 'F':
                Line();
                break;

            case 'G':
                Translate();
                break;

            case '+':
                state.angle += angle;
                break;

            case '-':
                state.angle -= angle;
                break;

            case '[':
                savedState.Push(state.Clone());
                break;

            case ']':
                state = savedState.Pop();
                break;
            }
        }
    }
        public void Dispose()
        {
            if (useSharedSystem)
            {
                systemObject.OnCachedSystemUpdated -= OnSharedSystemRecompiled;
            }
            else
            {
                compiledSystem?.Dispose();
                compiledSystem = null;
            }
            lSystemPendingCompletable?.Cancel();
            lastState?.currentSymbols.Dispose();
            lastState = null;

            currentState?.currentSymbols.Dispose();
            currentState = null;

            globalResourceHandle.Dispose();
        }
示例#10
0
    //Used for drawing the L-System with the initial angle
    void renderState(LSystemState state)
    {
        foreach (char character in state.State)
        {
            switch (character)
            {
            case '+': currentAngle -= state.Angle; break;

            case '-': currentAngle += state.Angle; break;

            case '[': stack.Push(new AnglePos(currentAngle, currentPosition)); break;

            case ']':
                AnglePos obj = stack.Pop();
                currentAngle    = obj.angle;
                currentPosition = obj.position;
                break;

            default:
                createObject(); break;
            }
        }
    }
        /// <summary>
        /// Root implementation of Step system, all other step calls funnel here
        /// </summary>
        /// <param name="runtimeParameters"></param>
        /// <param name="repeatLast">True if this system should just repeat the last update. Useful if a runtime parameter changed, or </param>
        private void StepSystemAsync(
            ArrayParameterRepresenation <float> runtimeParameters,
            bool repeatLast = false)
        {
            ICompletable <LSystemState <float> > pendingStateHandle;

            try
            {
                if (compiledSystem == null || compiledSystem.isDisposed)
                {
                    Debug.LogError("No Compiled system available!");
                }
                if (repeatLast)
                {
                    globalResourceHandle.UpdateUniqueIdReservationSpace(lastState);
                    pendingStateHandle = compiledSystem.StepSystemJob(
                        lastState,
                        runtimeParameters.GetCurrentParameters());
                }
                else
                {
                    globalResourceHandle.UpdateUniqueIdReservationSpace(currentState);
                    var sunlightJob = globalResourceHandle.ApplyPrestepEnvironment(
                        currentState,
                        compiledSystem.customSymbols);

                    pendingStateHandle = compiledSystem.StepSystemJob(
                        currentState,
                        runtimeParameters.GetCurrentParameters(),
                        parameterWriteDependency: sunlightJob);
                }
                if (pendingStateHandle == null)
                {
                    lSystemPendingCompletable = null;
                    return;
                }
            }
            catch (System.Exception e)
            {
                lastUpdateChanged = false;
                Debug.LogException(e);
                lSystemPendingCompletable = null;
                return;
            }

            lSystemPendingCompletable = CompletableExecutor.Instance.RegisterCompletable(pendingStateHandle);

            lSystemPendingCompletable.OnCompleted += (nextState) =>
            {
                UnityEngine.Profiling.Profiler.BeginSample("updating stepping handle state");
                if (repeatLast)
                {
                    // dispose the current state, since it is about to be replaced
                    currentState?.currentSymbols.Dispose();
                }
                else
                {
                    // dispose the last state
                    lastState?.currentSymbols.Dispose();
                    lastState = currentState;
                    totalSteps++;
                }
                currentState = nextState;
                // if there are immature markers, use those instead. avoiding an equality check saves time.
                var hasImmatureMarkers = systemObject.linkedFiles.immaturitySymbolMarkers.Length > 0;
                lastUpdateChanged = hasImmatureMarkers || !(currentState?.currentSymbols.Data.Equals(lastState.currentSymbols.Data) ?? false);

                lSystemPendingCompletable = null;
                UnityEngine.Profiling.Profiler.EndSample();
                OnSystemStateUpdated?.Invoke();
            };
        }
示例#12
0
    public void generate(bool once = false)
    {
        reset();

        string sentence;

        if (once)
        {
            sentence = _lSystem.getNewSentence();
        }
        else
        {
            sentence = _lSystem.getNewSentencebyStep();
        }

        if (sentence == null)
        {
            return;
        }
        curState = new LSystemState();

        for (int i = 0; i < sentence.Length; i++)
        {
            char c = sentence[i];
            if (_drawSymbols.Contains(c))
            {
                drawLine();
            }
            else if (_leafSymbols.Contains(c))
            {
                drawLeaf(_leafPath);
            }
            else if (_flowerSymbols.Contains(c))
            {
                drawFlower(_flowerPath);
            }
            else
            {
                switch (c)
                {
                case '+':
                    curState.angle += _angle;
                    break;

                case '-':
                    curState.angle -= _angle;
                    break;

                case '[':
                    stateStack.Push(curState.Clone());
                    break;

                case ']':
                    curState = stateStack.Pop();
                    break;

                default:
                    break;
                }
            }
        }
    }