Exemplo n.º 1
0
        void Update()
        {
            if (Loaded)
            {
                var adjustedPos = new Vector3(
                    -CameraRig.transform.position.x,
                    -CameraRig.transform.position.z,
                    CameraRig.transform.position.y - WorldYOffset);
                cubeCamPosNew = pyriteLevel.GetCubeForWorldCoordinates(adjustedPos);
                if (!cubeCamPos.Equals(cubeCamPosNew))
                {
                    if (DebugLogs)
                    {
                        Debug.Log(String.Format("NEW CUBE POSITION: ({0},{1},{2})", cubeCamPosNew.X, cubeCamPosNew.Y,
                                                cubeCamPosNew.Z));
                    }
                    cubeCamPos = cubeCamPosNew;
                    LoadCamCubes();
                }

                // Debugging Option
                if (ShowDebugCubes)
                {
                    var planePoint = CameraRig.transform.position;
                    planePoint.y = 0f;
                    Debug.DrawLine(CameraRig.transform.position, planePoint, Color.green, 0f, true);
                }

                ProcessQueues();
            }
        }
Exemplo n.º 2
0
        public Vector3 GetUnityWorldCoordinatesForCube(PyriteCube cube)
        {
            var xPos = WorldBoundsMin.x + WorldCubeScale.x * cube.X + WorldCubeScale.x * 0.5f;
            var yPos = WorldBoundsMin.z + WorldCubeScale.z * cube.Z + WorldCubeScale.z * 0.5f;
            var zPos = WorldBoundsMin.y + WorldCubeScale.y * cube.Y + WorldCubeScale.y * 0.5f;

            return(new Vector3(xPos, yPos, zPos));
        }
Exemplo n.º 3
0
        void LoadCamCubes()
        {
            var octIntCubeDict = new Dictionary <string, Intersection <CubeBounds> >();
            var boundBoxVector = new Vector3(BoundBoxSize, BoundBoxSize, BoundBoxSize);

            for (int detailLevel = pyriteQuery.DetailLevels.Length - 1; detailLevel > 0; detailLevel--)
            {
                //var detailLevel = 2;
                var detailLevel2   = detailLevel - 1;
                var pLevel         = pyriteQuery.DetailLevels[detailLevel];
                var pLevel2        = pyriteQuery.DetailLevels[detailLevel2];
                var camPos         = CameraRig.transform.position;
                var camPosAdjusted = OctreeAdjustedPosition(camPos);

                var cPos  = pLevel.GetCubeForWorldCoordinates(camPosAdjusted);
                var cPos2 = pLevel2.GetCubeForWorldCoordinates(camPosAdjusted);

                var cubeCamVector  = new Vector3(cPos.X + 0.5f, cPos.Y + 0.5f, cPos.Z + 0.5f);
                var cubeCamVector2 = new Vector3(cPos2.X + 0.5f, cPos2.Y + 0.5f, cPos2.Z + 0.5f);


                var minVector = cubeCamVector - boundBoxVector;
                var maxVector = cubeCamVector + boundBoxVector;

                var minCube2 = cubeCamVector2 - boundBoxVector;
                var maxCube2 = cubeCamVector2 + boundBoxVector;

                var minPC = new PyriteCube()
                {
                    X = (int)minCube2.x,
                    Y = (int)minCube2.y,
                    Z = (int)minCube2.z
                };
                var maxPC = new PyriteCube()
                {
                    X = (int)maxCube2.x,
                    Y = (int)maxCube2.y,
                    Z = (int)maxCube2.z
                };

                var minWorld2  = pLevel2.GetWorldCoordinatesForCube(minPC);
                var maxWorld2  = pLevel2.GetWorldCoordinatesForCube(maxPC);
                var minCubeC2  = pLevel.GetCubeForWorldCoordinates(minWorld2);
                var maxCubeC2  = pLevel.GetCubeForWorldCoordinates(maxWorld2);
                var minVector2 = new Vector3(minCubeC2.X + 0.5f, minCubeC2.Y + 0.5f, minCubeC2.Z + 0.5f);
                var maxVector2 = new Vector3(maxCubeC2.X + 0.5f, maxCubeC2.Y + 0.5f, maxCubeC2.Z + 0.5f);

                var octIntCubes  = pLevel.Octree.AllIntersections(new BoundingBox(minVector, maxVector)).ToList();
                var octIntCubes2 = pLevel.Octree.AllIntersections(new BoundingBox(minVector2, maxVector2)).ToList();
                if (DebugLogs)
                {
                    Debug.Log(string.Format("L{0}-Cube Count: {1}/{2}", detailLevel, octIntCubes.Count,
                                            octIntCubes2.Count));
                }

                // Load current level cubes
                foreach (var c in octIntCubes)
                {
                    var pCube   = CreateCubeFromCubeBounds(c.Object);
                    var cubeKey = string.Format("{0},{1}", detailLevel, pCube.GetKey());
                    if (octIntCubeDict.ContainsKey(cubeKey))
                    {
                        continue;
                    }
                    octIntCubeDict.Add(cubeKey, c);
                }

                // Replace Intersecting Higher Detail Cubes
                foreach (var c in octIntCubes2)
                {
                    var pCube   = CreateCubeFromCubeBounds(c.Object);
                    var cubeKey = string.Format("{0},{1}", detailLevel, pCube.GetKey());
                    if (octIntCubeDict.ContainsKey(cubeKey))
                    {
                        octIntCubeDict.Remove(cubeKey);
                        var cubeW    = pLevel.GetWorldCoordinatesForCube(pCube);
                        var cubeL    = pLevel2.GetCubeForWorldCoordinates(cubeW);
                        var cubeV    = new Vector3(cubeL.X + 0.5f, cubeL.Y + 0.5f, cubeL.Z + 0.5f);
                        var minCubeV = cubeV - boundBoxVector;
                        var maxCubeV = cubeV + boundBoxVector;

                        var q = pLevel2.Octree.AllIntersections(new BoundingBox(minCubeV, maxCubeV)).ToList();
                        if (DebugLogs)
                        {
                            Debug.LogFormat("Replacement Count: {0}", q.Count);
                        }

                        foreach (var c2 in q)
                        {
                            var pCube2   = CreateCubeFromCubeBounds(c2.Object);
                            var cubeKey2 = string.Format("{0},{1}", detailLevel2, pCube2.GetKey());
                            if (octIntCubeDict.ContainsKey(cubeKey2))
                            {
                                continue;
                            }
                            octIntCubeDict.Add(cubeKey2, c2);
                        }
                    }
                }
            }

            var octIntList  = octIntCubeDict.OrderByDescending(x => x.Key);
            int cubeCounter = 0;

            if (DebugLogs)
            {
                Debug.Log(string.Format("Octree Count: {0}", octIntList.Count()));
            }
            foreach (var i in octIntList)
            {
                cubeCounter++;
                var pCube       = CreateCubeFromCubeBounds(i.Value.Object);
                var cubeKey     = string.Format(i.Key);
                var detailLevel = GetDetailLevelFromKey(i.Key);
                var pLevel      = pyriteQuery.DetailLevels[detailLevel];
                var cubePos     = pLevel.GetWorldCoordinatesForCube(pCube);

                CubeTracker ct = null;
                // Setup object at cube location
                if (cubeDict.ContainsKey(cubeKey))
                {
                    ct = cubeDict[cubeKey];
                    cubeList.Remove(ct);
                    cubeList.AddFirst(ct);
                    if (!ct.Active)
                    {
                        ct.Active = true;
                        if (ShowLocatorCubes)
                        {
                            ct.trackObject.GetComponent <MeshRenderer>().material.color = Color.green;
                            ct.trackObject.SetActive(true);
                        }
                    }
                }
                else
                {
                    if ((LimitOctreeListCount && cubeList.Count < OctreeListCount) || (!LimitOctreeListCount && (cubeList.Last == null || cubeList.Last.Value.Active)))
                    {
                        ct = new CubeTracker(cubeKey)
                        {
                            gameObject = Instantiate(RenderCube, new Vector3(0, WorldYOffset, 0), Quaternion.identity) as GameObject,
                            manager    = this
                        };
                        ct.gameObject.transform.parent = OctreeTracking.transform;
                    }
                    else
                    {
                        // Reuse Last CubeTracker
                        if (DebugLogs)
                        {
                            Debug.Log("Reusing Cube");
                        }
                        ct = cubeList.Last.Value;
                        if (ct.Active)
                        {
                            Debug.LogError("ERROR: Active Object in List Tail. Too many required cubes.");
                            return;
                        }
                        cubeList.RemoveLast();
                        cubeDict.Remove(ct.DictKey);
                        ct.DictKey = cubeKey;

                        if (ct.gameObject != null)
                        {
                            ct.gameObject.name = cubeKey;
                            ct.ClearMesh();
                        }
                    }

                    if (DebugLogs)
                    {
                        Debug.Log("Request Cube");
                    }
                    RequestCube(pCube.X, pCube.Y, pCube.Z, detailLevel, ct.gameObject);

                    // Setup Locator GameObject
                    var adjustedPos = new Vector3(
                        -cubePos.x,
                        cubePos.z + WorldYOffset,
                        -cubePos.y);
                    GameObject gObj = null;
                    if (ShowLocatorCubes)
                    {
                        if (ct.trackObject)
                        {
                            ct.trackObject.transform.position   = adjustedPos;
                            ct.trackObject.transform.localScale = new Vector3(
                                pLevel.WorldCubeScale.x * .8f,
                                pLevel.WorldCubeScale.y * .8f,
                                pLevel.WorldCubeScale.z * .8f);
                            ct.trackObject.SetActive(true);
                            ct.trackObject.GetComponent <MeshRenderer>().material.color = Color.yellow;
                        }
                        else
                        {
                            gObj = Instantiate(LocatorCube, adjustedPos, Quaternion.identity) as GameObject;
                            gObj.transform.localScale = new Vector3(
                                pLevel.WorldCubeScale.x * .8f,
                                pLevel.WorldCubeScale.y * .8f,
                                pLevel.WorldCubeScale.z * .8f);
                            gObj.transform.parent = OctreeTracking.transform;
                            ct.trackObject        = gObj;
                        }
                    }

                    ct.pyriteQuery = pyriteQuery;
                    ct.Active      = true;
                    cubeList.AddFirst(ct);
                    cubeDict.Add(ct.DictKey, ct);
                }
            }
            if (DebugLogs)
            {
                Debug.Log(String.Format("CubeCounter: {0}  CubeList/Dict: {1}/{2}", cubeCounter, cubeList.Count,
                                        cubeDict.Count));
            }

            int cleanupCounter = 0;

            foreach (var cube in cubeList)
            {
                cleanupCounter++;
                if (cleanupCounter > cubeCounter)
                {
                    if (!cube.Active)
                    {
                        //Debug.Log("Break List OPTION");
                        //break;
                    }
                    cube.Active = false;

                    if (ShowLocatorCubes && cube.trackObject)
                    {
                        if (ShowLocatorCubes)
                        {
                            cube.trackObject.GetComponent <MeshRenderer>().material.color = Color.red;
                        }
                        else
                        {
                            cube.trackObject.SetActive(false);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        IEnumerator Load()
        {
            tempPosition       = transform.position;
            transform.position = Vector3.zero;

            pyriteQuery = new PyriteQuery(this,
                                          SetName,
                                          ModelVersion,
                                          PyriteServer,
                                          UpgradeFactor,
                                          UpgradeConstant,
                                          DowngradeFactor,
                                          DowngradeConstant);
            yield return(StartCoroutine(pyriteQuery.LoadAll(FilterDetailLevels ? DetailLevelsToFilter : null)));

            pyriteLevel = pyriteQuery.DetailLevels[DetailLevel];
            var setSize = pyriteLevel.SetSize;

            if (DebugLogs)
            {
                Debug.Log("Set Size " + setSize);
            }

            var adjustedPos = new Vector3(
                -CameraRig.transform.position.x,
                -CameraRig.transform.position.z,
                CameraRig.transform.position.y - WorldYOffset);

            cubeCamPos = pyriteLevel.GetCubeForWorldCoordinates(adjustedPos);

            var worldObject = new GameObject("OctreeParent") as GameObject;

            worldObject.transform.position = Vector3.zero;
            worldObject.transform.rotation = Quaternion.identity;
            OctreeTracking = new GameObject("OctreeTracking") as GameObject;
            OctreeTracking.transform.position = Vector3.zero;
            OctreeTracking.transform.rotation = Quaternion.identity;

            if (ShowCubes)
            {
                foreach (var i in pyriteLevel.Octree.AllItems())
                {
                    var pCube   = CreateCubeFromCubeBounds(i);
                    var cubePos = pyriteLevel.GetWorldCoordinatesForCube(pCube);

                    var adjustedCubePos = new Vector3(
                        -cubePos.x,
                        cubePos.z + WorldYOffset,
                        -cubePos.y);
                    var loc = Instantiate(TranslucentCube, adjustedCubePos, Quaternion.identity) as GameObject;
                    loc.name = string.Format("Mesh:{0},{1},{2}", pCube.X, pCube.Y, pCube.Z);
                    loc.transform.localScale = new Vector3(
                        pyriteLevel.WorldCubeScale.x,
                        pyriteLevel.WorldCubeScale.z,
                        pyriteLevel.WorldCubeScale.y);
                    loc.transform.parent = worldObject.transform;
                }
            }

            transform.position = tempPosition;
            LoadCamCubes();
        }