Пример #1
0
    void DrawNoiseToPlaneGPURenderTexture()
    {
        System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
        st.Start();
        for (int performanceLoopIndex = 0; performanceLoopIndex < noisePerformanceTestLoopTimes; ++performanceLoopIndex)
        {
            GPUNoiseGenerator.NoiseData noiseData = new GPUNoiseGenerator.NoiseData {
                frequency      = frequency,
                octaves        = octaves,
                side           = side,
                quarter        = quarter,
                lacunarity     = lacunarity,
                maxNoiseHeight = maxNoiseHeight,
                persistence    = persistance,
                amplitude      = amplitude,
                resolution     = noiseResolution
            };
            if (rt != null)
            {
                rt.Release();
            }
            rt = GPUNoiseGenerator.GenerateNoise(seed, noiseData, radius);
        }
        st.Stop();
        if (noisePerformanceTestLoopTimes != 1)
        {
            Debug.Log(string.Format("Generated noise with {0} {1} times and it took {2} ms to complete.", noiseSource.ToString(), noisePerformanceTestLoopTimes, st.ElapsedMilliseconds));
        }

        planeRenderer.material.mainTexture = rt;
    }
Пример #2
0
    // Draw noisemap to the plane
    void DrawNoiseToPlane()
    {
        GPUNoiseGenerator.NoiseData noiseData = new GPUNoiseGenerator.NoiseData {
            frequency      = frequency,
            octaves        = octaves,
            side           = side,
            quarter        = quarter,
            persistence    = persistance,
            lacunarity     = lacunarity,
            maxNoiseHeight = maxNoiseHeight,
            amplitude      = amplitude,
            resolution     = noiseResolution
        };

        heightMap = Noise.GenerateNoiseMap(seed, offset, noiseData, noiseScale, noiseSource, gridSize, zoom, noisePerformanceTestLoopTimes, radius);

        if (!useRenderTextureForDrawingTexture)
        {
            Texture2D noiseTexture = TextureGenerator.TextureFromHeightMap(heightMap);
            planeRenderer.material.mainTexture = noiseTexture;
        }
        else
        {
            rt = GPUNoiseGenerator.GenerateNoise(seed, noiseData, radius);
            planeRenderer.material.mainTexture = rt;
        }
    }
Пример #3
0
    // ...

    /*public struct GenerateNoise : IJob {
     *      public int seed;
     *      public Noise.NoiseSource noiseSource;
     *      public GPUNoiseGenerator.NoiseData noiseData;
     *      public NativeArray<float> heightMapX;
     *      public NativeArray<float> heightMapY;
     *
     *      public void Execute() {
     *              float[,] heightMap;
     *
     *              switch (noiseSource) {
     *                      case Noise.NoiseSource.GPU_2DFloatArray:
     *                              heightMap = GPUNoiseGenerator.GenerateNoiseArray(seed, noiseData);
     *                              break;
     *                      case Noise.NoiseSource.GPU_RenderTextureTo2DFloatArray:
     *                              heightMap = GPUNoiseGenerator.GenerateNoiseArrayFromRenderTexture(seed, noiseData);
     *                              break;
     *                      default:
     *                              throw new System.Exception("Not Implemented");
     *              }
     *
     *              float[] x = new float[heightMap.Length];
     *              float[] y = new float[heightMap.Length];
     *              for (int i = 0; i < heightMap.Length; ++i) {
     *                      x[i] = heightMap[i, 0];
     *                      y[i] = heightMap[i, 1];
     *              }
     *              heightMapX.CopyFrom(x);
     *              heightMapY.CopyFrom(y);
     *      }
     * }*/

    // Generate Planet LOD
    public Tuple <GameObject, List <RenderTexture> > GeneratePlanetChunk(int gridSize, Vector3 position, bool hasHeight, bool hasCollider, string side = "")
    {
        string  name      = "";
        Vector2 rotation  = Vector3.zero;
        Mesh    chunkMesh = null;

        Material[] materials;
        float[][,] heightMaps;
        List <RenderTexture> renderTextureList;

        // Jos full niin pitää kaikki sivut generoida
        if (side[0] == 'F')
        {
            name = "F";
            // TODO: LOD Full planet generation jos ees haluun sitä
            return(Tuple.Create(new GameObject(side), new List <RenderTexture>()));
        }
        // Generoidaan yksi sivu tai quarter
        else
        {
            chunkMesh = GenerateSphericalMesh(Mathf.RoundToInt(gridSize), side);             // generoi spherical meshen

            GPUNoiseGenerator.Side planetSide;

            switch (side[0])
            {
            case '0':
                planetSide = GPUNoiseGenerator.Side.Bottom;
                rotation   = new Vector2(270, 0);
                break;

            case '1':
                planetSide = GPUNoiseGenerator.Side.Left;
                rotation   = new Vector2(0, 90);
                break;

            case '2':
                planetSide = GPUNoiseGenerator.Side.Front;
                rotation   = new Vector2(0, 0);
                break;

            case '3':
                planetSide = GPUNoiseGenerator.Side.Right;
                rotation   = new Vector2(0, -90);
                break;

            case '4':
                planetSide = GPUNoiseGenerator.Side.Top;
                rotation   = new Vector2(90, 0);
                break;

            default:                     //case '5':
                planetSide = GPUNoiseGenerator.Side.Back;
                rotation   = new Vector2(180, 0);
                break;
            }


            // TODO: en tiiä pitäiskö vaan parametrina laittaa planet side ja side noise generaattoriin ettei tarttee jokaiselle laittaa niitä erikseen
            ocean.side          = planetSide;
            plainHills.side     = planetSide;
            largeMountains.side = planetSide;
            mediumDetail.side   = planetSide;

            side                   = side.Substring(1);
            ocean.quarter          = side;
            plainHills.quarter     = side;
            largeMountains.quarter = side;
            mediumDetail.quarter   = side;

            name = planetSide.ToString();
            if (side != "")
            {
                name += "_" + side;
            }

            float[,] oceanHeightMap;            // = new float[(int)ocean.resolution, (int)ocean.resolution];
            float[,] plainHillsHeightMap;       // = new float[(int)plainHills.resolution, (int)plainHills.resolution];
            float[,] largeMountainsHeightMap;   // = new float[(int)largeMountains.resolution, (int)largeMountains.resolution];
            float[,] mediumDetailHeightMap;     // = new float[(int)mediumDetail.resolution, (int)mediumDetail.resolution];

            switch (noiseSource)
            {
            case Noise.NoiseSource.GPU_2DFloatArray:
                oceanHeightMap          = GPUNoiseGenerator.GenerateNoiseArray(seed, ocean, gridSize);
                plainHillsHeightMap     = GPUNoiseGenerator.GenerateNoiseArray(seed, plainHills, radius, gridSize);
                largeMountainsHeightMap = GPUNoiseGenerator.GenerateNoiseArray(seed, largeMountains, radius, gridSize);
                mediumDetailHeightMap   = GPUNoiseGenerator.GenerateNoiseArray(seed, mediumDetail, gridSize);
                break;

            case Noise.NoiseSource.GPU_RenderTextureTo2DFloatArray:
                oceanHeightMap          = GPUNoiseGenerator.GenerateNoiseArrayFromRenderTexture(seed, ocean);
                plainHillsHeightMap     = GPUNoiseGenerator.GenerateNoiseArrayFromRenderTexture(seed, plainHills);
                largeMountainsHeightMap = GPUNoiseGenerator.GenerateNoiseArrayFromRenderTexture(seed, largeMountains);
                mediumDetailHeightMap   = GPUNoiseGenerator.GenerateNoiseArrayFromRenderTexture(seed, mediumDetail);
                break;

            default:
                throw new System.Exception("Not Implemented");
            }

            heightMaps = new float[][, ] {
                oceanHeightMap, plainHillsHeightMap, largeMountainsHeightMap, mediumDetailHeightMap
            };
            //heightMaps = new float[][,] { oceanHeightMap, plainHillsHeightMap, largeMountainsHeightMap };

            materials = new Material[1];

            // Height Map material

            /*materials[0] = new Material(Shader.Find("Unlit/Texture"));
             * Texture2D noiseTexture = TextureGenerator.TextureFromHeightMap(mediumDetailHeightMap);
             * Vector2 textureScale = new Vector2(1f / gridSize, 1f / gridSize);
             * materials[0].mainTextureScale = textureScale;
             * materials[0].mainTexture = noiseTexture;*/


            // Textured
            //materials[0] = new Material(Shader.Find("Shader Forge/Terrain"));
            materials[0] = new Material(Shader.Find("Shader Graphs/TestPlanetTextureShader"));
            RenderTexture oceanTex          = GPUNoiseGenerator.GenerateNoise(seed, ocean, radius);
            RenderTexture plainHillsTex     = GPUNoiseGenerator.GenerateNoise(seed, plainHills, radius);
            RenderTexture largeMountainsTex = GPUNoiseGenerator.GenerateNoise(seed, largeMountains, radius);
            RenderTexture mediumDetailTex   = GPUNoiseGenerator.GenerateNoise(seed, mediumDetail, radius);

            materials[0].SetTexture(OCEAN_NOISE_TEXTURE_NAME, oceanTex);
            materials[0].SetTexture(PLAIN_HILLS_NOISE_TEXTURE_NAME, plainHillsTex);
            materials[0].SetTexture(LARGE_MOUNTAINS_NOISE_TEXTURE_NAME, largeMountainsTex);
            materials[0].SetTexture(MEDIUM_DETAIL_NOISE_TEXTURE_NAME, mediumDetailTex);

            Vector2 tiling = new Vector2(1f / gridSize, 1f / gridSize);
            materials[0].SetVector("_Tiling", tiling);

            materials[0].SetFloat("_Brightness1", 6.36f);
            materials[0].SetFloat("_Brightness2", 1.66f);
            materials[0].SetFloat("_Brightness3", 0.4f);
            materials[0].SetFloat("_Brightness4", 22.73f);

            materials[0].SetTexture("_MainTex", oceanTex);

            renderTextureList = new List <RenderTexture> {
                oceanTex,
                plainHillsTex,
                largeMountainsTex,
                mediumDetailTex
            };

            //
            //mat.SetTexture("_MedTex", );
            //mat.SetTexture("_HighTex", );
            //mat.SetTexture("_DisplacementTexture", );
            //
        }

        return(Tuple.Create(
                   GeneratePlanetChunkObj(name, side, position, planetSize, rotation, chunkMesh, materials, ref heightMaps, gridSize, hasHeight, hasCollider),
                   renderTextureList
                   ));
    }