From 06f3bca80b68213ade1efeed3f41139d107db252 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:14:36 -0400 Subject: [PATCH] Perlin Noise script --- .../Examples/_Common/Scripts/PerlinNoise.cs | 51 +++++++++++++++++++ .../_Common/Scripts/PerlinNoise.cs.meta | 11 ++++ 2 files changed, 62 insertions(+) create mode 100644 Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs create mode 100644 Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs.meta diff --git a/Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs b/Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs new file mode 100644 index 000000000..4b55f21a3 --- /dev/null +++ b/Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs @@ -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(); + 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; + } + } +} diff --git a/Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs.meta b/Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs.meta new file mode 100644 index 000000000..c5bb41057 --- /dev/null +++ b/Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs.meta @@ -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: