コード例 #1
0
        /// <summary>
        /// Loads landscape from a file.
        /// </summary>
        /// <param name="file"> the file to be read.</param>
        /// <returns></returns>
        public async Task Load(StorageFile file)
        {
            if (file != null)
            {
                string xml = await FileIO.ReadTextAsync(file);

                //loading
                try {
                    LandscapeBuilder lb = new LandscapeBuilder {
                        XML = xml
                    };
                    BuilderModels.Clear();
                    foreach (string key in lb.Builders.Keys)
                    {
                        BuilderModel model = new BuilderModel(lb.Builders[key])
                        {
                            ID = key
                        };
                        BuilderModels.Add(model);
                    }
                    await Output.SetAsync("File was loaded sucessfully.");
                } catch (Exception) {
                    await Output.SetAsync("Unable to deserialize the file.");
                } finally { }
            }
            else
            {
                //cancelled
            }
        }
コード例 #2
0
ファイル: LandscapeManager.cs プロジェクト: gamesbylucy/shrub
 // Use this for initialization
 void Start()
 {
     m_baseCylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
     m_baseCylinder.transform.position   = transform.position + new Vector3(landscapeSize / 2, -1f, landscapeSize / 2);
     m_baseCylinder.transform.localScale = new Vector3(landscapeSize * 1.5f, 1, landscapeSize * 1.5f);
     m_landscapeBuilder = gameObject.AddComponent <LandscapeBuilder>();
     m_landscapeBuilder.build(landscapeSize, landscapeSize, baseSeedProbability, stepSpeed);
     m_landscapeBuilder.nodeReport();
 }
コード例 #3
0
        public override List <string> ValidityMessages(LandscapeBuilder landscapeBuilder)
        {
            List <string> messages = base.ValidityMessages(landscapeBuilder);

            if (Scale < 1)
            {
                messages.Add("Scale (valee : " + Scale + ") has to have the value of at least 1.");
            }
            return(messages);
        }
コード例 #4
0
 public string ValidityMessage(LandscapeBuilder landscapeBuilder)
 {
     if (IsValid(landscapeBuilder))
     {
         return("Valid.");
     }
     else
     {
         return(string.Join("\n", ValidityMessages(landscapeBuilder)));
     }
 }
コード例 #5
0
 public override bool IsValid(LandscapeBuilder landscapeBuilder)
 {
     if (!base.IsValid(landscapeBuilder))
     {
         return(false);
     }
     if (Scale < 1)
     {
         return(false);
     }
     return(true);
 }
コード例 #6
0
        /// <summary>
        /// An anti-code-dublication methood
        /// </summary>
        /// <returns>builder based on the Builder collection</returns>
        public LandscapeBuilder BuildLandscapeBuilder()
        {
            LandscapeBuilder landscapeBuilder = new LandscapeBuilder();

            foreach (BuilderModel builderModel in BuilderModels)
            {
                if (builderModel.ID == null)
                {
                    continue;
                }
                landscapeBuilder[builderModel.ID] = builderModel.Builder;
            }
            return(landscapeBuilder);
        }
コード例 #7
0
        public override List <string> ValidityMessages(LandscapeBuilder landscapeBuilder)
        {
            List <string> messages = base.ValidityMessages(landscapeBuilder);

            if (!landscapeBuilder.CheckValidityOf(SourceID))
            {
                messages.Add("Source Layer \"" + SourceID + "\" is missing or invalid.");
            }
            else if (!landscapeBuilder.TypeOf(SourceID).Equals(typeof(S)))
            {
                messages.Add("Source \"" + SourceID + "\" is of uncompattible type.");
            }
            return(messages);
        }
コード例 #8
0
 public override bool IsValid(LandscapeBuilder landscapeBuilder)
 {
     if (!base.IsValid(landscapeBuilder))
     {
         return(false);
     }
     if (!landscapeBuilder.CheckValidityOf(SourceID))
     {
         return(false);
     }
     if (!landscapeBuilder.TypeOf(SourceID).Equals(typeof(S)))
     {
         return(false);
     }
     return(true);
 }
コード例 #9
0
        public override List <string> ValidityMessages(LandscapeBuilder landscapeBuilder)
        {
            List <string> messages = base.ValidityMessages(landscapeBuilder);

            foreach (string source in Sources)
            {
                if (!landscapeBuilder.CheckValidityOf(source))
                {
                    messages.Add("Source Layer \"" + source + "\" is missing or invalid.");
                }
                else if (!landscapeBuilder.TypeOf(source).IsSubclassOf(typeof(AlgorithmBuilder <S>)))
                {
                    messages.Add("Source \"" + source + "\" is of uncompattible type.");
                }
            }
            return(messages);
        }
コード例 #10
0
 public override bool IsValid(LandscapeBuilder landscapeBuilder)
 {
     if (!base.IsValid(landscapeBuilder))
     {
         return(false);
     }
     foreach (string source in Sources)
     {
         if (!landscapeBuilder.CheckValidityOf(source))
         {
             return(false);
         }
         if (!landscapeBuilder.TypeOf(source).IsSubclassOf(typeof(AlgorithmBuilder <S>)))
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #11
0
        /// <summary>
        /// Evalutaes the builder.
        /// If invalid, lissts the problems.
        /// If valid, builds a landscape and generates the output (if the layer is present)
        /// </summary>
        public async Task Run()
        {
            const string     nln = "\n";
            LandscapeBuilder landscapeBuilder = BuildLandscapeBuilder();
            await Output.SetAsync(string.Join(nln, landscapeBuilder.GetFullReport()));

            if (landscapeBuilder.IsValid())
            {
                await Output.SetAsync(Output.Value + nln + "===  VALID  ===");

                Landscape landscape = landscapeBuilder.Build();
                try {
                    HeightMapImageOutputter outputter = (HeightMapImageOutputter)landscape.GetOutputter <byte[]>("img");

                    await Output.SetAsync(Output.Value + nln + "Generating the landscape.");

                    byte[] bytes = outputter.GetObject(new Coordinate());
                    await Output.SetAsync(Output.Value + nln + "Generating the image.");

                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => {
                        LatestBitmap = outputter.BytesToBitmap(bytes);
                        if (LatestBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || LatestBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
                        {
                            LatestBitmap = SoftwareBitmap.Convert(LatestBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                        }
                        SoftwareBitmapSource source = new SoftwareBitmapSource();
                        await source.SetBitmapAsync(LatestBitmap);
                        SoftwareBitmapSource.SetFromUIThread(source);
                    }).AsTask();

                    await Output.SetAsync(Output.Value + nln + "Preview is ready,");
                } catch (Exception err) {
                    await Output.SetAsync(Output.Value + nln + "Failed to retrieve the Bitmap Outputter."
                                          + nln + "To enable the preview add a HeightMapImageOutputterBuilder with id of \"img\"" + nln + err.Message);
                }
            }
            else
            {
                await Output.SetAsync(Output.Value + nln + "=== INVALID ===");
            }
        }
コード例 #12
0
        /// <summary>
        /// Saves the ladscape into the file.
        /// </summary>
        /// <param name="file"> the file in which it will be saved.</param>
        /// <param name="landscapeBuilder">the landscapeBuilder that is to be saved.</param>
        public async Task Save(StorageFile file, LandscapeBuilder landscapeBuilder)
        {
            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                await FileIO.WriteTextAsync(file, landscapeBuilder.XML);

                Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);

                if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
                {
                    await Output.SetAsync("File was saved sucessfully.");
                }
                else
                {
                }
            }
            else
            {
            }
        }
コード例 #13
0
        /// <summary>
        /// Creates a save dialog and saves the landscape into the file
        /// </summary>
        public async Task SaveDialog()
        {
            LandscapeBuilder landscapeBuilder = BuildLandscapeBuilder();

            if (landscapeBuilder.IsValid())
            {
                FileSavePicker savePicker = new FileSavePicker {
                    SuggestedStartLocation = PickerLocationId.DocumentsLibrary
                };
                savePicker.FileTypeChoices.Add("Landscape XML", new List <string>()
                {
                    ".xml"
                });
                savePicker.SuggestedFileName = "New Landscape";
                StorageFile file = await savePicker.PickSaveFileAsync();
                await Save(file, landscapeBuilder);
            }
            else
            {
                await Task.Run(Run);
            }
        }
コード例 #14
0
 public virtual List <string> ValidityMessages(LandscapeBuilder landscapeBuilder)
 {
     return(new List <string>());
 }
コード例 #15
0
 public override bool IsValid(LandscapeBuilder landscapeBuilder)
 {
     if (!base.IsValid(landscapeBuilder))
     {
         return(false);
     }
     if (BrushRadius < 1)
     {
         return(false);
     }
     if (DepositSpeed > 1 || DepositSpeed <= 0)
     {
         return(false);
     }
     if (ErodeSpeed > 1 || ErodeSpeed <= 0)
     {
         return(false);
     }
     if (EvaporationSpeed > 1 || EvaporationSpeed < 0)
     {
         return(false);
     }
     if (Friction > 1 || Friction < 0)
     {
         return(false);
     }
     if (InitialVolume <= 0)
     {
         return(false);
     }
     if (CoverageFactor <= 0)
     {
         return(false);
     }
     if (LayeringPower < 0)
     {
         return(false);
     }
     if (MaxIterations <= 0)
     {
         return(false);
     }
     if (MinModification < 0)
     {
         return(false);
     }
     if (MinSedimentCapacity < 0)
     {
         return(false);
     }
     if (OutputFactor <= 0)
     {
         return(false);
     }
     if (SedimentCapacityFactor <= 0)
     {
         return(false);
     }
     if (StepLength <= 0)
     {
         return(false);
     }
     if (MaxSectorSize <= 0)
     {
         return(false);
     }
     return(true);
 }
コード例 #16
0
 public virtual bool IsValid(LandscapeBuilder landscapeBuilder)
 {
     return(true);
 }
コード例 #17
0
        public override List <string> ValidityMessages(LandscapeBuilder landscapeBuilder)
        {
            List <string> messages = base.ValidityMessages(landscapeBuilder);

            if (BrushRadius < 1)
            {
                messages.Add("BrushRadius must be at least 1.");
            }
            if (DepositSpeed > 1 || DepositSpeed <= 0)
            {
                messages.Add("DepositSpeed must be between 1(no restriction) and 0 (no deposit). 0 is invalid.");
            }
            if (ErodeSpeed > 1 || ErodeSpeed <= 0)
            {
                messages.Add("ErodeSpeed must be between 1(no restriction) and 0 (no deposit). 0 is invalid.");
            }
            if (EvaporationSpeed > 1 || EvaporationSpeed < 0)
            {
                messages.Add("ErodeSpeed must be between 1 and 0.");
            }
            if (Friction > 1 || Friction < 0)
            {
                messages.Add("Friction must be between 1 and 0.");
            }
            if (InitialVolume <= 0)
            {
                messages.Add("InitialVolume must be positive.");
            }
            if (CoverageFactor <= 0)
            {
                messages.Add("CoverageFactor must be at least 1.");
            }
            if (LayeringPower < 0)
            {
                messages.Add("LayeringPower must be at least 1.");
            }
            if (MaxIterations <= 0)
            {
                messages.Add("MaxIterations must be at least 1.");
            }
            if (MinModification < 0)
            {
                messages.Add("MinModification must be positive.");
            }
            if (MinSedimentCapacity < 0)
            {
                messages.Add("MinSedimentCapacity must be positive.");
            }
            if (OutputFactor <= 0)
            {
                messages.Add("OutputFactor must be positive.");
            }
            if (SedimentCapacityFactor <= 0)
            {
                messages.Add("SedimentCapacityFactor must be positive.");
            }
            if (StepLength <= 0)
            {
                messages.Add("StepLength must be positive.");
            }
            if (MaxSectorSize <= 0)
            {
                messages.Add("MaxSectorSize must be at least 1.");
            }
            return(messages);
        }
コード例 #18
0
        private static readonly int SIZE = 9;        //128;

        //512;

        private static void Main()
        {
            /*Vec3 v1 = new Vec3(1);
             * Console.WriteLine("v1 is: " + v1);
             * Vec3 v2 = new Vec3(1, 2, 4);
             * Console.WriteLine("v2 is: " + v2);
             * Console.WriteLine("Dot(v1, v2): " + Vec3.Dot(v1, v2));
             * Console.WriteLine("Cross(v1, v2): " + Vec3.Cross(v1, v2));
             * Console.WriteLine("Cross(v2, v1): " + Vec3.Cross(v2, v1));
             * Console.WriteLine("v1 + v2: " + ( v1 + v2 ));1
             * Console.WriteLine("v2 + v1: " + ( v2 + v1 ));
             * Console.WriteLine("v1 - v2: " + ( v1 - v2 ));
             * Console.WriteLine("v2 - v1: " + ( v2 - v1 ));
             * Console.WriteLine("v1 * v2: " + ( v1 * v2 ));
             * Console.WriteLine("v1 * 5: " + ( v1 * 5 ));
             * Console.WriteLine("v1 / 5: " + ( v1 / 5 ));
             * Console.WriteLine("v1 is: " + v1);
             * Console.WriteLine("v2 is: " + v2);
             *
             * Console.WriteLine("90 degrees: " + Angle.RightAngle.Vec2);
             *
             * Console.WriteLine("180 degrees: " + Angle.StraightAngle.AngleRaw);
             * Console.WriteLine("180 degrees: " + Angle.StraightAngle.GetAngle(AngleFormat.Unit));
             * Console.WriteLine("180 degrees: " + Angle.StraightAngle.GetAngle(AngleFormat.Degrees));
             * Console.WriteLine("180 degrees: " + Angle.StraightAngle.GetAngle(AngleFormat.Radians));
             * Console.WriteLine("180 degrees: " + Angle.StraightAngle.Vec2);
             *
             * Console.WriteLine("Cross(v1, X): " + Vec3.Cross(v1, new Vec3(1, 0, 0)));
             * Console.WriteLine("Cross(v1, Y): " + Vec3.Cross(v1, new Vec3(0, 1, 0)));
             * Console.WriteLine("Cross(v1, Z): " + Vec3.Cross(v1, new Vec3(0, 0, 1)));
             * Console.WriteLine("v1.CrossX: " + v1.CrossX);
             * Console.WriteLine("v1.CrossY: " + v1.CrossY);
             * Console.WriteLine("v1.CrossZ: " + v1.CrossZ);
             *
             * Sector<float> sectorFloat = new Sector<float>(new Coordinate(0, 0), 1, 1);
             * sectorFloat[0, 0] = 0;
             * sectorFloat[1, 0] = 50;
             *
             * Console.WriteLine("should be 12.5: " + sectorFloat.ValueAt(0.5f, 0.5f));
             */
            Stopwatch sw = new Stopwatch();

            Console.WriteLine("Stopwatch Started");
            Console.WriteLine("Building...");
            sw.Start();

            LandscapeBuilder landscapeBuilder = new LandscapeBuilder();

            landscapeBuilder["random"] = new RandomBuilder()
            {
                Seed = 6
            };
            landscapeBuilder["vec2"] = new Vec2FieldBuilder()
            {
                SourceID = "random", Magnitude = Constants.SQRT_2_OVER_2_FLOAT
            };
            landscapeBuilder["mem1"] = new MemoryBuilder <Vec2>()
            {
                SourceID = "vec2"
            };
            landscapeBuilder["perlin"] = new ParlinGroupBuiler()
            {
                Vec2FieldID = "mem1", UpperTargetScale = SCALE, MaxPerlinScale = SCALE / 4, DeltaFactor = 0.625f, ScaleStep = 1.5f, RetFactor = 1f, BottomUp = false, OffsetGlobal = new Coordinate(64, 64), LowerTargetScale = 2
            };
            //landscapeBuilder["mem2"] = new MemoryBuilder<float>() { SourceID = "perlin" };
            //0.5325f
            landscapeBuilder["mem2"] = new MemoryBuilder <float>()
            {
                SourceID = "perlin"
            };
            landscapeBuilder["HE"] = new HydraulicErosionBuilder()
            {
                SourceID               = "mem2",
                LayeringPower          = 7,
                CoverageFactor         = 1 / 4f,
                BrushRadius            = 4,
                StepLength             = 2f,
                Gravity                = 4,
                OutputFactor           = 0.2f,
                Friction               = 0.5f,
                SedimentCapacityFactor = 1.125f,
                MinModification        = 0.01f,
                ErodeSpeed             = 0.5f,
                DepositSpeed           = 0.125f,
                SedimentFactor         = 2,
            };
            landscapeBuilder["HEmem"] = new MemoryStrictSectoringBuilder <float>()
            {
                SourceID = "HE", SectorSize = 32
            };
            landscapeBuilder["HEmblur"] = new BlurBuilder {
                SourceID = "HEmem"
            };
            landscapeBuilder["finalInterpol"] = new InterpolatorBuilder {
                SourceID = "HEmblur", Scale = 2
            };
            landscapeBuilder["finalblur"] = new BlurBuilder {
                SourceID = "finalInterpol"
            };
            landscapeBuilder["fianlMem"] = new MemoryBuilder <float>()
            {
                SourceID = "HEmblur"
            };
            landscapeBuilder["HEinv"] = new FloatAdderBuilder()
            {
                Sources = new string[] { "fianlMem" }, RetFactor = -1
            };
            landscapeBuilder["HEdiff"] = new FloatAdderBuilder()
            {
                Sources = new string[] { "HEinv", "mem2" }, RetFactor = 2
            };
            landscapeBuilder["output"] = new HeightMapImageOutputterBuilder()
            {
                SourceID = "mem2", Layers = 3, Size = 100, Min = -1, Max = 1
            };

            //landscapeBuilder["blur1"] = new BlurBuilder() { SourceID = "mem2" };
            //landscapeBuilder["brush"] = new BrushTestBuilder();

            Landscape landscape = landscapeBuilder.Build();

            //string xml = landscapeBuilder.XML;
            //Console.WriteLine(xml);
            //landscapeBuilder.XML = xml;
            //Console.WriteLine(landscapeBuilder.XML);
            Console.WriteLine("Elapsed={0}", sw.Elapsed);
            Console.WriteLine("Computing...");

            sw.Restart();
            Algorithm <float>  outputPerlin    = landscape.GetAlgorithm <float>("mem2");
            Algorithm <float>  outputHE        = landscape.GetAlgorithm <float>("fianlMem");
            Algorithm <float>  outputdiff      = landscape.GetAlgorithm <float>("HEdiff");
            Outputter <Bitmap> bitmapOutputter = landscape.GetOutputter <Bitmap>("output");

            RequstSector request = new RequstSector(new Coordinate(0, 0), SIZE, SIZE);

            //Sector<float> sectordiff = outputdiff.GetSector(request);
            //Sector<float> sectorHE = outputHE.GetSector(request);
            //Sector<float> sectorPerlin = outputPerlin.GetSector(request);

            sw.Stop();
            Console.WriteLine("Elapsed={0}", sw.Elapsed);
            //Console.WriteLine("Press \"Enter\" to save", sw.Elapsed);
            //Console.ReadKey();

            int width  = request.Width_units * 3;
            int height = request.Height_units;

            Bitmap bmp = bitmapOutputter.GetObject(new Coordinate());

            Console.WriteLine("Saving...");
            bmp.Save("D:\\output.png", ImageFormat.Png);

            //Console.WriteLine("min: " + min + " max: " + max + " avg: " + (double) total / (width * height) + " errors: " + errors);
            Console.WriteLine("Saved");
            GC.Collect();
            Console.ReadKey();
        }