Ejemplo n.º 1
0
        public void ExecuteInThread()
        {
            if (splineSys == null)
            {
                Stage = PreviewStage.Blank; return;
            }

            //line
            Vector3[][] nodes = new Vector3[splineSys.splines.Length][];
            for (int s = 0; s < splineSys.splines.Length; s++)
            {
                nodes[s] = splineSys.splines[s].nodes;
            }

            if (polyLine == null)
            {
                polyLine = new PolyLine(0);
            }
            polyLine.SetPointsThread(nodes);

            //nodes
            PrepareNodesMeshArrays();

            CoroutineManager.Enqueue(ApplyInMain, priority: -1000);
        }
Ejemplo n.º 2
0
        public void ExecuteInThread()
        {
            if (splineSys == null)
            {
                Stage = PreviewStage.Blank; return;
            }

            //line
            Vector3[][] points = splineSys.GetAllPoints(resPerUnit: 0.2f, minRes: 3, maxRes: 20);

            for (int l = 0; l < points.Length; l++)
            {
                for (int p = 0; p < points[l].Length; p++)
                {
                    points[l][p] = new Vector3(points[l][p].x / worldSize.x, points[l][p].y / worldHeight, points[l][p].z / worldSize.z);
                }
            }

            if (polyLine == null)
            {
                polyLine = new PolyLine(0);
            }
            polyLine.SetPointsThread(points);

            //nodes
            PrepareNodesMeshArrays();

            CoroutineManager.Enqueue(ApplyInMain, priority: -1000);
        }
Ejemplo n.º 3
0
        private void Generate(Graph graph, TerrainTile tile, DetailLevel det, StopToken stop)
        /// Note that referencing det.task is illegal since task could be changed
        {
            OnBeforeTileGenerate?.Invoke(tile, det.data, stop);

            //do not return (for draft) until the end (apply)
//				if (!stop.stop) graph.CheckClear(det.data, stop);
            if (!stop.stop)
            {
                graph.Generate(det.data, stop);
            }
            if (!stop.stop)
            {
                graph.Finalize(det.data, stop);
            }

            //finalize event
            OnTileFinalized?.Invoke(tile, det.data, stop);

            //flushing products for playmode (all except apply)
            if (MapMagicObject.isPlaying)
            {
                det.data.Clear(clearApply: false);
            }

            //welding (before apply since apply will flush 2d array)
            if (!stop.stop)
            {
                Weld.ReadEdges(det.data, det.edges);
            }
            if (!stop.stop)
            {
                Weld.WeldEdgesInThread(det.edges, tile.mapMagic.tiles, tile.coord, det.data.isDraft);
            }
            if (!stop.stop)
            {
                Weld.WriteEdges(det.data, det.edges);
            }

            //enqueue apply
            //was: while the playmode is applied on SwitchLod to avoid unnecessary lags for main

            if (det.data.isDraft)
            {
                det.coroutine = CoroutineManager.Enqueue(() => ApplyNow(det, stop), Priority + 1000, "ApplyNow " + coord);
            }

            else                     //main
            {
                IEnumerator coroutine = ApplyRoutine(det, stop);
                det.coroutine = CoroutineManager.Enqueue(coroutine, Priority, "ApplyRoutine " + coord);
            }

            det.generateReady = true;
        }
Ejemplo n.º 4
0
 public void NextFrame()
 {
     n--;
     if (n <= 0)
     {
         Success();
     }
     else
     {
         coroutineManager.Enqueue(NextFrame);
     }
 }
        public void ExecuteInThread()
        {
            if (matrix == null)
            {
                Stage = PreviewStage.Blank; return;
            }

            bytes = new byte[matrix.rect.Count * 2];
            matrix.ExportRaw16(bytes, matrix.rect.offset, matrix.rect.size);

            CoroutineManager.Enqueue(ApplyInMain, priority: -1000);
        }
Ejemplo n.º 6
0
        public void ExecuteInThread()
        {
            if (trns == null)
            {
                Stage = PreviewStage.Blank; return;
            }

            count = trns.CountInRect((Vector2D)worldPos, (Vector2D)worldSize);

            vertices   = new Vector3[trns.count * 6];
            directions = new Vector3[trns.count * 6];
            tris       = new int[trns.count * 6];

            int i = 0;

            for (int t = 0; t < trns.count; t++)
            {
                Vector3 pos = trns.arr[t].pos;
                pos -= worldPos;
                pos  = new Vector3(pos.x / worldSize.x, pos.y, pos.z / worldSize.z);
                if (heights != null)
                {
                    pos.y = pos.y / heights.worldSize.y;                                        //heights is null on first generate
                }
                vertices[i] = vertices[i + 1] = vertices[i + 2] = vertices[i + 3] = vertices[i + 4] = vertices[i + 5] = pos;

                directions[i]     = new Vector3(0, 2, 1);                       //background has Z of 1
                directions[i + 1] = new Vector3(-4.5f, -7, 1);
                directions[i + 2] = new Vector3(4.5f, -7, 1);

                directions[i + 3] = new Vector3(0, 0, 0);
                directions[i + 4] = new Vector3(-3f, -6, 0);
                directions[i + 5] = new Vector3(3f, -6, 0);

                tris[i]     = i;
                tris[i + 1] = i + 1;
                tris[i + 2] = i + 2;
                tris[i + 3] = i + 3;
                tris[i + 4] = i + 4;
                tris[i + 5] = i + 5;

                i += 6;
            }

            CoroutineManager.Enqueue(ApplyInMain, priority: -1000);
        }
Ejemplo n.º 7
0
        internal static void Dispatch <TAwaiter, TStateMachine>(CoroutineManager manager, ref TAwaiter awaiter, TStateMachine stateMachine)
            where TAwaiter : INotifyCompletion
            where TStateMachine : IAsyncStateMachine
        {
            var tid = Thread.CurrentThread.ManagedThreadId;

            awaiter.OnCompleted(() =>
            {
                if (tid == Thread.CurrentThread.ManagedThreadId)
                {
                    stateMachine.MoveNext();
                }
                else
                {
                    manager.Enqueue(stateMachine.MoveNext);
                }
            });
        }
Ejemplo n.º 8
0
        public void SwitchLod()
        /// Changes detail level based on main and draft avaialability and readyness
        {
            if (this == null)
            {
                return;                           //happens after scene switch
            }
            Profiler.BeginSample("Switch Lod");

            bool useMain  = main != null;
            bool useDraft = draft != null;

            //if both using main
            //if none disabling terrain

            //in editor
                        #if UNITY_EDITOR
            if (!MapMagicObject.isPlaying)
            {
                //if both detail levels are used - choosing the one should be displayed
                if (useMain && useDraft)
                {
                    //if generating Draft in DraftData - switching to draft
                    if (draft.data != null && mapMagic?.graph != null && !mapMagic.graph.AllOutputsReady(OutputLevel.Draft, draft.data))
                    {
                        useMain = false;
                    }

                    //if generating Both in MainData - switching to draft too
                    if (main.data != null && mapMagic?.graph != null && !mapMagic.graph.AllOutputsReady(OutputLevel.Draft | OutputLevel.Main, main.data))
                    {
                        useMain = false;
                    }

                    //if dragging graph dragfield - do not switch from draft back to main
                    if (mapMagic.guiDraggingField && ActiveTerrain == draft.terrain)
                    {
                        useMain = false;
                    }
                }
            }
            else
                        #endif

            //if playmode
            {
                //default case with drafts
                if (mapMagic.draftsInPlaymode)
                {
                    if ((int)distance > mapMagic.draftSwitchRange)
                    {
                        useMain = false;
                    }
                    if ((int)distance > mapMagic.tiles.generateRange && mapMagic.hideFarTerrains)
                    {
                        useDraft = false;
                    }
                    if (!draft.applyReady)
                    {
                        useDraft = false;                                        //hiding just moved terrains
                    }
                }

                //case no drafts at all
                else
                {
                    useDraft = false;
                    if ((int)distance > mapMagic.tiles.generateRange && mapMagic.hideFarTerrains)
                    {
                        useMain = false;
                    }
                }

                //starting apply if main is not fully applied yet
                if (useMain && !main.applyReady && main.generateReady && main.data.apply.Count != 0)
                {
                    IEnumerator coroutine = ApplyRoutine(main, null);
                    main.coroutine = CoroutineManager.Enqueue(coroutine, (int)(-distance * 100), "ApplyRoutinePlaymode " + coord);
                }

                //if main is not ready and using drafts
                if (useMain && useDraft && !main.applyReady)
                {
                    useMain = false;
                }
            }

            //debugging
            //string was = ActiveTerrain==main.terrain ? "main" : (ActiveTerrain==draft.terrain ? "draft" : "null");
            //string replaced = useMain ? "main" : (useDraft ? "draft" : "null");
            //Debug.Log("Switching lod. Was " + was + ", replaced with " + replaced);
            //if (was == "draft" && replaced == "main")
            //	Debug.Log("Test");

            //finding if lod switch is for real and switching active terrain
            Terrain newActiveTerrain;
            if (useMain)
            {
                newActiveTerrain = main.terrain;
            }
            else if (useDraft)
            {
                newActiveTerrain = draft.terrain;
            }
            else
            {
                newActiveTerrain = null;
            }

            bool lodSwitched = false;
            if (ActiveTerrain != newActiveTerrain)
            {
                lodSwitched   = true;
                ActiveTerrain = newActiveTerrain;
            }

            //disabling objects
            bool objsEnabled        = useMain;      // || (useDraft && mapMagic.draftsIfObjectsChanged);
            bool currentObjsEnabled = objectsPool.isActiveAndEnabled;
            if (!objsEnabled && currentObjsEnabled)
            {
                objectsPool.gameObject.SetActive(false);
            }
            if (objsEnabled && !currentObjsEnabled)
            {
                objectsPool.gameObject.SetActive(true);
            }


            //welding
            //TODO: check active terrain to know if the switch is for real
            if (lodSwitched &&
                mapMagic.tiles.Contains(coord))                  //otherwise error on SwitchLod called from Generate (when tile has been moved)
            {
                if (useMain)
                {
                    Weld.WeldSurroundingDraftsToThisMain(mapMagic.tiles, coord);
                    Weld.WeldCorners(mapMagic.tiles, coord);

                    //Weld.SetNeighbors(mapMagic.tiles, coord);
                    //Unity calls Terrain.SetConnectivityDirty on each terrain enable or disable that resets neighbors
                    //using autoConnect instead. AutoConnect is a crap but neighbors are broken
                }
                else if (useDraft && draft.applyReady)
                {
                    Weld.WeldThisDraftWithSurroundings(mapMagic.tiles, coord);
                }
            }

            if (lodSwitched)
            {
                OnLodSwitched?.Invoke(this, useMain, useDraft);
            }

            //CoroutineManager.Enqueue( ()=>Weld.SetNeighbors(mapMagic.tiles, coord) );
            //CoroutineManager.Enqueue( mapMagic.Tmp );
            //mapMagic.Tmp();

            Profiler.EndSample();
        }
Ejemplo n.º 9
0
 void IBindCoroutineWaitable.Bind(CoroutineManager.Container container)
 {
     coroutineManager = container.CoroutineManager;
     coroutineManager.Enqueue(NextFrame);
 }