Perlin Noise script

This commit is contained in:
MrGadget 2024-10-23 17:14:36 -04:00
parent d55faa864c
commit 06f3bca80b
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,51 @@
using UnityEngine;
using UnityEditor;
namespace Mirror.Examples.Common.Controllers.Player
{
[ExecuteInEditMode]
[AddComponentMenu("")]
public class PerlinNoise : MonoBehaviour
{
public float scale = 20f;
public float heightMultiplier = .03f;
public float offsetX = 5f;
public float offsetY = 5f;
[ContextMenu("Generate Terrain")]
void GenerateTerrain()
{
Terrain terrain = GetComponent<Terrain>();
if (terrain == null)
{
Debug.LogError("No Terrain component found on this GameObject.");
return;
}
Undo.RecordObject(terrain, "Generate Perlin Noise Terrain");
terrain.terrainData = GenerateTerrainData(terrain.terrainData);
}
TerrainData GenerateTerrainData(TerrainData terrainData)
{
int width = terrainData.heightmapResolution;
int height = terrainData.heightmapResolution;
float[,] heights = new float[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
float xCoord = (float)x / width * scale + offsetX;
float yCoord = (float)y / height * scale + offsetY;
heights[x, y] = Mathf.PerlinNoise(xCoord, yCoord) * heightMultiplier;
}
}
terrainData.SetHeights(0, 0, heights);
return terrainData;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 86898d6de7df85e4aaaaca9663b06602
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant: