Пример #1
0
        private void OnUIRemixed(UICore obj)
        {
            Logger.Log("UICore.OnUIRemixed: UI remixed!");

            obj.Panels.ForEach((panel) => { panel.FromThisScene = false; });
            obj.RemixUI(true);
        }
Пример #2
0
        private void BuildMipmapLevel(int level)
        {
            var tilesCount = Mathf.Max(1, (BaseLevelSize / Size) >> (MaxLevel - level));

            Logger.Log(string.Format("HeightMipmap.BuildMipmapLevel: Build mipmap level: {0}", level));

            CurrentLevel = level + 1;

            Reset(BaseLevelSize >> (MaxLevel - CurrentLevel), BaseLevelSize >> (MaxLevel - CurrentLevel), Mathf.Min(TopLevelSize << CurrentLevel, Size));

            for (int ty = 0; ty < tilesCount; ++ty)
            {
                for (int tx = 0; tx < tilesCount; ++tx)
                {
                    var offset          = (int)0;
                    var currentTileSize = Mathf.Min(TopLevelSize << level, Size);

                    for (int j = -2; j <= currentTileSize + 2; ++j)
                    {
                        for (int i = -2; i <= currentTileSize + 2; ++i)
                        {
                            TileData[offset++] = GetTileHeight(2 * (tx * currentTileSize + i), 2 * (ty * currentTileSize + j));
                        }
                    }

                    SaveTile("Base", level, tx, ty, TileData);
                }
            }
        }
Пример #3
0
    private void SaveScreenshot(Texture2D screenShotTexture, string fileName = "Screenshot")
    {
        if (screenShotTexture != null)
        {
            var filePath = string.Format("{0}/{1}_{2:yy.MM.dd-hh.mm.ss}_{3}", Application.dataPath, fileName, DateTime.Now, (int)UnityEngine.Random.Range(0.0f, 100.0f));

            switch (Format)
            {
            case ScreenshotFormat.JPG:
                File.WriteAllBytes(filePath + ".jpg", screenShotTexture.EncodeToJPG(100));
                break;

            case ScreenshotFormat.PNG:
                File.WriteAllBytes(filePath + ".png", screenShotTexture.EncodeToPNG());
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Logger.Log(string.Format("ScreenshotHelper: Screenshot Saved. {0}", filePath));

#if UNITY_EDITOR
            AssetDatabase.Refresh();
#endif
        }
        else
        {
            Logger.LogError("ScreenshotHelper: screenShotTexture is null!");
        }
    }
Пример #4
0
        void PreprocessSphericalDem(InputMap source, string tempFolder, string destinationFolder)
        {
            if (DestinationTileSize % DestinationMinTileSize != 0)
            {
                throw new InvalidParameterException("DestinationTileSize must be a multiple of DestinationMinTileSize!");
            }

            var startTime       = Time.realtimeSinceStartup;
            var destinationSize = DestinationTileSize << DestinationMaxLevel;

            IHeightFunction2D function1 = new SphericalHeightFunction(source, ProjectionHelper.Projection1, destinationSize);
            IHeightFunction2D function2 = new SphericalHeightFunction(source, ProjectionHelper.Projection2, destinationSize);
            IHeightFunction2D function3 = new SphericalHeightFunction(source, ProjectionHelper.Projection3, destinationSize);
            IHeightFunction2D function4 = new SphericalHeightFunction(source, ProjectionHelper.Projection4, destinationSize);
            IHeightFunction2D function5 = new SphericalHeightFunction(source, ProjectionHelper.Projection5, destinationSize);
            IHeightFunction2D function6 = new SphericalHeightFunction(source, ProjectionHelper.Projection6, destinationSize);

            HeightMipmap mipmap1 = new HeightMipmap(function1, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);
            HeightMipmap mipmap2 = new HeightMipmap(function2, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);
            HeightMipmap mipmap3 = new HeightMipmap(function3, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);
            HeightMipmap mipmap4 = new HeightMipmap(function4, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);
            HeightMipmap mipmap5 = new HeightMipmap(function5, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);
            HeightMipmap mipmap6 = new HeightMipmap(function6, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);

            HeightMipmap.SetCube(mipmap1, mipmap2, mipmap3, mipmap4, mipmap5, mipmap6);

            mipmap1.Compute(); mipmap1.Generate(0, 0, 0, destinationFolder + "/" + FileName + "1" + ".dat");
            mipmap2.Compute(); mipmap2.Generate(0, 0, 0, destinationFolder + "/" + FileName + "2" + ".dat");
            mipmap3.Compute(); mipmap3.Generate(0, 0, 0, destinationFolder + "/" + FileName + "3" + ".dat");
            mipmap4.Compute(); mipmap4.Generate(0, 0, 0, destinationFolder + "/" + FileName + "4" + ".dat");
            mipmap5.Compute(); mipmap5.Generate(0, 0, 0, destinationFolder + "/" + FileName + "5" + ".dat");
            mipmap6.Compute(); mipmap6.Generate(0, 0, 0, destinationFolder + "/" + FileName + "6" + ".dat");

            Logger.Log(string.Format("PreProcessTerrain.PreprocessDem: Computation time: {0} s", (Time.realtimeSinceStartup - startTime)));
        }
Пример #5
0
        private void Rotation(int r, int n, int x, int y, out int xp, out int yp)
        {
            switch (r)
            {
            case 0:
                xp = x;
                yp = y;
                break;

            case 1:
                xp = y;
                yp = n - 1 - x;
                break;

            case 2:
                xp = n - 1 - x;
                yp = n - 1 - y;
                break;

            case 3:
                xp = n - 1 - y;
                yp = x;
                break;

            default:
                xp = 0;
                yp = 0;

                Logger.LogError("HeightMipmap.Rotation: Something goes wrong!");
                Debug.Break();

                break;
            }
        }
Пример #6
0
        private void LoadAssembly(string path)
        {
            try
            {
                var assembly  = Assembly.LoadFile(path);
                var attrbutes = assembly.GetCustomAttributes(typeof(SpaceAddonAssembly), false) as SpaceAddonAssembly[];

                if (attrbutes == null || attrbutes.Length == 0)
                {
                    Logger.LogError(string.Format("AssemblyLoader.LoadAssembly: This is not an adddon assembly! {0}", path));
                }
                else
                {
                    var addonAssembly = attrbutes[0];
                    var mb            = GetAllSubclassesOf <Type, SpaceAddonMonoBehaviour, MonoBehaviour>(assembly);
                    var aet           = new AssemblyExternalTypes(typeof(MonoBehaviour), mb);
                    var ae            = new AssemblyExternal(path, addonAssembly.Name, addonAssembly.Version, assembly, aet);

                    ExternalAssemblies.Add(ae);

                    TotalLoaded++;

                    FireHotPlugin(ae);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(string.Format("AssemblyLoader.LoadAssembly: LoadAssembly Exception: {0}", ex.Message));
            }
        }
Пример #7
0
        private void OnSceneWillBeLoadedNow(EntryPoint sceneName, LoadSceneMode loadSceneMode)
        {
            Logger.Log("UICore.OnSceneWillBeLoadedNow: OnSceneWillBeLoadedNow!");

            Panels.ForEach((panel) => { panel.FromThisScene = false; });

            RemixUI(true);
        }
Пример #8
0
        public void Generate(int rootLevel, int rootTx, int rootTy, string file)
        {
            for (int level = 1; level <= MaxLevel; ++level)
            {
                BuildResiduals(level);
            }

            File.Delete(file);

            var tilesCount = MinLevel + ((1 << (Mathf.Max(MaxLevel - MinLevel, 0) * 2 + 2)) - 1) / 3;

            Logger.Log(string.Format("HeightMipmap.Generate: tiles count: {0}", tilesCount));

            long[] offsets   = new long[tilesCount * 2];
            byte[] byteArray = new byte[(7 * 4) + (+MaxR.Length * 4) + (offsets.Length * 8)];
            long   offset    = byteArray.Length;

            using (Stream stream = new FileStream(file, FileMode.Create))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.Write(byteArray, 0, byteArray.Length);
            }

            for (int l = 0; l < MinLevel; ++l)
            {
                ProduceTile(l, 0, 0, ref offset, offsets, file);
            }

            for (int l = MinLevel; l <= MaxLevel; ++l)
            {
                ProduceTilesLebeguesOrder(l - MinLevel, 0, 0, 0, ref offset, offsets, file);
            }

            Buffer.BlockCopy(new int[] { MinLevel }, 0, byteArray, 0, 4);
            Buffer.BlockCopy(new int[] { MaxLevel }, 0, byteArray, 4, 4);
            Buffer.BlockCopy(new int[] { Size }, 0, byteArray, 8, 4);
            Buffer.BlockCopy(new int[] { rootLevel }, 0, byteArray, 12, 4);
            Buffer.BlockCopy(new int[] { rootTx }, 0, byteArray, 16, 4);
            Buffer.BlockCopy(new int[] { rootTy }, 0, byteArray, 20, 4);
            Buffer.BlockCopy(new float[] { Scale }, 0, byteArray, 24, 4);
            Buffer.BlockCopy(MaxR, 0, byteArray, 28, 4 * MaxR.Length);
            Buffer.BlockCopy(offsets, 0, byteArray, 28 + (4 * MaxR.Length), 8 * offsets.Length);

            using (Stream stream = new FileStream(file, FileMode.Open))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.Write(byteArray, 0, byteArray.Length);
            }

            for (int i = 0; i < MaxR.Length; i++)
            {
                Logger.Log(string.Format("HeightMipmap.Generate: Level: {0}; MaxResidual: {1}", i, MaxR[i].ToString("F6")));
            }

            Logger.Log(string.Format("HeightMipmap.Generate: Saved file path: {0} ", file));
        }
Пример #9
0
        public override void Reset(int width, int height, int tileSize)
        {
            if (Width != width || Height != height)
            {
                Logger.Log(string.Format("HeightMipmap.Reset: Resetting to width {0} and height {1}; TileSize: {2}", height, width, tileSize));

                base.Reset(width, height, tileSize);

                if (Left != null)
                {
                    Left.SetCurrentLevel(CurrentLevel);
                    Left.Reset(width, height, tileSize);
                }

                if (Right != null)
                {
                    Right.SetCurrentLevel(CurrentLevel);
                    Right.Reset(width, height, tileSize);
                }

                if (Bottom != null)
                {
                    Bottom.SetCurrentLevel(CurrentLevel);
                    Bottom.Reset(width, height, tileSize);
                }

                if (Top != null)
                {
                    Top.SetCurrentLevel(CurrentLevel);
                    Top.Reset(width, height, tileSize);
                }
            }
            else
            {
                if (Left != null)
                {
                    Left.SetCurrentLevel(CurrentLevel);
                }

                if (Right != null)
                {
                    Right.SetCurrentLevel(CurrentLevel);
                }

                if (Bottom != null)
                {
                    Bottom.SetCurrentLevel(CurrentLevel);
                }

                if (Top != null)
                {
                    Top.SetCurrentLevel(CurrentLevel);
                }
            }
        }
Пример #10
0
        private void FirePlugins(List <AssemblyExternal> ExternalAssemblies)
        {
            int counter = 0;

            foreach (AssemblyExternal assembly in ExternalAssemblies)
            {
                counter += assembly.Types.SelectMany(kvp => kvp.Value).Count(v => FirePlugin(v));
            }

            Logger.Log(string.Format("{0} plugins fired at scene №: {1}", counter, SceneManager.GetActiveScene().buildIndex));
        }
Пример #11
0
        private void FirePlugins(List <AssemblyExternal> ExternalAssemblies, int level)
        {
            int counter = 0;

            foreach (AssemblyExternal assembly in ExternalAssemblies)
            {
                counter += assembly.Types.SelectMany(kvp => kvp.Value).Count(v => FirePlugin(v, level));
            }

            Logger.Log(string.Format("{0} plugins fired at scene №: {1}", counter, level));
        }
Пример #12
0
    public Texture2D LoadTextureFromResources(string textureName)
    {
        var path            = "Textures/" + textureName;
        var textureResource = Resources.Load(path, typeof(Texture2D)) as Texture2D;

        if (textureResource == null)
        {
            Logger.Log(string.Format("UniformsManager.LoadTextureFromResources: Failed to load texture from {0}", path));
        }

        return(textureResource);
    }
Пример #13
0
        /// <summary>
        /// Preprocess a map into files that can be used with a <see cref="OrthoCPUProducer"/>.
        /// </summary>
        /// <param name="source">The map to be preprocessed.</param>
        /// <param name="tempFolder">Where temporary files must be saved.</param>
        /// <param name="destinationFolder">Where the precomputed file must be saved.</param>
        void PreprocessPlaneOrtho(InputMap source, string tempFolder, string destinationFolder)
        {
            var startTime       = Time.realtimeSinceStartup;
            var destinationSize = DestinationTileSize << DestinationMaxLevel;

            IColorFunction2D function = new PlaneColorFunction(source, destinationSize);
            ColorMipmap      mipmap   = new ColorMipmap(function, destinationSize, DestinationTileSize, 2, DestinationChannels, tempFolder);

            mipmap.Compute();
            mipmap.Generate(0, 0, 0, destinationFolder + "/" + FileName + ".dat");

            Logger.Log(string.Format("PreProcessTerrain.PreprocessPlaneOrtho: Computation time: {0} s", (Time.realtimeSinceStartup - startTime)));
        }
Пример #14
0
        public HeightMipmap(IHeightFunction2D heightFunction, int topLevelSize, int baseLevelSize, int tileSize, string tempFolder) : base(baseLevelSize, baseLevelSize, tileSize, 1, 200)
        {
            HeightFunction = heightFunction;
            TopLevelSize   = topLevelSize;
            BaseLevelSize  = baseLevelSize;

            TempFolder = tempFolder;

            Size     = tileSize;
            Scale    = 1.0f;
            MinLevel = 0;
            MaxLevel = 0;

            var size = tileSize;

            while (size > topLevelSize)
            {
                MinLevel += 1;
                size     /= 2;
            }

            size = baseLevelSize;

            while (size > topLevelSize)
            {
                MaxLevel += 1;
                size     /= 2;
            }

            MaxR         = new float[MaxLevel + 1];
            TileData     = new float[(tileSize + 5) * (tileSize + 5) * 1];
            ConstantTile = -1;

            Left   = null;
            Right  = null;
            Bottom = null;
            Top    = null;

            if (!Directory.Exists(TempFolder))
            {
                Directory.CreateDirectory(TempFolder);
            }

            Logger.Log(string.Format("HeightMipmap.ctor: TopLevelSize: {0}; BaseLevelSize: {1}; TileSize: {2}; Scale: {3}; MinLevel: {4}; MaxLevel: {5}", TopLevelSize,
                                     BaseLevelSize,
                                     Size,
                                     Scale,
                                     MinLevel,
                                     MaxLevel));
        }
Пример #15
0
        private void Start()
        {
            Source = GetComponent <InputMap>();

            if (Source == null)
            {
                throw new NullReferenceException("Input map is null. Have you added a Input map component to PreProcess game object?");
            }

            ApplicationDataPath = Application.dataPath;

            try
            {
                switch ((int)Mode)
                {
                case (int)MODE.HEIGHT:
                    PreprocessDem();
                    break;

                case (int)MODE.COLOR:
                    PreprocessOrtho();
                    break;

                default:
                    Logger.LogWarning("PreProcessTerrain.Start: Nothing to produce/precompute!");
                    break;
                }
            }
            finally
            {
                if (DeleteTempOnFinish)
                {
                    var directory = new DirectoryInfo(ApplicationDataPath + TempFolder);

                    if (directory.Exists)
                    {
                        foreach (var file in directory.GetFiles())
                        {
                            file.Delete();
                        }
                    }
                }

#if UNITY_EDITOR
                AssetDatabase.Refresh();
#endif
            }
        }
Пример #16
0
        void PreprocessOrtho()
        {
            switch ((int)Type)
            {
            case (int)TYPE.PLANE:
                PreprocessPlaneOrtho(Source, ApplicationDataPath + TempFolder, ApplicationDataPath + DestinationFolder);
                break;

            case (int)TYPE.SPHERICAL:
                throw new NotImplementedException();

            default:
                Logger.LogWarning("PreProcessTerrain.Preprocess: Nothing to produce/precompute!");
                break;
            }
        }
Пример #17
0
        private void LoadDetectedAssemblies(List <string> allPaths)
        {
            if (allPaths == null)
            {
                DetectAssembies(out allPaths);
                Logger.LogError("Something wrong with path's array! Detecting assemblies again!");
            }

            for (int i = 0; i < allPaths.Count; i++)
            {
                string path = allPaths[i];

                //Delay(0.5f, () => { LoadAssembly(path); });
                LoadAssembly(path);
            }
        }
Пример #18
0
        private void ProduceTile(int level, int tx, int ty, ref long offset, long[] offsets, string file)
        {
            Logger.Log(string.Format("ColorMipmap.ProduceTile: Producing tile {0}:{1}:{2}!", level, tx, ty));

            ProduceTile(level, tx, ty);

            var tileID        = tx + ty * (1 << level) + ((1 << (2 * level)) - 1) / 3;
            var isConstant    = true;
            var constantValue = TileData[0];

            for (int i = 1; i < (Size + 2 * Border) * (Size + 2 * Border); ++i)
            {
                if (TileData[i] != TileData[i - 1])
                {
                    isConstant = false;

                    break;
                }
            }

            if (isConstant && ConstantTileIDs.ContainsKey(constantValue))
            {
                Logger.Log("ColorMipmap.ProduceTile: tile is const (All same value)!");

                var constantId = ConstantTileIDs[constantValue];

                offsets[2 * tileID]     = offsets[2 * constantId];
                offsets[2 * tileID + 1] = offsets[2 * constantId + 1];
            }
            else
            {
                using (Stream stream = new FileStream(file, FileMode.Open))
                {
                    stream.Seek(offset, SeekOrigin.Begin);
                    stream.Write(TileData, 0, TileData.Length);
                }

                offsets[2 * tileID] = offset;
                offset += TileData.Length;
                offsets[2 * tileID + 1] = offset;
            }

            if (isConstant && !ConstantTileIDs.ContainsKey(constantValue))
            {
                ConstantTileIDs.Add(constantValue, tileID);
            }
        }
Пример #19
0
        void PreprocessDem()
        {
            switch ((int)Type)
            {
            case (int)TYPE.PLANE:
                PreprocessPlaneDem(Source, ApplicationDataPath + TempFolder, ApplicationDataPath + DestinationFolder);
                break;

            case (int)TYPE.SPHERICAL:
                PreprocessSphericalDem(Source, ApplicationDataPath + TempFolder, ApplicationDataPath + DestinationFolder);
                break;

            default:
                Logger.LogWarning("PreProcessTerrain.Preprocess: Nothing to produce/precompute!");
                break;
            }
        }
Пример #20
0
        /// <summary>
        /// Preprocess a map into files that can be used with a <see cref="ResidualProducer"/>.
        /// </summary>
        /// <param name="source">The map to be preprocessed.</param>
        /// <param name="tempFolder">Where temporary files must be saved.</param>
        /// <param name="destinationFolder">Where the precomputed file must be saved.</param>
        void PreprocessPlaneDem(InputMap source, string tempFolder, string destinationFolder)
        {
            if (DestinationTileSize % DestinationMinTileSize != 0)
            {
                throw new InvalidParameterException("DestinationTileSize must be a multiple of DestinationMinTileSize!");
            }

            var startTime       = Time.realtimeSinceStartup;
            var destinationSize = DestinationTileSize << DestinationMaxLevel;

            IHeightFunction2D function = new PlaneHeightFunction(source, destinationSize);
            HeightMipmap      mipmap   = new HeightMipmap(function, DestinationMinTileSize, destinationSize, DestinationTileSize, tempFolder);

            mipmap.Compute();
            mipmap.Generate(0, 0, 0, destinationFolder + "/" + FileName + ".dat");

            Logger.Log(string.Format("PreProcessTerrain.PreprocessPlaneDem: Computation time: {0} s", (Time.realtimeSinceStartup - startTime)));
        }
Пример #21
0
        protected override void Pass()
        {
            Logger.Log(string.Format("AssemblyLoader Initiated at scene №: {0}", SceneManager.GetActiveScene().buildIndex));

            if (!Loaded)
            {
                DetectAndLoadAssemblies();
                Loaded = true;
            }

            if (SceneManager.GetActiveScene().buildIndex == 0 && Loaded)
            {
                //Delay((TotalDetected + 1) * 2, () => { SceneManager.LoadScene(1); });
                SceneManager.LoadScene(1);
            }

            base.Pass();
        }
Пример #22
0
        protected override void Pass()
        {
            Logger.Log(string.Format("AssemblyLoader.Pass: AssemblyLoader Initiated at scene: {0}", (EntryPoint)SceneManager.GetActiveScene().buildIndex));

            if (!Loaded)
            {
                DetectAndLoadAssemblies();
                Loaded = true;
            }

            if (SceneManager.GetActiveScene().buildIndex == (int)EntryPoint.Init && Loaded)
            {
                //Delay((TotalDetected + 1) * 2, () => { SceneManager.LoadScene((int)EntryPoint.MainMenu); });
                SceneManager.LoadScene((int)EntryPoint.MainMenu);
            }

            base.Pass();
        }
Пример #23
0
        private void BuildResiduals(int level)
        {
            var tilesCount = Mathf.Max(1, (BaseLevelSize / Size) >> (MaxLevel - level));

            Logger.Log(string.Format("HeightMipmap.BuildResiduals: Build residuals level: {0}", level));

            CurrentLevel = level;

            Reset(BaseLevelSize >> (MaxLevel - CurrentLevel), BaseLevelSize >> (MaxLevel - CurrentLevel), Mathf.Min(TopLevelSize << CurrentLevel, Size));

            var parentTile   = new float[(Size + 5) * (Size + 5)];
            var currentTile  = new float[(Size + 5) * (Size + 5)];
            var residualTile = new float[(Size + 5) * (Size + 5)];
            var levelMaxR    = float.NegativeInfinity;

            for (int ty = 0; ty < tilesCount; ++ty)
            {
                for (int tx = 0; tx < tilesCount; ++tx)
                {
                    float maxR, meanR, maxErr;

                    GetApproxTile(level - 1, tx / 2, ty / 2, parentTile);
                    GetTile(level, tx, ty, currentTile);
                    ComputeResidual(parentTile, currentTile, level, tx, ty, residualTile, out maxR, out meanR);
                    ComputeApproxTile(parentTile, residualTile, level, tx, ty, currentTile, out maxErr);

                    if (level < MaxLevel)
                    {
                        SaveTile("Approx", level, tx, ty, currentTile);
                    }

                    SaveTile("Residual", level, tx, ty, residualTile);

                    if (maxR > levelMaxR)
                    {
                        levelMaxR = maxR;
                    }

                    Logger.Log(string.Format("HeightMipmap.BuildResiduals: {0}-{1}-{2}; Max Residual: {3:F6}; Max Error: {4:F6}", level, tx, ty, maxR, maxErr));
                }
            }

            MaxR[level] = levelMaxR;
        }
Пример #24
0
        private void DetectAssembies(out List <string> allPaths)
        {
            var path = PathGlobals.GlobalModFolderPath;

            allPaths = new List <string>();

            try
            {
                allPaths.AddRange(Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories));
            }
            catch (Exception ex)
            {
                Logger.LogError(string.Format("AssemblyLoader.DetectAssembies: Exception: {0}", ex.Message));
            }

            TotalDetected = allPaths.Count;

            Logger.Log(string.Format("AssemblyLoader.DetectAssembies: Assembies Detected: {0}", allPaths.Count));
        }
Пример #25
0
        public void Generate(int rootLevel, int rootTx, int rootTy, string file)
        {
            File.Delete(file);

            var tilesCount = ((1 << (MaxLevel * 2 + 2)) - 1) / 3;

            Logger.Log(string.Format("ColorMipmap.Generate: tiles count: {0}", tilesCount));

            long[] offsets   = new long[tilesCount * 2];
            byte[] byteArray = new byte[(7 * 4) + (offsets.Length * 8)];
            long   offset    = byteArray.Length;

            using (Stream stream = new FileStream(file, FileMode.Create))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.Write(byteArray, 0, byteArray.Length);
            }

            for (int l = 0; l <= MaxLevel; ++l)
            {
                ProduceTilesLebeguesOrder(l, 0, 0, 0, ref offset, offsets, file);
            }

            Buffer.BlockCopy(new int[] { MaxLevel }, 0, byteArray, 0, 4);
            Buffer.BlockCopy(new int[] { Size }, 0, byteArray, 4, 4);
            Buffer.BlockCopy(new int[] { Channels }, 0, byteArray, 8, 4);
            Buffer.BlockCopy(new int[] { Border }, 0, byteArray, 12, 4);
            Buffer.BlockCopy(new int[] { rootLevel }, 0, byteArray, 16, 4);
            Buffer.BlockCopy(new int[] { rootTx }, 0, byteArray, 20, 4);
            Buffer.BlockCopy(new int[] { rootTy }, 0, byteArray, 24, 4);
            Buffer.BlockCopy(offsets, 0, byteArray, 28, 8 * offsets.Length);

            using (Stream stream = new FileStream(file, FileMode.Open))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.Write(byteArray, 0, byteArray.Length);
            }

            ConstantTileIDs.Clear();

            Logger.Log(string.Format("ColorMipmap.Generate: Saved file path: {0} ", file));
        }
Пример #26
0
        private void BuildBaseLevelTiles()
        {
            int tilesCount = BaseLevelSize / Size;

            Logger.Log(string.Format("ColorMipmap.BuildBaseLevelTiles: Build mipmap level: {0}!", MaxLevel));

            for (int ty = 0; ty < tilesCount; ++ty)
            {
                for (int tx = 0; tx < tilesCount; ++tx)
                {
                    var offset = (int)0;

                    for (int j = -Border; j < Size + Border; ++j)
                    {
                        for (int i = -Border; i < Size + Border; ++i)
                        {
                            var color = ColorFunction.GetValue(tx * Size + i, ty * Size + j) * 255.0f;

                            TileData[offset++] = (byte)Mathf.Round(color.x);

                            if (Channels > 1)
                            {
                                TileData[offset++] = (byte)Mathf.Round(color.y);
                            }

                            if (Channels > 2)
                            {
                                TileData[offset++] = (byte)Mathf.Round(color.z);
                            }

                            if (Channels > 3)
                            {
                                TileData[offset++] = (byte)Mathf.Round(color.w);
                            }
                        }
                    }

                    SaveTile("Base", MaxLevel, tx, ty, TileData);
                }
            }
        }
Пример #27
0
        protected override void Awake()
        {
            base.Awake();

            ApplicationDataPath = Application.dataPath;

            if (!UseCaching)
            {
                // If caching not used load all data into memory.
                if (Bytes == BYTES.BIT8)
                {
                    LoadRawFile8(ApplicationDataPath + FileName);
                }
                else if (Bytes == BYTES.BIT16)
                {
                    LoadRawFile16(ApplicationDataPath + FileName, ByteOrder == BYTE_ORDER.MAC);
                }

                Logger.Log(string.Format("RawInputMap.Awake: {0} loaded!", FileName));
            }
        }
Пример #28
0
        public ColorMipmap(IColorFunction2D colorFunction, int baseLevelSize, int tileSize, int border, int channels, string tempFolder) : base(baseLevelSize, baseLevelSize, tileSize, channels, 200)
        {
            ColorFunction = colorFunction;
            BaseLevelSize = baseLevelSize;
            Size          = tileSize;
            Border        = Mathf.Max(0, border);

            TempFolder = tempFolder;

            MaxLevel = 0;

            var size = BaseLevelSize;

            while (size > this.Size)
            {
                MaxLevel += 1;
                size     /= 2;
            }

            TileData = new byte[(this.Size + 2 * this.Border) * (this.Size + 2 * this.Border) * Channels];

            ConstantTileIDs = new Dictionary <int, int>();

            Left   = null;
            Right  = null;
            Bottom = null;
            Top    = null;

            if (!Directory.Exists(TempFolder))
            {
                Directory.CreateDirectory(TempFolder);
            }

            Logger.Log(string.Format("ColorMipmap.ctor: BaseLevelSize: {0}; TileSize: {1}; Border: {2}; MaxLevel: {3}; Channels: {4}", BaseLevelSize,
                                     Size,
                                     Border,
                                     MaxLevel,
                                     Channels));
        }
Пример #29
0
        private void BuildBaseLevelTiles()
        {
            var tilesCount = BaseLevelSize / Size;

            Logger.Log(string.Format("HeightMipmap.BuildBaseLevelTiles: Build mipmap level: {0}", MaxLevel));

            var maxR = float.NegativeInfinity;

            for (int ty = 0; ty < tilesCount; ++ty)
            {
                for (int tx = 0; tx < tilesCount; ++tx)
                {
                    var offset = (int)0;

                    for (int j = -2; j <= Size + 2; ++j)
                    {
                        for (int i = -2; i <= Size + 2; ++i)
                        {
                            var h = HeightFunction.GetValue(tx * Size + i, ty * Size + j);

                            TileData[offset++] = h;

                            if (h > maxR)
                            {
                                maxR = h;
                            }
                        }
                    }

                    SaveTile("Base", MaxLevel, tx, ty, TileData);
                }
            }

            Logger.Log(string.Format("HeightMipmap.BuildBaseLevelTiles: Max Residual:  {0}", maxR.ToString("F6")));

            MaxR[0] = maxR / Scale;
        }
Пример #30
0
        private void ProduceTile(int level, int tx, int ty, ref long offset, long[] offsets, string file)
        {
            var tileSize = Mathf.Min(TopLevelSize << level, this.Size);

            Logger.Log(string.Format("HeightMipmap.ProduceTile: Producing tile {0}:{1}:{2}!", level, tx, ty));

            if (level == 0)
            {
                CurrentLevel = 0;

                Reset(tileSize, tileSize, tileSize);

                for (int j = 0; j <= tileSize + 4; ++j)
                {
                    for (int i = 0; i <= tileSize + 4; ++i)
                    {
                        var index = i + j * (tileSize + 5);

                        TileData[index] = GetTileHeight(i - 2, j - 2) / Scale;
                    }
                }
            }
            else
            {
                LoadTile("Residual", level, tx, ty, TileData);
            }

            int tileid;

            if (level < MinLevel)
            {
                tileid = level;
            }
            else
            {
                var levelLength = Mathf.Max(level - MinLevel, 0);

                tileid = MinLevel + tx + ty * (1 << levelLength) + ((1 << (2 * levelLength)) - 1) / 3;
            }

            var isConstant = true;

            for (int i = 0; i < (tileSize + 5) * (tileSize + 5) * 1; ++i)
            {
                if (!BrainFuckMath.AlmostEquals(TileData[i], 0.0f))
                {
                    isConstant = false;

                    break;
                }
            }

            if (isConstant && ConstantTile != -1)
            {
                Logger.Log("HeightMipmap.ProduceTile: tile is const (All zeros)!");

                offsets[2 * tileid]     = offsets[2 * ConstantTile];
                offsets[2 * tileid + 1] = offsets[2 * ConstantTile + 1];
            }
            else
            {
                var data = new byte[TileData.Length * 2];

                for (int i = 0; i < TileData.Length; i++)
                {
                    short z = (short)Mathf.Round(TileData[i] / MaxR[level] * (float)short.MaxValue);

                    data[2 * i]     = (byte)(z & 0xFF);
                    data[2 * i + 1] = (byte)(z >> 8);
                }

                using (Stream stream = new FileStream(file, FileMode.Open))
                {
                    stream.Seek(offset, SeekOrigin.Begin);
                    stream.Write(data, 0, data.Length);
                }

                offsets[2 * tileid] = offset;
                offset += data.Length;
                offsets[2 * tileid + 1] = offset;
            }

            if (isConstant && ConstantTile == -1)
            {
                ConstantTile = tileid;
            }
        }