Пример #1
0
    public float CalculateUnscaledTerrainElevation(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }
        elevationMinMax.AddValue(elevation);
        return(elevation);
    }
Пример #2
0
    public static MinMax Union(IEnumerable <MinMax> ranges)
    {
        var minMax = new MinMax();

        foreach (var range in ranges)
        {
            minMax.AddValue(range.Min);
            minMax.AddValue(range.Max);
        }
        return(minMax);
    }
Пример #3
0
 private void UpdateMinMax()
 {
     foreach (Vector3 vec in vertices)
     {
         elevationMinMax.AddValue(vec.z);
     }
 }
Пример #4
0
    public Vector3 CalculatePointOnPlanet(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = settings.noiseLayers[i].useFirstLayerAsMask ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }
        elevation = settings.planetRadius * (1 + elevation);
        elevationMinMax.AddValue(elevation);
        return(pointOnUnitSphere * elevation);
    }
Пример #5
0
    public float CalculateUnscaledElevation(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        //Evaluamos el punto en la primera capa de ruido
        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        //Evaluamos el punto en las capas de ruido restantes con la opcion de usar la primera como mascara
        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }

        //Actualizamos el MinMax
        elevationMinMax.AddValue(elevation);
        return(elevation);
    }
Пример #6
0
        public Vector3 CalculatePointOnPlanet(Vector3 pointOnUnitSphere)
        {
            float firstLayerValue = 0;
            float elevation       = 0;

            if (_noiseFilters.Length > 0)
            {
                firstLayerValue = _noiseFilters[0].Evaluate(pointOnUnitSphere);
                if (_settings.NoiseLayers[0].Enabled)
                {
                    elevation = firstLayerValue;
                }
            }
            for (int i = 1; i < _noiseFilters.Length; i++)
            {
                if (_settings.NoiseLayers[i].Enabled)
                {
                    float mask = (_settings.NoiseLayers[i].UseFirstLayerMask) ? firstLayerValue : 1;
                    elevation += _noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
                }
            }

            elevation = _settings.PlanetRadius * (1 + elevation);
            _elevationMinMax.AddValue(elevation);
            return(pointOnUnitSphere * elevation);
        }
Пример #7
0
    public float CalculateUncsaledElevation(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;   //before layerrs it was noiseFilter.Evaluate(pointOnUnitSphere);

        if (noiseFilters.Length > 0) //Store the firstLayerValue so it can be used as a refernece for where the noise map can pop out
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)    //Starts at 1 causer we've already evaluated for the first layer just above
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask)?firstLayerValue:1;       //If current loise layer is using first layer as a mask it's equal to firstlayerMask and it is used,. if not set it equal to 1 which basically means not usiong the mask
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }
        elevationMinMax.AddValue(elevation);    //his way we can keep track of the minimum andf maximum vertices of the planet
        return(elevation);
    }
Пример #8
0
    public Vector3 CalculatePointOnPlanet(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                /*if (pointOnUnitSphere.x > 0 && pointOnUnitSphere.y > 0 &&  pointOnUnitSphere.z > 0 ) {
                 *  elevation = 1.0F;
                 * }
                 * else {
                 *  elevation = 0.0F;
                 * }*/
                elevation = firstLayerValue;
                //Debug.Log(@"elevation: " + elevation + @"_" + pointOnUnitSphere);
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }
        elevation = settings.planetRadius * (1 + elevation);
        elevationMinMax.AddValue(elevation);
        return(pointOnUnitSphere * elevation);
    }
Пример #9
0
    public Vector3 CalculaPontoNoPlaneta(Vector3 pontonaEsfera)
    {
        float firstLayerValue = 0;
        float elevation       = 0f;

        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pontonaEsfera);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pontonaEsfera) * mask;
            }
        }
        elevation = settings.RaioDoPlaneta * (1 + elevation);
        elevationMinMax.AddValue(elevation);
        return(pontonaEsfera * elevation);
    }
Пример #10
0
    // Calculate point on planet
    public float CalculateUnscaledElevation(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0; // initialize added elevation due to noise

        // Process first noise layer
        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        // Loop through all the overlapping noise layers to calculate total added elevation due to noise
        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)                                                  // check if layer is enabled
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1; // if true, then =firstLayerValue; else =1 (no mask)
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;                  // calculate added elevation due to noise layer
            }
        }

        // Add elevation to elevationMinMax
        elevationMinMax.AddValue(elevation);

        // Calculate point on planet, factoring in displacement due to total noise elevation
        return(elevation);
    }
Пример #11
0
    /**
     * Prend un point (vertice) sur la planète et calcul son niveau de bruit
     *
     *
     */
    public Vector3 ComputePoint(Vector3 vertice)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        if (noiseFilters.Length >= 1)
        {
            //Permet de créer un niveau de bruit
            firstLayerValue = noiseFilters[0].Evaluate(vertice);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(vertice) * mask;
            }
        }
        elevation = settings.planetRadius * (1 + elevation);
        elevationMinMax.AddValue(elevation);
        return(vertice * elevation);
    }
Пример #12
0
    public Vector3 PointOnUnitPlanet(Vector3 pointOnUnitSphere)
    {
        //float elevation = noiseFilter.Evaluate(pointOnUnitSphere);
        float firstLayerValue = 0;
        float elevation       = 0;

        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (shapeSetings.noiseLayers[0].enabled) // to have ability to enable and disable the layers
            {
                elevation += firstLayerValue;        // to add first layer to be used as a mask for next layers
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (shapeSetings.noiseLayers[i].enabled)                                                  // to have ability to enable and disable the layers
            {
                float mask = (shapeSetings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1; // if we checked to use first layer as a mask
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;                      // to add noise layers to gether
            }
        }
        elevation = shapeSetings.planetRadius * (1 + elevation); // (elevation +1) bec, planet point must be not  = to zero
        elevationMinMax.AddValue(elevation);                     // asssign max and min of planet
        return(pointOnUnitSphere * elevation);
    }
Пример #13
0
    // Calculates the current elevation of the point.
    public float CalculateUnscaledGeneration(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        // Determine points where the first layer of noise is petruding.
        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (settings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        // Loop through all noise filters and update the elevation of the planet's points accordingly.
        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }
        elevationMinMax.AddValue(elevation);
        return(elevation);
    }
Пример #14
0
    public float CalculateUnscaledElevation(Vector3 pointOnUnitSphere)
    {
        float elevation           = 0;
        float firstNoiseElevation = 0;

        if (noiseFilters.Length > 0)
        {
            if (settings.noiseLayers[0].enable)
            {
                firstNoiseElevation = noiseFilters[0].Evaluate(pointOnUnitSphere);
                elevation           = firstNoiseElevation;
            }
        }

        for (int i = 1; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enable)
            {
                float mask = (settings.noiseLayers[i].useFisrtLayerAsMask) ? firstNoiseElevation : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }

        //find the min & max values
        elevationMinMax.AddValue(elevation);

        return(elevation);
    }
Пример #15
0
    public Vector3 CalculatePointOnPlanet(Vector3 pointOnUnitSphere)
    {
        float firstLayerValue = 0;
        float elevation       = 0;

        if (noiseFilters.Length > 0)
        {
            firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
            if (shapeSettings.noiseLayers[0].enabled)
            {
                elevation = firstLayerValue;
            }
        }

        // Loop through all noise layers.
        for (int i = 1; i < noiseFilters.Length; ++i)
        {
            if (shapeSettings.noiseLayers[i].enabled)
            {
                float mask = (shapeSettings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }

        // Calculate point elevation.
        elevation = shapeSettings.planetRadius * (1 + elevation);
        elevationRange.AddValue(elevation);

        return(pointOnUnitSphere * elevation);
    }
Пример #16
0
    public void ConstructMesh()
    {
        int resolution = pointDensity * terrainSize;

        Vector3[] verticies = new Vector3[resolution * resolution];
        Debug.Log("Num Vertices = " + (resolution * resolution));
        int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        Debug.Log("Num Triangles = " + ((resolution - 1) * (resolution - 1) * 6));
        int triIndex = 0;

        mesh = meshFilter.sharedMesh;
        // set indexFormat from 16bit to 32bit so that we can have a more detailed mesh
        meshFilter.sharedMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                // (x,y) / pointDensity = game coordinate
                Vector2 point = new Vector2(x, y) / (float)pointDensity;

                // noiseVal is the height value at our coordinate
                float noiseVal = terrainGenerator.CalculatePointOnPlanet(point + center);
                elevationMinMax.AddValue(noiseVal);                 // track min/max values for our terrain

                // index of the current point
                int i = y + x * resolution;
                // set the vertex for our mesh
                verticies[i] = new Vector3(point.x, noiseVal, point.y);

                // create triangles for mesh
                // triangles are created in clockwise pattern
                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;                  // starting point
                    triangles[triIndex + 1] = i + resolution + 1; // point on the next row + 1
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + 1 + resolution;

                    triIndex += 6;
                }
            }
        }

        // update color generator with new minmax values
        colorGenerator.UpdateElevation(elevationMinMax);
        //colorGenerator.UpdateColors();

        // update mesh with new triangles
        mesh.Clear();
        mesh.vertices  = verticies;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
Пример #17
0
    public static TerrainFace GenerateTerrainFace(int resolution, Vector3 localUp, ShapeSettings settings)
    {
        var axisA          = new Vector3(localUp.y, localUp.z, localUp.x);
        var axisB          = Vector3.Cross(localUp, axisA);
        var shapeGenerator = new ShapeGenerator(settings);

        Vector3[] vertices  = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;
        var       minMax    = new MinMax();

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int     i                 = x + y * resolution; // Same as doing int i = 0 outside loop, and i++ inside loop
                Vector2 percent           = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube   = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;

                var elevation = shapeGenerator.CalculateElevation(pointOnUnitSphere);
                var vertex    = pointOnUnitSphere * elevation;
                minMax.AddValue(elevation);

                vertices[i] = vertex;

                if (x != resolution - 1 && y != resolution - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + resolution + 1;

                    triIndex += 6;
                }
            }
        }


        var mesh = new Mesh();

        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();

        return(new TerrainFace()
        {
            Mesh = mesh, ElevationRange = minMax
        });
    }
Пример #18
0
    private void OnValidate()
    {
        // Initialize if needed.
        this.Initialize();

        // Reset the PlanetMinMax values. We will set them again when we update
        // the TerrainFaces.
        PlanetMinMax.Reset();

        // Update all terrainFaces.
        for (var i = 0; i < directions.Length; i++)
        {
            terrainFaces[i].UpdateSettings(settings);
            waterFaces[i].UpdateSettings(settings);

            // Add the MinMax values of this face.
            PlanetMinMax.AddValue(terrainFaces[0].TerrainMinMax.Min);
            PlanetMinMax.AddValue(terrainFaces[0].TerrainMinMax.Max);
        }

        this.SetShaderValues();

        this.ToggleObjects(settings.ViewType);
    }
Пример #19
0
    public float Evaluate(Vector3 _pointOnUnitSphere)
    {
        float moisture = 0;

        for (var i = 0; i < noiseFilters.Length; i++)
        {
            if (settings.moistureNoiseLayers[i].enabled)
            {
                moisture += noiseFilters[i].Evaluate(_pointOnUnitSphere);
            }
        }

        moistureMinMax.AddValue(moisture);

        return(moisture);
    }
Пример #20
0
    public float Evaluate(Vector3 _pointOnUnitSphere)
    {
        float elevation = 0;

        for (var i = 0; i < noiseFilters.Length; i++)
        {
            if (settings.shapeNoiseLayers[i].enabled)
            {
                elevation += noiseFilters[i].Evaluate(_pointOnUnitSphere);
            }
        }

        elevationMinMax.AddValue(elevation);

        return(elevation);
    }
Пример #21
0
    public Mesh GenerateFaceMesh(Vector3 dir, int nbSegments)
    {
        Vector3 axisA = new Vector3(dir.y, dir.z, dir.x);
        Vector3 axisB = Vector3.Cross(dir, axisA);

        Vector3[] vertices  = new Vector3[nbSegments * nbSegments];
        int[]     triangles = new int[(nbSegments - 1) * (nbSegments - 1) * 6];
        int       triIndex  = 0;

        for (int y = 0; y < nbSegments; y++)
        {
            for (int x = 0; x < nbSegments; x++)
            {
                int     i               = x + y * nbSegments;
                Vector2 percent         = new Vector2(x, y) / (nbSegments - 1);
                Vector3 pointOnUnitCube = dir + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 even            = EvenSpherePoint(pointOnUnitCube);
                float   elevation       = CalculateElevation(even);
                elevationMinMax.AddValue(1 + elevation);
                even       *= 1 + elevation;
                vertices[i] = even;

                if (x != nbSegments - 1 && y != nbSegments - 1)
                {
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + nbSegments + 1;
                    triangles[triIndex + 2] = i + nbSegments;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + nbSegments + 1;
                    triIndex += 6;
                }
            }
        }

        Mesh mesh = new Mesh();

        mesh.name      = "face";
        mesh.vertices  = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        return(mesh);
    }
Пример #22
0
    protected override Vector3 EditPointOnPlanet(Vector3 point)
    {
        float elevation = 0;

        float firstMaskValue = 0;

        for (int i = 0; i < noiseFilters.Length; i++)
        {
            if (noiseLayers[i].enabled)
            {
                if (i == 0)
                {
                    firstMaskValue = noiseFilters[i].Evaluate(point);
                    elevation     += firstMaskValue;
                }
                else
                {
                    float mask = 1;
                    if (noiseLayers[i].useFirstLayerAs == NoiseLayer.UseFirstLayerAs.Mask)
                    {
                        mask = firstMaskValue;
                    }
                    else if (noiseLayers[i].useFirstLayerAs == NoiseLayer.UseFirstLayerAs.InverseMask)
                    {
                        if (firstMaskValue == 0)
                        {
                            mask = 1;
                        }
                        else
                        {
                            mask = 0;
                        }
                    }
                    elevation += noiseFilters[i].Evaluate(point) * mask;
                }
            }
        }
        elevation = radius * (1 + elevation);
        minMax.AddValue(elevation);
        return(point * elevation);
    }
Пример #23
0
        public float Evaluate(Vector3 p, float noiseScale, Vector3 offset)
        {
            Vector3 noisePos = p * noiseScale + offset;

            float distance = p.magnitude;
            float progress = Mathf.Clamp01(distance / radius);

            float noise = SimplexNoise.SimplexNoise.CalcPixel3D(noisePos.x, noisePos.y, noisePos.z);

            noise = noise * 2 - 1;
            m.AddValue(noise);
            return(progress + noise / 4);

            //float radiusProgress = distance / radius;



            ////float extra = Mathf.Cos(Mathf.PI * radiusProgress / 2);
            ////noise = (noise + extra / 5) * (extra + 1);

            //return noise;
        }
    private void CalculateVertices()
    {
        _mesh = new Mesh();
        GetComponent <MeshFilter>().mesh = _mesh;
        _mesh.Clear();

        List <Vector3> vertices = _meshData.Vertices();

        elevationMinMax = new MinMax();

        List <Vector3> verticesAfterElevation = new List <Vector3>();
        float          firstLayerValue        = 0;

        foreach (Vector3 v in vertices)
        {
            float elevation = 0;

            if (_noiseFilters.Count > 0)
            {
                firstLayerValue = _noiseFilters[0].Evaluate(v);
            }

            elevation = firstLayerValue;

            for (var i = 1; i < _noiseFilters.Count; i++)
            {
                var   noiseFilter = _noiseFilters[i];
                float mask        = firstLayerValue;
                elevation += noiseFilter.Evaluate(v) * mask;
            }

            elevation = (1 + elevation);
            elevationMinMax.AddValue(elevation);
            verticesAfterElevation.Add(v * elevation);
        }

        _mesh.vertices = verticesAfterElevation.ToArray();
    }
Пример #25
0
    public Vector3 CalculatePointOnPlanet(Vector3 pointOnUnitSphere)
    {
        float  craterHeight   = craterGenerator.CalculateCraterDepth(pointOnUnitSphere);
        float  elevation      = 0;
        float  noiseelevation = 0;
        float  mask;
        float  dist;
        String pointStr = pointOnUnitSphere.ToString();

        for (int i = 0; i < noiseFilters.Length; i++)
        {
            mask = 0f;
            if (settings.noiseLayers[i].enabled)
            {
                if (settings.noiseLayers[i].useMouseAsMask)
                {
                    if (interaction.noiseType == i)
                    {
                        // check if the point is in radius of the painted vertices
                        dist = (pointOnUnitSphere * settings.radius - interaction.interactionPoint).magnitude;
                        if (dist <= interaction.brushSize)
                        {
                            // the mask is the distance from point to brush
                            mask  = (interaction.brushSize - dist) / interaction.brushSize;
                            mask *= 0.05f;
                            if (masks[i].ContainsKey(pointStr))
                            {
                                masks[i][pointStr] += mask;
                                if (masks[i][pointStr] >= 1f)
                                {
                                    masks[i][pointStr] = 1f;
                                }
                                mask = masks[i][pointStr];
                            }
                            else
                            {
                                masks[i].Add(pointStr, mask);
                            }
                        }
                        else
                        {
                            // if dictionary contains value set to value otherwise 0
                            mask = (masks[i].ContainsKey(pointStr) ? masks[i][pointStr] : 0f);
                        }
                    }
                    else
                    {
                        mask = (masks[i].ContainsKey(pointStr) ? masks[i][pointStr] : 0f);
                    }
                }
                noiseelevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
            }
        }
        if (interaction.noiseType == -1)
        {
            if (masks[0].ContainsKey(pointStr))
            {
                if (masks[0][pointStr] > 0.01)
                {
                    dist = (pointOnUnitSphere * settings.radius - interaction.interactionPoint).magnitude;
                    if (dist <= interaction.brushSize)
                    {
                        mask = (interaction.brushSize - dist) / interaction.brushSize;
                        masks[0][pointStr] *= 1 - mask;
                    }
                }
            }
        }

        elevation += noiseelevation;
        elevation += craterHeight;
        if (craterHeight < 0)
        {
            elevation += -noiseelevation * 0.65f;
        }
        elevation = settings.radius * (1 + elevation);
        if (elevation < settings.radius && settings.zeroLvlIsOcean)
        {
            elevation = settings.radius;
        }
        if (settings.zeroLvlIsOcean)
        {
            elevationMinMax.AddValue(Mathf.Max(elevation, settings.radius));
        }
        else
        {
            elevationMinMax.AddValue(Mathf.Max(elevation, settings.radius - 1f));
        }
        return(pointOnUnitSphere * elevation);
    }
    public void ConstructMesh()
    {
        Vector3[] verticies = new Vector3[resolution * resolution];
        int[]     triangles = new int[(resolution - 1) * (resolution - 1) * 6];
        int       triIndex  = 0;

        mesh = meshFilter.sharedMesh;

        // noise for initial landmass
        OpenSimplexNoise baseNoise = new OpenSimplexNoise(1);
        // noise for adding more detail to landscape
        OpenSimplexNoise detailNoise = new OpenSimplexNoise(2);
        // noise for generating mountains
        OpenSimplexNoise mountainNoise = new OpenSimplexNoise(3);

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                float noiseVal = (float)baseNoise.eval(x / S, y / S) * depth;
                float detVal   = (float)detailNoise.eval(x / detailScale, y / detailScale) * detailHeight;

                float h = noiseVal + detVal;

                // we want to scale moutains based on height of terrain (for the time being)
                // higher terrain is more affected by the mountain noise
                // want it to always be additive though, don't want to subtract from mountains
                float normalizedH = h / (depth + detailHeight);                                          // between 0 and 1
                float mountVal    = (float)mountainNoise.eval(x / mountainScale, y / mountainScale) + 1; // between 0 and 2

                // height at which terrain is mountain
                if (normalizedH > mountainZone)
                {
                    float t = (normalizedH - mountainZone) / (1 - mountainZone);                     // how much to apply the mountain detail, from 1 to 2
                    //Debug.Log("Before height: " + h);
                    //Debug.Log("t: " + t + ", mountVal: " + mountVal + ", normalizedH: " + normalizedH);
                    // current height factor * mountain noise factor * max mountain height
                    h += t * mountVal * mountainHeight;
                    //Debug.Log("After height: " + h);
                }

                int     i       = y + x * resolution;
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                float   tHeight = h / resolution;
                tHeight = (tHeight < sandZone ? 0 : tHeight);
                elevationMinMax.AddValue(tHeight);
                verticies[i] = new Vector3((percent.x - 0.5f) * 2, tHeight, (percent.y - 0.5f) * 2);

                // create triangles for mesh
                if (x != resolution - 1 && y != resolution - 1)
                {
                    //Debug.Log(triIndex);
                    triangles[triIndex]     = i;
                    triangles[triIndex + 1] = i + resolution + 1;
                    triangles[triIndex + 2] = i + resolution;

                    triangles[triIndex + 3] = i;
                    triangles[triIndex + 4] = i + 1;
                    triangles[triIndex + 5] = i + 1 + resolution;

                    triIndex += 6;
                }
            }
        }

        // update color generator with new minmax values
        colorGenerator.UpdateElevation(elevationMinMax);
        colorGenerator.UpdateColors();

        // update mesh with new triangles
        mesh.Clear();
        mesh.vertices  = verticies;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
    }
Пример #27
0
    public Vector3[] TransformVertices(IList <Vertex> vertices, TileMap map)
    {
        Vector3[] vectors = new Vector3[vertices.Count];

        List <Edge> coastalEdges = map.Tiles
                                   .Where(t => t.type.isLand)
                                   .SelectMany(t => t.face.edges
                                               .Where(e => e.Tiles
                                                      .Where(et => !et.type.isLand)
                                                      .Count() >= 1
                                                      )
                                               ).ToList();

        for (int i = 0; i < vectors.Length; i++)
        {
            Vertex  vertex            = vertices[i];
            Vector3 pointOnUnitSphere = vertex.vector;
            float   elevation         = 0;
            if (vertex.Tiles.Where(t => t.type.isLand).Count() > 0)
            {
                if (coastalEdges.Count > 0)
                {
                    Edge  closestEdge     = coastalEdges[0];
                    float closestEdgeDist = float.MaxValue;
                    foreach (Edge edge in coastalEdges)
                    {
                        float edgeDist = Vector3.Distance(pointOnUnitSphere, edge.Midpoint);
                        if (edgeDist < closestEdgeDist)
                        {
                            closestEdge     = edge;
                            closestEdgeDist = edgeDist;
                        }
                    }

                    float distToEdgeSquared;

                    Vector3 n  = closestEdge.vertices[1].vector - closestEdge.vertices[0].vector;
                    Vector3 pa = closestEdge.vertices[0].vector - pointOnUnitSphere;

                    float c = Vector3.Dot(n, pa);

                    // Closest point is a
                    if (c > 0.0f)
                    {
                        distToEdgeSquared = Vector3.Dot(pa, pa);
                    }
                    else
                    {
                        Vector3 bp = pointOnUnitSphere - closestEdge.vertices[1].vector;

                        // Closest point is b
                        if (Vector3.Dot(n, bp) > 0.0f)
                        {
                            distToEdgeSquared = Vector3.Dot(bp, bp);
                        }
                        else
                        {
                            // Closest point is between a and b
                            Vector3 e = pa - n * (c / Vector3.Dot(n, n));
                            distToEdgeSquared = Vector3.Dot(e, e);
                        }
                    }

                    elevation += Mathf.Sqrt(distToEdgeSquared) * settings.mountainHeight;
                }

                //elevation += 0.02f;
            }

            float radius = settings.planetRadius * (1 + elevation);
            vectors[i] = pointOnUnitSphere * radius;
            elevationMinMax.AddValue(radius);
        }

        return(vectors);

        // Vector3 pointOnUnitSphere = vertex.vector;
        // float firstLayerValue = 0;
        // float elevation = 0;

        // if (noiseFilters.Length > 0) {
        //     firstLayerValue = noiseFilters[0].Evaluate(pointOnUnitSphere);
        //     if (settings.noiseLayers[0].enabled){
        //         elevation = firstLayerValue;
        //     }
        // }

        // for (int i = 1; i < noiseFilters.Length; i++) {
        //     if (settings.noiseLayers[i].enabled) {
        //         float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
        //         elevation += noiseFilters[i].Evaluate(pointOnUnitSphere) * mask;
        //     }
        // }
        // return pointOnUnitSphere * settings.planetRadius * (1+elevation);
    }