private void InternalSetup() { #if !UNITY_WSA _mainThread = Thread.CurrentThread; #endif _guiStyle.normal.textColor = Color.red; if (_geometryBufferCache == null) { _geometryBufferCache = new DictionaryCache <string, GeometryBuffer>(GeometryBufferCacheSize); } else { Debug.LogWarning("GeometryBuffer cache already initialized. Skipping initizliation."); } if (MaterialDataCache == null) { MaterialDataCache = new MaterialDataCache(MaterialDataCacheSize); } else { Debug.LogWarning("Material Data cache already initialized. Skipping initizliation."); } ObjectPooler.Current.CreatePoolForObject(BaseModelCube); // Optional pool only used in camera detection scenario if (PlaceHolderCube != null) { ObjectPooler.Current.CreatePoolForObject(PlaceHolderCube); } CacheWebRequest.InitializeCache(CacheSize, ProxyUrl); }
private IEnumerator GetModelForRequest(LoadCubeRequest loadRequest) { //var modelPath = loadRequest.Query.GetModelPath(loadRequest.LodIndex, loadRequest.X, loadRequest.Y, loadRequest.Z); var modelPath = pyriteQuery.GetModelPath(loadRequest.LodIndex, loadRequest.X, loadRequest.Y, loadRequest.Z); while (!Monitor.TryEnter(_eboCache)) { yield return(null); } // If the geometry data is being loaded or this is the first request to load it add the request the dependency list if (!_eboCache.ContainsKey(modelPath) || _eboCache[modelPath] == null) { yield return(StartCoroutine(AddDependentRequest(loadRequest, modelPath))); if (!_eboCache.ContainsKey(modelPath)) { // Model data was not present in cache nor has any request started constructing it //EboCacheMisses++; _eboCache[modelPath] = null; if (UseWwwForEbo) { var cachePath = CacheWebRequest.GetCacheFilePath(modelPath); WWW modelWww; if (CacheWebRequest.IsItemInCache(cachePath)) { //FileCacheHits++; modelWww = new WWW("file:///" + cachePath); yield return(modelWww); } else { //FileCacheMisses++; modelWww = new WWW(modelPath); yield return(modelWww); CacheWebRequest.AddToCache(cachePath, modelWww.bytes); } GeometryBuffer buffer = new GeometryBuffer(_geometryBufferAltitudeTransform, true) { Buffer = modelWww.bytes }; buffer.Process(); _eboCache[modelPath] = buffer; yield return(StartCoroutine(SucceedGetGeometryBufferRequest(modelPath, buffer))); } else { CacheWebRequest.GetBytes(modelPath, modelResponse => { lock (_eboCache) { if (modelResponse.Status == CacheWebRequest.CacheWebResponseStatus.Error) { Debug.LogError("Error getting model [" + modelPath + "] " + modelResponse.ErrorMessage); FailGetGeometryBufferRequest(loadRequest, modelPath); } else if (modelResponse.Status == CacheWebRequest.CacheWebResponseStatus.Cancelled) { _eboCache.Remove(modelPath); } else { if (modelResponse.IsCacheHit) { //FileCacheHits++; } else { //FileCacheMisses++; } GeometryBuffer buffer = new GeometryBuffer(_geometryBufferAltitudeTransform, true) { Buffer = modelResponse.Content }; buffer.Process(); _eboCache[modelPath] = buffer; SucceedGetGeometryBufferRequest(modelPath, buffer).Wait(); } } }, DependentRequestsExistBlocking); } } } else // The model data was in the cache { // Model was constructed move request to next step //EboCacheHits++; loadRequest.GeometryBuffer = _eboCache[modelPath]; MoveRequestForward(loadRequest); } Monitor.Exit(_eboCache); }
private IEnumerator GetMaterialForRequest(LoadCubeRequest loadRequest) { //var pyriteLevel = loadRequest.Query.DetailLevels[loadRequest.LodIndex]; //var textureCoordinates = pyriteLevel.TextureCoordinatesForCube(loadRequest.X, loadRequest.Y); //var texturePath = loadRequest.Query.GetTexturePath(loadRequest.LodIndex, (int)textureCoordinates.x, (int)textureCoordinates.y); var pyriteLevel = pyriteQuery.DetailLevels[loadRequest.LodIndex]; var textureCoordinates = pyriteLevel.TextureCoordinatesForCube(loadRequest.X, loadRequest.Y); var texturePath = pyriteQuery.GetTexturePath(loadRequest.LodIndex, (int)textureCoordinates.x, (int)textureCoordinates.y); while (!Monitor.TryEnter(MaterialDataCache)) { yield return(null); } // If the material data is not in the cache or in the middle of being constructed add this request as a dependency if (!MaterialDataCache.ContainsKey(texturePath) || MaterialDataCache[texturePath] == null) { // Add this request to list of requests that is waiting for the data yield return(StartCoroutine(AddDependentRequest(loadRequest, texturePath))); // Check if this is the first request for material (or it isn't in the cache) if (!MaterialDataCache.ContainsKey(texturePath)) { if (UseWwwForTextures) { // Material data was not in cache nor being constructed // Set to null to signal to other tasks that the key is in the process // of being filled MaterialDataCache[texturePath] = null; var materialData = CubeBuilderHelpers.GetDefaultMaterialData( (int)textureCoordinates.x, (int)textureCoordinates.y, loadRequest.LodIndex, texturePath); var cachePath = CacheWebRequest.GetCacheFilePath(texturePath); if (!CacheFill) { WWW textureWww; // = new WWW(texturePath); if (CacheWebRequest.IsItemInCache(cachePath)) { textureWww = new WWW("file:///" + cachePath); yield return(textureWww); } else { textureWww = new WWW(texturePath); yield return(textureWww); CacheWebRequest.AddToCache(cachePath, textureWww.bytes); } materialData.DiffuseTex = textureWww.texture; materialData.DiffuseTex.name = materialData.Name; } MaterialDataCache[texturePath] = materialData; MaterialDataCache.AddRef(texturePath); // Move forward dependent requests that wanted this material data yield return(StartCoroutine(SucceedGetMaterialDataRequests(texturePath, materialData))); } else { // Material data was not in cache nor being constructed // Set to null to signal to other tasks that the key is in the process // of being filled MaterialDataCache[texturePath] = null; var materialData = CubeBuilderHelpers.GetDefaultMaterialData( (int)textureCoordinates.x, (int)textureCoordinates.y, loadRequest.LodIndex, texturePath); _partiallyConstructedMaterialDatas[texturePath] = materialData; CacheWebRequest.GetBytes(texturePath, textureResponse => { CheckIfBackgroundThread(); if (textureResponse.Status == CacheWebRequest.CacheWebResponseStatus.Error) { Debug.LogError("Error getting texture [" + texturePath + "] " + textureResponse.ErrorMessage); FailGetMaterialDataRequest(loadRequest, texturePath); } else if (textureResponse.Status == CacheWebRequest.CacheWebResponseStatus.Cancelled) { lock (MaterialDataCache) { MaterialDataCache.Remove(texturePath); } } else { if (textureResponse.IsCacheHit) { //FileCacheHits++; } else { //FileCacheMisses++; } _texturesReadyForMaterialDataConstruction.ConcurrentEnqueue( new KeyValuePair <string, byte[]>(texturePath, textureResponse.Content)).Wait(); } }, DependentRequestsExistBlocking); } } } else // The material was in the cache { // Material data ready get it and move on //MaterialCacheHits++; loadRequest.MaterialData = MaterialDataCache[texturePath]; MaterialDataCache.AddRef(texturePath); MoveRequestForward(loadRequest); } Monitor.Exit(MaterialDataCache); }