Beispiel #1
0
        public IEnumerator CopyPast_Context_And_Relink([ValueSource("cutBeforeSource")] CutBefore cutBeforeEncapsultor)
        {
            VFXTaskType curBeforeSource = cutBeforeEncapsultor.taskType;

            EditorApplication.ExecuteMenuItem("Window/General/Game");

            var graph = MakeTemporaryGraph();

            var spawnerContext  = ScriptableObject.CreateInstance <VFXBasicSpawner>();
            var basicInitialize = ScriptableObject.CreateInstance <VFXBasicInitialize>();
            var basicUpdate     = ScriptableObject.CreateInstance <VFXBasicUpdate>();
            var quadOutput      = ScriptableObject.CreateInstance <VFXQuadOutput>();

            var arrayOfContext = new VFXContext[] { spawnerContext, basicInitialize, basicUpdate, quadOutput };

            quadOutput.SetSettingValue("blendMode", VFXAbstractParticleOutput.BlendMode.Additive);

            var setPosition = ScriptableObject.CreateInstance <SetAttribute>(); //only needed to allocate a minimal attributeBuffer

            setPosition.SetSettingValue("attribute", "position");
            basicInitialize.AddChild(setPosition);

            var capacity = 2u;

            //(basicInitialize.GetData() as VFXDataParticle).capacity = capacity; //voluntary overflow in case of capacity is not correctly copied

            basicInitialize.SetSettingValue("capacity", 2u); // pass through regular way to change capacity to have initialize and data up to date
            var spawnerBurst = ScriptableObject.CreateInstance <VFXSpawnerBurst>();

            spawnerBurst.inputSlots[0].value = 4.0f;

            spawnerContext.AddChild(spawnerBurst);
            graph.AddChild(spawnerContext);
            graph.AddChild(basicInitialize);
            graph.AddChild(basicUpdate);
            graph.AddChild(quadOutput);
            basicInitialize.LinkFrom(spawnerContext);
            basicUpdate.LinkFrom(basicInitialize);
            quadOutput.LinkFrom(basicUpdate);

            m_ViewController.NotifyUpdate();
            Assert.AreEqual(4, m_View.GetAllContexts().Count());

            //Copy partially in two passes
            var indexOffset     = cutBeforeSource.Select((o, i) => new { o = o, i = i }).Where(o => o.o.taskType == curBeforeSource).Select(o => o.i).First();
            var contextToCopy_A = arrayOfContext.Skip(indexOffset).Select(o => m_View.GetAllContexts().Where(u => u.controller.model == o).First() as UnityEditor.Experimental.GraphView.GraphElement);

            foreach (var graphElement in contextToCopy_A)
            {
                m_View.AddToSelection(graphElement);
            }
            m_View.DuplicateSelectionCallback();
            m_View.ClearSelection();
            m_ViewController.NotifyUpdate();

            if (indexOffset > 0)
            {
                var contextToCopy_B = arrayOfContext.Take(indexOffset).Select(o => m_View.GetAllContexts().Where(u => u.controller.model == o).First() as UnityEditor.Experimental.GraphView.GraphElement);
                foreach (var graphElement in contextToCopy_B)
                {
                    m_View.AddToSelection(graphElement);
                }
                m_View.DuplicateSelectionCallback();
                m_View.ClearSelection();
            }

            m_ViewController.NotifyUpdate();
            Assert.AreEqual(8, m_View.GetAllContexts().Count());

            //Restore missing link
            var allContext = m_View.GetAllContexts().Select(o => o.controller.model).ToArray();

            if (indexOffset > 0)
            {
                var from = allContext.Last();
                var to   = allContext[4];
                to.LinkFrom(from);
            }

            VFXBasicInitialize[] initializes = graph.children.OfType <VFXBasicInitialize>().ToArray();

            Assert.AreEqual(2, initializes.Length);
            Assert.AreEqual(2, (initializes[0].GetData() as VFXDataParticle).capacity);
            Assert.AreEqual(2, (initializes[1].GetData() as VFXDataParticle).capacity);
            Assert.AreEqual(2, initializes[0].GetType().GetField("capacity", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(initializes[0]));
            Assert.AreEqual(2, initializes[1].GetType().GetField("capacity", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(initializes[1]));

            graph.RecompileIfNeeded();

            var gameObj      = new GameObject("CreateAssetAndComponentToCopyPastPerfomedWell");
            var vfxComponent = gameObj.AddComponent <VisualEffect>();

            vfxComponent.visualEffectAsset = graph.visualEffectResource.asset;

            var cameraObj = new GameObject("CreateAssetAndComponentToCopyPastPerfomedWell_Camera");
            var camera    = cameraObj.AddComponent <Camera>();

            camera.transform.localPosition = Vector3.one;
            camera.transform.LookAt(vfxComponent.transform);

            int maxFrame = 512;

            while (vfxComponent.culled && --maxFrame > 0)
            {
                yield return(null);
            }
            Assert.IsTrue(maxFrame > 0);
            yield return(null);                                             //wait for exactly one more update if visible

            Assert.AreEqual(capacity * 2, vfxComponent.aliveParticleCount); //Excepted to have two viable equivalent particles system
            UnityEngine.Object.DestroyImmediate(vfxComponent);
            UnityEngine.Object.DestroyImmediate(cameraObj);
        }