Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jeevikid1234 · Jun 03 at 05:44 PM · map-generation

How can i add falloff to this like the terrain shud be surrounded with water or sand only,How can i add falloff map to this?

using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine;

[RequireComponent(typeof(MeshFilter))] public class MeshGeneratorV2 : MonoBehaviour { Mesh mesh; private int MESH_SCALE = 100; public GameObject[] objects; [SerializeField] private AnimationCurve heightCurve; private Vector3[] vertices; private int[] triangles;

 private Color[] colors;
 [SerializeField] private Gradient gradient;
 
 private float minTerrainheight;
 private float maxTerrainheight;

 public int xSize;
 public int zSize;

 public float scale; 
 public int octaves;
 public float lacunarity;

 public int seed;

 private float lastNoiseHeight;

 void Start()
 {
     // Use this method if you havn't filled out the properties in the inspector
     // SetNullProperties(); 

     mesh = new Mesh();
     GetComponent<MeshFilter>().mesh = mesh;
     CreateNewMap();
 }

 private void SetNullProperties() 
 {
     if (xSize <= 0) xSize = 50;
     if (zSize <= 0) zSize = 50;
     if (octaves <= 0) octaves = 5;
     if (lacunarity <= 0) lacunarity = 2;
     if (scale <= 0) scale = 50;
 } 

 public void CreateNewMap()
 {
     CreateMeshShape();
     CreateTriangles();
     ColorMap();
     UpdateMesh();
 }

 private void CreateMeshShape ()
 {
     // Creates seed
     Vector2[] octaveOffsets = GetOffsetSeed();

     if (scale <= 0) scale = 0.0001f;
         
     // Create vertices
     vertices = new Vector3[(xSize + 1) * (zSize + 1)];

     for (int i = 0, z = 0; z <= zSize; z++)
     {
         for (int x = 0; x <= xSize; x++)
         {
             // Set height of vertices
             float noiseHeight = GenerateNoiseHeight(z, x, octaveOffsets);
             SetMinMaxHeights(noiseHeight);
             vertices[i] = new Vector3(x, noiseHeight, z);
             i++;
         }
     }
 }

 private Vector2[] GetOffsetSeed()
 {
     seed = Random.Range(0, 1000);
     
     // changes area of map
     System.Random prng = new System.Random(seed);
     Vector2[] octaveOffsets = new Vector2[octaves];
                 
     for (int o = 0; o < octaves; o++) {
         float offsetX = prng.Next(-100000, 100000);
         float offsetY = prng.Next(-100000, 100000);
         octaveOffsets[o] = new Vector2(offsetX, offsetY);
     }
     return octaveOffsets;
 }

 private float GenerateNoiseHeight(int z, int x, Vector2[] octaveOffsets)
 {
     float amplitude = 20;
     float frequency = 1;
     float persistence = 0.5f;
     float noiseHeight = 0;

     // loop over octaves
     for (int y = 0; y < octaves; y++)
     {
         float mapZ = z / scale * frequency + octaveOffsets[y].y;
         float mapX = x / scale * frequency + octaveOffsets[y].x;

         //The *2-1 is to create a flat floor level
         float perlinValue = (Mathf.PerlinNoise(mapZ, mapX)) * 2 - 1;
         noiseHeight += heightCurve.Evaluate(perlinValue) * amplitude;
         frequency *= lacunarity;
         amplitude *= persistence;
     }
     return noiseHeight;
 }

 private void SetMinMaxHeights(float noiseHeight)
 {
     // Set min and max height of map for color gradient
     if (noiseHeight > maxTerrainheight)
         maxTerrainheight = noiseHeight;
     if (noiseHeight < minTerrainheight)
         minTerrainheight = noiseHeight;
 }


 private void CreateTriangles() 
 {
     // Need 6 vertices to create a square (2 triangles)
     triangles = new int[xSize * zSize * 6];

     int vert = 0;
     int tris = 0;
     // Go to next row
     for (int z = 0; z < xSize; z++)
     {
         // fill row
         for (int x = 0; x < xSize; x++)
         {
             triangles[tris + 0] = vert + 0;
             triangles[tris + 1] = vert + xSize + 1;
             triangles[tris + 2] = vert + 1;
             triangles[tris + 3] = vert + 1;
             triangles[tris + 4] = vert + xSize + 1;
             triangles[tris + 5] = vert + xSize + 2;

             vert++;
             tris += 6;
         }
         vert++;
     }
 }

 private void ColorMap()
 {
     colors = new Color[vertices.Length];

     // Loop over vertices and apply a color from the depending on height (y axis value)
     for (int i = 0, z = 0; z < vertices.Length; z++)
     {
         float height = Mathf.InverseLerp(minTerrainheight, maxTerrainheight, vertices[i].y);
         colors[i] = gradient.Evaluate(height);
         i++;
     }
 }

 

 

 private void MapEmbellishments() 
 {
     for (int i = 0; i < vertices.Length; i++)
     {
         // find actual position of vertices in the game
         Vector3 worldPt = transform.TransformPoint(mesh.vertices[i]);
         var noiseHeight = worldPt.y;
         // Stop generation if height difference between 2 vertices is too steep
         if(System.Math.Abs(lastNoiseHeight - worldPt.y) < 25)
         {
             // min height for object generation
             if (noiseHeight > 100)
             {
                 // Chance to generate
                 if (Random.Range(1, 5) == 1)
                 {
                     GameObject objectToSpawn = objects[Random.Range(0, objects.Length)];
                     var spawnAboveTerrainBy = noiseHeight * 2;
                     Instantiate(objectToSpawn, new Vector3(mesh.vertices[i].x * MESH_SCALE, spawnAboveTerrainBy, mesh.vertices[i].z * MESH_SCALE), Quaternion.identity);
                 }
             }
         }
         lastNoiseHeight = noiseHeight;
     }
 }

 private void UpdateMesh()
 {
     mesh.Clear();
     mesh.vertices = vertices;
     mesh.triangles = triangles;
     mesh.colors = colors;
     mesh.RecalculateNormals();
     mesh.RecalculateTangents();

     GetComponent<MeshCollider>().sharedMesh = mesh;
     gameObject.transform.localScale = new Vector3(MESH_SCALE, MESH_SCALE, MESH_SCALE);

     MapEmbellishments();
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex count on a procedurally generated map and some related doubts 2 Answers

How to make voxel map generator using 3D Perlin noise 0 Answers

Draw 2D array of Tiles to texture 1 Answer

Errors with the Google Maps SDK Demo 0 Answers

CTS INTEGRATION? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges