コード例 #1
0
ファイル: PlanetNoise.cs プロジェクト: Eklofakka/Spacebutter
    public static PlanetNoise Terran()
    {
        #region Noise
        PlanetNoise noise = new PlanetNoise();

        noise.Width  = 64;
        noise.Height = 64;

        noise.Frequency   = 2.0;
        noise.Scale       = 0.125;
        noise.Bias        = -0.75;
        noise.TFrequency  = 0.5;
        noise.Persistence = 0.25;
        noise.Min         = 0;
        noise.Max         = 1000;
        noise.FallOff     = 0.125;
        #endregion

        #region Gradient
        Gradient gradient = new Gradient();

        var grad1 = new GradientColorKey[5];
        grad1[0].color = new Color32(13, 0, 189, 255);
        grad1[0].time  = 0.0f;

        grad1[1].color = new Color32(14, 106, 148, 255);
        grad1[1].time  = 0.488f;

        grad1[2].color = new Color32(16, 141, 0, 255);
        grad1[2].time  = 0.521f;

        grad1[3].color = new Color32(149, 149, 149, 255);
        grad1[3].time  = 0.979f;

        grad1[4].color = new Color32(255, 255, 255, 255);
        grad1[4].time  = 1f;

        var alpha1 = new GradientAlphaKey[5];
        alpha1[0].alpha = 1.0F;
        alpha1[0].time  = 0.488f;

        alpha1[1].alpha = 1.0F;
        alpha1[1].time  = 0.0F;

        alpha1[2].alpha = 1.0F;
        alpha1[2].time  = 0.521f;

        alpha1[3].alpha = 1.0F;
        alpha1[3].time  = 0.979f;

        alpha1[4].alpha = 1.0F;
        alpha1[4].time  = 1f;

        gradient.SetKeys(grad1, alpha1);

        noise.Gradient = gradient;
        #endregion

        return(noise);
    }
コード例 #2
0
    private void Awake()
    {
        //icosahedronCorners cannot be initialized up above because it has to use Mathf.Sqrt()
        float t = (1f + Mathf.Sqrt(5f)) / 2f;

        icosahedron = new Vector3[]
        {
            new Vector3(-1, t, 0).normalized,
            new Vector3(1, t, 0).normalized,
            new Vector3(-1, -t, 0).normalized,
            new Vector3(1, -t, 0).normalized,

            new Vector3(0, -1, t).normalized,
            new Vector3(0, 1, t).normalized,
            new Vector3(0, -1, -t).normalized,
            new Vector3(0, 1, -t).normalized,

            new Vector3(t, 0, -1).normalized,
            new Vector3(t, 0, 1).normalized,
            new Vector3(-t, 0, -1).normalized,
            new Vector3(-t, 0, 1).normalized
        };

        noiseMaker = new PlanetNoise(noiseSettings);
    }
コード例 #3
0
    /// <summary>
    /// Generate the planets texture based on given values
    /// </summary>
    public void CreatePlanetTexture()
    {
        //Loop through all pixels setting pixel to planet color based on noise function
        for (int i = 0; i < planetTexture.width; i++)
        {
            for (int j = 0; j < planetTexture.height; j++)
            {
                float4 pos = new float4((i - planetTexture.width / 2) / planetSettings.planetFeatureSize, (j - planetTexture.height / 2) / planetSettings.planetFeatureSize, 0, planetSettings.planetSeed);
                if (PlanetNoise.GetStateFromPos(pos))
                {
                    planetTexture.SetPixel(i, j, planetSettings.terrainColor);
                }
                else
                {
                    planetTexture.SetPixel(i, j, planetSettings.waterColor);
                }
                //planetTexture.SetPixel(i, j, new Color(val, val, val));
            }
        }
        //Set middle points to black to provide reference to start location
        planetTexture.SetPixel(planetTexture.width / 2 - 1, planetTexture.height / 2, new Color(0, 0, 0, 1));
        planetTexture.SetPixel(planetTexture.width / 2 + 1, planetTexture.height / 2, new Color(0, 0, 0, 1));

        planetTexture.SetPixel(planetTexture.width / 2, planetTexture.height / 2, new Color(0, 0, 0, 1));

        planetTexture.SetPixel(planetTexture.width / 2, planetTexture.height / 2 - 1, new Color(0, 0, 0, 1));
        planetTexture.SetPixel(planetTexture.width / 2, planetTexture.height / 2 + 1, new Color(0, 0, 0, 1));

        //Apply changes to pixels
        planetTexture.Apply();
    }
コード例 #4
0
    protected override void OnUpdate()
    {
        if (flag)
        {
            return;
        }

        flag = true;

        ComponentGroup planetGroup = GetComponentGroup(typeof(Planet), typeof(PlanetNoise));
        ComponentGroup dataGroup   = GetComponentGroup(typeof(PlanetSharedData));

        ComponentDataArray <Planet>                 planetArray = planetGroup.GetComponentDataArray <Planet>();
        ComponentDataArray <PlanetNoise>            noiseArray  = planetGroup.GetComponentDataArray <PlanetNoise>();
        SharedComponentDataArray <PlanetSharedData> dataArray   = dataGroup.GetSharedComponentDataArray <PlanetSharedData>();

        GameObject prefab = dataArray[0].nodePrefab;

        for (int i = 0; i < planetArray.Length; ++i)
        {
            Planet        planet = planetArray[i];
            PlanetNoise   noise  = noiseArray[i];
            HyperDistance r      = planet.radius;

            for (int n = 0; n < 20; ++n)
            {
                Entity      nodeEntity = EntityManager.Instantiate(prefab);
                TerrainNode node       = EntityManager.GetComponentData <TerrainNode>(nodeEntity);
                node.level        = 0;
                node.planetData   = planet;
                node.noiseData    = noise;
                node.built        = 0;
                node.divided      = 0;
                node.hyperDistant = 1;

                int idx = n * 3;
                node.corner1 = icoVerts[idx];
                node.corner2 = icoVerts[idx + 1];
                node.corner3 = icoVerts[idx + 2];
                EntityManager.SetComponentData(nodeEntity, node);

                HyperPosition pos = math.normalize(node.corner1 + node.corner2 + node.corner3) * r;

                PrecisePosition prspos = new PrecisePosition {
                    pos = pos.prs
                };
                EntityManager.SetComponentData(nodeEntity, prspos);

                OctantPosition octpos = new OctantPosition {
                    pos = pos.oct
                };
                EntityManager.SetComponentData(nodeEntity, octpos);
            }
        }
    }
コード例 #5
0
    public void Initialize(float radius, int subs, Vector3[] corners, PlanetNoise noiseMaker, Platosphere parentSphere, int level = 0)
    {
        sphereRadius      = radius;
        meshSubdivisions  = subs;
        this.corners      = corners;
        this.noiseMaker   = noiseMaker;
        this.parentSphere = parentSphere;
        this.level        = level;

        Build();
    }
コード例 #6
0
 public void Execute()
 {
     for (int j = -1; j <= height; j++)
     {
         for (int i = -1; i <= width; i++)
         {
             float4 tilePos = new float4((position.x + i) / featureSize, (position.y + j) / featureSize, 0, seed);
             tileStates.Add(PlanetNoise.GetStateFromPos(tilePos));
         }
     }
 }
コード例 #7
0
    public static float GetNoiseValue(float x, float y, float z, PlanetNoise noiseData, int level = 0)
    {
        float localFreq = noiseData.frequency;
        float localAmp  = noiseData.amplitude;

        float maxValue = 0f;
        float ret      = 0f;

        FastNoise fastNoise = new FastNoise(noiseData.seed);

        for (int i = 0; i < noiseData.octaves + level * 1; ++i)
        {
            ret += fastNoise.GetSimplex(x * localFreq, y * localFreq, z * localFreq) * localAmp;

            maxValue += localAmp;

            localFreq *= noiseData.lacunarity;
            localAmp  *= noiseData.persistence;
        }

        return(ret / maxValue);
    }
コード例 #8
0
    private static Color GetVertexColor(PlanetNoise noiseMaker, float heightFromSeaLevel)
    {
        Color color = noiseMaker.ColorLayer(0).vertexColor;

        int len = noiseMaker.ColorLayersLength();

        for (int i = 1; i < len; ++i)
        {
            PlanetNoise.PlanetColorLayer layer = noiseMaker.ColorLayer(i);

            if (layer.heightThreshold < heightFromSeaLevel)
            {
                color = layer.vertexColor;
            }
            else
            {
                break;
            }
        }

        return(color);
    }
コード例 #9
0
    private void GenerateCubes()
    {
        if (RandomizeSeed)
        {
            seed = Random.Range(0, 10000);
        }

        foreach (GameObject cube in cubes)
        {
            StartCoroutine(Destroy(cube));
        }

        List <List <CombineInstance> > instances = new List <List <CombineInstance> >()
        {
            new List <CombineInstance>()
        };

        int count = 0;

        cubes.Clear();

        Vector3 center = Vector3.one * radius / 2;

        Debug.Log(center);

        for (int x = 0; x < radius; x++)
        {
            for (int y = 0; y < radius; y++)
            {
                for (int z = 0; z < radius; z++)
                {
                    float noiseValue = PlanetNoise.Generate(x * .1f + seed, y * .1f + seed, z * .1f + seed);

                    if (noiseValue >= .5 && (center - new Vector3(x, y, z)).sqrMagnitude < radius / 2 * radius / 2)
                    {
                        GameObject cube = (GameObject)Resources.Load("Prefabs/Cube", typeof(GameObject));
                        cube.transform.position = new Vector3(x, y, z);
                        if (count > 65533)
                        {
                            instances.Add(new List <CombineInstance>());
                            count = 0;
                        }

                        CombineInstance instance = new CombineInstance();
                        instance.mesh      = cube.GetComponent <MeshFilter>().sharedMesh;
                        instance.transform = cube.transform.localToWorldMatrix;
                        instances.Last().Add(instance);
                        //cubes.Add(cube);
                        count += 24;
                    }
                }
            }
        }

        int totalMeshCount = 0;

        foreach (List <CombineInstance> list in instances)
        {
            totalMeshCount++;
            GameObject obj    = new GameObject("Procedural Mesh " + totalMeshCount);
            MeshFilter filter = obj.AddComponent <MeshFilter>();
            filter.sharedMesh = new Mesh();
            MeshRenderer renderer = obj.AddComponent <MeshRenderer>();
            renderer.material = material;
            filter.sharedMesh.CombineMeshes(list.ToArray());



            cubes.Add(obj);
        }
    }
コード例 #10
0
 public static float GetValue(Vector3 pos, PlanetNoise noiseData, int level = 0)
 {
     return(GetValue(pos.x, pos.y, pos.z, noiseData, level));
 }
コード例 #11
0
    public static float GetValue(float x, float y, float z, PlanetNoise noiseData, int level = 0)
    {
        float ret = GetNoiseValue(x, y, z, noiseData, level);

        return(ret);
    }
コード例 #12
0
    public static Mesh BuildMesh(Vector3[] corners, int divisions, float sphereRadius, PlanetNoise noiseMaker, int nodeLevel)
    {
        // rez is the number of vertices on one side of the mesh/triangle
        // the part in parentheses is called the "Mersenne Number"
        int rez = 2 + ((int)Mathf.Pow(2, divisions) - 1);
        // nTris is the number of tris in the mesh
        int t     = rez - 2;
        int nTris = (t * (t + 1)) + (rez - 1);
        // nVerts is the number of vertices in the mesh
        // it is the formula for the "Triangle Sequence" of numbers
        int nVerts = (rez * (rez + 1)) / 2;

        Vector3[] vertices = new Vector3[nVerts];
        Vector3[] normals  = new Vector3[nVerts];
        Vector2[] uvs      = new Vector2[nVerts];
        Color[]   vColors  = new Color[nVerts];
        int[]     indices  = new int[nTris * 3];

        float dist01 = Vector3.Distance(corners[0], corners[1]);
        float dist12 = Vector3.Distance(corners[1], corners[2]);
        float dist20 = Vector3.Distance(corners[2], corners[0]);

        float lenAxis01 = dist01 / (rez - 1);
        float lenAxis12 = dist12 / (rez - 1);
        float lenAxis20 = dist20 / (rez - 1);

        Vector3 add1 = (corners[1] - corners[0]).normalized * lenAxis01;
        Vector3 add2 = (corners[2] - corners[1]).normalized * lenAxis12;

        int vIdx = 0;

        for (int i = 0; i < rez; ++i)
        {
            for (int n = 0; n <= i; ++n)
            {
                vertices[vIdx] = corners[0] + add1 * i + add2 * n;
                Vector3 normal   = (vertices[vIdx]).normalized;
                float   noiseVal = noiseMaker.GetValue(normal, nodeLevel);
                vertices[vIdx] = normal * (sphereRadius + noiseVal);

                normals[vIdx] = normal;

                //uvs[vIdx] = Random.insideUnitCircle;

                vColors[vIdx] = GetVertexColor(noiseMaker, noiseVal);

                ++vIdx;
            }
        }

        int indIdx          = 0;
        int rowStartIdx     = 1;
        int prevRowStartIdx = 0;

        for (int row = 0; row < rez - 1; ++row)
        {
            bool upright          = true;
            int  trisInRow        = 1 + row * 2;
            int  vertsInRowBottom = row + 2;

            int upTri   = 0;
            int downTri = 0;

            for (int tri = 0; tri < trisInRow; ++tri)
            {
                if (upright)
                {
                    indices[indIdx]     = rowStartIdx + upTri + 1;
                    indices[indIdx + 1] = rowStartIdx + upTri;
                    indices[indIdx + 2] = prevRowStartIdx + upTri;
                    ++upTri;
                }
                else
                {
                    indices[indIdx]     = prevRowStartIdx + downTri + 1;
                    indices[indIdx + 1] = rowStartIdx + downTri + 1;
                    indices[indIdx + 2] = prevRowStartIdx + downTri;
                    ++downTri;
                }

                indIdx += 3;
                upright = !upright;
            }

            prevRowStartIdx = rowStartIdx;
            rowStartIdx    += vertsInRowBottom;
        }

        Mesh mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.triangles = indices;
        mesh.colors    = vColors;
        //mesh.uv = uvs;
        //mesh.normals = normals;
        mesh.RecalculateNormals();
        return(mesh);
    }