Exemple #1
0
    public Texture2D GetCloudBase(UnityEngine.Gradient cloudGradient, int size, NoiseType type)
    {
        ModuleBase Generator = GetModule(type);
        //Billow Generator = new Billow(
        //    1f,
        //    2f,
        //    0.5f,
        //    6,
        //    Random.Range(0, int.MaxValue),
        //    QualityMode.Low);

        Noise2D map = new Noise2D(size, size / 2, Generator);

        map.GenerateSpherical(
            south,
            north,
            west,
            east);

        var tex = map.GetTexture(cloudGradient);

        tex.Apply();

        return(tex);
    }
Exemple #2
0
    public void CreateSphericalPerlinTexture(ref byte[] height, ref byte[] normal)
    {
        /*
         * Generate a spherical mapping of a height and normal map
         */
        int        mapX = 512;
        int        mapY = 256;
        float      south = -90.0f;
        float      north = 90.0f;
        float      west = -180.0f;
        float      east = 180.0f;
        Perlin     myPerlin = new Perlin();
        Texture2D  heightTexture, normalTexture;
        ModuleBase myModule = myPerlin;

        Noise2D heightMap;

        heightMap = new Noise2D(mapX, mapY, myModule);
        heightMap.GenerateSpherical(south, north, west, east);

        /* Get the heightMap, which is just a grayscale of the texture */
        heightTexture = heightMap.GetTexture(LibNoise.Unity.Gradient.Grayscale);
        height        = heightTexture.EncodeToPNG();

        /* Get the normal map. */
        normalTexture = heightMap.GetNormalMap(0);
        normal        = normalTexture.EncodeToPNG();


        //SAVE THE PICTURE
        SaveTexture(heightTexture);
    }
Exemple #3
0
    public byte[] CreateSphericalPerlinTexture()
    {
        /*
         * Generate a spherical texture2D of perlin noise
         */
        int        mapX     = 512;
        int        mapY     = 256;
        float      south    = -90.0f;
        float      north    = 90.0f;
        float      west     = -180.0f;
        float      east     = 180.0f;
        Perlin     myPerlin = new Perlin();
        ModuleBase myModule = myPerlin;
        Noise2D    heightMap;

        heightMap = new Noise2D(mapX, mapY, myModule);
        heightMap.GenerateSpherical(south, north, west, east);

        texture = heightMap.GetTexture(LibNoise.Unity.Gradient.Grayscale);


        byte[] bytes = texture.EncodeToPNG();

        return(bytes);
    }
    void Start()
    {
        var perlin = new Perlin();

        var heightMapBuilder = new Noise2D(512, 256, perlin);

        heightMapBuilder.GenerateSpherical(_south, _north, _west, _east);

        var image = heightMapBuilder.GetTexture(_gradient);

        renderer.material.mainTexture = image;
    }
    void Generate()
    {
        var generator = graph.GetGenerator();

        Noise2D map = new Noise2D(size, size / 2, generator);

        map.GenerateSpherical(
            south,
            north,
            west,
            east);

        ColorMap = map.GetTexture();
        ColorMap.Apply();
    }
    void Generate()
    {
        billowModule = new Billow();
        offsetModule = new Translate(0, 1, 0, billowModule);
        myModule     = new Turbulence(1, offsetModule);

        Noise2D heightMapBuilder = new Noise2D(terrain.Width, terrain.Height, myModule);

        heightMapBuilder.GenerateSpherical(-90, 90, -180, 180);

        weatherImage = heightMapBuilder.GetTexture();
        weatherImage.Apply();

        GetComponent <LODMaterial>().OurMaterial.SetTexture("_Weather", weatherImage);
    }
    private void backgroundProcessing()
    {
        // Create the module network
        ModuleBase moduleBase;

        switch (noise)
        {
        case NoiseType.Billow:
            moduleBase = new Billow();
            break;

        case NoiseType.RidgedMultifractal:
            moduleBase = new RidgedMultifractal();
            break;

        case NoiseType.Voronoi:
            moduleBase = new Voronoi(frequency, displacement, seed, false);

            break;

        case NoiseType.Mix:
            Perlin perlin = new Perlin();
            var    rigged = new RidgedMultifractal();
            moduleBase = new Add(perlin, rigged);
            break;

        case NoiseType.Practice:
            var bill = new Billow();
            bill.Frequency = frequency;
            moduleBase     = new Turbulence(turbulence / 10, bill);
            break;



        default:
            var defPerlin = new Perlin();
            defPerlin.OctaveCount = perlinOctaves;
            moduleBase            = defPerlin;

            break;
        }

        // Initialize the noise map
        this.m_noiseMap = new Noise2D(resolution, resolution, moduleBase);

        m_noiseMap.GenerateSpherical(_north, _south, _west, _east);
    }
    void ProcessTextures()
    ///summary
    ///This thread processes the textures
    ///incrementally in increasing resolutions.
    ///It uses abort checks that extend to
    ///the more computationaly intense parts
    ///of the code--halting and aborting threads
    ///that are no longer necessary mid process.
    {
        var processTimestamp = latestTimeProcessRequested;
        int count            = 0;

        //the crazyness in the for loop's i value is in order to allow it to be
        //used as a resolution manipulator without doing the math multiple times inside a loop

        for (int i = 16; i > 1; i = i / 2)
        {
            Debug.Log((count + 1) + " run start by " + processTimestamp);
            if (processTimestamp != latestTimeProcessRequested)
            {
                Debug.Log("Stopping Thread " + processTimestamp + " for thread " + latestTimeProcessRequested);
                drawInProgress = false;
                return;
            }
            if (i == 16)
            {
                reset = false;
            }
            count++;
            Noise2D placeHolder;
            Debug.Log("drawing map " + count + " by " + processTimestamp);
            placeHolder = new Noise2D(mapWidth / i, mapHeight / i, baseModule);
            placeHolder.GenerateSpherical(-90, 90, -180, 180, ref latestTimeProcessRequested, ref processTimestamp, ref reset);
            if (latestTimeProcessRequested != processTimestamp)
            {
                Debug.Log("Stopping Thread (2nd shallow catch)" + processTimestamp + " for thread" + latestTimeProcessRequested);
                drawInProgress = false;
                return;
            }
            Debug.Log("Map " + count + " drawn by " + processTimestamp);
            updatedMap = placeHolder;
            noiseMapUpdateAvailable = true;
        }
        Debug.Log("Thread Shutdown");
        drawInProgress = false;
    }
Exemple #9
0
    public void Generate(PlanetProfile profile)
    {
        Noise2D noise = new Noise2D(
            mapSize,
            mapSize / 2,
            profile.graph.GetGenerator(profile.GetArguments()));

        noise.GenerateSpherical(
            south,
            north,
            west,
            east);

        ColorMap = new Texture2D(
            mapSize,
            (int)(mapSize / 2f));
        ColorMap = noise.GetTexture(profile.ColorGradient);
        ColorMap.Apply();

        HeightMap = noise.GetTexture(profile.ElevationGradient);
        HeightMap.Apply();

        ////mapSize = 32;
        //imgs = new List<Texture2D>();
        //List<ModuleBase> generators = new List<ModuleBase>();

        //generators.Add(GetModule(profile));

        //// TODO USE A HEIGHTMAP THAT HAS THE SAME AMOUNT OF PXL THAN VERTICES
        //// e.g : 80 * 80
        //// if the map is 512 -> we get to have 6.4 planet's heightmap for the cost of a single map
        //Noise2D map = new Noise2D(mapSize, mapSize / 2, generators[0]);

        //map.GenerateSpherical(
        //    south,
        //    north,
        //    west,
        //    east);

        //ColorMap = map.GetTexture(profile.ColorMap);
        //ColorMap.Apply();

        //HeightMap = map.GetTexture(profile.ElevationMap);
        //HeightMap.Apply();
    }
    //make this itterative to approach mapwidth
    void ProcessTextures()
    {
        //MFU This needs to be moved downstream somehow
        //I may need to write a seperate texture draw process
        //Specficially for multicore rendering.
        var processTimestamp = latestTimeProcessRequested;

        int count = 0;

        //the crazyness in the for loop's i value is in order to allow it to be
        //used as a resolution manipulator without doing the math multiple times inside a loop

        for (int i = 16; i > 1; i = i / 2)
        {
            Debug.Log((count + 1) + " run start by " + processTimestamp);
            if (processTimestamp != latestTimeProcessRequested)
            {
                Debug.Log("Stopping Thread " + processTimestamp + " for thread " + latestTimeProcessRequested);
                drawInProgress = false;
                return;
            }
            if (i == 16)
            {
                reset = false;
            }
            count++;
            Noise2D placeHolder;
            Debug.Log("drawing map " + count + " by " + processTimestamp);
            placeHolder = new Noise2D(mapWidth / i, mapHeight / i, baseModule);
            placeHolder.GenerateSpherical(-90, 90, -180, 180, ref latestTimeProcessRequested, ref processTimestamp, ref reset);
            if (latestTimeProcessRequested != processTimestamp)
            {
                Debug.Log("Stopping Thread (2nd shallow catch)" + processTimestamp + " for thread" + latestTimeProcessRequested);
                drawInProgress = false;
                return;
            }
            Debug.Log("Map " + count + " drawn by " + processTimestamp);
            updatedMap = placeHolder;
            noiseMapUpdateAvailable = true;
        }
        Debug.Log("Thread Shutdown");
        drawInProgress = false;
    }
Exemple #11
0
    //  Other Functions
    //    ----------------------------------------------------------------------------


    void Generate()
    {
        //Perlin mySphere = new Perlin();


        // ------------------------------------------------------------------------------------------

        // - Compiled Terrain -

        ModuleBase myModule;

        myModule = Generator.LibnoiseModualGen.GetRTSGenerator(1);


        // ------------------------------------------------------------------------------------------

        // - Generate -

        // this part generates the heightmap to a texture,
        // and sets the renderer material texture of a sphere to the generated texture


        Noise2D heightMap;

        heightMap = new Noise2D(mapSizeX, mapSizeY, myModule);

        heightMap.GenerateSpherical(south, north, west, east);


        texture = heightMap.GetTexture(GradientPresets.Grayscale);

        ///

#if !UNITY_WEBPLAYER
        byte[] bytes = texture.EncodeToPNG();
        File.WriteAllBytes(Application.dataPath + "/Terain/TextureGenOutput/sphere/" + TextureName + ".png", bytes);
#endif
        ///

        sphereRenderer.material.mainTexture = texture;
    }
Exemple #12
0
    void Run()
    {
        while (!stop)
        {
            while (threadRunning)
            {
                DateTime      time   = DateTime.Now;
                System.Random random = new System.Random(time.GetHashCode());
                baseModule = new Perlin(2 + random.Next(250, 1000) / 1000.0f, .5 * strengthAdjustment, .5, 5, 3, QualityMode.Low);
                RidgedMultifractal ridged = new RidgedMultifractal(3 * random.Next(30, 50), .5, random.Next(1, 3), 3, QualityMode.Low);
                baseModule = new Add(baseModule, ridged);

                noiseMap = new Noise2D(200, 100, baseModule);
                noiseMap.GenerateSpherical(-90, 90, -180, 180);
                noiseMapTransfer         = noiseMap;
                modBaseTransfer          = baseModule;
                updatedNoiseMapAvailable = true;
                threadRunning            = false;
            }
        }
    }
 // use when creating
 public bool Generate(IModule module, string preset)
 {
     NoiseModule    = module;
     GradientPreset = _presetLoader.GetPreset(preset);
     if (GradientPreset != null && module != null)
     {
         NoiseMap = new Noise2D(Width, Height, NoiseModule);
         //_noiseMap.GeneratePlanar (0, 4, 0, 2);
         NoiseMap.GenerateSpherical(-90, 90, -180, 180);
         Logger.Log("terrain generated.");
         Gradient = GradientCreator.CreateGradientServer(new List <GradientPresets.GradientKeyData>(GradientPreset));
         ColorMap = ColorConvert.SysColList(NoiseMap.GetTexture(Gradient));
         Logger.Log("bitmap generated.");
         Settings.preset = preset;
         return(true);
     }
     else
     {
         Logger.LogError("Generate: Gradient Preset is null!");
     }
     return(false);
 }
    public void Generate()
    {
        ModuleBase myModule = new Perlin(frequency, lacunarity, persistence, octaves, seed, quality);

        Noise2D heightMapBuilder = new Noise2D(width, height, myModule);

        heightMapBuilder.GenerateSpherical(south, north, west, east);

        LibNoiseGradient g = new LibNoiseGradient(gradient.colorKeys[0].color);

        foreach (var col in gradient.colorKeys)
        {
            g[col.time] = col.color;
        }

        image            = heightMapBuilder.GetTexture(g);
        image.filterMode = imageFilterMode;
        image.wrapMode   = imageWrapMode;
        image.Apply();

        GetComponent <LODMaterial>().OurMaterial.mainTexture = image;
    }
Exemple #15
0
    void TestGenerate()
    {
        /*
         * Generate a noise map and save it to a file to test it
         */
        int        mapX     = 512;
        int        mapY     = 256;
        float      south    = -90.0f;
        float      north    = 90.0f;
        float      west     = -180.0f;
        float      east     = 180.0f;
        Perlin     myPerlin = new Perlin();
        ModuleBase myModule = myPerlin;
        Noise2D    heightMap;

        heightMap = new Noise2D(mapX, mapY, myModule);
        heightMap.GenerateSpherical(south, north, west, east);

        /* Encode the texture to a png */
        Texture2D heightMapTexture = heightMap.GetTexture(LibNoise.Unity.Gradient.Grayscale);

        SaveTexture(heightMapTexture);
    }
Exemple #16
0
        public void Render()
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            Noise2D map = new Noise2D(
                size,
                size / 2,
                GetInputValue <SerializableModuleBase>("Input", this.Input));

            map.GenerateSpherical(
                south,
                north,
                west,
                east);

            tex = map.GetTexture(grad);
            tex.Apply();

            watch.Stop();
            RenderTime = watch.ElapsedMilliseconds;
        }
    //map generation script
    public void GenerateMap()
    {
        #region variables and setup
        //this is the base noise module that will be manipulated
        baseModule = null;
        //this is the noisemap that will be generated
        noiseMap = null;
        MapDisplay display = FindObjectOfType <MapDisplay>();

        if (!useRandomSeed)
        {
            seedValue = seed.GetHashCode();
        }
        else
        {
            seedValue = UnityEngine.Random.Range(0, 10000000);
        }
        #endregion

        #region Noise Function Setup
        for (int i = 0; i < noiseFunctions.Length; i++)
        {
            noiseFunctions[i].seed = seedValue + i;
        }

        //generates noise for every noisefunction
        for (int i = 0; i < noiseFunctions.Length; i++)
        {
            if (noiseFunctions[i].enabled)
            {
                noiseFunctions[i].MakeNoise();
            }
        }

        //manipulates the base module based on the noise modules
        for (int i = 0; i < noiseFunctions.Length; i++)
        {
            //for first valid noise pattern simply pass the noise function
            if (baseModule == null && noiseFunctions[i].enabled)
            {
                baseModule = noiseFunctions[i].moduleBase;
            }

            //all others valid add to the previous iteration of the baseModule
            else if (noiseFunctions[i].enabled)
            {
                baseModule = new Add(baseModule, noiseFunctions[i].moduleBase);
            }
        }

        //clamps the module to between 1 and 0
        if (clamped)
        {
            baseModule = new Clamp(0, 1, baseModule);
        }

        noiseMap = new Noise2D(mapWidth, mapHeight, baseModule);
        #endregion

        #region Planet Generator
        if (mapType == MapType.Planet)
        {
            noiseMap.GenerateSpherical(-90, 90, -180, 180);
            Color[] colorMap = new Color[noiseMap.Width * noiseMap.Height];
            textures[0] = new Texture2D(noiseMap.Width, noiseMap.Height);
            if (renderType == RenderType.Greyscale)
            {
                textures[0] = noiseMap.GetTexture(LibNoise.Unity.Gradient.Grayscale);
            }
            else
            {
                for (int y = 0; y < noiseMap.Height; y++)
                {
                    for (int x = 0; x < noiseMap.Width; x++)
                    {
                        float currentHeight = noiseMap[x, y];
                        for (int i = 0; i < regions.Length; i++)
                        {
                            if (currentHeight <= regions[i].height)
                            {
                                colorMap[y * noiseMap.Width + x] = regions[i].color;
                                break;
                            }
                        }
                    }
                }

                textures[0].SetPixels(colorMap);
            }
            textures[0].Apply();

            Mesh newMesh = SphereMagic.CreatePlanet(PlanetItterations, radius, baseModule, heightMultiplier, regions);

            display.DrawMesh(newMesh, textures[0]);
        }
        #endregion

        #region Flat Terrain
        else if (mapType == MapType.FlatTerrain)
        {
            display.TextureRender = FindObjectOfType <Renderer>();
            textures[0]           = noiseMap.GetTexture(LibNoise.Unity.Gradient.Grayscale);
            Color[] colorMap = new Color[noiseMap.Width * noiseMap.Height];
            for (int y = 0; y < noiseMap.Height; y++)
            {
                for (int x = 0; x < noiseMap.Width; x++)
                {
                    float currentHeight = noiseMap[x, y];
                    for (int i = 0; i < regions.Length; i++)
                    {
                        if (currentHeight <= regions[i].height)
                        {
                            colorMap[y * noiseMap.Width + x] = regions[i].color;
                            break;
                        }
                    }
                }
            }

            textures[0].SetPixels(colorMap);
            textures[0].Apply();

            display.DrawMesh(FlatMeshGenerator.GenerateTerrainMesh(noiseMap, heightMultiplier, heightAdjuster), textures[0]);
        }
        #endregion
    }
    /// <summary>
    /// The Core Map Generation Method, in essence this method
    /// takes the stack of noise methods and processes them
    /// sequentially and generates a resultant noise map.
    /// It then calls a function to generate the Mesh (or 3D object)
    /// and applies the noise to the mesh inside of the function
    /// to draw the mesh.
    ///
    /// Also in this located in this region is the map texture generator,
    /// which actually makes up a signifigant bulk of the computational
    /// cost of most map generation schemas I've found useful.
    /// -RGS
    /// </summary>
    public void GenerateMap()
    {
        //MFU Need to work on the hiearchy / code reduction
        //in this method in particular.

        //MFU After reorganization add easier initial generation
        //calls with different types of arguments (files / settings mixtures).

        #region variables and setup
        //autoUpdate saftey catch disabled after implementing multithreading, may be added again if this is pulled back out.
        //if (autoUpdate && (mapWidth > 400 || mapHeight > 200))
        //{
        //    mapWidth = 400;
        //    mapHeight = 200;
        //     Debug.Log("Texture resolution reduced to 400x200 max during Auto Update!");
        //}

        //this is the base noise module that will be manipulated
        baseModule = null;

        //this is the noisemap that will be generated
        noiseMap = null;

        //next two commands interface with multithreaded renderer
        HaltThreads();
        reset = true;


        MapDisplay display = FindObjectOfType <MapDisplay>();


        if (!useRandomSeed)
        {
            seedValue = seed.GetHashCode();
        }
        else
        {
            seedValue = UnityEngine.Random.Range(0, 10000000);
        }


        #endregion

        #region Noise Function Setup
        for (int i = 0; i < noiseFunctions.Length; i++)
        {
            noiseFunctions[i].seed = seedValue + i;
            noiseFunctions[i].MakeNoise();

            //for first valid noise pattern simply pass the noise function
            if (baseModule == null && noiseFunctions[i].enabled)
            {
                baseModule = noiseFunctions[i].moduleBase;
            }

            //all others valid add to the previous iteration of the baseModule
            else if (noiseFunctions[i].enabled)
            {
                //this is where I want to do blend mode adjustments using
                //libNoise add, blend, subtract, multiply etc as an effect (along with falloffs maybe)
                baseModule = new Add(baseModule, noiseFunctions[i].moduleBase);
            }
        }

        //clamps the module to between 1 and 0, sort of...
        //because of the way coherent noise works, it's not possible to completely
        //eliminate the possibility that a value will fall outside these ranges.

        if (clamped)
        {
            baseModule = new Clamp(0, 1, baseModule);
        }
        noiseMap = new Noise2D(mapWidth, mapHeight, baseModule);
        #endregion

        #region Planet Generator
        if (mapType == MapType.Planet)
        {
            mapHeight  = mapWidth / 2;
            renderType = RenderType.Color;

            noiseMap = new Noise2D(100, 100, baseModule);
            noiseMap.GenerateSpherical(-90, 90, -180, 180);
            mapTexture = GetMapTexture(renderType, noiseMap);
            if ((oceans) && (!waterMesh.activeSelf))
            {
                waterMesh.SetActive(true);
            }
            if (waterMesh != null)
            {
                //MFU better Sea Level Autoupdate / switch
                waterMesh.transform.localScale = 2 * (new Vector3(radius + seaLevelOffset, radius + seaLevelOffset, radius + seaLevelOffset));

                if (!oceans)
                {
                    waterMesh.SetActive(false);
                }
            }
            display.DrawMesh(SphereMagic.CreatePlanet(PlanetItterations, radius, baseModule, heightMultiplier, regions), mapTexture);
        }
        #endregion

        #region Flat Terrain Generator
        //non functional

        else if (mapType == MapType.FlatTerrain)
        {
            noiseMap = new Noise2D(100, 100, baseModule);
            noiseMap.GeneratePlanar(-1, 1, -1, 1, seamless);
            display.TextureRender = FindObjectOfType <Renderer>();
            mapTexture            = GetMapTexture(renderType, noiseMap);
            display.DrawMesh(FlatMeshGenerator.GenerateTerrainMesh(noiseMap, heightMultiplier, heightAdjuster), mapTexture);
        }
        #endregion

        #region Start Multithreaded Noisemapping
        if (drawInProgress)
        {
            HaltThreads();
        }

        latestTimeProcessRequested = DateTime.Now.GetHashCode();
        drawInProgress             = true;
        stop = false;
        StartCoroutine(TextureRefiner());
        #endregion
    }
 protected override void ConfigureRenderer(Noise2D renderer)
 {
     renderer.GenerateSpherical(_offsetSouth, _offsetNorth, _offsetWest, _offsetEast);
 }