Provides a noise module that outputs the value selected from one of two source modules chosen by the output value from a control module. [OPERATOR]
Inheritance: ModuleBase
Ejemplo n.º 1
0
    public void generate()
    {
        // Get the terrain data of the currently active terrain
        var terrainData = Terrain.activeTerrain.terrainData;

        // A new ridged multifractal generator
        var generator = new RidgedMultifractal(frequency, lacu, octaves, (int)(Random.value * 0xffffff), QualityMode.High);

        // The thresholded output -- choose either 0.0 or 1.0, based on the output
        var clamped = new LibNoise.Operator.Select(new Const(0.0f), new Const(1.0f), generator);

        // Set the threshold and falloff rate
        clamped.SetBounds(0f, threshold);
        clamped.FallOff = falloff;

        // Create a 2D noise generator for the terrain heightmap, using the generator we just created
        var noise = new Noise2D(terrainData.heightmapResolution, clamped);

        // Generate a plane from [0, 1] on x, [0, 1] on y
        noise.GeneratePlanar(0, 1, 0, 1);

        // Get the data in an array so we can use it to set the heights
        // var data = noise.GetData(true, 0, 0, true);
        var data = noise.GetNormalizedData();

        // .. and actually set the heights
        terrainData.SetHeights(0, 0, data);
    }
        private static List<LoadedModule> GetModules(XmlNodeList moduleList)
        {
            List<LoadedModule> loadedModules = new List<LoadedModule>();

            foreach (XmlNode node in moduleList)
            {
                string id = node.Attributes["guid"].Value;
                Point position = new Point(double.Parse(node.Attributes["position"].Value.Split(',')[0]), double.Parse(node.Attributes["position"].Value.Split(',')[1]));
                ModuleBase module = null;

                List<string> links = new List<string>();

                switch (node.Attributes["type"].Value)
                {
                    case "Billow":
                        Billow billow = new Billow();
                        billow.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        billow.Lacunarity = double.Parse(node.SelectSingleNode("Lacunarity").InnerText);
                        billow.OctaveCount = int.Parse(node.SelectSingleNode("OctaveCount").InnerText);
                        billow.Persistence = double.Parse(node.SelectSingleNode("Persistence").InnerText);
                        billow.Quality = (QualityMode)Enum.Parse(typeof(QualityMode), node.SelectSingleNode("Quality").InnerText);
                        billow.Seed = int.Parse(node.SelectSingleNode("Seed").InnerText);
                        module = billow;
                        break;
                    case "Checker":
                        module = new Checker();
                        break;
                    case "Const":
                        Const con = new Const();
                        con.Value = double.Parse(node.SelectSingleNode("Value").InnerText);
                        module = con;
                        break;
                    case "Cylinders":
                        Cylinders cylinder = new Cylinders();
                        cylinder.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        module = cylinder;
                        break;
                    case "Perlin":
                        Perlin perlin = new Perlin();
                        perlin.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        perlin.Lacunarity = double.Parse(node.SelectSingleNode("Lacunarity").InnerText);
                        perlin.OctaveCount = int.Parse(node.SelectSingleNode("OctaveCount").InnerText);
                        perlin.Persistence = double.Parse(node.SelectSingleNode("Persistence").InnerText);
                        perlin.Quality = (QualityMode)Enum.Parse(typeof(QualityMode), node.SelectSingleNode("Quality").InnerText);
                        perlin.Seed = int.Parse(node.SelectSingleNode("Seed").InnerText);
                        module = perlin;
                        break;
                    case "RidgedMultifractal":
                        RidgedMultifractal ridgedMF = new RidgedMultifractal();
                        ridgedMF.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        ridgedMF.Lacunarity = double.Parse(node.SelectSingleNode("Lacunarity").InnerText);
                        ridgedMF.OctaveCount = int.Parse(node.SelectSingleNode("OctaveCount").InnerText);
                        ridgedMF.Quality = (QualityMode)Enum.Parse(typeof(QualityMode), node.SelectSingleNode("Quality").InnerText);
                        ridgedMF.Seed = int.Parse(node.SelectSingleNode("Seed").InnerText);
                        module = ridgedMF;
                        break;
                    case "Spheres":
                        Spheres spheres = new Spheres();
                        spheres.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        module = spheres;
                        break;
                    case "Voronoi":
                        Voronoi voronoi = new Voronoi();
                        voronoi.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        voronoi.Displacement = double.Parse(node.SelectSingleNode("Displacement").InnerText);
                        voronoi.UseDistance = bool.Parse(node.SelectSingleNode("UseDistance").InnerText);
                        voronoi.Seed = int.Parse(node.SelectSingleNode("Seed").InnerText);
                        module = voronoi;
                        break;
                    case "Abs":
                        module = new Abs();
                        XmlNode absInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(absInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Add":
                        module = new Add();
                        XmlNode addInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(addInputs.SelectSingleNode("Left").InnerText);
                        links.Add(addInputs.SelectSingleNode("Right").InnerText);
                        break;
                    case "Blend":
                        module = new Blend();
                        XmlNode blendInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(blendInputs.SelectSingleNode("Left").InnerText);
                        links.Add(blendInputs.SelectSingleNode("Right").InnerText);
                        links.Add(blendInputs.SelectSingleNode("Operator").InnerText);
                        break;
                    case "Cache":
                        module = new Cache();
                        XmlNode cacheInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(cacheInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Clamp":
                        Clamp clamp = new Clamp();
                        clamp.Maximum = double.Parse(node.SelectSingleNode("Maximum").InnerText);
                        clamp.Minimum = double.Parse(node.SelectSingleNode("Minimum").InnerText);
                        module = clamp;

                        XmlNode clampInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(clampInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Curve":
                        Curve curve = new Curve();
                        module = curve;

                        foreach (XmlNode cpNode in node.SelectSingleNode("ControlPoints").ChildNodes)
                        {
                            double x = double.Parse(cpNode.InnerText.Split(',')[0]);
                            double y = double.Parse(cpNode.InnerText.Split(',')[1]);
                            curve.Add(x, y);
                        }

                        XmlNode curveInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(curveInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Displace":
                        module = new Displace();
                        XmlNode displaceInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(displaceInputs.SelectSingleNode("Primary").InnerText);
                        links.Add(displaceInputs.SelectSingleNode("X").InnerText);
                        links.Add(displaceInputs.SelectSingleNode("Y").InnerText);
                        links.Add(displaceInputs.SelectSingleNode("Z").InnerText);
                        break;
                    case "Exponent":
                        Exponent exponent = new Exponent();
                        exponent.Value = double.Parse(node.SelectSingleNode("Value").InnerText);
                        module = exponent;

                        XmlNode exponentInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(exponentInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Invert":
                        module = new Invert();
                        XmlNode invertInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(invertInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Max":
                        module = new Max();
                        XmlNode maxInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(maxInputs.SelectSingleNode("Left").InnerText);
                        links.Add(maxInputs.SelectSingleNode("Right").InnerText);
                        break;
                    case "Min":
                        module = new Min();
                        XmlNode minInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(minInputs.SelectSingleNode("Left").InnerText);
                        links.Add(minInputs.SelectSingleNode("Right").InnerText);
                        break;
                    case "Multiply":
                        module = new Multiply();
                        XmlNode multiplyInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(multiplyInputs.SelectSingleNode("Left").InnerText);
                        links.Add(multiplyInputs.SelectSingleNode("Right").InnerText);
                        break;
                    case "Power":
                        module = new Power();
                        XmlNode powerInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(powerInputs.SelectSingleNode("Left").InnerText);
                        links.Add(powerInputs.SelectSingleNode("Right").InnerText);
                        break;
                    case "Rotate":
                        Rotate rotate = new Rotate();
                        rotate.X = double.Parse(node.SelectSingleNode("X").InnerText);
                        rotate.Y = double.Parse(node.SelectSingleNode("Y").InnerText);
                        rotate.Z = double.Parse(node.SelectSingleNode("Z").InnerText);
                        module = rotate;

                        XmlNode rotateInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(rotateInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Scale":
                        Scale scale = new Scale();
                        scale.X = double.Parse(node.SelectSingleNode("X").InnerText);
                        scale.Y = double.Parse(node.SelectSingleNode("Y").InnerText);
                        scale.Z = double.Parse(node.SelectSingleNode("Z").InnerText);
                        module = scale;

                        XmlNode scaleInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(scaleInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "ScaleBias":
                        ScaleBias scaleBias = new ScaleBias();
                        scaleBias.Scale = double.Parse(node.SelectSingleNode("Scale").InnerText);
                        scaleBias.Bias = double.Parse(node.SelectSingleNode("Bias").InnerText);
                        module = scaleBias;

                        XmlNode scaleBiasInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(scaleBiasInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Select":
                        Select select = new Select();
                        select.Minimum = double.Parse(node.SelectSingleNode("Minimum").InnerText);
                        select.Maximum = double.Parse(node.SelectSingleNode("Maximum").InnerText);
                        select.FallOff = double.Parse(node.SelectSingleNode("FallOff").InnerText);
                        module = select;

                        XmlNode selectInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(selectInputs.SelectSingleNode("Primary").InnerText);
                        links.Add(selectInputs.SelectSingleNode("Secondary").InnerText);
                        links.Add(selectInputs.SelectSingleNode("Controller").InnerText);
                        break;
                    case "Subtract":
                        module = new Subtract();
                        XmlNode subtractInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(subtractInputs.SelectSingleNode("Left").InnerText);
                        links.Add(subtractInputs.SelectSingleNode("Right").InnerText);
                        break;
                    case "Terrace":
                        Terrace terrace = new Terrace();
                        module = terrace;

                        foreach (XmlNode cpNode in node.SelectSingleNode("ControlPoints").ChildNodes)
                        {
                            terrace.Add(double.Parse(cpNode.InnerText));
                        }

                        XmlNode terraceInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(terraceInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Translate":
                        Translate translate = new Translate();
                        translate.X = double.Parse(node.SelectSingleNode("X").InnerText);
                        translate.Y = double.Parse(node.SelectSingleNode("Y").InnerText);
                        translate.Z = double.Parse(node.SelectSingleNode("Z").InnerText);
                        module = translate;

                        XmlNode translateInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(translateInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Turbulence":
                        Turbulence turbulence = new Turbulence();
                        turbulence.Frequency = double.Parse(node.SelectSingleNode("Frequency").InnerText);
                        turbulence.Power = double.Parse(node.SelectSingleNode("Power").InnerText);
                        turbulence.Roughness = int.Parse(node.SelectSingleNode("Roughness").InnerText);
                        turbulence.Seed = int.Parse(node.SelectSingleNode("Seed").InnerText);
                        module = turbulence;

                        XmlNode turbulenceInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(turbulenceInputs.SelectSingleNode("Input").InnerText);
                        break;
                    case "Final":
                        module = new Final();
                        XmlNode finalInputs = node.SelectSingleNode("ModuleInputs");
                        links.Add(finalInputs.SelectSingleNode("Input").InnerText);
                        break;
                    default:
                        break;
                }

                LoadedModule lm = new LoadedModule(id, module, position, links);
                loadedModules.Add(lm);
            }

            return loadedModules;
        }