mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-17 18:40:33 +00:00
Perlin Noise script
This commit is contained in:
parent
d55faa864c
commit
06f3bca80b
51
Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs
Normal file
51
Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs.meta
Normal file
11
Assets/Mirror/Examples/_Common/Scripts/PerlinNoise.cs.meta
Normal 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:
|
Loading…
Reference in New Issue
Block a user