Exemplo n.º 1
0
 public void EnqueueLoadCubeRequest(LoadCubeRequest loadRequest)
 {
     _loadCubeRequests.Enqueue(loadRequest);
 }
Exemplo n.º 2
0
        public IEnumerator Load()
        {
            DebugLog("+Load()");

            var pyriteQuery = new PyriteQuery(this, SetName, ModelVersion, PyriteServer);
            yield return StartCoroutine(pyriteQuery.LoadAll());
            DebugLog("CubeQuery complete.");

            var pyriteLevel =
                pyriteQuery.DetailLevels[DetailLevel];

            var allOctCubes = pyriteQuery.DetailLevels[DetailLevel].Octree.AllItems();

            foreach (var octCube in allOctCubes)
            {
                var pCube = CreateCubeFromCubeBounds(octCube);

                var x = pCube.X;
                var y = pCube.Y;
                var z = pCube.Z;
                var cubePos = pyriteLevel.GetWorldCoordinatesForCube(pCube);

                if (UseCameraDetection)
                {
                    // Move cube to the orientation we want also move it up since the model is around -600
                    var g =
                        (GameObject)
                            //Instantiate(PlaceHolderCube, new Vector3(-cubePos.x, cubePos.z + 600, -cubePos.y),
                            //Instantiate(PlaceHolderCube, new Vector3(-cubePos.x, cubePos.z, -cubePos.y),
                            Instantiate(PlaceHolderCube, new Vector3(cubePos.x, cubePos.y, cubePos.z),
                                Quaternion.identity);

                    //var loc = Instantiate(LocatorCube, new Vector3(cubePos.x, cubePos.y, cubePos.z), Quaternion.identity) as GameObject;
                    var loc = Instantiate(LocatorCube, cubePos, Quaternion.identity) as GameObject;
                    loc.transform.parent = gameObject.transform;

                    g.transform.parent = gameObject.transform;
                    //g.GetComponent<MeshRenderer>().material.color = _colorList[_colorSelector%_colorList.Length];
                    g.GetComponent<IsRendered>().SetCubePosition(x, y, z, DetailLevel, pyriteQuery, this);

                    g.transform.localScale = new Vector3(
                        pyriteLevel.WorldCubeScale.x,
                        pyriteLevel.WorldCubeScale.z,
                        pyriteLevel.WorldCubeScale.y);
                    _colorSelector++;
                }
                else
                {
                    var loadRequest = new LoadCubeRequest(x, y, z, DetailLevel, pyriteQuery, null);
                    EnqueueLoadCubeRequest(loadRequest);
                }
            }

            if (CameraRig != null)
            {
                //DebugLog("Moving camera");
                // Hardcoding some values for now

                //var min = new Vector3(pyriteLevel.ModelBoundsMin.x, pyriteLevel.ModelBoundsMin.y,
                //    pyriteLevel.ModelBoundsMin.z);
                //var max = new Vector3(pyriteLevel.ModelBoundsMax.x, pyriteLevel.ModelBoundsMax.y,
                //    pyriteLevel.ModelBoundsMax.z);

                //min += pyriteLevel.WorldCubeScale/2;
                //max -= pyriteLevel.WorldCubeScale/2;
                //var newCameraPosition = min + (max - min)/2.0f;

                //newCameraPosition += new Vector3(0, 0, (max - min).z*1.4f);
                //CameraRig.transform.position = newCameraPosition;
                //CameraRig.transform.rotation = Quaternion.Euler(0, 180, 0);
                //DebugLog("Done moving camera");

                //var delta = pyriteLevel.ModelBoundsMax - pyriteLevel.ModelBoundsMin;
                //var center = pyriteLevel.ModelBoundsMin + new Vector3(-delta.x / 2, delta.z /2 , -delta.y);
                //CameraRig.transform.position = center;

                //var allOctCubes = pyriteQuery.DetailLevels[DetailLevel].Octree.AllItems();

                // RPL CONVERSION
                List<GridPos> gList = new List<GridPos>();
                Dictionary<string, CubeBounds> gDict = new Dictionary<string, CubeBounds>();
                foreach (var octCube in allOctCubes)
                {
                    var pCube = CreateCubeFromCubeBounds(octCube);

                    var x = pCube.X;
                    var y = pCube.Y;
                    var z = pCube.Z;
                    var gPos = new GridPos(x, y, z);
                    gList.Add(gPos);
                    gDict.Add(gPos.ToKeyString(), octCube);
                }

                int midIndex = gList.Count / 2;
                var gMid = gList.OrderBy(n => n.x).ThenBy(n => n.y).ThenBy(n => n.z).ToList()[midIndex];
                var cubeBound = CreateCubeFromCubeBounds(gDict[gMid.ToKeyString()]);
                var cubeVector3 = pyriteLevel.GetWorldCoordinatesForCube(cubeBound);
                CameraRig.transform.position = cubeVector3;

                var r = Instantiate(LocatorCube, cubeVector3, Quaternion.identity) as GameObject;
                r.GetComponent<MeshRenderer>().material.color = Color.green;
                r.transform.localScale = new Vector3(12f, 12f, 12f);

                //Instantiate(RenderCubes);
                //RenderCubes.GetComponent<RenderCubes3D>().GridMinSize = (int)(pyriteQuery.DetailLevels[DetailLevel].WorldCubeScale.x);   // World Size cut to 3x3x3 Sections
                //RenderCubes.GetComponent<RenderCubes3D>().CreateCubeLayers(CameraRig.transform.position);

            }
            DebugLog("-Load()");
        }
Exemplo n.º 3
0
        private IEnumerator ProcessLoadCubeRequest(LoadCubeRequest loadRequest)
        {
            DebugLog("+LoadCube(L{3}:{0}_{1}_{2})", loadRequest.X, loadRequest.Y, loadRequest.Z, loadRequest.Lod);
            var modelPath = loadRequest.Query.GetModelPath(loadRequest.Lod, loadRequest.X, loadRequest.Y, loadRequest.Z);
            var pyriteLevel =
                loadRequest.Query.DetailLevels[loadRequest.Lod];

            var buffer = new GeometryBuffer();

            WWW loader;
            if (!UseEbo)
            {
                if (!_objCache.ContainsKey(modelPath))
                {
                    _objCache[modelPath] = null;
                    yield return StartCoroutine(StartRequest(modelPath));
                    if (!UseWww)
                    {
                        var client = new RestClient(modelPath);
                        var request = new RestRequest(Method.GET);
                        request.AddHeader("Accept-Encoding", "gzip, deflate");
                        client.ExecuteAsync(request, (r, h) =>
                        {
                            LogResponseError(r, modelPath);
                            if (r.RawBytes != null)
                            {
                                _objCache[modelPath] = r.Content;
                            }
                            else
                            {
                                Debug.LogError("Error getting model data");
                            }
                        });
                    }
                    else
                    {
                        loader = WwwExtensions.CreateWWW(modelPath + "?fmt=obj", true);
                        yield return loader;
                        LogWwwError(loader, modelPath);
                        _objCache[modelPath] = loader.GetDecompressedText();
                    }
                    while (_objCache[modelPath] == null)
                    {
                        yield return null;
                    }
                    EndRequest(modelPath);
                }
                while (_objCache[modelPath] == null)
                {
                    yield return null;
                }
                CubeBuilderHelpers.SetGeometryData(_objCache[modelPath], buffer);
            }
            else
            {
                if (!_eboCache.ContainsKey(modelPath))
                {
                    _eboCache[modelPath] = null;
                    yield return StartCoroutine(StartRequest(modelPath));
                    if (!UseWww)
                    {
                        var client = new RestClient(modelPath);
                        var request = new RestRequest(Method.GET);
                        client.ExecuteAsync(request, (r, h) =>
                        {
                            LogResponseError(r, modelPath);
                            if (r.RawBytes != null)
                            {
                                _eboCache[modelPath] = r.RawBytes;
                            }
                            else
                            {
                                Debug.LogError("Error getting model data");
                            }
                        });
                    }
                    else
                    {
                        loader = WwwExtensions.CreateWWW(modelPath);
                        yield return loader;
                        LogWwwError(loader, modelPath);
                        _eboCache[modelPath] = loader.GetDecompressedBytes();
                    }
                    while (_eboCache[modelPath] == null)
                    {
                        yield return null;
                    }
                    EndRequest(modelPath);
                }
                // Loop while other tasks finish getting ebo data
                while (_eboCache[modelPath] == null)
                {
                    // Another task is in the process of filling out this cache entry.
                    // Loop until it is set
                    yield return null;
                }
                buffer.EboBuffer = _eboCache[modelPath];
            }

            var textureCoordinates = pyriteLevel.TextureCoordinatesForCube(loadRequest.X, loadRequest.Y);
            var materialDataKey = string.Format("model.mtl_{0}_{1}_{2}", textureCoordinates.x, textureCoordinates.y,
                loadRequest.Lod);
            if (!_materialDataCache.ContainsKey(materialDataKey))
            {
                var materialData = new List<MaterialData>();
                _materialDataCache[materialDataKey] = null;
                CubeBuilderHelpers.SetDefaultMaterialData(materialData, (int) textureCoordinates.x,
                    (int) textureCoordinates.y, loadRequest.Lod);

                foreach (var m in materialData)
                {
                    var texturePath = loadRequest.Query.GetTexturePath(loadRequest.Query.GetLodKey(loadRequest.Lod),
                        (int) textureCoordinates.x,
                        (int) textureCoordinates.y);
                    if (!_textureCache.ContainsKey(texturePath))
                    {
                        // Set to null to signal to other tasks that the key is in the process
                        // of being filled
                        _textureCache[texturePath] = null;
                        yield return StartCoroutine(StartRequest(texturePath));
                        byte[] textureData = null;
                        if (!UseWww)
                        {
                            var client = new RestClient(texturePath);
                            var request = new RestRequest(Method.GET);
                            client.ExecuteAsync(request, (r, h) =>
                            {
                                LogResponseError(r, texturePath);
                                if (r.RawBytes != null)
                                {
                                    textureData = r.RawBytes;
                                }
                                else
                                {
                                    Debug.LogError("Error getting texture data");
                                }
                            });
                        }
                        else
                        {
                            // Do not request compression for textures
                            var texloader = WwwExtensions.CreateWWW(texturePath, false);
                            yield return texloader;
                            LogWwwError(texloader, texturePath);
                            textureData = texloader.bytes;
                        }

                        while (textureData == null)
                        {
                            yield return null;
                        }

                        var texture = new Texture2D(1, 1, TextureFormat.DXT1, false);
                        texture.LoadImage(textureData);
                        _textureCache[texturePath] = texture;
                        EndRequest(texturePath);
                    }

                    // Loop while other tasks finish creating texture
                    while (_textureCache[texturePath] == null)
                    {
                        // Another task is in the process of filling out this cache entry.
                        // Loop until it is set
                        yield return null;
                    }
                    m.DiffuseTex = _textureCache[texturePath];
                }
                _materialDataCache[materialDataKey] = materialData;
            }
            while (_materialDataCache[materialDataKey] == null)
            {
                // Another task is in the process of filling out this cache entry.
                // Loop until it is set
                yield return null;
            }
            Build(buffer, _materialDataCache[materialDataKey], loadRequest.X, loadRequest.Y, loadRequest.Z,
                loadRequest.Lod, loadRequest.RegisterCreatedObjects);
            DebugLog("-LoadCube(L{3}:{0}_{1}_{2})", loadRequest.X, loadRequest.Y, loadRequest.Z, loadRequest.Lod);
        }
Exemplo n.º 4
0
        private IEnumerator OnRenderRoutine()
        {
            if (_manager != null)
            {
                _loadCubeRequest = new LoadCubeRequest(_x, _y, _z, _lod, _pyriteQuery, createdObjects =>
                        {
                            if (!_loadCubeRequest.Cancelled)
                            {
                                _cubes.AddRange(createdObjects);
                                StartCoroutine(StopRenderCheck(Camera.main));
                            }
                            else
                            {
                                foreach (var createdObject in createdObjects)
                                {
                                    Destroy(createdObject);
                                }
                            }
                        });

                _manager.EnqueueLoadCubeRequest(_loadCubeRequest);
            }
            else if (_cubeLoader != null)
            {
                _cube = new Cube {MapPosition = new Vector3(_x, _y, _z), Query = _pyriteQuery, Lod = _lod};
                _cubeLoader.AddToQueue(_cube);
                while (_cube.GameObject == null)
                {
                    yield return null;
                }
                _cubes.AddRange(new[] {_cube.GameObject});
                yield return StartCoroutine(StopRenderCheck(Camera.main));
            }
        }